Skip to content

Commit

Permalink
fix: Ensure that glob patterns are normalized (#16287)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
nzakas committed Sep 12, 2022
1 parent 38e8171 commit 6a923ff
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/eslint/eslint-helpers.js
Expand Up @@ -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) {

Expand Down
22 changes: 19 additions & 3 deletions tests/lib/cli.js
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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.`));
Expand Down
23 changes: 23 additions & 0 deletions tests/lib/eslint/flat-eslint.js
Expand Up @@ -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,
Expand Down

0 comments on commit 6a923ff

Please sign in to comment.