From 8049075b891ca55194b557310fe8ac434aaed3e0 Mon Sep 17 00:00:00 2001 From: GweesinChan Date: Sun, 1 Sep 2024 17:25:21 +0800 Subject: [PATCH 01/14] fix: fix some unit tests errors on win --- .../data-context/src/codegen/code-generator.ts | 3 ++- .../test/unit/sources/FileDataSource.spec.ts | 14 +++++++------- .../test/unit/sources/ProjectDataSource.spec.ts | 13 +++++++------ 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/packages/data-context/src/codegen/code-generator.ts b/packages/data-context/src/codegen/code-generator.ts index f9c7250797a8..aeeecbd973c0 100644 --- a/packages/data-context/src/codegen/code-generator.ts +++ b/packages/data-context/src/codegen/code-generator.ts @@ -5,6 +5,7 @@ import * as ejs from 'ejs' import fm from 'front-matter' import _ from 'lodash' import Debug from 'debug' +import { toPosix } from '../util' const debug = Debug('cypress:data-context:codegen:code-generator') @@ -179,7 +180,7 @@ export async function hasNonExampleSpec (testTemplateDir: string, specs: string[ return templateFiles.some((templateFile) => templateFile.substring(testTemplateDir.length + 1) === spec) } - return specs.some((spec) => !specInTemplates(spec)) + return specs.map(((spec) => toPosix(spec))).some((spec) => !specInTemplates(spec)) } export async function getExampleSpecPaths (testTemplateDir: string): Promise { diff --git a/packages/data-context/test/unit/sources/FileDataSource.spec.ts b/packages/data-context/test/unit/sources/FileDataSource.spec.ts index 33503f096b6d..532db28fbccd 100644 --- a/packages/data-context/test/unit/sources/FileDataSource.spec.ts +++ b/packages/data-context/test/unit/sources/FileDataSource.spec.ts @@ -42,8 +42,8 @@ describe('FileDataSource', () => { ) expect(files).to.have.length(2) - expect(files[0]).to.eq(path.join(projectPath, 'root-script-1.js')) - expect(files[1]).to.eq(path.join(projectPath, 'root-script-2.js')) + expect(files[0]).to.eq(fileUtil.toPosix(path.join(projectPath, 'root-script-1.js'))) + expect(files[1]).to.eq(fileUtil.toPosix(path.join(projectPath, 'root-script-2.js'))) }) it('finds files matching relative patterns in working dir', async () => { @@ -213,7 +213,7 @@ describe('FileDataSource', () => { expect(files).to.eq(mockMatches) expect(matchGlobsStub).to.have.been.calledWith( - ['cypress/e2e/**.cy.js'], + ['/cypress/e2e/**.cy.js'], { ...defaultGlobbyOptions, cwd: '/' }, ) }) @@ -279,7 +279,7 @@ describe('FileDataSource', () => { expect(files).to.eq(mockMatches) expect(matchGlobsStub).to.have.been.calledWith( - ['cypress/e2e/**.cy.js'], + ['/cypress/e2e/**.cy.js'], { ...defaultGlobbyOptions, cwd: '/', @@ -317,8 +317,8 @@ describe('FileDataSource', () => { expect(files).to.eq(mockMatches) expect(matchGlobsStub).to.have.been.calledWith( [ - 'node_modules/cypress/e2e/**.cy.js', - 'cypress/e2e/**.cy.js', + '/node_modules/cypress/e2e/**.cy.js', + '/cypress/e2e/**.cy.js', ], { ...defaultGlobbyOptions, @@ -355,7 +355,7 @@ describe('FileDataSource', () => { expect(files).to.eq(mockMatches) expect(matchGlobsStub).to.have.been.calledWith( - ['cypress/e2e/**.cy.js'], + ['/cypress/e2e/**.cy.js'], { ...defaultGlobbyOptions, cwd: '/', diff --git a/packages/data-context/test/unit/sources/ProjectDataSource.spec.ts b/packages/data-context/test/unit/sources/ProjectDataSource.spec.ts index 110d450953ce..4fed71f68331 100644 --- a/packages/data-context/test/unit/sources/ProjectDataSource.spec.ts +++ b/packages/data-context/test/unit/sources/ProjectDataSource.spec.ts @@ -14,6 +14,7 @@ import type { FindSpecs } from '../../../src/actions' import { createTestDataContext } from '../helper' import { defaultExcludeSpecPattern, defaultSpecPattern } from '@packages/config' import FixturesHelper from '@tooling/system-tests' +import { toPosix } from '../../../src/util' chai.use(sinonChai) const { expect } = chai @@ -265,7 +266,7 @@ describe('getLongestCommonPrefixFromPaths', () => { 'cypress/component/bar/meta-component-test.cy.ts', ]) - expect(lcp).to.equal('cypress/component') + expect(toPosix(lcp)).to.equal('cypress/component') }) it('with src and cypress', () => { @@ -292,7 +293,7 @@ describe('getLongestCommonPrefixFromPaths', () => { 'src/frontend/MyComponent.cy.ts', ]) - expect(lcp).to.equal('src/frontend') + expect(toPosix(lcp)).to.equal('src/frontend') }) }) @@ -776,14 +777,14 @@ describe('ProjectDataSource', () => { const defaultSpecFileName = await ctx.project.defaultSpecFileName() - expect(defaultSpecFileName).to.equal('cypress/e2e/spec.cy.js') + expect(toPosix(defaultSpecFileName)).to.equal('cypress/e2e/spec.cy.js') }) it('yields default if the spec pattern is default', async () => { sinon.stub(ctx.project, 'specPatterns').resolves({ specPattern: [defaultSpecPattern.e2e] }) const defaultSpecFileName = await ctx.project.defaultSpecFileName() - expect(defaultSpecFileName).to.equal('cypress/e2e/spec.cy.js') + expect(toPosix(defaultSpecFileName)).to.equal('cypress/e2e/spec.cy.js') }) it('yields common prefix if there are existing specs', async () => { @@ -795,7 +796,7 @@ describe('ProjectDataSource', () => { const defaultSpecFileName = await ctx.project.defaultSpecFileName() - expect(defaultSpecFileName).to.equal('cypress/e2e/foo/spec.cy.js') + expect(toPosix(defaultSpecFileName)).to.equal('cypress/e2e/foo/spec.cy.js') }) it('yields spec pattern guess if there are no existing specs', async () => { @@ -831,7 +832,7 @@ describe('ProjectDataSource', () => { const defaultSpecFileName = await ctx.project.defaultSpecFileName() - expect(defaultSpecFileName).to.equal('cypress/component/ComponentName.cy.jsx', defaultSpecFileName) + expect(toPosix(defaultSpecFileName)).to.equal('cypress/component/ComponentName.cy.jsx', defaultSpecFileName) }) it('yields non-jsx extension if there are jsx files but specPattern disallows', async () => { From bfec809677f6b3c0dcc53248a75171e2f0e3bf70 Mon Sep 17 00:00:00 2001 From: GweesinChan Date: Wed, 4 Sep 2024 14:29:14 +0800 Subject: [PATCH 02/14] fix: fix line wrap causes errors --- packages/data-context/src/util/file.ts | 4 ++++ .../test/unit/codegen/code-generator.spec.ts | 13 ++++++------- packages/data-context/test/unit/helper.ts | 6 ++++++ .../test/unit/sources/ProjectDataSource.spec.ts | 6 +++--- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/packages/data-context/src/util/file.ts b/packages/data-context/src/util/file.ts index 307d4f782092..fffbe83c1ccf 100644 --- a/packages/data-context/src/util/file.ts +++ b/packages/data-context/src/util/file.ts @@ -3,3 +3,7 @@ import path from 'path' export function toPosix (file: string, sep: string = path.sep) { return file.split(sep).join(path.posix.sep) } + +export function toOS (file: string, sep: string = path.posix.sep) { + return file.split(sep).join(path.sep) +} diff --git a/packages/data-context/test/unit/codegen/code-generator.spec.ts b/packages/data-context/test/unit/codegen/code-generator.spec.ts index 01964fec5002..b3df266a0490 100644 --- a/packages/data-context/test/unit/codegen/code-generator.spec.ts +++ b/packages/data-context/test/unit/codegen/code-generator.spec.ts @@ -1,6 +1,5 @@ import { parse } from '@babel/parser' import { expect } from 'chai' -import dedent from 'dedent' import fs from 'fs-extra' import path from 'path' import { DataContext } from '../../../src' @@ -14,7 +13,7 @@ import { } from '../../../src/codegen/code-generator' import { SpecOptions } from '../../../src/codegen/spec-options' import templates from '../../../src/codegen/templates' -import { createTestDataContext } from '../helper' +import { createTestDataContext, dedentWithOSLineWrap } from '../helper' import { CT_FRAMEWORKS } from '@packages/scaffold-config' import { defaultSpecPattern } from '@packages/config' @@ -128,7 +127,7 @@ describe('code-generator', () => { type: 'text', status: 'add', file: fileAbsolute, - content: `${dedent` + content: `${dedentWithOSLineWrap` describe('template spec', () => { it('passes', () => { cy.visit('https://example.cypress.io') @@ -167,7 +166,7 @@ describe('code-generator', () => { type: 'text', status: 'add', file: fileAbsolute, - content: dedent` + content: dedentWithOSLineWrap` describe('Button.tsx', () => { it('playground', () => { // cy.mount() @@ -208,7 +207,7 @@ describe('code-generator', () => { type: 'text', status: 'add', file: fileAbsolute, - content: dedent`import ${codeGenArgs.componentName} from '${codeGenArgs.componentPath}' + content: dedentWithOSLineWrap`import ${codeGenArgs.componentName} from '${codeGenArgs.componentPath}' describe('<${codeGenArgs.componentName} />', () => { it('renders', () => { @@ -253,7 +252,7 @@ describe('code-generator', () => { type: 'text', status: 'add', file: fileAbsolute, - content: dedent` + content: dedentWithOSLineWrap` import React from 'react' import { ${codeGenArgs.componentName} } from '${codeGenArgs.componentPath}' @@ -300,7 +299,7 @@ describe('code-generator', () => { type: 'text', status: 'add', file: fileAbsolute, - content: dedent` + content: dedentWithOSLineWrap` import React from 'react' import ${codeGenArgs.componentName} from '${codeGenArgs.componentPath}' diff --git a/packages/data-context/test/unit/helper.ts b/packages/data-context/test/unit/helper.ts index b2f5be39ec6d..e6df98f9b29f 100644 --- a/packages/data-context/test/unit/helper.ts +++ b/packages/data-context/test/unit/helper.ts @@ -17,6 +17,8 @@ import { remoteSchema } from '@packages/graphql/src/stitching/remoteSchema' import type { OpenModeOptions, RunModeOptions } from '@packages/types' import { MAJOR_VERSION_FOR_CONTENT } from '@packages/types' import { RelevantRunInfo } from '../../src/gen/graphcache-config.gen' +import dedent from 'dedent' +import os from 'os' type SystemTestProject = typeof fixtureDirs[number] type SystemTestProjectPath = `${string}/system-tests/projects/${T}` @@ -122,3 +124,7 @@ export function createRelevantRun (runNumber: number): RelevantRunInfo { totalFailed: 0, } } + +export function dedentWithOSLineWrap (literals: string|TemplateStringsArray, ...placeholders: any[]): string { + return dedent(literals, ...placeholders).replace(/\n/g, os.EOL) +} diff --git a/packages/data-context/test/unit/sources/ProjectDataSource.spec.ts b/packages/data-context/test/unit/sources/ProjectDataSource.spec.ts index 4fed71f68331..a82405958426 100644 --- a/packages/data-context/test/unit/sources/ProjectDataSource.spec.ts +++ b/packages/data-context/test/unit/sources/ProjectDataSource.spec.ts @@ -14,7 +14,7 @@ import type { FindSpecs } from '../../../src/actions' import { createTestDataContext } from '../helper' import { defaultExcludeSpecPattern, defaultSpecPattern } from '@packages/config' import FixturesHelper from '@tooling/system-tests' -import { toPosix } from '../../../src/util' +import { toPosix, toOS } from '../../../src/util' chai.use(sinonChai) const { expect } = chai @@ -43,7 +43,7 @@ describe('matchedSpecs', () => { fileExtension: '.js', fileName: 'screenshot_element_capture_spec', name: 'cypress/integration/screenshot_element_capture_spec.js', - relative: 'cypress/integration/screenshot_element_capture_spec.js', + relative: toOS('cypress/integration/screenshot_element_capture_spec.js'), relativeToCommonRoot: 'screenshot_element_capture_spec.js', specFileExtension: '.js', specType: 'integration', @@ -130,7 +130,7 @@ describe('transformSpec', () => { specType: 'integration', baseName: 'spec.cy.js', fileName: 'spec', - relative: 'src/spec.cy.js', + relative: toOS('src/spec.cy.js'), name: 'src/spec.cy.js', relativeToCommonRoot: 'C:/Windows/Project/src/spec.cy.js', } From c1bd58b533f1ad5c3ee6d23c77e714ee0f2b7309 Mon Sep 17 00:00:00 2001 From: GweesinChan Date: Wed, 4 Sep 2024 14:41:09 +0800 Subject: [PATCH 03/14] refactor: make expects across different OS --- .../test/unit/sources/ProjectDataSource.spec.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/data-context/test/unit/sources/ProjectDataSource.spec.ts b/packages/data-context/test/unit/sources/ProjectDataSource.spec.ts index a82405958426..d294d8d5499a 100644 --- a/packages/data-context/test/unit/sources/ProjectDataSource.spec.ts +++ b/packages/data-context/test/unit/sources/ProjectDataSource.spec.ts @@ -14,7 +14,7 @@ import type { FindSpecs } from '../../../src/actions' import { createTestDataContext } from '../helper' import { defaultExcludeSpecPattern, defaultSpecPattern } from '@packages/config' import FixturesHelper from '@tooling/system-tests' -import { toPosix, toOS } from '../../../src/util' +import { toOS } from '../../../src/util' chai.use(sinonChai) const { expect } = chai @@ -266,7 +266,7 @@ describe('getLongestCommonPrefixFromPaths', () => { 'cypress/component/bar/meta-component-test.cy.ts', ]) - expect(toPosix(lcp)).to.equal('cypress/component') + expect(lcp).to.equal(toOS('cypress/component')) }) it('with src and cypress', () => { @@ -293,7 +293,7 @@ describe('getLongestCommonPrefixFromPaths', () => { 'src/frontend/MyComponent.cy.ts', ]) - expect(toPosix(lcp)).to.equal('src/frontend') + expect(lcp).to.equal(toOS('src/frontend')) }) }) @@ -777,14 +777,14 @@ describe('ProjectDataSource', () => { const defaultSpecFileName = await ctx.project.defaultSpecFileName() - expect(toPosix(defaultSpecFileName)).to.equal('cypress/e2e/spec.cy.js') + expect(defaultSpecFileName).to.equal(toOS('cypress/e2e/spec.cy.js')) }) it('yields default if the spec pattern is default', async () => { sinon.stub(ctx.project, 'specPatterns').resolves({ specPattern: [defaultSpecPattern.e2e] }) const defaultSpecFileName = await ctx.project.defaultSpecFileName() - expect(toPosix(defaultSpecFileName)).to.equal('cypress/e2e/spec.cy.js') + expect(defaultSpecFileName).to.equal(toOS('cypress/e2e/spec.cy.js')) }) it('yields common prefix if there are existing specs', async () => { @@ -796,7 +796,7 @@ describe('ProjectDataSource', () => { const defaultSpecFileName = await ctx.project.defaultSpecFileName() - expect(toPosix(defaultSpecFileName)).to.equal('cypress/e2e/foo/spec.cy.js') + expect(defaultSpecFileName).to.equal(toOS('cypress/e2e/foo/spec.cy.js')) }) it('yields spec pattern guess if there are no existing specs', async () => { @@ -832,7 +832,7 @@ describe('ProjectDataSource', () => { const defaultSpecFileName = await ctx.project.defaultSpecFileName() - expect(toPosix(defaultSpecFileName)).to.equal('cypress/component/ComponentName.cy.jsx', defaultSpecFileName) + expect(defaultSpecFileName).to.equal(toOS('cypress/component/ComponentName.cy.jsx'), defaultSpecFileName) }) it('yields non-jsx extension if there are jsx files but specPattern disallows', async () => { From dd943fe56440a7e29b45f1105496b510c31cfa89 Mon Sep 17 00:00:00 2001 From: GweesinChan Date: Wed, 4 Sep 2024 14:42:31 +0800 Subject: [PATCH 04/14] fix: fix errors caused by line wrap --- packages/data-context/src/codegen/code-generator.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/data-context/src/codegen/code-generator.ts b/packages/data-context/src/codegen/code-generator.ts index aeeecbd973c0..3b7be1fcb3a4 100644 --- a/packages/data-context/src/codegen/code-generator.ts +++ b/packages/data-context/src/codegen/code-generator.ts @@ -5,7 +5,7 @@ import * as ejs from 'ejs' import fm from 'front-matter' import _ from 'lodash' import Debug from 'debug' -import { toPosix } from '../util' +import { toOS } from '../util' const debug = Debug('cypress:data-context:codegen:code-generator') @@ -180,7 +180,7 @@ export async function hasNonExampleSpec (testTemplateDir: string, specs: string[ return templateFiles.some((templateFile) => templateFile.substring(testTemplateDir.length + 1) === spec) } - return specs.map(((spec) => toPosix(spec))).some((spec) => !specInTemplates(spec)) + return specs.map(((spec) => toOS(spec))).some((spec) => !specInTemplates(spec)) } export async function getExampleSpecPaths (testTemplateDir: string): Promise { From a7714727d81bb229322902c748ea40f89e092d72 Mon Sep 17 00:00:00 2001 From: GweesinChan Date: Wed, 4 Sep 2024 16:09:28 +0800 Subject: [PATCH 05/14] fix: fix absolute path join absolute path problem on windows --- .../data-context/test/unit/sources/FileDataSource.spec.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/data-context/test/unit/sources/FileDataSource.spec.ts b/packages/data-context/test/unit/sources/FileDataSource.spec.ts index 532db28fbccd..83957fc32500 100644 --- a/packages/data-context/test/unit/sources/FileDataSource.spec.ts +++ b/packages/data-context/test/unit/sources/FileDataSource.spec.ts @@ -67,7 +67,9 @@ describe('FileDataSource', () => { 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) + + // project path on windows contains drive letter, cannot be joined directly + const nestedScriptPath = path.join(projectPath, 'cypress', path.parse(projectPath).base) await fs.mkdirs(nestedScriptPath) await fs.writeFile(path.join(nestedScriptPath, 'nested-script.js'), '') @@ -76,7 +78,7 @@ describe('FileDataSource', () => { // to the working directory let files = await fileDataSource.getFilesByGlob( projectPath, - `./cypress${projectPath}/nested-script.js`, + `./cypress/${path.parse(projectPath).base}/nested-script.js`, ) expect(files).to.have.length(1) From 93a445c407f7651491b869028a85aa8d9e22a4ce Mon Sep 17 00:00:00 2001 From: GweesinChan Date: Wed, 4 Sep 2024 16:34:28 +0800 Subject: [PATCH 06/14] fix: make expects across different OS --- .../data-context/test/unit/sources/ProjectDataSource.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/data-context/test/unit/sources/ProjectDataSource.spec.ts b/packages/data-context/test/unit/sources/ProjectDataSource.spec.ts index d294d8d5499a..4f8c258ec1f5 100644 --- a/packages/data-context/test/unit/sources/ProjectDataSource.spec.ts +++ b/packages/data-context/test/unit/sources/ProjectDataSource.spec.ts @@ -817,7 +817,7 @@ describe('ProjectDataSource', () => { const defaultSpecFileName = await ctx.project.defaultSpecFileName() - expect(defaultSpecFileName).to.equal('cypress/component-tests/foo/ComponentName.spec.js') + expect(defaultSpecFileName).to.equal(toOS('cypress/component-tests/foo/ComponentName.spec.js')) }) describe('jsx/tsx handling', () => { From 95e4f47523f68b95dc0397383ad022c2e6b24142 Mon Sep 17 00:00:00 2001 From: GweesinChan Date: Wed, 4 Sep 2024 16:35:26 +0800 Subject: [PATCH 07/14] fix: fix args match errors with all platforms --- .../data-context/test/unit/sources/FileDataSource.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/data-context/test/unit/sources/FileDataSource.spec.ts b/packages/data-context/test/unit/sources/FileDataSource.spec.ts index 83957fc32500..f502ff2d5e02 100644 --- a/packages/data-context/test/unit/sources/FileDataSource.spec.ts +++ b/packages/data-context/test/unit/sources/FileDataSource.spec.ts @@ -254,7 +254,7 @@ describe('FileDataSource', () => { expect(files).to.eq(mockMatches) expect(matchGlobsStub).to.have.been.calledWith( - ['cypress/e2e/**.cy.js'], + ['/my/project/cypress/e2e/**.cy.js'], { ...defaultGlobbyOptions, cwd: '/my/project' }, ) }) @@ -267,7 +267,7 @@ describe('FileDataSource', () => { expect(files).to.eq(mockMatches) expect(matchGlobsStub).to.have.been.calledWith( - ['cypress/my/project/e2e/**.cy.js'], + ['/my/project/cypress/my/project/e2e/**.cy.js'], { ...defaultGlobbyOptions, cwd: '/my/project' }, ) }) From c94c169b3ddb70a2d9420e229743582d91f22a1a Mon Sep 17 00:00:00 2001 From: GweesinChan Date: Thu, 5 Sep 2024 00:48:17 +0800 Subject: [PATCH 08/14] docs: add changelog --- cli/CHANGELOG.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/cli/CHANGELOG.md b/cli/CHANGELOG.md index 46aca11025b4..67c5da62944d 100644 --- a/cli/CHANGELOG.md +++ b/cli/CHANGELOG.md @@ -1,4 +1,14 @@ +## + +_Released (PENDING)_ + +**Bugfixes:** + +- Fixed an issue where run Cypress internal unit test cases error on Windows. Addressed in [#30157](https://github.com/cypress-io/cypress/pull/30157) + +- + ## 13.14.2 _Released 9/4/2024_ From 0a209fee2318ec50dbc43a94970f38fca6d9b3e4 Mon Sep 17 00:00:00 2001 From: GweesinChan Date: Thu, 5 Sep 2024 00:48:46 +0800 Subject: [PATCH 09/14] chore: remove template sentence --- cli/CHANGELOG.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/cli/CHANGELOG.md b/cli/CHANGELOG.md index 67c5da62944d..37a8d489c680 100644 --- a/cli/CHANGELOG.md +++ b/cli/CHANGELOG.md @@ -7,8 +7,6 @@ _Released (PENDING)_ - Fixed an issue where run Cypress internal unit test cases error on Windows. Addressed in [#30157](https://github.com/cypress-io/cypress/pull/30157) -- - ## 13.14.2 _Released 9/4/2024_ From 9e40bdd93a675d2a4b001310a83e0a47ecf57f1b Mon Sep 17 00:00:00 2001 From: GweesinChan Date: Thu, 5 Sep 2024 01:01:48 +0800 Subject: [PATCH 10/14] chore: fix ci actions error `Error: Expected line number 2 to include "## x.x.x"` --- cli/CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cli/CHANGELOG.md b/cli/CHANGELOG.md index 37a8d489c680..41a677c049d8 100644 --- a/cli/CHANGELOG.md +++ b/cli/CHANGELOG.md @@ -1,7 +1,7 @@ -## +## 13.14.3 -_Released (PENDING)_ +_Released 10/1/2024 (PENDING)_ **Bugfixes:** From f8034433e4f0900458adbc178780c84b1ff9c546 Mon Sep 17 00:00:00 2001 From: GweesinChan Date: Fri, 6 Sep 2024 14:47:25 +0800 Subject: [PATCH 11/14] fix: fix getFilesByGlob cannot remove cwd path on windows --- .../data-context/src/sources/FileDataSource.ts | 9 +++++---- .../test/unit/sources/FileDataSource.spec.ts | 14 +++++++------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/packages/data-context/src/sources/FileDataSource.ts b/packages/data-context/src/sources/FileDataSource.ts index 7c2b1d52f6be..f36149d70680 100644 --- a/packages/data-context/src/sources/FileDataSource.ts +++ b/packages/data-context/src/sources/FileDataSource.ts @@ -5,7 +5,7 @@ import os from 'os' import globby, { GlobbyOptions } from 'globby' import Debug from 'debug' -import { toPosix } from '../util/file' +import { toOS, toPosix } from '../util/file' const debug = Debug('cypress:data-context:sources:FileDataSource') @@ -36,14 +36,15 @@ export class FileDataSource { async getFilesByGlob (cwd: string, glob: string | string[], globOptions: GlobbyOptions = {}): Promise { const globs = ([] as string[]).concat(glob).map((globPattern) => { - const workingDirectoryPrefix = path.join(cwd, path.sep) + const workingDirectoryPrefix = toOS(path.join(cwd, path.sep)) + const globPatternWithOS = toOS(globPattern) // If the pattern includes the working directory, we strip it from the pattern. // 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. - if (globPattern.startsWith(workingDirectoryPrefix)) { - return globPattern.replace(workingDirectoryPrefix, '') + if (globPatternWithOS.startsWith(workingDirectoryPrefix)) { + return globPatternWithOS.replace(workingDirectoryPrefix, '') } return globPattern diff --git a/packages/data-context/test/unit/sources/FileDataSource.spec.ts b/packages/data-context/test/unit/sources/FileDataSource.spec.ts index f502ff2d5e02..ccb62a9561de 100644 --- a/packages/data-context/test/unit/sources/FileDataSource.spec.ts +++ b/packages/data-context/test/unit/sources/FileDataSource.spec.ts @@ -215,7 +215,7 @@ describe('FileDataSource', () => { expect(files).to.eq(mockMatches) expect(matchGlobsStub).to.have.been.calledWith( - ['/cypress/e2e/**.cy.js'], + ['cypress/e2e/**.cy.js'], { ...defaultGlobbyOptions, cwd: '/' }, ) }) @@ -254,7 +254,7 @@ describe('FileDataSource', () => { expect(files).to.eq(mockMatches) expect(matchGlobsStub).to.have.been.calledWith( - ['/my/project/cypress/e2e/**.cy.js'], + ['cypress/e2e/**.cy.js'], { ...defaultGlobbyOptions, cwd: '/my/project' }, ) }) @@ -267,7 +267,7 @@ describe('FileDataSource', () => { expect(files).to.eq(mockMatches) expect(matchGlobsStub).to.have.been.calledWith( - ['/my/project/cypress/my/project/e2e/**.cy.js'], + ['cypress/my/project/e2e/**.cy.js'], { ...defaultGlobbyOptions, cwd: '/my/project' }, ) }) @@ -281,7 +281,7 @@ describe('FileDataSource', () => { expect(files).to.eq(mockMatches) expect(matchGlobsStub).to.have.been.calledWith( - ['/cypress/e2e/**.cy.js'], + ['cypress/e2e/**.cy.js'], { ...defaultGlobbyOptions, cwd: '/', @@ -319,8 +319,8 @@ describe('FileDataSource', () => { expect(files).to.eq(mockMatches) expect(matchGlobsStub).to.have.been.calledWith( [ - '/node_modules/cypress/e2e/**.cy.js', - '/cypress/e2e/**.cy.js', + 'node_modules/cypress/e2e/**.cy.js', + 'cypress/e2e/**.cy.js', ], { ...defaultGlobbyOptions, @@ -357,7 +357,7 @@ describe('FileDataSource', () => { expect(files).to.eq(mockMatches) expect(matchGlobsStub).to.have.been.calledWith( - ['/cypress/e2e/**.cy.js'], + ['cypress/e2e/**.cy.js'], { ...defaultGlobbyOptions, cwd: '/', From 337acb416824142716b9d3d2404bcbeaf01f25e7 Mon Sep 17 00:00:00 2001 From: GweesinChan Date: Sat, 28 Sep 2024 23:03:19 +0800 Subject: [PATCH 12/14] refactor: add comment for toOS function and remove unused parameters --- package.json | 3 ++- packages/data-context/src/util/file.ts | 13 +++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 8f6aeb311e83..0afc731e0212 100644 --- a/package.json +++ b/package.json @@ -271,5 +271,6 @@ "devtools-protocol": "0.0.1346313", "sharp": "0.29.3", "vue-template-compiler": "2.6.12" - } + }, + "packageManager": "yarn@1.22.22+sha1.ac34549e6aa8e7ead463a7407e1c7390f61a6610" } diff --git a/packages/data-context/src/util/file.ts b/packages/data-context/src/util/file.ts index fffbe83c1ccf..0415db4f256c 100644 --- a/packages/data-context/src/util/file.ts +++ b/packages/data-context/src/util/file.ts @@ -4,6 +4,15 @@ export function toPosix (file: string, sep: string = path.sep) { return file.split(sep).join(path.posix.sep) } -export function toOS (file: string, sep: string = path.posix.sep) { - return file.split(sep).join(path.sep) +/** + * Converts a POSIX file path to the current operating system's file path format. + * + * This function takes a file path string that uses POSIX separators ('/') and + * replaces them with the current operating system's path separators. + * + * @param file The file path to convert. + * @returns The converted file path with the current OS's path separators. + */ +export function toOS (file: string) { + return file.split(path.posix.sep).join(path.sep) } From 8e03b8142ccbb8987b014f21284b72ea9e09e2d3 Mon Sep 17 00:00:00 2001 From: GweesinChan Date: Sat, 28 Sep 2024 23:03:39 +0800 Subject: [PATCH 13/14] test: add unit test for toOS function --- .../data-context/test/unit/util/file.spec.ts | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 packages/data-context/test/unit/util/file.spec.ts diff --git a/packages/data-context/test/unit/util/file.spec.ts b/packages/data-context/test/unit/util/file.spec.ts new file mode 100644 index 000000000000..f5ad70735165 --- /dev/null +++ b/packages/data-context/test/unit/util/file.spec.ts @@ -0,0 +1,26 @@ +import { toOS } from '../../../src/util' +import { expect } from 'chai' +import sinon from 'sinon' +import path from 'path' + +describe('file', () => { + describe('#toOS', () => { + const posixPath = '/foo/bar/baz' + + it('should convert posix path to Windows path', () => { + sinon.stub(path, 'sep').value('\\') + + expect(toOS(posixPath)).to.equal('\\foo\\bar\\baz') + }) + + it('should return the same path if OS path separator is posix', () => { + sinon.stub(path, 'sep').value('/') + + expect(toOS(posixPath)).to.equal(posixPath) + }) + + afterEach(() => { + sinon.restore() + }) + }) +}) From 343cd31ca329e89141019b3eb84e5ed010bbaf84 Mon Sep 17 00:00:00 2001 From: GweesinChan Date: Sat, 28 Sep 2024 23:05:54 +0800 Subject: [PATCH 14/14] revert: revert packageManager change --- package.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/package.json b/package.json index 0afc731e0212..8f6aeb311e83 100644 --- a/package.json +++ b/package.json @@ -271,6 +271,5 @@ "devtools-protocol": "0.0.1346313", "sharp": "0.29.3", "vue-template-compiler": "2.6.12" - }, - "packageManager": "yarn@1.22.22+sha1.ac34549e6aa8e7ead463a7407e1c7390f61a6610" + } }