Skip to content

Commit

Permalink
fix: windows drive letter match issue
Browse files Browse the repository at this point in the history
  • Loading branch information
zxch3n committed Nov 19, 2022
1 parent 552f60f commit 5c58ede
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/pure/ApiProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class ApiProcess {

log.info('Start api process at port', port)
log.info('[RUN]', `${this.vitest.cmd} ${this.vitest.args.join(' ')}`)
const cwd = sanitizeFilePath(this.workspace)
const cwd = sanitizeFilePath(this.workspace, false)
log.info('[RUN.cwd]', cwd)

const logs = [] as string[]
Expand Down
6 changes: 3 additions & 3 deletions src/pure/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,19 @@ export class TestRunner {
const command = vitestCommand.cmd
const args = [
...vitestCommand.args,
...(testFile ? testFile.map(f => sanitizeFilePath(f)) : []),
...(testFile ? testFile.map(f => sanitizeFilePath(f, true)) : []),
] as string[]
if (updateSnapshot)
args.push('--update')

if (testNamePattern)
args.push('-t', testNamePattern.replace(/[$^+?()[\]]/g, '\\$&'))

const workspacePath = sanitizeFilePath(this.workspacePath)
const workspacePath = sanitizeFilePath(this.workspacePath, false)
const outputs: string[] = []
const env = { ...process.env, ...workspaceEnv }
let testResultFiles = [] as File[]
const output = await runVitestWithApi({ cmd: sanitizeFilePath(command), args }, this.workspacePath, {
const output = await runVitestWithApi({ cmd: sanitizeFilePath(command, false), args }, this.workspacePath, {
log: (line) => {
log.info(`${filterColorFormatOutput(line.trimEnd())}\r\n`)
outputs.push(filterColorFormatOutput(line))
Expand Down
22 changes: 15 additions & 7 deletions src/pure/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ export function getVitestPath(projectRoot: string): string | undefined {
return

if (existsSync(path.resolve(node_modules, 'vitest', 'vitest.mjs')))
return sanitizeFilePath(path.resolve(node_modules, 'vitest', 'vitest.mjs'))
return sanitizeFilePath(path.resolve(node_modules, 'vitest', 'vitest.mjs'), false)

const suffixes = ['.js', '', '.cmd']
for (const suffix of suffixes) {
if (existsSync(path.resolve(node_modules, '.bin', `vitest${suffix}`))) {
return sanitizeFilePath(
path.resolve(node_modules, '.bin', `vitest${suffix}`),
false,
)
}
}
Expand Down Expand Up @@ -62,7 +63,7 @@ export function getVitestCommand(
return {
cmd: 'node',
args: [
sanitizeFilePath(path.resolve(node_modules, 'vitest', 'vitest.mjs')),
sanitizeFilePath(path.resolve(node_modules, 'vitest', 'vitest.mjs'), false),
],
}
}
Expand Down Expand Up @@ -124,14 +125,21 @@ export function isNodeAvailable(
})
}

const capitalizeFirstLetter = (string: string) =>
string.charAt(0).toUpperCase() + string.slice(1)
const removeDriveLetter = (string: string) => {
if (string.match(/^[a-zA-Z]:/))
return string.slice(2)

return string
}
const replaceDoubleSlashes = (string: string) => string.replace(/\\/g, '/')

export function sanitizeFilePath(path: string) {
if (isWindows)
return replaceDoubleSlashes(path)
export function sanitizeFilePath(path: string, isTestPattern: boolean) {
if (isWindows) {
if (isTestPattern)
return replaceDoubleSlashes(removeDriveLetter(path))
else
return replaceDoubleSlashes(path)
}

return path
}
Expand Down

0 comments on commit 5c58ede

Please sign in to comment.