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

fix: (cspell-glob) Make sure cwd can be set. #2316

Merged
merged 1 commit into from
Jan 24, 2022
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
84 changes: 43 additions & 41 deletions packages/cspell-glob/src/GlobMatcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,50 +226,52 @@ describe('Validate Options', () => {
pattern: string;
file: string;
options: string | GlobMatchOptions | undefined;
root: string | undefined;
expected: Partial<GlobMatch> | boolean;
}
test.each`
pattern | file | options | expected
${'*.yaml'} | ${'.github/workflows/test.yaml'} | ${{}} | ${{ matched: true }}
${'*.yaml'} | ${'.github/workflows/test.yaml'} | ${{ dot: false }} | ${{ matched: false }}
${'*.yaml'} | ${'.github/workflows/test.yaml'} | ${{ mode: 'include' }} | ${{ matched: false }}
${'*.yaml'} | ${'.github/workflows/test.yaml'} | ${{ dot: true }} | ${{ matched: true, pattern: p('**/*.yaml') }}
${'*.yaml'} | ${'.github/workflows/test.yaml'} | ${{ dot: true }} | ${true}
${'**/*.yaml'} | ${'.github/workflows/test.yaml'} | ${{ mode: 'exclude' }} | ${{ matched: true }}
${'**/*.yaml'} | ${'.github/workflows/test.yaml'} | ${{ mode: 'include' }} | ${{ matched: false }}
${'.github/**/*.yaml'} | ${'.github/workflows/test.yaml'} | ${{ dot: true }} | ${true}
${'.github/**/*.yaml'} | ${'.github/workflows/test.yaml'} | ${{ dot: false }} | ${true}
${'.github/**/*.yaml'} | ${'.github/workflows/test.yaml'} | ${{}} | ${true}
${'.github/**/*.yaml'} | ${'.github/test.yaml'} | ${{}} | ${true}
${'.github/**/*.yaml'} | ${'package/.github/workflows/test.yaml'} | ${{}} | ${false}
${'**/.github/**/*.yaml'} | ${'package/.github/workflows/test.yaml'} | ${{}} | ${true}
${'.github'} | ${'package/.github/workflows/test.yaml'} | ${{}} | ${true}
${'**/.github/**'} | ${'package/.github/workflows/test.yaml'} | ${{}} | ${true}
${'package/**'} | ${'package/.github/workflows/test.yaml'} | ${{}} | ${true}
${'package/**'} | ${'package/.github/workflows/test.yaml'} | ${{ dot: false }} | ${false}
${'package/**'} | ${'package/.github/workflows/test.yaml'} | ${{ mode: 'include' }} | ${false}
${'workflows'} | ${'package/.github/workflows/test.yaml'} | ${{}} | ${true}
${'workflows'} | ${'package/.github/workflows/test.yaml'} | ${{ dot: false }} | ${false}
${'package/'} | ${'package/src/test.yaml'} | ${{}} | ${true}
${'package/'} | ${'package/src/test.yaml'} | ${{ dot: false }} | ${true}
${'package/'} | ${'package/src/test.yaml'} | ${{ mode: 'include' }} | ${true}
${'package/'} | ${'repo/package/src/test.yaml'} | ${{}} | ${true}
${'package/'} | ${'repo/package/src/test.yaml'} | ${{ mode: 'include' }} | ${false}
${'/package/'} | ${'package/src/test.yaml'} | ${{}} | ${true}
${'/package/'} | ${'package/src/test.yaml'} | ${{ dot: false }} | ${true}
${'/package/'} | ${'package/src/test.yaml'} | ${{ mode: 'include' }} | ${true}
${'/package/'} | ${'repo/package/src/test.yaml'} | ${{}} | ${false}
${'/package/'} | ${'repo/package/src/test.yaml'} | ${{ mode: 'include' }} | ${false}
${'src'} | ${'package/src/test.yaml'} | ${{ mode: 'include' }} | ${false}
${'*.yaml|!test.yaml'} | ${'.github/workflows/test.yaml'} | ${{}} | ${{ matched: false, pattern: p('!**/test.yaml'), isNeg: true }}
${'*.yaml|!/test.yaml'} | ${'test.yaml'} | ${{}} | ${{ matched: false, pattern: p('!test.yaml'), isNeg: true }}
${'*.yaml|!/node_modules'} | ${'node_modules/test.yaml'} | ${{}} | ${{ matched: false, pattern: p('!node_modules/**'), isNeg: true }}
${'*.{!yaml}'} | ${'.github/workflows/test.yaml'} | ${{}} | ${false}
${'test.*|!*.{yaml,yml}'} | ${'.github/workflows/test.yaml'} | ${{}} | ${{ matched: false, isNeg: true }}
${'i18/nl_NL'} | ${'i18/nl_NL/file.txt'} | ${{ mode: 'exclude' }} | ${{ matched: true }}
${'i18/nl_NL'} | ${'code/i18/nl_NL/file.txt'} | ${{ mode: 'exclude' }} | ${{ matched: false }}
`('Test options: $pattern, $file, $options', ({ pattern, file, options, expected }: TestCase) => {
const root = '/Users/code/project/cspell/';
pattern | file | root | options | expected
${'*.yaml'} | ${'.github/workflows/test.yaml'} | ${undefined} | ${{}} | ${{ matched: true }}
${'*.yaml'} | ${'.github/workflows/test.yaml'} | ${undefined} | ${{ dot: false }} | ${{ matched: false }}
${'*.yaml'} | ${'.github/workflows/test.yaml'} | ${undefined} | ${{ mode: 'include' }} | ${{ matched: false }}
${'*.yaml'} | ${'.github/workflows/test.yaml'} | ${undefined} | ${{ dot: true }} | ${{ matched: true, pattern: p('**/*.yaml') }}
${'*.yaml'} | ${'.github/workflows/test.yaml'} | ${undefined} | ${{ dot: true }} | ${true}
${'**/*.yaml'} | ${'.github/workflows/test.yaml'} | ${undefined} | ${{ mode: 'exclude' }} | ${{ matched: true }}
${'**/*.yaml'} | ${'.github/workflows/test.yaml'} | ${undefined} | ${{ mode: 'include' }} | ${{ matched: false }}
${'.github/**/*.yaml'} | ${'.github/workflows/test.yaml'} | ${undefined} | ${{ dot: true }} | ${true}
${'.github/**/*.yaml'} | ${'.github/workflows/test.yaml'} | ${undefined} | ${{ dot: false }} | ${true}
${'.github/**/*.yaml'} | ${'.github/workflows/test.yaml'} | ${undefined} | ${{}} | ${true}
${'.github/**/*.yaml'} | ${'.github/test.yaml'} | ${undefined} | ${{}} | ${true}
${'.github/**/*.yaml'} | ${'package/.github/workflows/test.yaml'} | ${undefined} | ${{}} | ${false}
${'**/.github/**/*.yaml'} | ${'package/.github/workflows/test.yaml'} | ${undefined} | ${{}} | ${true}
${'.github'} | ${'package/.github/workflows/test.yaml'} | ${undefined} | ${{}} | ${true}
${'**/.github/**'} | ${'package/.github/workflows/test.yaml'} | ${undefined} | ${{}} | ${true}
${'package/**'} | ${'package/.github/workflows/test.yaml'} | ${undefined} | ${{}} | ${true}
${'package/**'} | ${'package/.github/workflows/test.yaml'} | ${undefined} | ${{ dot: false }} | ${false}
${'package/**'} | ${'package/.github/workflows/test.yaml'} | ${undefined} | ${{ mode: 'include' }} | ${false}
${'workflows'} | ${'package/.github/workflows/test.yaml'} | ${undefined} | ${{}} | ${true}
${'workflows'} | ${'package/.github/workflows/test.yaml'} | ${undefined} | ${{ dot: false }} | ${false}
${'package/'} | ${'package/src/test.yaml'} | ${undefined} | ${{}} | ${true}
${'package/'} | ${'package/src/test.yaml'} | ${undefined} | ${{ dot: false }} | ${true}
${'package/'} | ${'package/src/test.yaml'} | ${undefined} | ${{ mode: 'include' }} | ${true}
${'package/'} | ${'repo/package/src/test.yaml'} | ${undefined} | ${{}} | ${true}
${'package/'} | ${'repo/package/src/test.yaml'} | ${undefined} | ${{ mode: 'include' }} | ${false}
${'/package/'} | ${'package/src/test.yaml'} | ${undefined} | ${{}} | ${true}
${'/package/'} | ${'package/src/test.yaml'} | ${undefined} | ${{ dot: false }} | ${true}
${'/package/'} | ${'package/src/test.yaml'} | ${undefined} | ${{ mode: 'include' }} | ${true}
${'/package/'} | ${'repo/package/src/test.yaml'} | ${undefined} | ${{}} | ${false}
${'/package/'} | ${'repo/package/src/test.yaml'} | ${undefined} | ${{ mode: 'include' }} | ${false}
${'src'} | ${'package/src/test.yaml'} | ${undefined} | ${{ mode: 'include' }} | ${false}
${'*.yaml|!test.yaml'} | ${'.github/workflows/test.yaml'} | ${undefined} | ${{}} | ${{ matched: false, pattern: p('!**/test.yaml'), isNeg: true }}
${'*.yaml|!/test.yaml'} | ${'test.yaml'} | ${undefined} | ${{}} | ${{ matched: false, pattern: p('!test.yaml'), isNeg: true }}
${'*.yaml|!/node_modules'} | ${'node_modules/test.yaml'} | ${undefined} | ${{}} | ${{ matched: false, pattern: p('!node_modules/**'), isNeg: true }}
${'*.{!yaml}'} | ${'.github/workflows/test.yaml'} | ${undefined} | ${{}} | ${false}
${'test.*|!*.{yaml,yml}'} | ${'.github/workflows/test.yaml'} | ${undefined} | ${{}} | ${{ matched: false, isNeg: true }}
${'i18/nl_NL'} | ${'i18/nl_NL/file.txt'} | ${undefined} | ${{ mode: 'exclude' }} | ${{ matched: true }}
${'i18/nl_NL'} | ${'code/i18/nl_NL/file.txt'} | ${undefined} | ${{ mode: 'exclude' }} | ${{ matched: false }}
${'${cwd}/**/i18/nl_NL'} | ${'code/i18/nl_NL/file.txt'} | ${process.cwd()} | ${{ mode: 'exclude' }} | ${{ matched: true }}
`('Test options: $pattern, $file, $options, root', ({ pattern, file, options, root, expected }: TestCase) => {
root = root || '/Users/code/project/cspell/';
const filename = path.join(root, file);
const patterns = pattern.split('|');
options == options ?? root;
Expand Down
22 changes: 20 additions & 2 deletions packages/cspell-glob/src/GlobMatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ interface NormalizedGlobMatchOptions {
*/
root: string;

/**
* The directory to use as the current working directory.
* @default: process.cwd();
*/
cwd: string;

/**
* Allows matching against directories with a leading `.`.
*
Expand Down Expand Up @@ -111,11 +117,12 @@ export class GlobMatcher {
dot = isExcludeMode,
nodePath = _nodePath,
nested = isExcludeMode,
cwd = process.cwd(),
nobrace,
} = options;
} = clean(options);

const normalizedRoot = nodePath.resolve(nodePath.normalize(root));
this.options = { root: normalizedRoot, dot, nodePath, nested, mode, nobrace };
this.options = { root: normalizedRoot, dot, nodePath, nested, mode, nobrace, cwd };

patterns = Array.isArray(patterns)
? patterns
Expand Down Expand Up @@ -219,3 +226,14 @@ function buildMatcherFn(patterns: GlobPatternWithRoot[], options: NormalizedGlob
};
return fn;
}

function clean<T>(obj: T): T {
if (typeof obj !== 'object') return obj;
const r = <Record<string, unknown>>obj;
for (const key of Object.keys(r)) {
if (r[key] === undefined) {
delete r[key];
}
}
return obj;
}
6 changes: 6 additions & 0 deletions packages/cspell-glob/src/globHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ describe('Validate fileOrGlobToGlob', () => {
file | root | path | expected | comment
${'*.json'} | ${'.'} | ${posix} | ${g('*.json', pp('.'))} | ${'posix'}
${'*.json'} | ${'.'} | ${win32} | ${g('*.json', pw('.'))} | ${'win32'}
${{ glob: '*.json' }} | ${'.'} | ${posix} | ${g('*.json', pp('.'))} | ${'posix'}
${{ glob: '*.json', root: pp('./data') }} | ${'.'} | ${posix} | ${g('*.json', pp('./data'))} | ${'posix'}
${pp('./*.json')} | ${'.'} | ${posix} | ${g('*.json', pp('.'))} | ${''}
${pw('./*.json')} | ${'.'} | ${win32} | ${g('*.json', pw('.'))} | ${''}
${pp('./package.json')} | ${'.'} | ${posix} | ${g('package.json', pp('.'))} | ${''}
Expand Down Expand Up @@ -303,6 +305,10 @@ describe('Validate Glob Normalization to root', () => {
normalizeGlobToRoot(p, root, path)
);
expect(r).toEqual(expectedGlobs);
const again = normalizeGlobPatterns(r, { root, nested: false, nodePath: path }).map((p) =>
normalizeGlobToRoot(p, root, path)
);
expect(again).toEqual(r);
});

function nOpts(opts: Partial<NormalizeOptions> = {}): Required<NormalizeOptions> {
Expand Down