From 11e60b59a453194268a06d6ba16eec709570ef01 Mon Sep 17 00:00:00 2001 From: Milos Djermanovic Date: Mon, 22 Jan 2024 18:54:55 +0100 Subject: [PATCH] test: add more tests for ignoring files and directories (#18018) * test: add more tests for ignoring files and directories Refs #17964 * add one more test (cherry picked from commit 8c1b8dda169920c4e3b99f6548f9c872d65ee426) --- .../tests/format/foo.js | 1 + .../tests/format/jsfmt.spec.js | 1 + .../tests/format/subdir/foo.js | 1 + .../tests/format/subdir/jsfmt.spec.js | 1 + tests/lib/eslint/flat-eslint.js | 114 ++++++++++++++++++ 5 files changed, 118 insertions(+) create mode 100644 tests/fixtures/ignores-directory-deep/tests/format/foo.js create mode 100644 tests/fixtures/ignores-directory-deep/tests/format/jsfmt.spec.js create mode 100644 tests/fixtures/ignores-directory-deep/tests/format/subdir/foo.js create mode 100644 tests/fixtures/ignores-directory-deep/tests/format/subdir/jsfmt.spec.js diff --git a/tests/fixtures/ignores-directory-deep/tests/format/foo.js b/tests/fixtures/ignores-directory-deep/tests/format/foo.js new file mode 100644 index 00000000000..8b1a393741c --- /dev/null +++ b/tests/fixtures/ignores-directory-deep/tests/format/foo.js @@ -0,0 +1 @@ +// empty diff --git a/tests/fixtures/ignores-directory-deep/tests/format/jsfmt.spec.js b/tests/fixtures/ignores-directory-deep/tests/format/jsfmt.spec.js new file mode 100644 index 00000000000..8b1a393741c --- /dev/null +++ b/tests/fixtures/ignores-directory-deep/tests/format/jsfmt.spec.js @@ -0,0 +1 @@ +// empty diff --git a/tests/fixtures/ignores-directory-deep/tests/format/subdir/foo.js b/tests/fixtures/ignores-directory-deep/tests/format/subdir/foo.js new file mode 100644 index 00000000000..8b1a393741c --- /dev/null +++ b/tests/fixtures/ignores-directory-deep/tests/format/subdir/foo.js @@ -0,0 +1 @@ +// empty diff --git a/tests/fixtures/ignores-directory-deep/tests/format/subdir/jsfmt.spec.js b/tests/fixtures/ignores-directory-deep/tests/format/subdir/jsfmt.spec.js new file mode 100644 index 00000000000..8b1a393741c --- /dev/null +++ b/tests/fixtures/ignores-directory-deep/tests/format/subdir/jsfmt.spec.js @@ -0,0 +1 @@ +// empty diff --git a/tests/lib/eslint/flat-eslint.js b/tests/lib/eslint/flat-eslint.js index ee417830788..9eb642c2957 100644 --- a/tests/lib/eslint/flat-eslint.js +++ b/tests/lib/eslint/flat-eslint.js @@ -1723,6 +1723,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); + }); });