Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalize glob patterns with trailing slashes #2788

Merged
merged 4 commits into from Sep 18, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 12 additions & 7 deletions lib/glob-helpers.cjs
Expand Up @@ -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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cwd needs to be an argument here. AVA determines the directory to which patterns are relevant and it may not be the CWD.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you for pointing this out, I see now I think we should receive the CWD as an argument in the normalizeGlobs here:

export function normalizeGlobs({extensions, files: filePatterns, ignoredByWatcher: ignoredByWatcherPatterns, providers}) {

Then we pass it to normalizePatterns call in the same function.

Also i noticed this line in cli.js

ava/lib/cli.js

Line 396 in dc93f37

pattern: normalizePattern(path.relative(projectDir, path.resolve(process.cwd(), pattern))),

which using nearly the same function path.relative, but path.resolve is being used in the second parameter to get the absolute path

// 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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the helper function is all that useful, especially since it hides the critical path.relative() bit which is then called directly below.

}

return pattern;
const normalizedPattern = path.relative(cwd, pattern);
return normalizedPattern;
}

exports.normalizePattern = normalizePattern;
Expand Down
22 changes: 22 additions & 0 deletions test-tap/globs.js
Expand Up @@ -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();
});