Repository navigation
Expand file tree
/
Copy pathpatches.test.ts
More file actions
153 lines (135 loc) · 6.95 KB
/
Copy pathpatches.test.ts
File metadata and controls
153 lines (135 loc) · 6.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
// Regression coverage for the dependency patch under `patches/`, which keeps merged v8 coverage
// records honest: `ast-v8-to-istanbul` must not publish a branch counter no execution could
// produce. The defect made whole files unscored (#3425).
//
// The cases run on records captured from a real inspector session and pinned in
// `patches.fixture.json`, because V8 serves a repeatedly-evaluated script from its compile cache as
// function-level coverage, which would make a live-capture assertion flaky. Each pinned record
// carries the hand-instrumented entered/taken counts that established its expected counters.
//
// #3448's other patch, on `@vitest/coverage-v8`, is gone: Vitest 5 keeps a module's script wrapper
// offset through the merge of the workers' coverage files instead of letting read order or a pending
// dynamic import decide it (vitest-dev/vitest#10506, #10643), so no patched module is left to
// unit-test. Its symptom was a whole file scored from offsets shifted off its source by the wrapper,
// and a shifted record still carries only plausible counts -- `sanitizeIstanbulCoverage` cannot see
// it. What does catch it is the run's own shape: `check:coverage-crap` blocks when a touched file
// is absent from the map or holds a count no execution can produce, and the 5.0.4 upgrade was
// validated by diffing every file's mapped statements and branches against the 4.1.11 map.
import assert from 'node:assert/strict';
import fs from 'node:fs';
import { createRequire } from 'node:module';
import { pathToFileURL } from 'node:url';
import { parseAstAsync } from 'vitest/node';
import { test } from 'node:test';
import { sanitizeIstanbulCoverage, type IstanbulCoverageMap } from './model.ts';
const PROVIDER_ENTRY = import.meta.resolve('@vitest/coverage-v8');
/** Resolves a dependency of the coverage provider rather than of this repository. */
const providerRequire = createRequire(PROVIDER_ENTRY);
/**
* Loads one such dependency, so the test uses exactly the copy the provider loads.
* Named exports stay named, and a CommonJS package keeps its `default`.
*/
async function loadProviderDependency<T>(name: string): Promise<T> {
return (await import(pathToFileURL(providerRequire.resolve(name)).href)) as T;
}
const loadConverter = async () =>
(await loadProviderDependency<{ default: Converter }>('ast-v8-to-istanbul')).default;
type Converter = (input: unknown) => Promise<IstanbulCoverageMap>;
type IstanbulRecord = {
path: string;
statementMap: Record<string, unknown>;
s: Record<string, number>;
f: Record<string, number>;
b: Record<string, number[]>;
branchMap: Record<string, { type: string; line: number }>;
};
type V8Function = {
functionName: string;
isBlockCoverage: boolean;
ranges: { startOffset: number; endOffset: number; count: number }[];
};
/** One pinned capture: the module, the raw V8 record, and what really executed. */
type Capture = {
wrapperLength: number;
url: string;
code: string;
functions: V8Function[];
branches: Record<string, { line: number; entered: number; taken: number; counters: number[] }>;
};
const captures = JSON.parse(
fs.readFileSync(new URL('./patches.fixture.json', import.meta.url), 'utf8'),
) as Record<string, Capture>;
const TARGET = '/virtual/target.mjs';
async function convertRecord(convert: Converter, capture: Capture): Promise<IstanbulRecord> {
const converted = await convert({
code: capture.code,
ast: await parseAstAsync(capture.code),
coverage: { functions: capture.functions, url: capture.url },
wrapperLength: capture.wrapperLength,
});
return converted[TARGET] as IstanbulRecord;
}
/** The counters the converter publishes for the `if` on `line`. */
function ifCounters(record: IstanbulRecord, line: number): number[] {
const id = Object.keys(record.b).find(
(key) => record.branchMap[key].type === 'if' && record.branchMap[key].line === line,
);
assert.ok(id, `the converter recorded no if branch on line ${line}`);
return record.b[id].map(Number);
}
// `async-fallback-operand` is the reported shape: `params.release ?? await probe(...)` feeding an
// `if` with no `else`. Six callers short-circuit the `??` and take the branch, two await and do
// not. The converter invents the missing path's counter by subtracting one block-containment
// lookup from another, and inside an async function those two lookups land on counts of different
// paths, so it published `[6,-6]`. The record's block tree is V8's own:
test('an async branch with an awaited operand drops the path it cannot count', async () => {
const convert = await loadConverter();
const capture = captures['async-fallback-operand'];
const branch = capture.branches.i1;
assert.deepEqual([branch.entered, branch.taken], [8, 6]);
const record = await convertRecord(convert, capture);
// The taken path is provable and stays. The entry count is not recoverable from a record whose
// continuation block is counted for the awaited path only, so the implicit else is left out
// rather than published as `[6,-6]` — the same one-location branch the reference
// v8-to-istanbul converter produces for this input.
assert.deepEqual(ifCounters(record, branch.line), [6]);
});
test('a branch whose awaited continuation out-counts it publishes no impossible count', async () => {
const convert = await loadConverter();
const capture = captures['early-return-then-fallback'];
const record = await convertRecord(convert, capture);
assert.deepEqual([capture.branches.i1.entered, capture.branches.i1.taken], [8, 3]);
assert.deepEqual([capture.branches.i2.entered, capture.branches.i2.taken], [5, 2]);
// The branch ahead of the await is counted correctly and must not move; the one behind it was
// `[2,-2]` before the patch.
assert.deepEqual(ifCounters(record, capture.branches.i1.line), [3, 5]);
assert.deepEqual(ifCounters(record, capture.branches.i2.line), [2]);
});
test('sync and non-awaited branches keep counting their implicit else', async () => {
const convert = await loadConverter();
for (const name of ['sync-fallback-operand', 'plain-async']) {
const capture = captures[name];
const branch = capture.branches.i1;
assert.deepEqual(
ifCounters(await convertRecord(convert, capture), branch.line),
[6, 2],
`branch counters moved for a record the defect never touched (${name})`,
);
}
});
test('the patch changes nothing about a record it does not apply to', async () => {
const convert = await loadConverter();
for (const capture of Object.values(captures)) {
const record = await convertRecord(convert, capture);
const { map, dropped } = sanitizeIstanbulCoverage({ [TARGET]: record });
assert.deepEqual(dropped, [], 'the converter published an impossible counter');
assert.deepEqual(Object.keys(map), [TARGET]);
for (const branch of Object.values(capture.branches)) {
assert.deepEqual(
ifCounters(record, branch.line),
branch.counters,
`counters drifted from the pinned expectation (line ${branch.line})`,
);
}
}
});