From 937d4e7f738642b4d08496347c4b868864e2c758 Mon Sep 17 00:00:00 2001 From: Tyler Biethman Date: Tue, 16 Aug 2022 10:09:59 -0500 Subject: [PATCH] fix: ensure absolute directories in glob patterns are leading prior to removal (#23368) * fix: ensure absolute directories in glob patterns are leading prior to removal * Removing errant slash --- .../data-context/src/sources/FileDataSource.ts | 6 +++++- .../test/unit/sources/FileDataSource.spec.ts | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/packages/data-context/src/sources/FileDataSource.ts b/packages/data-context/src/sources/FileDataSource.ts index 410776163a43..73351368be32 100644 --- a/packages/data-context/src/sources/FileDataSource.ts +++ b/packages/data-context/src/sources/FileDataSource.ts @@ -36,7 +36,11 @@ export class FileDataSource { // The working directory path may include characters that conflict with glob // syntax (brackets, parentheses, etc.) and cause our searches to inadvertently fail. // We scope our search to the working directory using the `cwd` globby option. - return globPattern.replace(cwd, '.') + if (globPattern.startsWith(cwd)) { + return globPattern.replace(cwd, '.') + } + + return globPattern }) const ignoreGlob = (globOptions?.ignore ?? []).concat('**/node_modules/**') diff --git a/packages/data-context/test/unit/sources/FileDataSource.spec.ts b/packages/data-context/test/unit/sources/FileDataSource.spec.ts index c7f02869435f..9d8f2a3c55a6 100644 --- a/packages/data-context/test/unit/sources/FileDataSource.spec.ts +++ b/packages/data-context/test/unit/sources/FileDataSource.spec.ts @@ -58,6 +58,24 @@ describe('FileDataSource', () => { expect(files).to.have.length(2) }) + it('does not replace working directory in glob pattern if it is not leading', async () => { + // Create a redundant structure within the project dir matching its absolute path + // and write a new script in that location + const nestedScriptPath = path.join(projectPath, 'cypress', projectPath) + + await fs.mkdirs(nestedScriptPath) + await fs.writeFile(path.join(nestedScriptPath, 'nested-script.js'), '') + + // Verify that the glob pattern is not impacted if if contains directories equivalent + // to the working directory + let files = await fileDataSource.getFilesByGlob( + projectPath, + `./cypress${projectPath}/nested-script.js`, + ) + + expect(files).to.have.length(1) + }) + it('finds files matching multiple patterns', async () => { const files = await fileDataSource.getFilesByGlob( projectPath,