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

refactor: lifts toPosix to its own file, removes circular dependency between stack_utils and source_map_utils #30983

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
8 changes: 4 additions & 4 deletions packages/driver/src/cypress/source_map_utils.ts
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ import type { BasicSourceMapConsumer } from 'source-map'
import mappingsWasm from 'source-map/lib/mappings.wasm'

import $utils from './utils'
import stackUtils from './stack_utils'
import { toPosix } from './util/to_posix'

const sourceMapExtractionRegex = /\/\/\s*[@#]\s*sourceMappingURL\s*=\s*(data:[^\s]*)/g
const regexDataUrl = /data:[^;\n]+(?:;charset=[^;\n]+)?;base64,([a-zA-Z0-9+/]+={0,2})/ // matches data urls
@@ -23,7 +23,7 @@ const initializeSourceMapConsumer = async (script, sourceMap): Promise<BasicSour

const consumer = await new SourceMapConsumer(sourceMap)

sourceMapConsumers[stackUtils.toPosix(script.fullyQualifiedUrl)] = consumer
sourceMapConsumers[toPosix(script.fullyQualifiedUrl)] = consumer

return consumer
}
@@ -56,7 +56,7 @@ const extractSourceMap = (fileContents) => {
}

const getSourceContents = (filePath, sourceFile) => {
const posixFilePath = stackUtils.toPosix(filePath)
const posixFilePath = toPosix(filePath)

if (!sourceMapConsumers[posixFilePath]) return null

@@ -72,7 +72,7 @@ const getSourceContents = (filePath, sourceFile) => {
}

const getSourcePosition = (filePath, position) => {
const posixFilePath = stackUtils.toPosix(filePath)
const posixFilePath = toPosix(filePath)
const sourceMapConsumer = sourceMapConsumers[posixFilePath]

if (!sourceMapConsumer) return null
9 changes: 1 addition & 8 deletions packages/driver/src/cypress/stack_utils.ts
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@ import { codeFrameColumns } from '@babel/code-frame'

import $utils from './utils'
import $sourceMapUtils from './source_map_utils'

import { toPosix } from './util/to_posix'
// Intentionally deep-importing from @packages/errors so as to not bundle the entire @packages/errors in the client unnecessarily
import { getStackLines, replacedStack, stackWithoutMessage, splitStack, unsplitStack, stackLineRegex } from '@packages/errors/src/stackUtils'

@@ -184,12 +184,6 @@ const getCodeFrameFromSource = (sourceCode, { line, column: originalColumn, rela
}
}

export const toPosix = (file: string) => {
return Cypress.config('platform') === 'win32'
? file.replaceAll('\\', '/')
: file
}

const getRelativePathFromRoot = (relativeFile: string, absoluteFile?: string) => {
// at this point relativeFile is relative to the cypress config
// we need it to be relative to the repo root, which is different for monorepos
@@ -549,5 +543,4 @@ export default {
stackWithUserInvocationStackSpliced,
captureUserInvocationStack,
getInvocationDetails,
toPosix,
}
5 changes: 5 additions & 0 deletions packages/driver/src/cypress/util/to_posix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const toPosix = (file: string) => {
return Cypress.config('platform') === 'win32'
? file.replaceAll('\\', '/')
: file
}
40 changes: 40 additions & 0 deletions packages/driver/test/unit/cypress/util/toPosix.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @vitest-environment jsdom
*/

import { vi, describe, it, expect, beforeEach, MockedFunction } from 'vitest'

import { toPosix } from '../../../../src/cypress/util/to_posix'

describe('toPosix', () => {
let config: MockedFunction<any>

beforeEach(() => {
config = vi.fn()

// @ts-expect-error
global.Cypress = {
config,
}
})

describe('on windows', () => {
beforeEach(() => {
config.mockReturnValue('win32')
})

it('replaces backslashes with forward slashes', () => {
expect(toPosix('C:\\some\\file')).toEqual('C:/some/file')
})
})

describe(`on other OS'`, () => {
beforeEach(() => {
config.mockReturnValue('darwin64')
})

it('performs as an identity function', () => {
expect(toPosix('/some/file')).toEqual('/some/file')
})
})
})