fix(providence-analytics): optimisedGlob allows cwd with trailing slash

This commit is contained in:
Thijs Louisse 2024-11-06 11:48:51 +01:00 committed by Thijs Louisse
parent 0582868e41
commit cc2a646295
3 changed files with 58 additions and 2 deletions

View file

@ -0,0 +1,5 @@
---
'providence-analytics': patch
---
optimisedGlob: allow providing cwd with trailing slash

View file

@ -116,7 +116,6 @@ export const parseGlobToRegex = memoize(
} }
regexResultStr += currentChar; regexResultStr += currentChar;
} }
return new RegExp(`^${regexResultStr}$`); return new RegExp(`^${regexResultStr}$`);
}, },
); );
@ -138,6 +137,15 @@ function isRootGlob(glob) {
return glob.startsWith('/') || glob.startsWith('!/') || Boolean(glob.match(/^([A-Z]:\\|\\\\)/)); return glob.startsWith('/') || glob.startsWith('!/') || Boolean(glob.match(/^([A-Z]:\\|\\\\)/));
} }
/**
* Makes sure cwd does not end with a slash
* @param {string} str
* @returns {string}
*/
function normalizeCwd(str) {
return str.endsWith('/') ? str.slice(0, -1) : str;
}
/** /**
* @param {DirentWithPath} dirent * @param {DirentWithPath} dirent
* @param {{cwd:string}} cfg * @param {{cwd:string}} cfg
@ -369,6 +377,8 @@ export async function optimisedGlob(globOrGlobs, providedOptions = {}) {
...providedOptions, ...providedOptions,
}; };
options.cwd = normalizeCwd(options.cwd);
if (!options.onlyFiles) { if (!options.onlyFiles) {
// This makes behavior aligned with globby // This makes behavior aligned with globby
options.onlyDirectories = true; options.onlyDirectories = true;
@ -403,6 +413,7 @@ export async function optimisedGlob(globOrGlobs, providedOptions = {}) {
globstar: options.globstar, globstar: options.globstar,
extglob: options.extglob, extglob: options.extglob,
}); });
if (isNegative) { if (isNegative) {
matchRegexesNegative.push(regexForGlob); matchRegexesNegative.push(regexForGlob);
} else { } else {
@ -416,8 +427,8 @@ export async function optimisedGlob(globOrGlobs, providedOptions = {}) {
const fullStartPath = path.join(cwd, startPath); const fullStartPath = path.join(cwd, startPath);
try { try {
const allDirEntsRelativeToCwd = await getAllDirentsRelativeToCwd(fullStartPath, { const allDirEntsRelativeToCwd = await getAllDirentsRelativeToCwd(fullStartPath, {
cwd,
fs: options.fs, fs: options.fs,
cwd,
}); });
globEntries.push(...allDirEntsRelativeToCwd); globEntries.push(...allDirEntsRelativeToCwd);

View file

@ -157,6 +157,37 @@ function runSuiteForOptimisedGlob() {
'my/folder/some/file.js', 'my/folder/some/file.js',
]); ]);
}); });
it('supports patterns like "my", "my/**" and "my/**/*" ', async () => {
const files = await runOptimisedGlobAndCheckGlobbyParity('my/**', testCfg);
const allMy = [
'my/folder/some/anotherFile.d.ts',
'my/folder/some/anotherFile.js',
'my/folder/some/file.d.ts',
'my/folder/some/file.js',
'my/folder/lvl1/some/anotherFile.d.ts',
'my/folder/lvl1/some/anotherFile.js',
'my/folder/lvl1/some/file.d.ts',
'my/folder/lvl1/some/file.js',
'my/folder/lvl1/lvl2/some/anotherFile.d.ts',
'my/folder/lvl1/lvl2/some/anotherFile.js',
'my/folder/lvl1/lvl2/some/file.d.ts',
'my/folder/lvl1/lvl2/some/file.js',
'my/folder/lvl1/lvl2/lvl3/some/anotherFile.d.ts',
'my/folder/lvl1/lvl2/lvl3/some/anotherFile.js',
'my/folder/lvl1/lvl2/lvl3/some/file.d.ts',
'my/folder/lvl1/lvl2/lvl3/some/file.js',
];
expect(files).to.deep.equal(allMy);
const files2 = await runOptimisedGlobAndCheckGlobbyParity('my/**/*', testCfg);
expect(files2).to.deep.equal(allMy);
// TODO: "my" (this will need a code change: preprocess 'my' to 'my/**')
});
}); });
describe('Accolade patterns', () => { describe('Accolade patterns', () => {
@ -351,6 +382,15 @@ function runSuiteForOptimisedGlob() {
}); });
expect(files).to.deep.equal(['file.js']); expect(files).to.deep.equal(['file.js']);
}); });
it('supports cwd ending with "/"', async () => {
const files = await runOptimisedGlobAndCheckGlobbyParity('my/folder/*/some/file.js', {
...testCfg,
cwd: '/fakeFs/',
});
expect(files).to.deep.equal(['my/folder/lvl1/some/file.js']);
});
}); });
}); });
} }