Skip to content

Commit

Permalink
fix(providence-analytics): optimisedGlob allows cwd with trailing slash
Browse files Browse the repository at this point in the history
  • Loading branch information
tlouisse committed Nov 6, 2024
1 parent 0582868 commit cc2a646
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/happy-bears-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'providence-analytics': patch
---

optimisedGlob: allow providing cwd with trailing slash
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ export const parseGlobToRegex = memoize(
}
regexResultStr += currentChar;
}

return new RegExp(`^${regexResultStr}$`);
},
);
Expand All @@ -138,6 +137,15 @@ function isRootGlob(glob) {
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 {{cwd:string}} cfg
Expand Down Expand Up @@ -369,6 +377,8 @@ export async function optimisedGlob(globOrGlobs, providedOptions = {}) {
...providedOptions,
};

options.cwd = normalizeCwd(options.cwd);

if (!options.onlyFiles) {
// This makes behavior aligned with globby
options.onlyDirectories = true;
Expand Down Expand Up @@ -403,6 +413,7 @@ export async function optimisedGlob(globOrGlobs, providedOptions = {}) {
globstar: options.globstar,
extglob: options.extglob,
});

if (isNegative) {
matchRegexesNegative.push(regexForGlob);
} else {
Expand All @@ -416,8 +427,8 @@ export async function optimisedGlob(globOrGlobs, providedOptions = {}) {
const fullStartPath = path.join(cwd, startPath);
try {
const allDirEntsRelativeToCwd = await getAllDirentsRelativeToCwd(fullStartPath, {
cwd,
fs: options.fs,
cwd,
});

globEntries.push(...allDirEntsRelativeToCwd);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,37 @@ function runSuiteForOptimisedGlob() {
'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', () => {
Expand Down Expand Up @@ -351,6 +382,15 @@ function runSuiteForOptimisedGlob() {
});
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']);
});
});
});
}
Expand Down

0 comments on commit cc2a646

Please sign in to comment.