Skip to content

Commit

Permalink
fix: correctly resolve folder id
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Mar 20, 2024
1 parent f05a782 commit 204d15b
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 42 deletions.
24 changes: 14 additions & 10 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { ChildProcess } from 'node:child_process'
import { fork } from 'node:child_process'
import { pathToFileURL } from 'node:url'
import { gte } from 'semver'
import { basename, dirname, normalize } from 'pathe'
import * as vscode from 'vscode'
Expand All @@ -10,12 +11,13 @@ import type { BirpcEvents, VitestEvents, VitestRPC } from './api/rpc'
import { createVitestRpc } from './api/rpc'
import { resolveVitestPackage } from './api/resolve'
import type { TestTree } from './testTree'
import type { WorkerRunnerOptions } from './worker/types'

const _require = require

export class VitestReporter {
constructor(
protected folderFsPath: string,
protected id: string,
protected handlers: ResolvedMeta['handlers'],
) {}

Expand All @@ -35,8 +37,8 @@ export class VitestReporter {

private createHandler<K extends Exclude<keyof ResolvedMeta['handlers'], 'clearListeners' | 'removeListener'>>(name: K) {
return (callback: VitestEvents[K]) => {
this.handlers[name]((folder, ...args) => {
if (folder === this.folderFsPath)
this.handlers[name]((id, ...args) => {
if (id === this.id)
(callback as any)(...args)
})
}
Expand Down Expand Up @@ -79,11 +81,12 @@ export class VitestFolderAPI extends VitestReporter {
constructor(
folder: vscode.WorkspaceFolder,
private meta: ResolvedMeta,
public readonly id: string,
id: string,
) {
super(normalize(folder.uri.fsPath), meta.handlers)
const normalizedId = normalize(id)
super(normalizedId, meta.handlers)
WEAKMAP_API_FOLDER.set(this, folder)
this.id = normalize(id)
this.id = normalizedId
// TODO: make it prettier, but still unique
this.tag = new vscode.TestTag(this.id)
}
Expand Down Expand Up @@ -271,7 +274,7 @@ function createChildVitestProcess(tree: TestTree, meta: VitestMeta[]) {
'--require',
pnp,
'--experimental-loader',
pnpLoaders[0],
pathToFileURL(pnpLoaders[0]).toString(),
]
: undefined
const vitest = fork(
Expand Down Expand Up @@ -325,18 +328,19 @@ function createChildVitestProcess(tree: TestTree, meta: VitestMeta[]) {
}
})
vitest.on('spawn', () => {
vitest.send({
const runnerOptions: WorkerRunnerOptions = {
type: 'init',
meta: meta.map(m => ({
vitestNodePath: m.vitestNodePath,
folder: normalize(m.folder.uri.fsPath),
env: getConfig(m.folder).env || undefined,
configFile: m.configFile,
workspaceFile: m.workspaceFile,
id: m.id,
})),
loader: pnpLoaders[0] && gte(process.version, '18.19.0') ? pnpLoaders[0] : undefined,
})
}

vitest.send(runnerOptions)
})
})
}
Expand Down
8 changes: 4 additions & 4 deletions src/runner/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class TestRunner extends vscode.Disposable {
this.endTestRuns()
})

api.onWatcherRerun(() => this.startTestRun())
api.onWatcherRerun(files => this.startTestRun(files))

api.onTaskUpdate((packs) => {
packs.forEach(([testId, result]) => {
Expand Down Expand Up @@ -108,9 +108,9 @@ export class TestRunner extends vscode.Disposable {
const testNamePatern = formatTestPattern(tests)
const files = getTestFiles(tests)
if (testNamePatern)
log.info(`Running ${files.length} files with name pattern: ${testNamePatern}`)
log.info(`Running ${files.length} file(s) with name pattern: ${testNamePatern}`)
else
log.info(`Running ${files.length} files`)
log.info(`Running ${files.length} file(s):`, files)
await this.api.runFiles(files, testNamePatern)
}

Expand All @@ -131,7 +131,7 @@ export class TestRunner extends vscode.Disposable {
}
}

private startTestRun() {
private startTestRun(_files: string[]) {
// TODO: refactor to use different requests, otherwise test run doesn't mark the result value!
const currentRequest = this.testRunRequests.values().next().value as vscode.TestRunRequest | undefined
if (currentRequest) {
Expand Down
2 changes: 1 addition & 1 deletion src/worker/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function createWorkerRPC(vitest: Vitest[], channel: ChannelOptions) {

async function globTestFiles(vitest: Vitest, filters?: string[]) {
const cwd = process.cwd()
process.chdir(vitest.config.root)
process.chdir(dirname(getId(vitest)))
const files = await vitest.globTestFiles(filters)
process.chdir(cwd)
return files
Expand Down
13 changes: 13 additions & 0 deletions src/worker/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export interface WorkerMeta {
vitestNodePath: string
id: string
configFile?: string
workspaceFile?: string
env: Record<string, any> | undefined
}

export interface WorkerRunnerOptions {
type: 'init'
meta: WorkerMeta[]
loader?: string
}
41 changes: 14 additions & 27 deletions src/worker/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,24 @@ import type { BirpcReturn } from 'birpc'
import type { File, Reporter, TaskResultPack, UserConsoleLog, Vitest } from 'vitest'
import type { BirpcEvents, BirpcMethods } from '../api/rpc'
import { createWorkerRPC } from './rpc'

interface VitestMeta {
folder: string
vitestNodePath: string
id: string
configFile?: string
workspaceFile?: string
env: Record<string, any> | undefined
}

interface RunnerOptions {
type: 'init'
meta: VitestMeta[]
loader?: string
}
import type { WorkerMeta, WorkerRunnerOptions } from './types'

class VSCodeReporter implements Reporter {
private rpc!: BirpcReturn<BirpcEvents, BirpcMethods>
private ctx!: Vitest
private folder!: string
private id!: string

initVitest(ctx: Vitest) {
initVitest(ctx: Vitest, id: string) {
this.ctx = ctx
this.folder = ctx.config.root
this.id = id
}

initRpc(rpc: BirpcReturn<BirpcEvents, BirpcMethods>) {
this.rpc = rpc
}

onUserConsoleLog(log: UserConsoleLog) {
this.rpc.onConsoleLog(this.folder, log)
this.rpc.onConsoleLog(this.id, log)
}

onTaskUpdate(packs: TaskResultPack[]) {
Expand All @@ -53,29 +39,30 @@ class VSCodeReporter implements Reporter {
})
})

this.rpc.onTaskUpdate(this.folder, packs)
this.rpc.onTaskUpdate(this.id, packs)
}

onFinished(files?: File[], errors?: unknown[]) {
this.rpc.onFinished(this.folder, files, errors)
this.rpc.onFinished(this.id, files, errors)
}

onCollected(files?: File[]) {
this.rpc.onCollected(this.folder, files)
this.rpc.onCollected(this.id, files)
}

onWatcherStart(files?: File[], errors?: unknown[]) {
this.rpc.onWatcherStart(this.folder, files, errors)
this.rpc.onWatcherStart(this.id, files, errors)
}

onWatcherRerun(files: string[], trigger?: string) {
this.rpc.onWatcherRerun(this.folder, files, trigger)
this.rpc.onWatcherRerun(this.id, files, trigger)
}
}

async function initVitest(meta: VitestMeta) {
async function initVitest(meta: WorkerMeta) {
const vitestMode = await import(meta.vitestNodePath) as typeof import('vitest/node')
const reporter = new VSCodeReporter()
_debug('root', dirname(meta.id))
const vitest = await vitestMode.createVitest(
'test',
{
Expand All @@ -95,7 +82,7 @@ async function initVitest(meta: VitestMeta) {
},
},
)
reporter.initVitest(vitest)
reporter.initVitest(vitest, meta.id)
return {
vitest,
reporter,
Expand All @@ -107,7 +94,7 @@ const cwd = process.cwd()
process.on('message', async function init(message: any) {
if (message.type === 'init') {
process.off('message', init)
const data = message as RunnerOptions
const data = message as WorkerRunnerOptions

try {
if (data.loader)
Expand Down

0 comments on commit 204d15b

Please sign in to comment.