Skip to content

Commit

Permalink
fix: fix potential failed issue
Browse files Browse the repository at this point in the history
  • Loading branch information
zxch3n committed May 18, 2022
1 parent 4db3fbe commit edba084
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 27 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"url": "https://github.com/vitest-dev/vscode/issues"
},
"engines": {
"vscode": "^1.59.0"
"vscode": "^1.63.0"
},
"categories": [
"Testing"
Expand Down
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export function getConfig() {
const config = workspace.getConfiguration('vitest')
return {
env: config.get('nodeEnv') as null | Record<string, string>,
commandLine: config.get('commandLine') as string,
commandLine: (config.get('commandLine') || undefined) as string | undefined,
include: config.get('include') as string[],
exclude: config.get('exclude') as string[],
enable: config.get('enable') as boolean,
Expand Down
7 changes: 4 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { effect } from '@vue/reactivity'
import { extensionId, getConfig } from './config'
import { TestFileDiscoverer } from './discover'
import { isVitestEnv } from './pure/isVitestEnv'
import { getVitestCommand, getVitestVersion } from './pure/utils'
import { getVitestCommand, getVitestVersion, stringToCmd } from './pure/utils'
import { debugHandler, runHandler, updateSnapshot } from './runHandler'
import { TestFile, WEAKMAP_TEST_DATA } from './TestData'
import { TestWatcher } from './watch'
Expand Down Expand Up @@ -53,10 +53,11 @@ export async function activate(context: vscode.ExtensionContext) {
const vitestVersion = await getVitestVersion(vitestCmd)
console.dir({ vitestVersion })

if (semver.gte(vitestVersion, '0.8.0')) {
const customTestCmd = getConfig().commandLine
if (semver.gte(vitestVersion, '0.8.0') || customTestCmd) {
// enable run/debug/watch tests only if vitest version >= 0.8.0
const testWatcher: undefined | TestWatcher = registerWatchHandler(
vitestCmd,
vitestCmd ?? stringToCmd(customTestCmd!),
ctrl,
fileDiscoverer,
context,
Expand Down
6 changes: 3 additions & 3 deletions src/pure/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,16 @@ export interface FormattedTestResults {
export class TestRunner {
constructor(
private workspacePath: string,
private vitestCommand: { cmd: string; args: string[] } | undefined,
private defaultVitestCommand: { cmd: string; args: string[] } | undefined,
) {}

async scheduleRun(
testFile: string[] | undefined,
testNamePattern: string | undefined,
log: (msg: string) => void = () => {},
workspaceEnv: Record<string, string> = {},
vitestCommand: { cmd: string; args: string[] } = this.vitestCommand
? this.vitestCommand
vitestCommand: { cmd: string; args: string[] } = this.defaultVitestCommand
? this.defaultVitestCommand
: { cmd: 'npx', args: ['vitest'] },
updateSnapshot = false,
): Promise<FormattedTestResults> {
Expand Down
58 changes: 39 additions & 19 deletions src/pure/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,34 +33,46 @@ export function getVitestCommand(
projectRoot: string,
): { cmd: string; args: string[] } | undefined {
const node_modules = path.resolve(projectRoot, 'node_modules')
if (!existsSync(node_modules))
return

const suffixes = ['']
if (isWindows)
suffixes.unshift('.cmd', '.CMD')
try {
if (!existsSync(node_modules))
return getVitestCommand(path.dirname(projectRoot))

const suffixes = ['']
if (isWindows)
suffixes.unshift('.cmd', '.CMD')

for (const suffix of suffixes) {
if (existsSync(path.resolve(node_modules, '.bin', `vitest${suffix}`))) {
return {
cmd: path.resolve(node_modules, '.bin', `vitest${suffix}`),
args: [],
}
}
}

for (const suffix of suffixes) {
if (existsSync(path.resolve(node_modules, '.bin', `vitest${suffix}`))) {
if (existsSync(path.resolve(node_modules, 'vitest', 'vitest.mjs'))) {
return {
cmd: path.resolve(node_modules, '.bin', `vitest${suffix}`),
args: [],
cmd: 'node',
args: [
sanitizeFilePath(path.resolve(node_modules, 'vitest', 'vitest.mjs')),
],
}
}
}

if (existsSync(path.resolve(node_modules, 'vitest', 'vitest.mjs'))) {
return {
cmd: 'node',
args: [
sanitizeFilePath(path.resolve(node_modules, 'vitest', 'vitest.mjs')),
],
}
return getVitestCommand(path.dirname(projectRoot))
}
catch (e) {
console.error(e)
}
}

export interface Cmd {
cmd: string
args: string[]
}

export async function getVitestVersion(
vitestCommand?: { cmd: string; args: string[] },
vitestCommand?: Cmd,
): Promise<string> {
let process
if (vitestCommand == null) {
Expand Down Expand Up @@ -137,3 +149,11 @@ export function execWithLog(

return { child, promise }
}

export function stringToCmd(cmdStr: string): Cmd {
const list = cmdStr.split(' ')
return {
cmd: list[0],
args: list.slice(1),
}
}

0 comments on commit edba084

Please sign in to comment.