Skip to content

Commit f4a1fe2

Browse files
test: add more tests for ignoring files and directories (#18068)
* test: add more tests for ignoring files and directories * add one more test (cherry picked from commit 8c1b8dd) Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
1 parent 69dd1d1 commit f4a1fe2

File tree

5 files changed

+118
-0
lines changed

5 files changed

+118
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// empty
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// empty
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// empty
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// empty

tests/lib/eslint/flat-eslint.js

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1790,6 +1790,120 @@ describe("FlatESLint", () => {
17901790
assert.strictEqual(results[0].filePath, getFixturePath("ignores-directory/subdir/subsubdir/a.js"));
17911791
});
17921792

1793+
// https://github.com/eslint/eslint/issues/17964#issuecomment-1879840650
1794+
it("should allow directories to be unignored without also unignoring all files in them", async () => {
1795+
eslint = new FlatESLint({
1796+
cwd: getFixturePath("ignores-directory-deep"),
1797+
overrideConfigFile: true,
1798+
overrideConfig: {
1799+
ignores: [
1800+
1801+
// ignore all files and directories
1802+
"tests/format/**/*",
1803+
1804+
// unignore all directories
1805+
"!tests/format/**/*/",
1806+
1807+
// unignore only specific files
1808+
"!tests/format/**/jsfmt.spec.js"
1809+
]
1810+
}
1811+
});
1812+
const results = await eslint.lintFiles(["."]);
1813+
1814+
assert.strictEqual(results.length, 2);
1815+
assert.strictEqual(results[0].errorCount, 0);
1816+
assert.strictEqual(results[0].warningCount, 0);
1817+
assert.strictEqual(results[0].filePath, getFixturePath("ignores-directory-deep/tests/format/jsfmt.spec.js"));
1818+
assert.strictEqual(results[1].errorCount, 0);
1819+
assert.strictEqual(results[1].warningCount, 0);
1820+
assert.strictEqual(results[1].filePath, getFixturePath("ignores-directory-deep/tests/format/subdir/jsfmt.spec.js"));
1821+
});
1822+
1823+
it("should allow only subdirectories to be ignored by a pattern ending with '/'", async () => {
1824+
eslint = new FlatESLint({
1825+
cwd: getFixturePath("ignores-directory-deep"),
1826+
overrideConfigFile: true,
1827+
overrideConfig: {
1828+
ignores: [
1829+
"tests/format/*/"
1830+
]
1831+
}
1832+
});
1833+
const results = await eslint.lintFiles(["."]);
1834+
1835+
assert.strictEqual(results.length, 2);
1836+
assert.strictEqual(results[0].errorCount, 0);
1837+
assert.strictEqual(results[0].warningCount, 0);
1838+
assert.strictEqual(results[0].filePath, getFixturePath("ignores-directory-deep/tests/format/foo.js"));
1839+
assert.strictEqual(results[1].errorCount, 0);
1840+
assert.strictEqual(results[1].warningCount, 0);
1841+
assert.strictEqual(results[1].filePath, getFixturePath("ignores-directory-deep/tests/format/jsfmt.spec.js"));
1842+
});
1843+
1844+
it("should allow only contents of a directory but not the directory itself to be ignored by a pattern ending with '**/*'", async () => {
1845+
eslint = new FlatESLint({
1846+
cwd: getFixturePath("ignores-directory-deep"),
1847+
overrideConfigFile: true,
1848+
overrideConfig: {
1849+
ignores: [
1850+
"tests/format/**/*",
1851+
"!tests/format/jsfmt.spec.js"
1852+
]
1853+
}
1854+
});
1855+
const results = await eslint.lintFiles(["."]);
1856+
1857+
assert.strictEqual(results.length, 1);
1858+
assert.strictEqual(results[0].errorCount, 0);
1859+
assert.strictEqual(results[0].warningCount, 0);
1860+
assert.strictEqual(results[0].filePath, getFixturePath("ignores-directory-deep/tests/format/jsfmt.spec.js"));
1861+
});
1862+
1863+
it("should skip ignored files in an unignored directory", async () => {
1864+
eslint = new FlatESLint({
1865+
cwd: getFixturePath("ignores-directory-deep"),
1866+
overrideConfigFile: true,
1867+
overrideConfig: {
1868+
ignores: [
1869+
1870+
// ignore 'tests/format/' and all its contents
1871+
"tests/format/**",
1872+
1873+
// unignore 'tests/format/', but its contents is still ignored
1874+
"!tests/format/"
1875+
]
1876+
}
1877+
});
1878+
1879+
await assert.rejects(async () => {
1880+
await eslint.lintFiles(["."]);
1881+
}, /All files matched by '.' are ignored/u);
1882+
});
1883+
1884+
it("should skip files in an ignored directory even if they are matched by a negated pattern", async () => {
1885+
eslint = new FlatESLint({
1886+
cwd: getFixturePath("ignores-directory-deep"),
1887+
overrideConfigFile: true,
1888+
overrideConfig: {
1889+
ignores: [
1890+
1891+
// ignore 'tests/format/' and all its contents
1892+
"tests/format/**",
1893+
1894+
// this patterns match some or all of its contents, but 'tests/format/' is still ignored
1895+
"!tests/format/jsfmt.spec.js",
1896+
"!tests/format/**/jsfmt.spec.js",
1897+
"!tests/format/*",
1898+
"!tests/format/**/*"
1899+
]
1900+
}
1901+
});
1902+
1903+
await assert.rejects(async () => {
1904+
await eslint.lintFiles(["."]);
1905+
}, /All files matched by '.' are ignored/u);
1906+
});
17931907

17941908
});
17951909

0 commit comments

Comments
 (0)