Skip to content

Commit

Permalink
test: add more tests for ignoring files and directories (#18068)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
snitin315 and mdjermanovic committed Feb 1, 2024
1 parent 69dd1d1 commit f4a1fe2
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/fixtures/ignores-directory-deep/tests/format/foo.js
@@ -0,0 +1 @@
// empty
@@ -0,0 +1 @@
// empty
@@ -0,0 +1 @@
// empty
@@ -0,0 +1 @@
// empty
114 changes: 114 additions & 0 deletions tests/lib/eslint/flat-eslint.js
Expand Up @@ -1790,6 +1790,120 @@ describe("FlatESLint", () => {
assert.strictEqual(results[0].filePath, getFixturePath("ignores-directory/subdir/subsubdir/a.js"));
});

// https://github.com/eslint/eslint/issues/17964#issuecomment-1879840650
it("should allow directories to be unignored without also unignoring all files in them", async () => {
eslint = new FlatESLint({
cwd: getFixturePath("ignores-directory-deep"),
overrideConfigFile: true,
overrideConfig: {
ignores: [

// ignore all files and directories
"tests/format/**/*",

// unignore all directories
"!tests/format/**/*/",

// unignore only specific files
"!tests/format/**/jsfmt.spec.js"
]
}
});
const results = await eslint.lintFiles(["."]);

assert.strictEqual(results.length, 2);
assert.strictEqual(results[0].errorCount, 0);
assert.strictEqual(results[0].warningCount, 0);
assert.strictEqual(results[0].filePath, getFixturePath("ignores-directory-deep/tests/format/jsfmt.spec.js"));
assert.strictEqual(results[1].errorCount, 0);
assert.strictEqual(results[1].warningCount, 0);
assert.strictEqual(results[1].filePath, getFixturePath("ignores-directory-deep/tests/format/subdir/jsfmt.spec.js"));
});

it("should allow only subdirectories to be ignored by a pattern ending with '/'", async () => {
eslint = new FlatESLint({
cwd: getFixturePath("ignores-directory-deep"),
overrideConfigFile: true,
overrideConfig: {
ignores: [
"tests/format/*/"
]
}
});
const results = await eslint.lintFiles(["."]);

assert.strictEqual(results.length, 2);
assert.strictEqual(results[0].errorCount, 0);
assert.strictEqual(results[0].warningCount, 0);
assert.strictEqual(results[0].filePath, getFixturePath("ignores-directory-deep/tests/format/foo.js"));
assert.strictEqual(results[1].errorCount, 0);
assert.strictEqual(results[1].warningCount, 0);
assert.strictEqual(results[1].filePath, getFixturePath("ignores-directory-deep/tests/format/jsfmt.spec.js"));
});

it("should allow only contents of a directory but not the directory itself to be ignored by a pattern ending with '**/*'", async () => {
eslint = new FlatESLint({
cwd: getFixturePath("ignores-directory-deep"),
overrideConfigFile: true,
overrideConfig: {
ignores: [
"tests/format/**/*",
"!tests/format/jsfmt.spec.js"
]
}
});
const results = await eslint.lintFiles(["."]);

assert.strictEqual(results.length, 1);
assert.strictEqual(results[0].errorCount, 0);
assert.strictEqual(results[0].warningCount, 0);
assert.strictEqual(results[0].filePath, getFixturePath("ignores-directory-deep/tests/format/jsfmt.spec.js"));
});

it("should skip ignored files in an unignored directory", async () => {
eslint = new FlatESLint({
cwd: getFixturePath("ignores-directory-deep"),
overrideConfigFile: true,
overrideConfig: {
ignores: [

// ignore 'tests/format/' and all its contents
"tests/format/**",

// unignore 'tests/format/', but its contents is still ignored
"!tests/format/"
]
}
});

await assert.rejects(async () => {
await eslint.lintFiles(["."]);
}, /All files matched by '.' are ignored/u);
});

it("should skip files in an ignored directory even if they are matched by a negated pattern", async () => {
eslint = new FlatESLint({
cwd: getFixturePath("ignores-directory-deep"),
overrideConfigFile: true,
overrideConfig: {
ignores: [

// ignore 'tests/format/' and all its contents
"tests/format/**",

// this patterns match some or all of its contents, but 'tests/format/' is still ignored
"!tests/format/jsfmt.spec.js",
"!tests/format/**/jsfmt.spec.js",
"!tests/format/*",
"!tests/format/**/*"
]
}
});

await assert.rejects(async () => {
await eslint.lintFiles(["."]);
}, /All files matched by '.' are ignored/u);
});

});

Expand Down

0 comments on commit f4a1fe2

Please sign in to comment.