Skip to content

Commit

Permalink
fix: filter disabled workspace upfront
Browse files Browse the repository at this point in the history
  • Loading branch information
zxch3n committed Jul 23, 2022
1 parent f89ce2f commit a4fc22b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 44 deletions.
12 changes: 12 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export interface VitestWorkspaceConfig {
args: string[]
version?: string
isCompatible: boolean
isDisabled: boolean
}

export async function getVitestWorkspaceConfigs(): Promise<VitestWorkspaceConfig[]> {
Expand All @@ -88,20 +89,23 @@ export async function getVitestWorkspaceConfigs(): Promise<VitestWorkspaceConfig
return undefined
})

const disabled = getRootConfig().disabledWorkspaceFolders
const out: VitestWorkspaceConfig = cmd
? {
workspace,
version,
cmd: cmd.cmd,
args: cmd.args,
isCompatible: isCompatibleVitestConfig({ version, workspace }),
isDisabled: disabled.includes(workspace.name),
}
: {
version,
workspace,
cmd: 'npx',
args: ['vitest'],
isCompatible: isCompatibleVitestConfig({ version, workspace }),
isDisabled: disabled.includes(workspace.name),
}
return out
}))
Expand All @@ -110,3 +114,11 @@ export async function getVitestWorkspaceConfigs(): Promise<VitestWorkspaceConfig
function isCompatibleVitestConfig(config: Pick<VitestWorkspaceConfig, 'version' | 'workspace'>) {
return !!((config.version && semver.gte(config.version, '0.12.0')) || getConfig(config.workspace).commandLine)
}

/**
* @returns vitest workspaces filtered by `disabledWorkspaceFolders`
*/
export function getValidWorkspaces(workspaces: vscode.WorkspaceFolder[]): vscode.WorkspaceFolder[] {
const { disabledWorkspaceFolders } = getRootConfig()
return workspaces.filter(workspace => !disabledWorkspaceFolders.includes(workspace.name)) || []
}
11 changes: 6 additions & 5 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ function workspacesCompatibilityCheck(workspaceConfigs: VitestWorkspaceConfig[],
})

workspaceConfigs.filter(x => !x.isCompatible).forEach((config) => {
vscode.window.showWarningMessage(`Because Vitest version < 0.12.0 for ${config.workspace.name} `
+ ', run/debug/watch tests from Vitest extension disabled for that workspace.\n')
vscode.window.showWarningMessage('Because Vitest version < 0.12.0'
+ `, run/debug/watch tests are disabled in workspace "${config.workspace.name}" \n`)
})

if (workspaceConfigs.every(x => !x.isCompatible)) {
Expand Down Expand Up @@ -208,23 +208,24 @@ function registerRunDebugWatchHandler(
context: vscode.ExtensionContext,
) {
const testWatchers = registerWatchHandlers(
workspaceConfigs.filter(x => x.isCompatible),
workspaceConfigs.filter(x => x.isCompatible && !x.isDisabled),
ctrl,
fileDiscoverer,
context,
) ?? []

const workspaces = workspaceConfigs.filter(x => x.isCompatible && !x.isDisabled).map(x => x.workspace)
ctrl.createRunProfile(
'Run Tests',
vscode.TestRunProfileKind.Run,
runHandler.bind(null, ctrl, fileDiscoverer, testWatchers),
runHandler.bind(null, ctrl, fileDiscoverer, testWatchers, workspaces),
true,
)

ctrl.createRunProfile(
'Debug Tests',
vscode.TestRunProfileKind.Debug,
debugHandler.bind(null, ctrl, fileDiscoverer),
debugHandler.bind(null, ctrl, fileDiscoverer, workspaces),
true,
)
}
Expand Down
71 changes: 32 additions & 39 deletions src/runHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
getAllTestCases,
testItemIdMap,
} from './TestData'
import { getConfig, getRootConfig } from './config'
import { getConfig } from './config'
import { TestWatcher, syncFilesTestStatus } from './watch'
import { log } from './log'
import type { TestFileDiscoverer } from './discover'
Expand All @@ -25,13 +25,11 @@ export async function runHandler(
ctrl: vscode.TestController,
discover: TestFileDiscoverer,
watchers: TestWatcher[],
workspaces: vscode.WorkspaceFolder[],
request: vscode.TestRunRequest,
cancellation: vscode.CancellationToken,
) {
if (
vscode.workspace.workspaceFolders === undefined
|| vscode.workspace.workspaceFolders.length === 0
) {
if (workspaces.length === 0) {
log.info('ERROR: No workspace folder found')
vscode.window.showErrorMessage('Cannot run tests: No workspace folder found')
return
Expand All @@ -51,36 +49,33 @@ export async function runHandler(
run.end()
})

const { disabledWorkspaceFolders } = getRootConfig()
await Promise.allSettled(vscode.workspace.workspaceFolders
.filter(folder => !disabledWorkspaceFolders.includes(folder.name))
.map(async (folder) => {
const runner = new TestRunner(
folder.uri.fsPath,
getVitestCommand(folder.uri.fsPath),
)

const items = request.include ?? ctrl.items

const testForThisWorkspace = gatherTestItemsFromWorkspace(items, folder.uri.fsPath)
if (testForThisWorkspace.length === 0)
return

log.info(`[Workspace "${folder.name}] Run tests from workspace`)
try {
await runTest(ctrl, runner, run, testForThisWorkspace, 'run', discover)
log.info(`[Workspace "${folder.name}] Test run finished`)
}
catch (e) {
log.error(`[Workspace "${folder.name}] Run error`)
if (e instanceof Error) {
const err = e
console.error(e)
log.info(`[Workspace ${folder.name}] Error: ${e.toString()}`)
testForThisWorkspace.forEach(test => run.errored(test, new vscode.TestMessage(err.toString())))
}
await Promise.allSettled(workspaces.map(async (folder) => {
const runner = new TestRunner(
folder.uri.fsPath,
getVitestCommand(folder.uri.fsPath),
)

const items = request.include ?? ctrl.items

const testForThisWorkspace = gatherTestItemsFromWorkspace(items, folder.uri.fsPath)
if (testForThisWorkspace.length === 0)
return

log.info(`[Workspace "${folder.name}] Run tests from workspace`)
try {
await runTest(ctrl, runner, run, testForThisWorkspace, 'run', discover)
log.info(`[Workspace "${folder.name}] Test run finished`)
}
catch (e) {
log.error(`[Workspace "${folder.name}] Run error`)
if (e instanceof Error) {
const err = e
console.error(e)
log.info(`[Workspace ${folder.name}] Error: ${e.toString()}`)
testForThisWorkspace.forEach(test => run.errored(test, new vscode.TestMessage(err.toString())))
}
}))
}
}))

run.end()
log.info('Tests run end')
Expand Down Expand Up @@ -113,21 +108,19 @@ export async function updateSnapshot(
export async function debugHandler(
ctrl: vscode.TestController,
discover: TestFileDiscoverer,
workspaces: vscode.WorkspaceFolder[],
request: vscode.TestRunRequest,
cancellation: vscode.CancellationToken,
) {
if (
vscode.workspace.workspaceFolders === undefined
|| vscode.workspace.workspaceFolders.length === 0
)
if (workspaces.length === 0)
return

const run = ctrl.createTestRun(request)
cancellation.onCancellationRequested(() => {
run.end()
})

for (const folder of vscode.workspace.workspaceFolders) {
for (const folder of workspaces) {
const items = request.include ?? ctrl.items
const testsInThisWorkspace = gatherTestItemsFromWorkspace(items, folder.uri.fsPath)
if (testsInThisWorkspace.length === 0)
Expand Down

0 comments on commit a4fc22b

Please sign in to comment.