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: fix some unit tests errors on win #30157

Open
wants to merge 21 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
8049075
fix: fix some unit tests errors on win
gweesin Sep 1, 2024
cdb084b
Merge branch 'develop' into test/fix-run-on-win
jennifer-shehane Sep 3, 2024
bfec809
fix: fix line wrap causes errors
gweesin Sep 4, 2024
c1bd58b
refactor: make expects across different OS
gweesin Sep 4, 2024
dd943fe
fix: fix errors caused by line wrap
gweesin Sep 4, 2024
a771472
fix: fix absolute path join absolute path problem on windows
gweesin Sep 4, 2024
93a445c
fix: make expects across different OS
gweesin Sep 4, 2024
95e4f47
fix: fix args match errors with all platforms
gweesin Sep 4, 2024
11f7fa3
Merge branch 'develop' into test/fix-run-on-win
jennifer-shehane Sep 4, 2024
95fd454
Merge branch 'develop' into test/fix-run-on-win
gweesin Sep 4, 2024
c94c169
docs: add changelog
gweesin Sep 4, 2024
0a209fe
chore: remove template sentence
gweesin Sep 4, 2024
9e40bdd
chore: fix ci actions error
gweesin Sep 4, 2024
e592012
Merge branch 'develop' into test/fix-run-on-win
gweesin Sep 6, 2024
f803443
fix: fix getFilesByGlob cannot remove cwd path on windows
gweesin Sep 6, 2024
87a29cf
Merge branch 'develop' into test/fix-run-on-win
gweesin Sep 17, 2024
e693c60
Merge branch 'develop' into test/fix-run-on-win
gweesin Sep 25, 2024
a082335
Merge branch 'develop' into test/fix-run-on-win
gweesin Sep 28, 2024
337acb4
refactor: add comment for toOS function and remove unused parameters
gweesin Sep 28, 2024
8e03b81
test: add unit test for toOS function
gweesin Sep 28, 2024
343cd31
revert: revert packageManager change
gweesin Sep 28, 2024
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
4 changes: 4 additions & 0 deletions cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

_Released 9/10/2024 (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)

**Misc:**

- Pass along the related log to the `createSnapshot` function for protocol usage. Addressed in [#30244](https://github.com/cypress-io/cypress/pull/30244).
Expand Down
3 changes: 2 additions & 1 deletion packages/data-context/src/codegen/code-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as ejs from 'ejs'
import fm from 'front-matter'
import _ from 'lodash'
import Debug from 'debug'
import { toOS } from '../util'

const debug = Debug('cypress:data-context:codegen:code-generator')

Expand Down Expand Up @@ -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) => toOS(spec))).some((spec) => !specInTemplates(spec))
}

export async function getExampleSpecPaths (testTemplateDir: string): Promise<string[]> {
Expand Down
9 changes: 5 additions & 4 deletions packages/data-context/src/sources/FileDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -36,14 +36,15 @@ export class FileDataSource {

async getFilesByGlob (cwd: string, glob: string | string[], globOptions: GlobbyOptions = {}): Promise<string[]> {
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
Expand Down
4 changes: 4 additions & 0 deletions packages/data-context/src/util/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
gweesin marked this conversation as resolved.
Show resolved Hide resolved
return file.split(sep).join(path.sep)
}
13 changes: 6 additions & 7 deletions packages/data-context/test/unit/codegen/code-generator.spec.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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'

Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -167,7 +166,7 @@ describe('code-generator', () => {
type: 'text',
status: 'add',
file: fileAbsolute,
content: dedent`
content: dedentWithOSLineWrap`
describe('Button.tsx', () => {
it('playground', () => {
// cy.mount()
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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}'

Expand Down Expand Up @@ -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}'

Expand Down
6 changes: 6 additions & 0 deletions packages/data-context/test/unit/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends SystemTestProject> = `${string}/system-tests/projects/${T}`
Expand Down Expand Up @@ -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)
}
10 changes: 6 additions & 4 deletions packages/data-context/test/unit/sources/FileDataSource.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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'), '')
Expand All @@ -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)
Expand Down
19 changes: 10 additions & 9 deletions packages/data-context/test/unit/sources/ProjectDataSource.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 { toOS } from '../../../src/util'

chai.use(sinonChai)
const { expect } = chai
Expand Down Expand Up @@ -42,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',
Expand Down Expand Up @@ -129,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',
}
Expand Down Expand Up @@ -265,7 +266,7 @@ describe('getLongestCommonPrefixFromPaths', () => {
'cypress/component/bar/meta-component-test.cy.ts',
])

expect(lcp).to.equal('cypress/component')
expect(lcp).to.equal(toOS('cypress/component'))
})

it('with src and cypress', () => {
Expand All @@ -292,7 +293,7 @@ describe('getLongestCommonPrefixFromPaths', () => {
'src/frontend/MyComponent.cy.ts',
])

expect(lcp).to.equal('src/frontend')
expect(lcp).to.equal(toOS('src/frontend'))
})
})

Expand Down Expand Up @@ -776,14 +777,14 @@ describe('ProjectDataSource', () => {

const defaultSpecFileName = await ctx.project.defaultSpecFileName()

expect(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(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 () => {
Expand All @@ -795,7 +796,7 @@ describe('ProjectDataSource', () => {

const defaultSpecFileName = await ctx.project.defaultSpecFileName()

expect(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 () => {
Expand All @@ -816,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', () => {
Expand All @@ -831,7 +832,7 @@ describe('ProjectDataSource', () => {

const defaultSpecFileName = await ctx.project.defaultSpecFileName()

expect(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 () => {
Expand Down