From 6a923ff9257a4f009cefed049ebb59a4b5acdab5 Mon Sep 17 00:00:00 2001 From: "Nicholas C. Zakas" Date: Sun, 11 Sep 2022 21:39:19 -0700 Subject: [PATCH] fix: Ensure that glob patterns are normalized (#16287) * fix: Ensure that glob patterns are normalized Glob patterns must be normalized to posix-style file paths before being passed in to globby. Fixes #16259 * Remove .only; fix failing test * Fix Windows tests --- lib/eslint/eslint-helpers.js | 2 +- tests/lib/cli.js | 22 +++++++++++++++++++--- tests/lib/eslint/flat-eslint.js | 23 +++++++++++++++++++++++ 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/lib/eslint/eslint-helpers.js b/lib/eslint/eslint-helpers.js index 8001ddd32ee..ead1af5fda9 100644 --- a/lib/eslint/eslint-helpers.js +++ b/lib/eslint/eslint-helpers.js @@ -134,7 +134,7 @@ async function findFiles({ stats.forEach((stat, index) => { const filePath = filePaths[index]; - const pattern = patterns[index]; + const pattern = normalizeToPosix(patterns[index]); if (stat) { diff --git a/tests/lib/cli.js b/tests/lib/cli.js index 60fdac64602..a58a8c910fc 100644 --- a/tests/lib/cli.js +++ b/tests/lib/cli.js @@ -172,11 +172,23 @@ describe("cli", () => { }); describe("when there is a local config file", () => { - const code = "lib/cli.js"; it(`should load the local config file with configType:${configType}`, async () => { - await cli.execute(code, null, useFlatConfig); + await cli.execute("lib/cli.js", null, useFlatConfig); }); + + if (useFlatConfig) { + it(`should load the local config file with glob pattern and configType:${configType}`, async () => { + await cli.execute("lib/cli*.js", null, useFlatConfig); + }); + } + + // only works on Windows + if (os.platform() === "win32") { + it(`should load the local config file with Windows slashes glob pattern and configType:${configType}`, async () => { + await cli.execute("lib\\cli*.js", null, useFlatConfig); + }); + } }); describe("Formatters", () => { @@ -480,9 +492,13 @@ describe("cli", () => { describe("when executing without no-error-on-unmatched-pattern flag", () => { it(`should throw an error on unmatched glob pattern with configType:${configType}`, async () => { - const filePath = getFixturePath("unmatched-patterns"); + let filePath = getFixturePath("unmatched-patterns"); const globPattern = "unmatched*.js"; + if (useFlatConfig) { + filePath = filePath.replace(/\\/gu, "/"); + } + await stdAssert.rejects(async () => { await cli.execute(`"${filePath}/${globPattern}"`, null, useFlatConfig); }, new Error(`No files matching '${filePath}/${globPattern}' were found.`)); diff --git a/tests/lib/eslint/flat-eslint.js b/tests/lib/eslint/flat-eslint.js index 47d8cbbfbd8..c69cba4d69f 100644 --- a/tests/lib/eslint/flat-eslint.js +++ b/tests/lib/eslint/flat-eslint.js @@ -884,6 +884,29 @@ describe("FlatESLint", () => { assert.strictEqual(results[1].suppressedMessages.length, 0); }); + // only works on a Windows machine + if (os.platform() === "win32") { + + it("should resolve globs with Windows slashes when 'globInputPaths' option is true", async () => { + eslint = new FlatESLint({ + ignore: false, + cwd: getFixturePath(".."), + overrideConfig: { files: ["**/*.js", "**/*.js2"] }, + overrideConfigFile: getFixturePath("eslint.config.js") + + }); + const results = await eslint.lintFiles(["fixtures\\files\\*"]); + + assert.strictEqual(results.length, 2); + assert.strictEqual(results[0].messages.length, 0); + assert.strictEqual(results[1].messages.length, 0); + assert.strictEqual(results[0].suppressedMessages.length, 0); + assert.strictEqual(results[1].suppressedMessages.length, 0); + }); + + } + + it("should not resolve globs when 'globInputPaths' option is false", async () => { eslint = new FlatESLint({ ignore: false,