coverage-check 0.1.1 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +109 -51
- package/bin/coverage-check.mjs +4 -0
- package/dist/src/cli.d.mts +1 -0
- package/dist/src/cli.mjs +14 -0
- package/dist/src/commands/check-args.d.mts +20 -0
- package/dist/src/commands/check-args.mjs +89 -0
- package/dist/src/commands/check.d.mts +4 -0
- package/dist/src/commands/check.mjs +128 -0
- package/dist/src/commands/store-put.d.mts +11 -0
- package/dist/src/commands/store-put.mjs +104 -0
- package/{src/coverage-check.mts → dist/src/coverage-check.d.mts} +3 -9
- package/dist/src/coverage-check.mjs +4 -0
- package/dist/src/diff-parser.d.mts +17 -0
- package/dist/src/diff-parser.mjs +127 -0
- package/dist/src/github-comment.d.mts +9 -0
- package/dist/src/github-comment.mjs +66 -0
- package/dist/src/lcov-merge.d.mts +5 -0
- package/dist/src/lcov-merge.mjs +29 -0
- package/dist/src/lcov-parser.d.mts +8 -0
- package/dist/src/lcov-parser.mjs +44 -0
- package/dist/src/load-artifacts.d.mts +9 -0
- package/dist/src/load-artifacts.mjs +41 -0
- package/dist/src/patch-coverage.d.mts +5 -0
- package/dist/src/patch-coverage.mjs +65 -0
- package/dist/src/report.d.mts +4 -0
- package/dist/src/report.mjs +65 -0
- package/dist/src/rules.d.mts +4 -0
- package/dist/src/rules.mjs +30 -0
- package/dist/src/s3-suite-store.d.mts +28 -0
- package/dist/src/s3-suite-store.mjs +147 -0
- package/dist/src/s3-utils.d.mts +2 -0
- package/dist/src/s3-utils.mjs +14 -0
- package/dist/src/step-summary.d.mts +9 -0
- package/dist/src/step-summary.mjs +70 -0
- package/dist/src/store-factory.d.mts +11 -0
- package/dist/src/store-factory.mjs +23 -0
- package/dist/src/suite-store.d.mts +51 -0
- package/dist/src/suite-store.mjs +154 -0
- package/dist/src/types.d.mts +36 -0
- package/dist/src/types.mjs +1 -0
- package/package.json +20 -5
- package/bin/coverage-check.mts +0 -6
- package/src/cli.mts +0 -15
- package/src/cli.test.mts +0 -45
- package/src/commands/check.mts +0 -200
- package/src/commands/check.test.mts +0 -642
- package/src/commands/store-put.mts +0 -100
- package/src/commands/store-put.test.mts +0 -154
- package/src/diff-parser.mts +0 -127
- package/src/diff-parser.test.mts +0 -178
- package/src/github-comment.mts +0 -74
- package/src/github-comment.test.mts +0 -64
- package/src/lcov-merge.mts +0 -34
- package/src/lcov-merge.test.mts +0 -57
- package/src/lcov-parser.mts +0 -46
- package/src/lcov-parser.test.mts +0 -86
- package/src/load-artifacts.mts +0 -42
- package/src/load-artifacts.test.mts +0 -115
- package/src/patch-coverage.mts +0 -82
- package/src/patch-coverage.test.mts +0 -91
- package/src/report.mts +0 -87
- package/src/report.test.mts +0 -159
- package/src/rules.mts +0 -34
- package/src/rules.test.mts +0 -98
- package/src/suite-store.mts +0 -62
- package/src/suite-store.test.mts +0 -115
- package/src/types.mts +0 -43
package/src/commands/check.mts
DELETED
|
@@ -1,200 +0,0 @@
|
|
|
1
|
-
import { readFileSync, writeFileSync } from "node:fs";
|
|
2
|
-
import { parseLcov } from "../lcov-parser.mts";
|
|
3
|
-
import { mergeLcov } from "../lcov-merge.mts";
|
|
4
|
-
import { getChangedLines } from "../diff-parser.mts";
|
|
5
|
-
import { loadRules } from "../rules.mts";
|
|
6
|
-
import { computePatchCoverage } from "../patch-coverage.mts";
|
|
7
|
-
import { collapseRanges, renderFailureComment, renderPassComment } from "../report.mts";
|
|
8
|
-
import { upsertComment } from "../github-comment.mts";
|
|
9
|
-
import { collectLcovFiles, buildStripPrefixes } from "../load-artifacts.mts";
|
|
10
|
-
import { FileSystemSuiteStore } from "../suite-store.mts";
|
|
11
|
-
import type { LcovData } from "../types.mts";
|
|
12
|
-
import type { SuiteStore } from "../suite-store.mts";
|
|
13
|
-
import type { GhRunner } from "../github-comment.mts";
|
|
14
|
-
|
|
15
|
-
const stdout = (msg: string) => process.stdout.write(`${msg}\n`);
|
|
16
|
-
const stderr = (msg: string) => process.stderr.write(`${msg}\n`);
|
|
17
|
-
|
|
18
|
-
export type CheckArgs = {
|
|
19
|
-
rules: string;
|
|
20
|
-
artifacts: string;
|
|
21
|
-
base: string;
|
|
22
|
-
head: string;
|
|
23
|
-
pr: number | null;
|
|
24
|
-
repo: string;
|
|
25
|
-
json: string | null;
|
|
26
|
-
stripPrefixes: string[];
|
|
27
|
-
store: SuiteStore | null;
|
|
28
|
-
suite: string | null;
|
|
29
|
-
gh?: GhRunner;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
function parseArgs(argv: string[]): CheckArgs {
|
|
33
|
-
const args: CheckArgs = {
|
|
34
|
-
rules: ".coverage-rules.yml",
|
|
35
|
-
artifacts: "./coverage-artifacts",
|
|
36
|
-
base: "origin/main",
|
|
37
|
-
head: "HEAD",
|
|
38
|
-
pr: null,
|
|
39
|
-
repo: process.env["GITHUB_REPOSITORY"] ?? "",
|
|
40
|
-
json: null,
|
|
41
|
-
stripPrefixes: [],
|
|
42
|
-
store: null,
|
|
43
|
-
suite: null,
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
for (let i = 0; i < argv.length; i++) {
|
|
47
|
-
const flag = argv[i]!;
|
|
48
|
-
const next = argv[i + 1];
|
|
49
|
-
const val = (): string => {
|
|
50
|
-
if (next === undefined) throw new Error(`${flag} requires a value`);
|
|
51
|
-
i++;
|
|
52
|
-
return next;
|
|
53
|
-
};
|
|
54
|
-
switch (flag) {
|
|
55
|
-
case "--rules":
|
|
56
|
-
args.rules = val();
|
|
57
|
-
break;
|
|
58
|
-
case "--artifacts":
|
|
59
|
-
args.artifacts = val();
|
|
60
|
-
break;
|
|
61
|
-
case "--base":
|
|
62
|
-
args.base = val();
|
|
63
|
-
break;
|
|
64
|
-
case "--head":
|
|
65
|
-
args.head = val();
|
|
66
|
-
break;
|
|
67
|
-
case "--pr": {
|
|
68
|
-
const raw = val();
|
|
69
|
-
if (!/^\d+$/.test(raw) || raw === "0")
|
|
70
|
-
throw new Error(`--pr must be a positive integer, got: ${JSON.stringify(raw)}`);
|
|
71
|
-
args.pr = parseInt(raw, 10);
|
|
72
|
-
break;
|
|
73
|
-
}
|
|
74
|
-
case "--repo":
|
|
75
|
-
args.repo = val();
|
|
76
|
-
break;
|
|
77
|
-
case "--json":
|
|
78
|
-
args.json = val();
|
|
79
|
-
break;
|
|
80
|
-
case "--strip-prefix":
|
|
81
|
-
args.stripPrefixes.push(val());
|
|
82
|
-
break;
|
|
83
|
-
case "--store":
|
|
84
|
-
args.store = new FileSystemSuiteStore(val());
|
|
85
|
-
break;
|
|
86
|
-
case "--suite":
|
|
87
|
-
args.suite = val();
|
|
88
|
-
break;
|
|
89
|
-
default:
|
|
90
|
-
throw new Error(`unknown flag: ${flag}`);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
return args;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
export async function main(argv: string[]): Promise<number> {
|
|
98
|
-
let args: CheckArgs;
|
|
99
|
-
try {
|
|
100
|
-
args = parseArgs(argv);
|
|
101
|
-
} catch (err) {
|
|
102
|
-
/* c8 ignore next */
|
|
103
|
-
stderr(`coverage-check: ${err instanceof Error ? err.message : err}`);
|
|
104
|
-
return 2;
|
|
105
|
-
}
|
|
106
|
-
return runCheck(args);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
export async function runCheck(args: CheckArgs): Promise<number> {
|
|
110
|
-
let rules;
|
|
111
|
-
try {
|
|
112
|
-
rules = loadRules(args.rules);
|
|
113
|
-
} catch (err) {
|
|
114
|
-
stderr(`coverage-check: failed to load rules: ${err}`);
|
|
115
|
-
return 2;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
const stripPrefixes = buildStripPrefixes(args.stripPrefixes);
|
|
119
|
-
const reports: LcovData[] = [];
|
|
120
|
-
|
|
121
|
-
// Merge in suites from the store (skip the current suite — fresh artifacts take precedence)
|
|
122
|
-
if (args.store !== null) {
|
|
123
|
-
const suites = await args.store.list();
|
|
124
|
-
for (const suite of suites) {
|
|
125
|
-
if (suite === args.suite) continue;
|
|
126
|
-
const buf = await args.store.get(suite);
|
|
127
|
-
if (buf !== null) {
|
|
128
|
-
reports.push(parseLcov(buf.toString("utf8"), stripPrefixes));
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// Add current run's lcov files
|
|
134
|
-
const lcovFiles = collectLcovFiles(args.artifacts);
|
|
135
|
-
for (const f of lcovFiles) {
|
|
136
|
-
reports.push(parseLcov(readFileSync(f, "utf8"), stripPrefixes));
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
if (reports.length === 0) {
|
|
140
|
-
stderr(`coverage-check: no coverage data found — skipping`);
|
|
141
|
-
return 0;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
const lcov = mergeLcov(reports);
|
|
145
|
-
|
|
146
|
-
let diff;
|
|
147
|
-
try {
|
|
148
|
-
diff = await getChangedLines(args.base, args.head);
|
|
149
|
-
} catch (err) {
|
|
150
|
-
stderr(`coverage-check: git diff failed: ${err}`);
|
|
151
|
-
return 2;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
const { buckets, informational } = computePatchCoverage(diff, lcov, rules);
|
|
155
|
-
const passed = buckets.every((b) => b.passed);
|
|
156
|
-
const result = { buckets, informational, passed };
|
|
157
|
-
|
|
158
|
-
if (args.json) {
|
|
159
|
-
writeFileSync(args.json, JSON.stringify(result, null, 2));
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
const runUrl =
|
|
163
|
-
process.env["GITHUB_SERVER_URL"] && process.env["GITHUB_RUN_ID"]
|
|
164
|
-
? `${process.env["GITHUB_SERVER_URL"]}/${args.repo}/actions/runs/${process.env["GITHUB_RUN_ID"]}`
|
|
165
|
-
: "N/A";
|
|
166
|
-
|
|
167
|
-
if (!passed) {
|
|
168
|
-
stdout("\ncoverage-check: FAILED\n");
|
|
169
|
-
for (const bucket of buckets.filter((b) => !b.passed)) {
|
|
170
|
-
/* c8 ignore next -- buckets always have coverable>0 by construction */
|
|
171
|
-
const pct =
|
|
172
|
-
bucket.coverable > 0 ? `${((bucket.hit / bucket.coverable) * 100).toFixed(1)}%` : "—";
|
|
173
|
-
stdout(
|
|
174
|
-
` ${bucket.rule}: ${pct} (${bucket.hit}/${bucket.coverable}) — threshold ${bucket.threshold}%`,
|
|
175
|
-
);
|
|
176
|
-
for (const file of bucket.files.filter((f) => f.uncoveredLines.length > 0)) {
|
|
177
|
-
stdout(` ${file.file}: ${collapseRanges(file.uncoveredLines)}`);
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
} else {
|
|
181
|
-
stdout("\ncoverage-check: PASSED\n");
|
|
182
|
-
for (const bucket of buckets) {
|
|
183
|
-
/* c8 ignore next -- buckets always have coverable>0 by construction */
|
|
184
|
-
const pct =
|
|
185
|
-
bucket.coverable > 0 ? `${((bucket.hit / bucket.coverable) * 100).toFixed(1)}%` : "—";
|
|
186
|
-
stdout(` ${bucket.rule}: ${pct} ✓`);
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
if (args.pr !== null && args.repo) {
|
|
191
|
-
const body = passed ? renderPassComment(runUrl) : renderFailureComment(result, runUrl);
|
|
192
|
-
try {
|
|
193
|
-
await upsertComment(body, args.repo, args.pr, passed, args.gh);
|
|
194
|
-
} catch (err) {
|
|
195
|
-
stderr(`coverage-check: failed to post PR comment: ${err}`);
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
return passed ? 0 : 1;
|
|
200
|
-
}
|