From 95190f30f30fc8803063a225a523ccf4c03f8776 Mon Sep 17 00:00:00 2001 From: Elsaid Achraf <60660214+asaid-0@users.noreply.github.com> Date: Mon, 19 Jul 2021 17:01:23 +0200 Subject: [PATCH 1/3] Fix glob patterns with trailing slashes not working --- lib/glob-helpers.cjs | 19 ++++++++++++------- test-tap/globs.js | 22 ++++++++++++++++++++++ 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/lib/glob-helpers.cjs b/lib/glob-helpers.cjs index 7f0329658..ff15415d1 100644 --- a/lib/glob-helpers.cjs +++ b/lib/glob-helpers.cjs @@ -110,21 +110,26 @@ function normalizeFileForMatching(cwd, file) { exports.normalizeFileForMatching = normalizeFileForMatching; +const normalizeNegatedPattern = (cwd, pattern) => { + // Remove `!` from pattern + const patternPath = pattern.slice(1); + const relativePath = path.relative(cwd, patternPath); + return `!${relativePath}`; +}; + function normalizePattern(pattern) { + const cwd = process.cwd(); // Always use `/` in patterns, harmonizing matching across platforms if (process.platform === 'win32') { pattern = slash(pattern); } - if (pattern.startsWith('./')) { - return pattern.slice(2); - } - - if (pattern.startsWith('!./')) { - return `!${pattern.slice(3)}`; + if (pattern.startsWith('!')) { + return normalizeNegatedPattern(cwd, pattern); } - return pattern; + const normalizedPattern = path.relative(cwd, pattern); + return normalizedPattern; } exports.normalizePattern = normalizePattern; diff --git a/test-tap/globs.js b/test-tap/globs.js index de789a6ae..76ac4084b 100644 --- a/test-tap/globs.js +++ b/test-tap/globs.js @@ -335,3 +335,25 @@ test('findFiles finds non-ignored files (.cjs, .jsx)', async t => { actual.sort(); t.same(actual, expected); }); + +test('normalizePatterns', t => { + const patterns = [ + 'test/sub/../**/*', + 'test/sub/', + 'test//sub/', + '!./test/sub/../**/*', + '!test/sub/', + '!test//sub/' + ]; + const expected = [ + 'test/**/*', + 'test/sub', + 'test/sub', + '!test/**/*', + '!test/sub', + '!test/sub' + ]; + const actual = globs.normalizePatterns(patterns); + t.same(actual, expected); + t.end(); +}); From 089f7346ffc5cc443ab1091a3297b6c2e0796ad9 Mon Sep 17 00:00:00 2001 From: Mark Wubben Date: Sat, 18 Sep 2021 19:10:38 +0200 Subject: [PATCH 2/3] Remove trailing slashes only --- lib/glob-helpers.cjs | 23 +++++++++++------------ test-tap/globs.js | 28 ++++++---------------------- 2 files changed, 17 insertions(+), 34 deletions(-) diff --git a/lib/glob-helpers.cjs b/lib/glob-helpers.cjs index f7942c8fd..3a0a51702 100644 --- a/lib/glob-helpers.cjs +++ b/lib/glob-helpers.cjs @@ -111,26 +111,25 @@ function normalizeFileForMatching(cwd, file) { exports.normalizeFileForMatching = normalizeFileForMatching; -const normalizeNegatedPattern = (cwd, pattern) => { - // Remove `!` from pattern - const patternPath = pattern.slice(1); - const relativePath = path.relative(cwd, patternPath); - return `!${relativePath}`; -}; - function normalizePattern(pattern) { - const cwd = process.cwd(); // Always use `/` in patterns, harmonizing matching across platforms if (process.platform === 'win32') { pattern = slash(pattern); } - if (pattern.startsWith('!')) { - return normalizeNegatedPattern(cwd, pattern); + if (pattern.endsWith('/')) { + pattern = pattern.slice(0, -1) + } + + if (pattern.startsWith('./')) { + return pattern.slice(2); + } + + if (pattern.startsWith('!./')) { + return `!${pattern.slice(3)}`; } - const normalizedPattern = path.relative(cwd, pattern); - return normalizedPattern; + return pattern; } exports.normalizePattern = normalizePattern; diff --git a/test-tap/globs.js b/test-tap/globs.js index 8160341e6..73db32d47 100644 --- a/test-tap/globs.js +++ b/test-tap/globs.js @@ -25,6 +25,12 @@ test('ignores relativeness in patterns', t => { t.end(); }); +test('ignores trailing slashes in (simple) patterns', t => { + const {filePatterns} = globs.normalizeGlobs({files: ['foo/', '!bar/', 'foo/{bar/,baz/}'], extensions: ['js'], providers: []}); + t.same(filePatterns, ['foo', '!bar', 'foo/{bar/,baz/}']); + t.end(); +}); + test('isTest with defaults', t => { const options = { ...globs.normalizeGlobs({ @@ -335,25 +341,3 @@ test('findFiles finds non-ignored files (.cjs, .jsx)', async t => { actual.sort(); t.same(actual, expected); }); - -test('normalizePatterns', t => { - const patterns = [ - 'test/sub/../**/*', - 'test/sub/', - 'test//sub/', - '!./test/sub/../**/*', - '!test/sub/', - '!test//sub/' - ]; - const expected = [ - 'test/**/*', - 'test/sub', - 'test/sub', - '!test/**/*', - '!test/sub', - '!test/sub' - ]; - const actual = globs.normalizePatterns(patterns); - t.same(actual, expected); - t.end(); -}); From b4c01b8e214f4a44c16b1b90d85a453cb1464000 Mon Sep 17 00:00:00 2001 From: Mark Wubben Date: Sat, 18 Sep 2021 19:14:53 +0200 Subject: [PATCH 3/3] Fix formatting --- lib/glob-helpers.cjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/glob-helpers.cjs b/lib/glob-helpers.cjs index 3a0a51702..1145f793e 100644 --- a/lib/glob-helpers.cjs +++ b/lib/glob-helpers.cjs @@ -118,7 +118,7 @@ function normalizePattern(pattern) { } if (pattern.endsWith('/')) { - pattern = pattern.slice(0, -1) + pattern = pattern.slice(0, -1); } if (pattern.startsWith('./')) {