-
-
Notifications
You must be signed in to change notification settings - Fork 277
Expand file tree
/
Copy pathrun.test.ts
More file actions
160 lines (144 loc) · 5.95 KB
/
Copy pathrun.test.ts
File metadata and controls
160 lines (144 loc) · 5.95 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
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
import assert from 'node:assert/strict';
import { execFileSync } from 'node:child_process';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { afterEach, beforeEach, test } from 'node:test';
import { run } from './run.ts';
let repo: string;
function git(...args: string[]): void {
execFileSync('git', args, { cwd: repo, stdio: 'pipe' });
}
function write(rel: string, content: string): void {
const abs = path.join(repo, rel);
fs.mkdirSync(path.dirname(abs), { recursive: true });
fs.writeFileSync(abs, content);
}
function writeLcov(records: string): void {
write('coverage/lcov.info', records);
}
// Redirect the module's stdout/stderr writers so the entrypoint's rendering
// runs without spamming the test reporter.
function capture(fn: () => number): { code: number; out: string } {
const chunks: string[] = [];
const original = process.stdout.write.bind(process.stdout);
const originalErr = process.stderr.write.bind(process.stderr);
process.stdout.write = ((chunk: string | Uint8Array): boolean => {
chunks.push(String(chunk));
return true;
}) as typeof process.stdout.write;
process.stderr.write = ((chunk: string | Uint8Array): boolean => {
chunks.push(String(chunk));
return true;
}) as typeof process.stderr.write;
try {
const code = fn();
return { code, out: chunks.join('') };
} finally {
process.stdout.write = original;
process.stderr.write = originalErr;
}
}
beforeEach(() => {
repo = fs.mkdtempSync(path.join(os.tmpdir(), 'cov-changed-'));
git('init', '-q');
git('config', 'user.email', 'test@example.com');
git('config', 'user.name', 'Test');
git('checkout', '-q', '-b', 'main');
write('src/base.ts', 'export const base = 1;\n');
git('add', '-A');
git('commit', '-q', '-m', 'base');
git('checkout', '-q', '-b', 'feature');
});
afterEach(() => {
fs.rmSync(repo, { recursive: true, force: true });
});
test('passes and prints n/a for a docs-only change without touching coverage', () => {
write('README.md', '# hello\nnew docs line\n');
git('add', '-A');
git('commit', '-q', '-m', 'docs');
writeLcov('SF:src/base.ts\nDA:1,1\nend_of_record\n');
const { code, out } = capture(() => run(['--base', 'main'], repo));
assert.equal(code, 0);
assert.match(out, /Changed-line coverage gate: PASS/);
assert.match(out, /0\/0 \(n\/a\)/);
});
test('reads a large stacked diff beyond the subprocess default buffer', () => {
write('README.md', '# large stack\n' + 'documentation line\n'.repeat(80_000));
git('add', '-A');
git('commit', '-q', '-m', 'large docs stack');
writeLcov('SF:src/base.ts\nDA:1,1\nend_of_record\n');
const { code, out } = capture(() => run(['--base', 'main'], repo));
assert.equal(code, 0);
assert.match(out, /Changed-line coverage gate: PASS/);
assert.match(out, /0\/0 \(n\/a\)/);
});
test('fails when a changed source line is uncovered and names that line', () => {
write('src/feature.ts', 'export const covered = 1;\nexport const uncovered = 2;\n');
git('add', '-A');
git('commit', '-q', '-m', 'feature');
writeLcov('SF:src/feature.ts\nDA:1,3\nDA:2,0\nend_of_record\n');
const { code, out } = capture(() => run(['--base', 'main'], repo));
assert.equal(code, 1);
assert.match(out, /Changed-line coverage gate: FAIL/);
assert.match(out, /`src\/feature\.ts`: 2/);
});
test('fails when a changed workspace package line is uncovered and names that line', () => {
write(
'packages/contracts/src/feature.ts',
'export const covered = 1;\nexport const uncovered = 2;\n',
);
git('add', '-A');
git('commit', '-q', '-m', 'package feature');
writeLcov('SF:packages/contracts/src/feature.ts\nDA:1,3\nDA:2,0\nend_of_record\n');
const { code, out } = capture(() => run(['--base', 'main'], repo));
assert.equal(code, 1);
assert.match(out, /Changed-line coverage gate: FAIL/);
assert.match(out, /`packages\/contracts\/src\/feature\.ts`: 2/);
});
test('waiver env keeps the job green, still reporting numbers to the job summary', () => {
write('src/feature.ts', 'export const covered = 1;\nexport const uncovered = 2;\n');
git('add', '-A');
git('commit', '-q', '-m', 'feature');
writeLcov('SF:src/feature.ts\nDA:1,3\nDA:2,0\nend_of_record\n');
const summaryPath = path.join(repo, 'summary.md');
const previous = {
waiver: process.env.AGENT_DEVICE_COVERAGE_WAIVER,
summary: process.env.GITHUB_STEP_SUMMARY,
};
process.env.AGENT_DEVICE_COVERAGE_WAIVER = 'true';
process.env.GITHUB_STEP_SUMMARY = summaryPath;
try {
const { code, out } = capture(() => run(['--base', 'main'], repo));
assert.equal(code, 0);
assert.match(out, /WAIVED/);
assert.match(out, /1\/2/);
const summary = fs.readFileSync(summaryPath, 'utf8');
assert.match(summary, /## Changed-line coverage gate/);
assert.match(summary, /Changed-branch coverage \(non-gating\)/);
assert.match(summary, /Changed executable lines excluded/);
} finally {
if (previous.waiver === undefined) delete process.env.AGENT_DEVICE_COVERAGE_WAIVER;
else process.env.AGENT_DEVICE_COVERAGE_WAIVER = previous.waiver;
if (previous.summary === undefined) delete process.env.GITHUB_STEP_SUMMARY;
else process.env.GITHUB_STEP_SUMMARY = previous.summary;
}
});
test('errors when the lcov report is missing rather than silently passing', () => {
write('src/feature.ts', 'export const x = 1;\n');
git('add', '-A');
git('commit', '-q', '-m', 'feature');
const { code, out } = capture(() => run(['--base', 'main'], repo));
assert.equal(code, 1);
assert.match(out, /no lcov report/);
});
test('a pure move owes nothing regardless of the host diff.renames setting', () => {
git('config', 'diff.renames', 'false');
git('mv', 'src/base.ts', 'src/moved.ts');
git('commit', '-q', '-m', 'move');
writeLcov('SF:src/moved.ts\nDA:1,0\nend_of_record\n');
const { code, out } = capture(() => run(['--base', 'main'], repo));
assert.equal(code, 0);
assert.match(out, /Changed-line coverage gate: PASS/);
assert.match(out, /0\/0 \(n\/a\)/);
});