forked from hqwuzhaoyi/gpt-subtitle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoverage-total.js
210 lines (188 loc) · 6.16 KB
/
coverage-total.js
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
const fs = require("fs");
const path = require("path");
// Params
const pathToPreviousReport = process.argv[2];
// Functions
/**
* Reads the coverage-summary.{XXX}.json file and returns the parsed JSON object
* @param {*} pathToReport
* @returns
*/
function readPreviousCoverageSummary(pathToReport) {
if (!pathToReport) {
console.warn("Previous coverage results were not provided.");
return;
}
// Read the JSON file
const prevCoverage = JSON.parse(fs.readFileSync(pathToReport, "utf8"));
return prevCoverage;
}
/**
* Reads go through all the apps and packages and returns
* an object with the paths to the coverage-summary.json files
* @param {*} pathToReport
* @returns
* */
function getAllPathsForPackagesSummaries() {
const getDirectories = (source) =>
fs
.readdirSync(source, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name);
const appsPath = path.join(__dirname, "apps");
const appsNames = getDirectories(appsPath);
const appsSummaries = appsNames.reduce((summary, appName) => {
return {
...summary,
[appName]: path.join(
appsPath,
appName,
"coverage",
"coverage-summary.json"
),
};
}, {});
const packagesPath = path.join(__dirname, "packages");
const packageNames = getDirectories(packagesPath);
const packagesSummaries = packageNames.reduce((summary, packageName) => {
return {
...summary,
[packageName]: path.join(
packagesPath,
packageName,
"coverage",
"coverage-summary.json"
),
};
}, {});
return { ...appsSummaries, ...packagesSummaries };
}
/**
* Reads all the coverage-summary.json files and returns
* an object with the total coverage for each package and the total coverage
* @param {*} packagesSummaryPaths
* @returns
* */
function readSummaryPerPackageAndCreateJoinedSummaryReportWithTotal(
packagesSummaryPaths
) {
return Object.keys(packagesSummaryPaths).reduce(
(summary, packageName) => {
const reportPath = packagesSummaryPaths[packageName];
if (fs.existsSync(reportPath)) {
const report = JSON.parse(fs.readFileSync(reportPath, "utf8"));
const { total } = summary;
Object.keys(report.total).forEach((key) => {
if (total[key]) {
total[key].total += report.total[key].total;
total[key].covered += report.total[key].covered;
total[key].skipped += report.total[key].skipped;
total[key].pct = Number(
((total[key].covered / total[key].total) * 100).toFixed(2)
);
} else {
total[key] = { ...report.total[key] };
}
});
return { ...summary, [packageName]: report.total, total };
}
return summary;
},
{ total: {} }
);
}
/**
* Takes the current coverage and the previous coverage and returns
* an object with the additional field pctDiff
* @param {*} packagesSummaryPaths
* @returns
* */
function creteDiffCoverageReport(currCoverage, prevCoverage = {}) {
return Object.keys(currCoverage).reduce((summary, packageName) => {
const currPackageCoverage = currCoverage[packageName];
const prevPackageCoverage = prevCoverage[packageName];
if (prevPackageCoverage) {
const coverageKeys = ["lines", "statements", "functions", "branches"];
coverageKeys.forEach((key) => {
const prevPct = prevPackageCoverage[key]?.pct || 0;
const currPct = currPackageCoverage[key]?.pct || 0;
currPackageCoverage[key] = {
...currPackageCoverage[key],
pctDiff: (parseFloat(currPct) - parseFloat(prevPct)).toFixed(2),
};
});
}
return { ...summary, [packageName]: currPackageCoverage };
}, {});
}
function formatPtcWithDiff(ptc, ptcDiff) {
return appendDiff(formatDecimal(ptc), ptcDiff && formatDecimal(ptcDiff));
}
function formatDecimal(ptc) {
return parseFloat(ptc).toFixed(2);
}
function appendDiff(ptc, ptcDiff) {
if (!ptcDiff || ptcDiff === ptc) {
return ptc;
}
return `${ptc} (${ptcDiff > 0 ? "+" : ""}${ptcDiff}%)`;
}
/**
* Takes the coverage report and returns an object with the
* coverage for each package and the total coverage suitable
* for the visual representation in a console table
* @param {*} coverageReport
* @returns
* */
function createCoverageReportForVisualRepresentation(coverageReport) {
return Object.keys(coverageReport).reduce((report, packageName) => {
const { lines, statements, functions, branches } =
coverageReport[packageName];
return {
...report,
[packageName]: {
lines: formatPtcWithDiff(lines.pct, lines.pctDiff),
statements: formatPtcWithDiff(statements.pct, statements.pctDiff),
functions: formatPtcWithDiff(functions.pct, functions.pctDiff),
branches: formatPtcWithDiff(branches.pct, branches.pctDiff),
},
};
}, {});
}
function writeCoverageReportToFile(coverageReport) {
function createDateTimeSuffix() {
const date = new Date();
return `${date.getFullYear()}-${
date.getMonth() + 1
}-${date.getDate()}_${date.getHours()}-${date.getMinutes()}`;
}
const dir = path.join(__dirname, "coverage");
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
fs.writeFileSync(
`coverage/coverage-total.${createDateTimeSuffix()}.json`,
JSON.stringify(coverageReport, null, 2)
);
}
// Execution Stages
// 0. Read previous coverage-total.{XXX}.json file
const prevCoverageReport = readPreviousCoverageSummary(pathToPreviousReport);
// 1. Read all coverage-total.json files && Merge them into one object
const packagesSummaryPaths = getAllPathsForPackagesSummaries();
const currCoverageReport =
readSummaryPerPackageAndCreateJoinedSummaryReportWithTotal(
packagesSummaryPaths
);
// 2. Calculate diff
const diffCoverageReport = creteDiffCoverageReport(
currCoverageReport,
prevCoverageReport
);
// 3. Create report for visual representation
const coverageReportForVisualRepresentation =
createCoverageReportForVisualRepresentation(diffCoverageReport);
// 4. Print report
console.table(coverageReportForVisualRepresentation);
// 5. Save report to file
writeCoverageReportToFile(diffCoverageReport);