|
| 1 | +import * as path from 'node:path'; |
| 2 | +import * as fs from 'node:fs/promises'; |
| 3 | +import type { EvaluationSummary } from '../../types'; |
| 4 | +import type { CoverageFiles, CoverageSummary } from './result-types'; |
| 5 | +import { createCoverageMap } from 'istanbul-lib-coverage'; |
| 6 | + |
| 7 | +export async function computeCoverage( |
| 8 | + projectPath: string, |
| 9 | + resultsPath: string, |
| 10 | +): Promise<{ |
| 11 | + coverage?: EvaluationSummary['coverage']; |
| 12 | + coverageFiles?: CoverageFiles; |
| 13 | +}> { |
| 14 | + let coverage: EvaluationSummary['coverage']; |
| 15 | + let coverageFiles: CoverageFiles | undefined; |
| 16 | + |
| 17 | + const finalCoveragePath = path.join( |
| 18 | + projectPath, |
| 19 | + 'coverage', |
| 20 | + 'coverage-final.json', |
| 21 | + ); |
| 22 | + |
| 23 | + try { |
| 24 | + let normalizedTotal: CoverageSummary | undefined; |
| 25 | + |
| 26 | + const coverageData = JSON.parse( |
| 27 | + await fs.readFile(finalCoveragePath, 'utf8'), |
| 28 | + ); |
| 29 | + |
| 30 | + // Derive from coverage-final using istanbul-lib-coverage |
| 31 | + const coverageMap = createCoverageMap(coverageData); |
| 32 | + const summary = coverageMap.getCoverageSummary().toJSON(); |
| 33 | + const coverageJson = coverageMap.toJSON(); |
| 34 | + |
| 35 | + coverageFiles = {}; |
| 36 | + |
| 37 | + for (const filePath of Object.keys(coverageJson)) { |
| 38 | + if (filePath === 'total') continue; |
| 39 | + const fileCoverage = coverageMap.fileCoverageFor(filePath); |
| 40 | + const fileSummary = fileCoverage.toSummary().toJSON(); |
| 41 | + let source: string | undefined; |
| 42 | + try { |
| 43 | + source = await fs.readFile(filePath, 'utf8'); |
| 44 | + } catch { |
| 45 | + source = undefined; |
| 46 | + } |
| 47 | + |
| 48 | + let lineHits: Record<string, number> | undefined; |
| 49 | + let branchesByLine: |
| 50 | + | Record<string, { covered: number | null; total: number | null }> |
| 51 | + | undefined; |
| 52 | + try { |
| 53 | + lineHits = fileCoverage.getLineCoverage() as Record<string, number>; |
| 54 | + const branches = fileCoverage.getBranchCoverageByLine?.(); |
| 55 | + if (branches && typeof branches === 'object') { |
| 56 | + branchesByLine = {}; |
| 57 | + for (const [line, data] of Object.entries( |
| 58 | + branches as Record<string, any>, |
| 59 | + )) { |
| 60 | + branchesByLine[line] = { |
| 61 | + covered: data.covered ?? null, |
| 62 | + total: data.total ?? null, |
| 63 | + }; |
| 64 | + } |
| 65 | + } |
| 66 | + } catch { |
| 67 | + // ignore |
| 68 | + } |
| 69 | + |
| 70 | + coverageFiles[filePath] = { |
| 71 | + branches: { pct: fileSummary.branches.pct }, |
| 72 | + functions: { pct: fileSummary.functions.pct }, |
| 73 | + lines: { pct: fileSummary.lines.pct }, |
| 74 | + statements: { pct: fileSummary.statements.pct }, |
| 75 | + lineHits, |
| 76 | + branchesByLine, |
| 77 | + source, |
| 78 | + }; |
| 79 | + } |
| 80 | + normalizedTotal = { |
| 81 | + branches: { pct: summary.branches.pct }, |
| 82 | + functions: { pct: summary.functions.pct }, |
| 83 | + lines: { pct: summary.lines.pct }, |
| 84 | + statements: { pct: summary.statements.pct }, |
| 85 | + }; |
| 86 | + |
| 87 | + if (normalizedTotal) { |
| 88 | + coverage = { |
| 89 | + branches: normalizedTotal.branches?.pct ?? null, |
| 90 | + functions: normalizedTotal.functions?.pct ?? null, |
| 91 | + lines: normalizedTotal.lines?.pct ?? null, |
| 92 | + statements: normalizedTotal.statements?.pct ?? null, |
| 93 | + }; |
| 94 | + |
| 95 | + const targetCoveragePath = path.join( |
| 96 | + resultsPath, |
| 97 | + 'coverage', |
| 98 | + 'coverage-summary.json', |
| 99 | + ); |
| 100 | + await fs.mkdir(path.dirname(targetCoveragePath), { recursive: true }); |
| 101 | + await fs.writeFile( |
| 102 | + targetCoveragePath, |
| 103 | + JSON.stringify({ total: normalizedTotal }, null, 2), |
| 104 | + ); |
| 105 | + |
| 106 | + await fs.writeFile( |
| 107 | + path.join(resultsPath, 'coverage', 'coverage-final.json'), |
| 108 | + JSON.stringify(coverageFiles ?? {}, null, 2), |
| 109 | + ); |
| 110 | + } |
| 111 | + } catch { |
| 112 | + coverage = undefined; |
| 113 | + coverageFiles = undefined; |
| 114 | + } |
| 115 | + |
| 116 | + return { coverage, coverageFiles }; |
| 117 | +} |
0 commit comments