Skip to content

Commit 61f8696

Browse files
test(ci): gate PRs on changed-line coverage (#1418) (#1447)
* test(ci): gate PRs on changed-line coverage (#1418) Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * refactor: split coverage-changed model into small helpers for fallow Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * refactor(ci): simplify coverage-changed reporting and CLI surface Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --------- Co-authored-by: Michał Pierzchała <[email protected]> Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent edca35d commit 61f8696

6 files changed

Lines changed: 799 additions & 0 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,24 @@ jobs:
251251
- name: Setup toolchain
252252
uses: ./.github/actions/setup-node-pnpm
253253

254+
- name: Test changed-line coverage gate
255+
run: pnpm check:coverage-changed:test
256+
254257
- name: Run coverage
255258
env:
256259
OUTPUT_ECONOMY_BASE: ${{ github.event_name == 'pull_request' && github.event.pull_request.base.sha || github.event.before }}
257260
run: pnpm test:coverage
258261

262+
# Reuses the lcov the coverage step just wrote (never runs coverage twice)
263+
# and fails when changed-line coverage < the threshold in
264+
# scripts/coverage-changed/model.ts. The `coverage-waiver` PR label maps to
265+
# the waiver env, which skips the failure but still prints the numbers.
266+
- name: Enforce changed-line coverage gate
267+
if: always() && github.event_name == 'pull_request'
268+
env:
269+
AGENT_DEVICE_COVERAGE_WAIVER: ${{ contains(github.event.pull_request.labels.*.name, 'coverage-waiver') }}
270+
run: pnpm check:coverage-changed --base "${{ github.event.pull_request.base.sha }}"
271+
259272
typecheck:
260273
name: Typecheck
261274
runs-on: ubuntu-latest

‎package.json‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@
123123
"check:fallow": "fallow audit",
124124
"check:affected": "node --experimental-strip-types scripts/check-affected/run.ts",
125125
"check:affected:test": "node --experimental-strip-types --test scripts/check-affected/model.test.ts scripts/check-affected/run.test.ts",
126+
"check:coverage-changed": "node --experimental-strip-types scripts/coverage-changed/run.ts",
127+
"check:coverage-changed:test": "node --experimental-strip-types --test scripts/coverage-changed/model.test.ts scripts/coverage-changed/run.test.ts",
126128
"check:layering": "node --experimental-strip-types --test scripts/layering/model.test.ts && node --experimental-strip-types scripts/layering/check.ts",
127129
"depgraph": "node --experimental-strip-types scripts/depgraph/build.ts",
128130
"depgraph:test": "node --experimental-strip-types --test scripts/depgraph/model.test.ts",
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
import assert from 'node:assert/strict';
2+
import { test } from 'node:test';
3+
import {
4+
CHANGED_LINE_COVERAGE_THRESHOLD,
5+
computeChangedCoverage,
6+
ignoredLineSet,
7+
isIncludableSource,
8+
isTestFile,
9+
normalizePath,
10+
parseLcov,
11+
parseUnifiedDiff,
12+
type ChangedFileDiff,
13+
} from './model.ts';
14+
15+
test('normalizePath strips root, b/ and ./ prefixes and posixifies', () => {
16+
assert.equal(normalizePath('/repo/src/a.ts', '/repo'), 'src/a.ts');
17+
assert.equal(normalizePath('b/src/a.ts'), 'src/a.ts');
18+
assert.equal(normalizePath('./src/a.ts'), 'src/a.ts');
19+
assert.equal(normalizePath('src\\a.ts'), 'src/a.ts');
20+
});
21+
22+
test('parseLcov reads DA hits and BRDA taken, treating "-" as not taken', () => {
23+
const lcov = [
24+
'SF:/repo/src/a.ts',
25+
'DA:1,3',
26+
'DA:2,0',
27+
'BRDA:1,0,0,3',
28+
'BRDA:2,1,0,-',
29+
'end_of_record',
30+
'SF:src/b.ts',
31+
'DA:10,1',
32+
'end_of_record',
33+
].join('\n');
34+
const index = parseLcov(lcov, '/repo');
35+
const a = index.get('src/a.ts');
36+
assert.ok(a);
37+
assert.equal(a.lineHits.get(1), 3);
38+
assert.equal(a.lineHits.get(2), 0);
39+
assert.deepEqual(a.branches, [
40+
{ line: 1, taken: 3 },
41+
{ line: 2, taken: 0 },
42+
]);
43+
assert.equal(index.get('src/b.ts')?.lineHits.get(10), 1);
44+
});
45+
46+
test('parseUnifiedDiff numbers added lines per hunk and ignores deleted lines', () => {
47+
const diff = [
48+
'diff --git a/src/a.ts b/src/a.ts',
49+
'--- a/src/a.ts',
50+
'+++ b/src/a.ts',
51+
'@@ -1,0 +1,2 @@',
52+
'+const a = 1;',
53+
'+const b = 2;',
54+
'@@ -10,1 +12,1 @@',
55+
'-old line',
56+
'+new line',
57+
].join('\n');
58+
const [file] = parseUnifiedDiff(diff);
59+
assert.equal(file.path, 'src/a.ts');
60+
assert.equal(file.deleted, false);
61+
assert.deepEqual(file.added, [1, 2, 12]);
62+
});
63+
64+
test('parseUnifiedDiff marks deleted files and keeps rename destinations', () => {
65+
const diff = [
66+
'diff --git a/src/gone.ts b/src/gone.ts',
67+
'--- a/src/gone.ts',
68+
'+++ /dev/null',
69+
'@@ -1,1 +0,0 @@',
70+
'-was here',
71+
'diff --git a/src/old.ts b/src/new.ts',
72+
'rename from src/old.ts',
73+
'rename to src/new.ts',
74+
'--- a/src/old.ts',
75+
'+++ b/src/new.ts',
76+
'@@ -3,0 +4,1 @@',
77+
'+added on new path',
78+
].join('\n');
79+
const files = parseUnifiedDiff(diff);
80+
const gone = files.find((f) => f.path === 'src/gone.ts');
81+
assert.equal(gone?.deleted, true);
82+
const renamed = files.find((f) => f.path === 'src/new.ts');
83+
assert.deepEqual(renamed?.added, [4]);
84+
});
85+
86+
test('source/test classification matches the coverage include glob', () => {
87+
assert.equal(isIncludableSource('src/core/x.ts'), true);
88+
assert.equal(isIncludableSource('src/core/x.test.ts'), false);
89+
assert.equal(isIncludableSource('src/core/__tests__/x.ts'), false);
90+
assert.equal(isIncludableSource('scripts/y.ts'), false);
91+
assert.equal(isIncludableSource('src/core/x.tsx'), false);
92+
assert.equal(isTestFile('src/a.test.ts'), true);
93+
assert.equal(isTestFile('src/__tests__/a.ts'), true);
94+
assert.equal(isTestFile('src/a.ts'), false);
95+
});
96+
97+
test('ignoredLineSet handles next, next N and start/stop ranges', () => {
98+
const lines = [
99+
'const a = 1;', // 1
100+
'/* v8 ignore next */', // 2
101+
'const b = 2;', // 3 ignored
102+
'/* c8 ignore next 2 */', // 4
103+
'const c = 3;', // 5 ignored
104+
'const d = 4;', // 6 ignored
105+
'/* istanbul ignore start */', // 7 ignored
106+
'const e = 5;', // 8 ignored
107+
'/* istanbul ignore stop */', // 9
108+
'const f = 6;', // 10
109+
];
110+
const ignored = ignoredLineSet(lines);
111+
assert.deepEqual(
112+
[...ignored].sort((a, b) => a - b),
113+
[3, 5, 6, 7, 8],
114+
);
115+
});
116+
117+
const coverage = parseLcov(
118+
[
119+
'SF:src/covered.ts',
120+
'DA:1,5',
121+
'DA:2,0',
122+
'DA:3,2',
123+
'BRDA:1,0,0,5',
124+
'BRDA:2,0,0,0',
125+
'end_of_record',
126+
].join('\n'),
127+
);
128+
129+
function diff(path: string, added: number[], deleted = false): ChangedFileDiff {
130+
return { path, added, deleted };
131+
}
132+
133+
test('gate fails when changed-line coverage is below threshold and lists offenders', () => {
134+
const result = computeChangedCoverage({
135+
diffs: [diff('src/covered.ts', [1, 2, 3])],
136+
coverage,
137+
fileLines: () => null,
138+
});
139+
// lines 1 & 3 covered, line 2 uncovered -> 2/3 = 66.67% < 70.
140+
assert.equal(result.totalLines, 3);
141+
assert.equal(result.coveredLines, 2);
142+
assert.equal(result.passed, false);
143+
assert.deepEqual(result.offenders[0]?.uncoveredLines, [2]);
144+
assert.equal(result.offenders[0]?.path, 'src/covered.ts');
145+
// Branches on changed lines are reported, not gated.
146+
assert.equal(result.branch.total, 2);
147+
assert.equal(result.branch.covered, 1);
148+
});
149+
150+
test('non-executable added lines (absent from DA) never enter the denominator', () => {
151+
const result = computeChangedCoverage({
152+
diffs: [diff('src/covered.ts', [1, 3, 99])],
153+
coverage,
154+
fileLines: () => null,
155+
});
156+
assert.equal(result.totalLines, 2);
157+
assert.equal(result.coveredLines, 2);
158+
assert.equal(result.pct, 100);
159+
assert.equal(result.passed, true);
160+
});
161+
162+
test('deleted files and docs-only changes are a trivial pass with null pct', () => {
163+
const result = computeChangedCoverage({
164+
diffs: [diff('src/covered.ts', [1], true), diff('README.md', [1, 2])],
165+
coverage,
166+
fileLines: () => null,
167+
});
168+
assert.equal(result.pct, null);
169+
assert.equal(result.totalLines, 0);
170+
assert.equal(result.passed, true);
171+
});
172+
173+
test('waiver forces a pass but still reports the numbers', () => {
174+
const result = computeChangedCoverage({
175+
diffs: [diff('src/covered.ts', [1, 2, 3])],
176+
coverage,
177+
waived: true,
178+
fileLines: () => null,
179+
});
180+
assert.equal(result.waived, true);
181+
assert.equal(result.passed, true);
182+
assert.equal(result.pct, 66.67);
183+
});
184+
185+
test('includable source absent from lcov counts as excluded-path code lines', () => {
186+
const result = computeChangedCoverage({
187+
diffs: [diff('src/sdk/thing.ts', [1, 2, 3])],
188+
coverage,
189+
fileLines: () => ['const kept = 1;', '', '// a comment'],
190+
});
191+
assert.equal(result.totalLines, 0);
192+
assert.equal(result.excluded.totalLines, 1);
193+
assert.equal(result.excluded.files[0]?.reason, 'excluded-path');
194+
assert.deepEqual(result.excluded.files[0]?.lines, [1]);
195+
});
196+
197+
test('changed line suppressed by an ignore directive is tallied, not gated', () => {
198+
const cov = parseLcov(['SF:src/x.ts', 'DA:1,1', 'end_of_record'].join('\n'));
199+
const result = computeChangedCoverage({
200+
diffs: [diff('src/x.ts', [1, 3])],
201+
coverage: cov,
202+
fileLines: () => ['const a = 1;', '/* v8 ignore next */', 'const suppressed = 3;'],
203+
});
204+
// Line 3 is code-like, dropped from DA by the directive on line 2, and inside
205+
// its ignore range: it lands in the non-gating excluded tally. Only line 1
206+
// (present in DA) feeds the gate denominator, so the tally never inflates it.
207+
assert.equal(result.totalLines, 1);
208+
assert.equal(result.coveredLines, 1);
209+
assert.equal(result.excluded.totalLines, 1);
210+
assert.equal(result.excluded.files[0]?.reason, 'ignored-lines');
211+
assert.deepEqual(result.excluded.files[0]?.lines, [3]);
212+
});
213+
214+
test('threshold constant is the single source of truth', () => {
215+
assert.equal(CHANGED_LINE_COVERAGE_THRESHOLD, 70);
216+
});

0 commit comments

Comments
 (0)