Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/cspell-gitignore/src/GitIgnoreFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ describe('GitIgnoreFile', () => {
expect(gif?.isIgnored(require.resolve('vitest'))).toBe(true);
});

test.each`
patterns | filename | expected
${['!keep.txt', '*.txt']} | ${'keep.txt'} | ${true}
${['*.txt', '!a.txt', 'a.txt']} | ${'a.txt'} | ${true}
${['tmp/*', '!tmp/.gitkeep']} | ${'tmp/.gitkeep'} | ${false}
${['tmp/*', '!tmp/.gitkeep']} | ${'tmp/a.txt'} | ${true}
`('honors rule order in a parsed .gitignore: $patterns, $filename', ({ patterns, filename, expected }) => {
const content = `# Ordered ignore rules\n\n${patterns.join('\n')}\n`;
const gif = GitIgnoreFile.parseGitignore(content, path.join(__dirname, '.gitignore'));

expect(gif.isIgnored(path.join(__dirname, filename))).toBe(expected);
});

test('getGlobs', () => {
const gif = sampleGitIgnoreFile();
expect(gif.getGlobs(__dirname).sort()).toEqual([
Expand Down
22 changes: 22 additions & 0 deletions packages/cspell-glob/src/GlobMatcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,28 @@ describe('Tests .gitignore file contents', () => {
});
});

describe('Rule ordering', () => {
test.each`
patterns | filename | expected
${['*ignored/', '!un-ignored/', 'node_modules/']} | ${'/test/un-ignored/node_modules/test.txt'} | ${true}
${['*ignored/', '!un-ignored/', 'node_modules/', '!un-ignored/node_modules/']} | ${'/test/un-ignored/node_modules/test.txt'} | ${false}
${['!keep.txt', '*.txt']} | ${'/test/keep.txt'} | ${true}
${['*.txt', '!a.txt', 'a.txt']} | ${'/test/a.txt'} | ${true}
${['tmp/*', '!tmp/.gitkeep']} | ${'/test/tmp/.gitkeep'} | ${false}
${['tmp/*', '!tmp/.gitkeep']} | ${'/test/tmp/a.txt'} | ${true}
`('honors the final matching ignore rule', ({ patterns, filename, expected }) => {
const matcher = new GlobMatcher(patterns, { root: '/test' });

expect(matcher.match(filename)).toBe(expected);
});

test('reports the first matching generated variant from one source rule', () => {
const matcher = new GlobMatcher('*.js', { root: '/test' });

expect(matcher.matchEx('/test/file.js')).toEqual(expect.objectContaining({ glob: '**/*.js', index: 0, isNeg: false, matched: true }));
});
});

describe('Tests .gitignore like file contents', () => {
const pattern = `
# This is a comment
Expand Down
26 changes: 15 additions & 11 deletions packages/cspell-glob/src/GlobMatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export class GlobMatcher {
this.patterns = globPatterns;
this.root = normalizedRoot;
this.dot = dot;
this.matchEx = buildMatcherFn(this.id, this.patterns, this.options);
this.matchEx = buildMatcherFn(this.id, globPatterns, this.options);
}

/**
Expand All @@ -190,7 +190,7 @@ type GlobMatchFn = (filename: string) => GlobMatch;

interface GlobRule {
/** The pattern */
pattern: GlobPatternWithRoot;
pattern: GlobPatternNormalized;
/**
* Index of the glob in the list.
*/
Expand Down Expand Up @@ -221,7 +221,7 @@ interface GlobRule {
*/
function buildMatcherFn(
_id: number,
patterns: GlobPatternWithRoot[],
patterns: GlobPatternNormalized[],
options: NormalizedGlobMatchOptions,
): GlobMatchFn {
// outputBuildMatcherFnPerfData(_id, patterns, options);
Expand Down Expand Up @@ -250,9 +250,6 @@ function buildMatcherFn(
};
return { pattern, index, isNeg, fn, reg };
});
const negRules = rules.filter((r) => r.isNeg);
const posRules = rules.filter((r) => !r.isNeg);

const mapRoots = new Map<string, URL>();

// const negRegEx = negRules.map((r) => r.reg).map((r) => r.toString());
Expand Down Expand Up @@ -285,7 +282,9 @@ function buildMatcherFn(
return lastRel;
}

function testRules(rules: GlobRule[], matched: boolean): GlobMatch | undefined {
function testRules(): GlobMatch | undefined {
let result: GlobMatch | undefined;
let lastMatchedPattern: GlobPatternNormalized | undefined;
for (const rule of rules) {
const pattern = rule.pattern;
const root = pattern.root;
Expand All @@ -305,20 +304,25 @@ function buildMatcherFn(
const relPathToFile = relativeToRoot(rootURL);
isMatch = isRelativeValueNested(relPathToFile) && rule.fn(relPathToFile);
}
if (isMatch) {
return {
matched,
if (
isMatch &&
(pattern.rawRoot !== lastMatchedPattern?.rawRoot || pattern.rawGlob !== lastMatchedPattern?.rawGlob)
) {
result = {
matched: !rule.isNeg,
glob: pattern.glob,
root,
pattern,
index: rule.index,
isNeg: rule.isNeg,
};
lastMatchedPattern = pattern;
}
}
return result;
}

const result = testRules(negRules, false) || testRules(posRules, true) || { matched: false };
const result = testRules() || { matched: false };
traceMode && logMatchTest(_id, filename, result);
return result;
};
Expand Down