-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathfix-coverage-paths.ts
More file actions
110 lines (91 loc) · 3.2 KB
/
fix-coverage-paths.ts
File metadata and controls
110 lines (91 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* eslint-disable no-console */
import * as fs from 'fs';
import { globSync } from 'glob';
import * as path from 'path';
interface Location {
line: number;
column: number;
}
interface Range {
start: Location;
end: Location;
}
// JSON structure for file coverage
interface FileCoverage {
path: string;
statementMap: { [key: string]: Range };
fnMap: { [key: string]: { name: string; decl: Range; loc: Range; line: number } };
branchMap: { [key: string]: { loc: Range; type: string; locations: Range[]; line: number } };
s: { [key: string]: number };
f: { [key: string]: number };
b: { [key: string]: number[] };
[key: string]: unknown;
}
interface CoverageMap {
[filePath: string]: FileCoverage;
}
/**
* Finds all coverage files (vitest and playwright) and normalizes the `path` property within them.
*/
function fixAllCoveragePaths(): void {
const searchPattern = '.nyc_output/*.json';
const coverageFiles = globSync(searchPattern, { ignore: '**/node_modules/**' });
if (coverageFiles.length === 0) {
console.log('No coverage files found to process.');
return;
}
console.log(`Found ${coverageFiles.length} coverage file(s) to process.`);
coverageFiles.forEach(processCoverageFile);
console.log('✅ All coverage files have been processed.');
}
/**
* Reads a single coverage file, fixes its internal paths, and writes it back.
* @param filePath
*/
function processCoverageFile(filePath: string): void {
console.log(`\nProcessing: ${filePath}`);
const resolvedCoverageFile = path.resolve(process.cwd(), filePath);
try {
const fileContent: string = fs.readFileSync(resolvedCoverageFile, 'utf8');
const coverageData = JSON.parse(fileContent) as CoverageMap;
const fixedCoverageData: CoverageMap = {};
let pathsFixed = 0;
for (const key in coverageData) {
if (Object.prototype.hasOwnProperty.call(coverageData, key)) {
const fileCoverage = coverageData[key];
let newKey = key;
let pathChanged = false;
// Fix the key if it's missing leading slash
if (!path.isAbsolute(key) && (key.startsWith('home/') || key.startsWith('Users/'))) {
newKey = `/${key}`;
pathChanged = true;
}
// Fix the path property if it's missing leading slash
if (
fileCoverage &&
typeof fileCoverage.path === 'string' &&
!path.isAbsolute(fileCoverage.path) &&
(fileCoverage.path.startsWith('home/') || fileCoverage.path.startsWith('Users/'))
) {
fileCoverage.path = `/${fileCoverage.path}`;
pathChanged = true;
}
if (pathChanged) {
pathsFixed++;
}
// Use the corrected key
fixedCoverageData[newKey] = fileCoverage;
}
}
if (pathsFixed > 0) {
fs.writeFileSync(resolvedCoverageFile, JSON.stringify(fixedCoverageData, null, 2));
console.log(`Fixed ${pathsFixed} path(s) in ${filePath}`);
} else {
console.log(`No paths needed fixing in ${filePath}`);
}
} catch (error) {
const errorMessage = error instanceof Error ? error.message : 'An unknown error occurred';
console.error(`Error processing file ${filePath}: ${errorMessage}`);
}
}
fixAllCoveragePaths();