Skip to content

Commit

Permalink
fix(coverage): don't crash when re-run removes earlier run's reports
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Jan 21, 2024
1 parent 36bc699 commit fa1a924
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
25 changes: 25 additions & 0 deletions packages/coverage-istanbul/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ export class IstanbulCoverageProvider extends BaseCoverageProvider implements Co
coverageFilesDirectory!: string
pendingPromises: Promise<void>[] = []

onRunFinish: (() => void) | null = null
runningPromise: Promise<void> | null = null
cancelRun = false

initialize(ctx: Vitest) {
const config: CoverageIstanbulOptions = ctx.config.coverage

Expand Down Expand Up @@ -145,6 +149,20 @@ export class IstanbulCoverageProvider extends BaseCoverageProvider implements Co
}

async clean(clean = true) {
if (this.runningPromise) {
this.cancelRun = true
await this.runningPromise
}

this.runningPromise = new Promise<void>((resolve) => {
this.onRunFinish = () => {
resolve()
this.onRunFinish = null
this.runningPromise = null
this.cancelRun = false
}
})

if (clean && existsSync(this.options.reportsDirectory))
await fs.rm(this.options.reportsDirectory, { recursive: true, force: true, maxRetries: 10 })

Expand Down Expand Up @@ -176,11 +194,17 @@ export class IstanbulCoverageProvider extends BaseCoverageProvider implements Co
}

await Promise.all(chunk.map(async (filename) => {
if (this.cancelRun)
return

const contents = await fs.readFile(filename, 'utf-8')
const coverage = JSON.parse(contents) as CoverageMap

coverageMapByTransformMode.merge(coverage)
}))

if (this.cancelRun)
return this.onRunFinish?.()
}

// Source maps can change based on projectName and transform mode.
Expand Down Expand Up @@ -247,6 +271,7 @@ export class IstanbulCoverageProvider extends BaseCoverageProvider implements Co

await fs.rm(this.coverageFilesDirectory, { recursive: true })
this.coverageFiles = new Map()
this.onRunFinish?.()
}

async getCoverageMapForUncoveredFiles(coveredFiles: string[]) {
Expand Down
25 changes: 25 additions & 0 deletions packages/coverage-v8/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage
coverageFilesDirectory!: string
pendingPromises: Promise<void>[] = []

onRunFinish: (() => void) | null = null
runningPromise: Promise<void> | null = null
cancelRun = false

initialize(ctx: Vitest) {
const config: CoverageV8Options = ctx.config.coverage

Expand Down Expand Up @@ -109,6 +113,20 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage
}

async clean(clean = true) {
if (this.runningPromise) {
this.cancelRun = true
await this.runningPromise
}

this.runningPromise = new Promise<void>((resolve) => {
this.onRunFinish = () => {
resolve()
this.onRunFinish = null
this.runningPromise = null
this.cancelRun = false
}
})

if (clean && existsSync(this.options.reportsDirectory))
await fs.rm(this.options.reportsDirectory, { recursive: true, force: true, maxRetries: 10 })

Expand Down Expand Up @@ -166,10 +184,16 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage
}

await Promise.all(chunk.map(async (filename) => {
if (this.cancelRun)
return

const contents = await fs.readFile(filename, 'utf-8')
const coverage = JSON.parse(contents) as RawCoverage
merged = mergeProcessCovs([merged, coverage])
}))

if (this.cancelRun)
return this.onRunFinish?.()
}

const converted = await this.convertCoverage(merged, projectName, transformMode)
Expand Down Expand Up @@ -239,6 +263,7 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage

this.coverageFiles = new Map()
await fs.rm(this.coverageFilesDirectory, { recursive: true })
this.onRunFinish?.()
}

private async getUntestedFiles(testedFiles: string[]): Promise<RawCoverage> {
Expand Down
13 changes: 4 additions & 9 deletions packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,6 @@ export class Vitest {

try {
await this.initCoverageProvider()
await this.coverageProvider?.clean(this.config.coverage.clean)
await this.initBrowserProviders()
}
finally {
Expand Down Expand Up @@ -377,7 +376,7 @@ export class Vitest {
// populate once, update cache on watch
await this.cache.stats.populateStats(this.config.root, files)

await this.runFiles(files)
await this.runFiles(files, true)
}

await this.reportCoverage(true)
Expand Down Expand Up @@ -472,7 +471,7 @@ export class Vitest {
await project.initializeGlobalSetup()
}

async runFiles(paths: WorkspaceSpec[]) {
async runFiles(paths: WorkspaceSpec[], isFirstRun = false) {
const filepaths = paths.map(([, file]) => file)
this.state.collectPaths(filepaths)

Expand All @@ -492,6 +491,8 @@ export class Vitest {
this.invalidates.clear()
this.snapshot.clear()
this.state.clearErrors()
await this.coverageProvider?.clean(isFirstRun ? this.config.coverage.clean : this.config.coverage.cleanOnRerun)

await this.initializeGlobalSetup(paths)

try {
Expand Down Expand Up @@ -530,9 +531,6 @@ export class Vitest {
files = files.filter(file => filteredFiles.some(f => f[1] === file))
}

if (this.coverageProvider && this.config.coverage.cleanOnRerun)
await this.coverageProvider.clean()

await this.report('onWatcherRerun', files, trigger)
await this.runFiles(files.flatMap(file => this.getProjectsByTestFile(file)))

Expand Down Expand Up @@ -632,9 +630,6 @@ export class Vitest {

this.changedTests.clear()

if (this.coverageProvider && this.config.coverage.cleanOnRerun)
await this.coverageProvider.clean()

const triggerIds = new Set(triggerId.map(id => relative(this.config.root, id)))
const triggerLabel = Array.from(triggerIds).join(', ')
await this.report('onWatcherRerun', files, triggerLabel)
Expand Down

0 comments on commit fa1a924

Please sign in to comment.