@diffci.com/diffci 0.3.32 → 0.3.34
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 +23 -0
- package/dist-client/src/client/check-plan-junit.d.ts +25 -0
- package/dist-client/src/client/check-plan-junit.js +215 -0
- package/dist-client/src/client/check-plan.d.ts +42 -0
- package/dist-client/src/client/check-plan.js +143 -0
- package/dist-client/src/client/cli.js +212 -7
- package/dist-client/src/client/command-batches.d.ts +1 -0
- package/dist-client/src/client/command-batches.js +3 -1
- package/dist-client/src/client/enforce-misses-collect.js +6 -1
- package/dist-client/src/client/enforce-misses.d.ts +3 -2
- package/dist-client/src/client/enforce-misses.js +19 -59
- package/dist-client/src/client/failure-baseline.js +0 -0
- package/dist-client/src/client/full-command.d.ts +8 -0
- package/dist-client/src/client/full-command.js +31 -0
- package/dist-client/src/client/job-summary.js +1 -1
- package/dist-client/src/client/observe.d.ts +4 -0
- package/dist-client/src/client/observe.js +31 -2
- package/dist-client/src/client/report.d.ts +4 -0
- package/dist-client/src/client/test-results.js +7 -0
- package/dist-client/src/client/verify.d.ts +18 -0
- package/dist-client/src/client/verify.js +39 -10
- package/dist-client/src/client/watch.d.ts +36 -0
- package/dist-client/src/client/watch.js +76 -0
- package/node_modules/@diffci.com/core/README.md +36 -5
- package/node_modules/@diffci.com/core/dist/cache/graph-cache.d.ts +1 -1
- package/node_modules/@diffci.com/core/dist/cache/graph-cache.js +3 -1
- package/node_modules/@diffci.com/core/dist/cache/graph-cache.js.map +1 -1
- package/node_modules/@diffci.com/core/dist/git/git-diff.d.ts +12 -0
- package/node_modules/@diffci.com/core/dist/git/git-diff.js +86 -0
- package/node_modules/@diffci.com/core/dist/git/git-diff.js.map +1 -1
- package/node_modules/@diffci.com/core/dist/git/types.d.ts +8 -0
- package/node_modules/@diffci.com/core/dist/planner/test-command.js +16 -0
- package/node_modules/@diffci.com/core/dist/planner/test-command.js.map +1 -1
- package/node_modules/@diffci.com/core/dist/repo/adapters/index.js +2 -1
- package/node_modules/@diffci.com/core/dist/repo/adapters/index.js.map +1 -1
- package/node_modules/@diffci.com/core/dist/repo/adapters/python.d.ts +32 -0
- package/node_modules/@diffci.com/core/dist/repo/adapters/python.js +273 -0
- package/node_modules/@diffci.com/core/dist/repo/adapters/python.js.map +1 -0
- package/node_modules/@diffci.com/core/dist/repo/adapters/types.d.ts +2 -0
- package/node_modules/@diffci.com/core/dist/repo/adapters/types.js.map +1 -1
- package/node_modules/@diffci.com/core/dist/repo/file-references.d.ts +15 -0
- package/node_modules/@diffci.com/core/dist/repo/file-references.js +45 -0
- package/node_modules/@diffci.com/core/dist/repo/file-references.js.map +1 -1
- package/node_modules/@diffci.com/core/dist/repo/graph.d.ts +4 -0
- package/node_modules/@diffci.com/core/dist/repo/graph.js +182 -21
- package/node_modules/@diffci.com/core/dist/repo/graph.js.map +1 -1
- package/node_modules/@diffci.com/core/dist/repo/impact.d.ts +1 -0
- package/node_modules/@diffci.com/core/dist/repo/impact.js +37 -9
- package/node_modules/@diffci.com/core/dist/repo/impact.js.map +1 -1
- package/node_modules/@diffci.com/core/dist/repo/types.d.ts +5 -0
- package/node_modules/@diffci.com/core/src/cache/graph-cache.ts +3 -1
- package/node_modules/@diffci.com/core/src/git/git-diff.ts +72 -0
- package/node_modules/@diffci.com/core/src/git/types.ts +5 -0
- package/node_modules/@diffci.com/core/src/planner/test-command.ts +14 -0
- package/node_modules/@diffci.com/core/src/repo/adapters/index.ts +2 -1
- package/node_modules/@diffci.com/core/src/repo/adapters/python.ts +252 -0
- package/node_modules/@diffci.com/core/src/repo/adapters/types.ts +2 -0
- package/node_modules/@diffci.com/core/src/repo/file-references.ts +43 -0
- package/node_modules/@diffci.com/core/src/repo/graph.ts +154 -20
- package/node_modules/@diffci.com/core/src/repo/impact.ts +34 -9
- package/node_modules/@diffci.com/core/src/repo/types.ts +5 -0
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -17,6 +17,29 @@ The `diffci-mcp` binary exposes the same local analysis through MCP. Install
|
|
|
17
17
|
the public Codex, Claude Code, Cursor, Grok, or Meta Muse Code integration from
|
|
18
18
|
the [cross-agent installation guide](https://github.com/DiffCI/core/blob/main/docs/agent-installation.md).
|
|
19
19
|
|
|
20
|
+
## Compare a selection with full-suite results
|
|
21
|
+
|
|
22
|
+
```sh
|
|
23
|
+
diffci plan --base "$BASE" --head "$HEAD" > plan.json
|
|
24
|
+
# run the full suite with a JUnit reporter
|
|
25
|
+
diffci check-plan --plan plan.json --results junit.xml
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
`diffci plan` saves Core's advisory plan for a clean checkout at `--head`.
|
|
29
|
+
`check-plan` compares a saved Core execution plan with a full-suite JUnit
|
|
30
|
+
report from the same revision. Without `--base-results base.xml`, skipped failures
|
|
31
|
+
are `possibleMisses`. With a base report, unique matching failures are
|
|
32
|
+
`preExisting`; a skipped failure is `missed` only if its matching base testcase
|
|
33
|
+
passed. Absent, skipped or ambiguous base identities remain `possibleMisses`. Selected and unattributed failures are also reported. Missed, possible
|
|
34
|
+
misses or unattributed failures exit 1; invalid inputs exit 2. Use `--root packages/web`
|
|
35
|
+
for a nested reporter root and `--repo /checkout` for absolute paths. Use
|
|
36
|
+
`--base-repo /base/checkout` if the base report came from another checkout. Exit 0
|
|
37
|
+
describes the comparison, not whether the full suite passed. The command
|
|
38
|
+
runs no tests, writes no files and sends nothing.
|
|
39
|
+
|
|
40
|
+
See the [reporter configuration and scheduled-job example](https://github.com/DiffCI/core/blob/main/docs/using-diffci.md#compare-a-plan-with-a-full-suite-junit-report)
|
|
41
|
+
for Vitest and Jest. Use a CLI build containing this command.
|
|
42
|
+
|
|
20
43
|
## Source and license
|
|
21
44
|
|
|
22
45
|
This package is MIT-licensed. Its current source lives in
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export interface JunitFailure {
|
|
2
|
+
name: string;
|
|
3
|
+
classname?: string;
|
|
4
|
+
suite?: string;
|
|
5
|
+
file?: string;
|
|
6
|
+
suiteFiles: readonly string[];
|
|
7
|
+
suiteNames: readonly string[];
|
|
8
|
+
suiteLevel?: boolean;
|
|
9
|
+
}
|
|
10
|
+
export interface JunitFailures {
|
|
11
|
+
testCases: number;
|
|
12
|
+
failures: JunitFailure[];
|
|
13
|
+
}
|
|
14
|
+
/** Read only structural failure evidence; logs, comments and CDATA are never tags. */
|
|
15
|
+
export declare function readJunit(xml: string, onCase?: (test: JunitFailure, failed: boolean, skipped: boolean) => void): JunitFailures;
|
|
16
|
+
/** Lexical mapping only: never guess the checkout root from a suffix or access the filesystem. */
|
|
17
|
+
export declare function normalizeTestPath(value: string, repoRoot: string, root?: string): string;
|
|
18
|
+
export type JunitAttribution = {
|
|
19
|
+
file: string;
|
|
20
|
+
} | {
|
|
21
|
+
reason: string;
|
|
22
|
+
candidates?: string[];
|
|
23
|
+
};
|
|
24
|
+
export declare function attributeJunitFile(test: JunitFailure, repoRoot: string, root?: string): JunitAttribution;
|
|
25
|
+
export declare function junitFile(test: JunitFailure, repoRoot: string, root?: string): string;
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
import { posix, win32 } from "node:path";
|
|
3
|
+
import { SaxesParser } from "saxes";
|
|
4
|
+
const MAX_BYTES = 16 * 1024 * 1024;
|
|
5
|
+
const MAX_DEPTH = 128;
|
|
6
|
+
const MAX_TAGS = 100_000;
|
|
7
|
+
const IGNORED_ATTRIBUTE_URIS = new Set([
|
|
8
|
+
"http://www.w3.org/2000/xmlns/",
|
|
9
|
+
"http://www.w3.org/2001/XMLSchema-instance",
|
|
10
|
+
"http://www.w3.org/XML/1998/namespace",
|
|
11
|
+
]);
|
|
12
|
+
function invalid(message) {
|
|
13
|
+
throw new Error(`Invalid JUnit report: ${message}`);
|
|
14
|
+
}
|
|
15
|
+
function declaredCount(value, name) {
|
|
16
|
+
if (value === undefined)
|
|
17
|
+
return undefined;
|
|
18
|
+
const count = Number(value);
|
|
19
|
+
if (!/^\d+$/.test(value.trim()) || !Number.isSafeInteger(count)) {
|
|
20
|
+
invalid(`${name} must be a nonnegative integer.`);
|
|
21
|
+
}
|
|
22
|
+
return count;
|
|
23
|
+
}
|
|
24
|
+
/** Read only structural failure evidence; logs, comments and CDATA are never tags. */
|
|
25
|
+
export function readJunit(xml, onCase) {
|
|
26
|
+
if (Buffer.byteLength(xml, "utf8") > MAX_BYTES) {
|
|
27
|
+
invalid("XML exceeds the 16 MiB limit.");
|
|
28
|
+
}
|
|
29
|
+
const result = { testCases: 0, failures: [] };
|
|
30
|
+
const frames = [];
|
|
31
|
+
const suites = [];
|
|
32
|
+
let tags = 0;
|
|
33
|
+
let metadataBytes = 0;
|
|
34
|
+
const parser = new SaxesParser({ xmlns: true });
|
|
35
|
+
function emit(failure, failed, skipped) {
|
|
36
|
+
// Passing cases are transient callback data, not retained failure output.
|
|
37
|
+
if (failed) {
|
|
38
|
+
for (const value of [failure.name, failure.classname, failure.file, failure.suite,
|
|
39
|
+
...failure.suiteFiles, ...failure.suiteNames]) {
|
|
40
|
+
metadataBytes += value === undefined ? 0 : Buffer.byteLength(value, "utf8");
|
|
41
|
+
if (metadataBytes > MAX_BYTES)
|
|
42
|
+
invalid("Failure metadata exceeds the 16 MiB limit.");
|
|
43
|
+
}
|
|
44
|
+
result.failures.push(failure);
|
|
45
|
+
}
|
|
46
|
+
onCase?.(failure, failed, skipped);
|
|
47
|
+
}
|
|
48
|
+
function inherited() {
|
|
49
|
+
return suites[suites.length - 1]?.metadata ?? { suiteFiles: [], suiteNames: [] };
|
|
50
|
+
}
|
|
51
|
+
// Keep the parser's default throwing error handler: partial XML is not evidence.
|
|
52
|
+
parser.on("doctype", () => invalid("DOCTYPE declarations are not supported."));
|
|
53
|
+
parser.on("xmldecl", (declaration) => {
|
|
54
|
+
if (declaration.version !== "1.0")
|
|
55
|
+
invalid("Only XML 1.0 is supported.");
|
|
56
|
+
if (declaration.encoding && declaration.encoding.toUpperCase() !== "UTF-8") {
|
|
57
|
+
invalid("Only UTF-8 XML is supported.");
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
parser.on("opentagstart", () => {
|
|
61
|
+
if (++tags > MAX_TAGS)
|
|
62
|
+
invalid("XML exceeds the 100000 tag limit.");
|
|
63
|
+
if (frames.length >= MAX_DEPTH)
|
|
64
|
+
invalid("XML exceeds the 128 level depth limit.");
|
|
65
|
+
});
|
|
66
|
+
parser.on("opentag", (tag) => {
|
|
67
|
+
const name = tag.name;
|
|
68
|
+
const parent = frames[frames.length - 1];
|
|
69
|
+
if (tag.uri !== "")
|
|
70
|
+
invalid("Namespaced JUnit elements are not supported.");
|
|
71
|
+
// Namespace declarations and schema hints carry no testcase identity. Unknown qualified
|
|
72
|
+
// metadata is still refused so x:file/x:failures cannot silently disappear.
|
|
73
|
+
if (Object.values(tag.attributes).some((attribute) => attribute.uri !== "" && !IGNORED_ATTRIBUTE_URIS.has(attribute.uri))) {
|
|
74
|
+
invalid("Unsupported namespaced JUnit attribute.");
|
|
75
|
+
}
|
|
76
|
+
const attributes = Object.fromEntries(Object.entries(tag.attributes).filter(([, attribute]) => attribute.uri === "").map(([key, attribute]) => [key, attribute.value]));
|
|
77
|
+
const frame = { name };
|
|
78
|
+
if (!parent && name !== "testsuite" && name !== "testsuites") {
|
|
79
|
+
invalid("The root must be testsuite or testsuites.");
|
|
80
|
+
}
|
|
81
|
+
if (name === "testsuite" || name === "testsuites") {
|
|
82
|
+
if (parent && parent.name !== "testsuite" && parent.name !== "testsuites") {
|
|
83
|
+
invalid("A test suite must be inside a suite container.");
|
|
84
|
+
}
|
|
85
|
+
const outer = inherited();
|
|
86
|
+
const suite = {
|
|
87
|
+
metadata: {
|
|
88
|
+
suite: name === "testsuite" ? attributes.name : outer.suite,
|
|
89
|
+
suiteFiles: attributes.file === undefined ? outer.suiteFiles : [...outer.suiteFiles, attributes.file],
|
|
90
|
+
suiteNames: name !== "testsuite" || attributes.name === undefined ? outer.suiteNames : [...outer.suiteNames, attributes.name],
|
|
91
|
+
},
|
|
92
|
+
element: name,
|
|
93
|
+
name: attributes.name,
|
|
94
|
+
file: attributes.file,
|
|
95
|
+
declaredTests: declaredCount(attributes.tests, "tests"),
|
|
96
|
+
declaredFailures: declaredCount(attributes.failures, "failures"),
|
|
97
|
+
declaredErrors: declaredCount(attributes.errors, "errors"),
|
|
98
|
+
testCases: 0,
|
|
99
|
+
failures: 0,
|
|
100
|
+
errors: 0,
|
|
101
|
+
};
|
|
102
|
+
frame.suite = suite;
|
|
103
|
+
suites.push(suite);
|
|
104
|
+
}
|
|
105
|
+
else if (name === "testcase") {
|
|
106
|
+
if (parent?.name !== "testsuite" && parent?.name !== "testsuites")
|
|
107
|
+
invalid("A testcase must be inside a testsuite.");
|
|
108
|
+
frame.testCase = {
|
|
109
|
+
skipped: false,
|
|
110
|
+
failed: attributes.failure !== undefined,
|
|
111
|
+
errored: false,
|
|
112
|
+
failure: {
|
|
113
|
+
name: attributes.name ?? "",
|
|
114
|
+
classname: attributes.classname,
|
|
115
|
+
file: attributes.file,
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
result.testCases += 1;
|
|
119
|
+
}
|
|
120
|
+
else if (name === "failure" || name === "error") {
|
|
121
|
+
if (parent?.testCase) {
|
|
122
|
+
if (name === "failure")
|
|
123
|
+
parent.testCase.failed = true;
|
|
124
|
+
else
|
|
125
|
+
parent.testCase.errored = true;
|
|
126
|
+
}
|
|
127
|
+
else if (parent?.suite) {
|
|
128
|
+
emit({ name: attributes.message ?? `(suite ${name})`, suiteLevel: true, ...inherited() }, true, false);
|
|
129
|
+
for (const suite of suites) {
|
|
130
|
+
if (name === "failure")
|
|
131
|
+
suite.failures += 1;
|
|
132
|
+
else
|
|
133
|
+
suite.errors += 1;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
else
|
|
137
|
+
invalid("Failure evidence must be a direct testcase or suite child.");
|
|
138
|
+
}
|
|
139
|
+
else if (name === "skipped" && parent?.testCase) {
|
|
140
|
+
parent.testCase.skipped = true;
|
|
141
|
+
}
|
|
142
|
+
frames.push(frame);
|
|
143
|
+
});
|
|
144
|
+
parser.on("closetag", () => {
|
|
145
|
+
const frame = frames.pop();
|
|
146
|
+
if (frame.testCase) {
|
|
147
|
+
const testCase = frame.testCase;
|
|
148
|
+
if (testCase.failed || testCase.errored || onCase) {
|
|
149
|
+
emit({ ...testCase.failure, ...inherited() }, testCase.failed || testCase.errored, testCase.skipped);
|
|
150
|
+
}
|
|
151
|
+
for (const suite of suites) {
|
|
152
|
+
suite.testCases += 1;
|
|
153
|
+
if (testCase.failed)
|
|
154
|
+
suite.failures += 1;
|
|
155
|
+
if (testCase.errored)
|
|
156
|
+
suite.errors += 1;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
if (frame.suite) {
|
|
160
|
+
const suite = frame.suite;
|
|
161
|
+
// jest-junit can emit extra load-error cases without increasing its headers.
|
|
162
|
+
// A higher header, however, means testcase evidence is missing from the report.
|
|
163
|
+
if ((suite.declaredTests ?? 0) > suite.testCases ||
|
|
164
|
+
(suite.declaredFailures ?? 0) > suite.failures ||
|
|
165
|
+
(suite.declaredErrors ?? 0) > suite.errors) {
|
|
166
|
+
invalid("Declared tests, failures or errors exceed the observed testcase evidence.");
|
|
167
|
+
}
|
|
168
|
+
suites.pop();
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
parser.write(xml).close();
|
|
172
|
+
if (result.testCases === 0 && result.failures.length === 0)
|
|
173
|
+
invalid("The report contains no testcases.");
|
|
174
|
+
return result;
|
|
175
|
+
}
|
|
176
|
+
/** Lexical mapping only: never guess the checkout root from a suffix or access the filesystem. */
|
|
177
|
+
export function normalizeTestPath(value, repoRoot, root = ".") {
|
|
178
|
+
const slashed = value.replace(/\\/g, "/");
|
|
179
|
+
if (!slashed || /[\x00-\x1f\x7f]/.test(slashed) || slashed.endsWith("/") || slashed.endsWith("/.")
|
|
180
|
+
|| slashed.split("/").includes(".."))
|
|
181
|
+
return "";
|
|
182
|
+
const windows = /^[a-z]:\//i.test(slashed) || slashed.startsWith("//");
|
|
183
|
+
const path = windows ? win32 : posix;
|
|
184
|
+
if (path.isAbsolute(slashed)) {
|
|
185
|
+
const checkout = repoRoot.replace(/\\/g, "/");
|
|
186
|
+
if (!path.isAbsolute(checkout) || (windows && !win32.isAbsolute(repoRoot)))
|
|
187
|
+
return slashed;
|
|
188
|
+
const inside = path.relative(checkout, slashed).replace(/\\/g, "/");
|
|
189
|
+
return inside && inside !== ".." && !inside.startsWith("../") && !path.isAbsolute(inside) ? inside : slashed;
|
|
190
|
+
}
|
|
191
|
+
if (/^[a-z][a-z\d+.-]*:/i.test(slashed))
|
|
192
|
+
return "";
|
|
193
|
+
return posix.normalize(posix.join(root, slashed));
|
|
194
|
+
}
|
|
195
|
+
export function attributeJunitFile(test, repoRoot, root = ".") {
|
|
196
|
+
if (test.suiteLevel)
|
|
197
|
+
return { reason: "Suite-level failure has no testcase file identity" };
|
|
198
|
+
const explicit = test.file !== undefined ? [test.file] : test.suiteFiles;
|
|
199
|
+
const values = explicit.length ? explicit : [test.classname, ...test.suiteNames]
|
|
200
|
+
.filter((value) => value !== undefined && /\.[cm]?[jt]sx?$/.test(value));
|
|
201
|
+
if (!values.length)
|
|
202
|
+
return { reason: "No file attribute or recognizable JavaScript file metadata" };
|
|
203
|
+
const paths = values.map(value => normalizeTestPath(value, repoRoot, root));
|
|
204
|
+
const candidates = [...new Set(paths.filter(Boolean))].sort();
|
|
205
|
+
if (paths.some(path => !path || posix.isAbsolute(path) || win32.isAbsolute(path))) {
|
|
206
|
+
return { reason: "File path is invalid or outside the checkout; check --repo/--base-repo", candidates };
|
|
207
|
+
}
|
|
208
|
+
if (candidates.length !== 1)
|
|
209
|
+
return { reason: "File metadata identifies multiple files", candidates };
|
|
210
|
+
return { file: candidates[0] };
|
|
211
|
+
}
|
|
212
|
+
export function junitFile(test, repoRoot, root = ".") {
|
|
213
|
+
const attribution = attributeJunitFile(test, repoRoot, root);
|
|
214
|
+
return "file" in attribution ? attribution.file : "";
|
|
215
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export interface PlanCheckFailure {
|
|
2
|
+
name: string;
|
|
3
|
+
classname?: string;
|
|
4
|
+
suite?: string;
|
|
5
|
+
reportedFile?: string;
|
|
6
|
+
file?: string;
|
|
7
|
+
}
|
|
8
|
+
export interface UnattributedPlanFailure extends PlanCheckFailure {
|
|
9
|
+
reason: string;
|
|
10
|
+
candidates?: string[];
|
|
11
|
+
}
|
|
12
|
+
export interface PlanCheckReport {
|
|
13
|
+
schemaVersion: 1;
|
|
14
|
+
mode: "SELECTIVE" | "FULL";
|
|
15
|
+
fullFallback: boolean;
|
|
16
|
+
baselineProvided: boolean;
|
|
17
|
+
summary: {
|
|
18
|
+
failedTests: number;
|
|
19
|
+
missed: number;
|
|
20
|
+
possibleMisses: number;
|
|
21
|
+
preExisting: number;
|
|
22
|
+
selected: number;
|
|
23
|
+
unattributed: number;
|
|
24
|
+
};
|
|
25
|
+
possibleMisses: PlanCheckFailure[];
|
|
26
|
+
preExisting: PlanCheckFailure[];
|
|
27
|
+
missed: PlanCheckFailure[];
|
|
28
|
+
selected: PlanCheckFailure[];
|
|
29
|
+
unattributed: UnattributedPlanFailure[];
|
|
30
|
+
}
|
|
31
|
+
export interface PlanCheckOptions {
|
|
32
|
+
/** JUnit results from the plan's base revision, produced with the same reporter configuration. */
|
|
33
|
+
baseResults?: string;
|
|
34
|
+
/** Absolute checkout root that produced the base report. Defaults to repoRoot. */
|
|
35
|
+
baseRepoRoot?: string;
|
|
36
|
+
/** Absolute checkout root for absolute reporter paths. Defaults to the current directory. */
|
|
37
|
+
repoRoot?: string;
|
|
38
|
+
/** Reporter root, relative to the repository root. */
|
|
39
|
+
root?: string;
|
|
40
|
+
}
|
|
41
|
+
/** This checks selection against supplied results, not suite success or report/revision provenance. */
|
|
42
|
+
export declare function checkPlan(planInput: unknown, junitXml: string, options?: PlanCheckOptions): PlanCheckReport;
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
/** Compare one advisory selection with failures in the supplied full-suite JUnit report. */
|
|
3
|
+
import { createHash } from "node:crypto";
|
|
4
|
+
import { posix } from "node:path";
|
|
5
|
+
import { readJunit, junitFile, attributeJunitFile, normalizeTestPath } from "./check-plan-junit.js";
|
|
6
|
+
function record(value) {
|
|
7
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
8
|
+
}
|
|
9
|
+
function planPaths(value, field) {
|
|
10
|
+
if (!Array.isArray(value))
|
|
11
|
+
throw new Error(`Plan ${field} must be an array of repository-relative file paths`);
|
|
12
|
+
const paths = new Set();
|
|
13
|
+
for (const path of value) {
|
|
14
|
+
if (typeof path !== "string" || path.length === 0 || /[\\\x00-\x1f\x7f]/.test(path)
|
|
15
|
+
|| path.startsWith("/") || /^[a-z][a-z\d+.-]*:/i.test(path)
|
|
16
|
+
|| path.split("/").some((part) => part === "" || part === "." || part === "..")) {
|
|
17
|
+
throw new Error(`Plan ${field} contains a noncanonical repository-relative path`);
|
|
18
|
+
}
|
|
19
|
+
if (paths.has(path))
|
|
20
|
+
throw new Error(`Plan ${field} contains the duplicate path ${JSON.stringify(path)}`);
|
|
21
|
+
paths.add(path);
|
|
22
|
+
}
|
|
23
|
+
return paths;
|
|
24
|
+
}
|
|
25
|
+
function readSelection(input) {
|
|
26
|
+
if (!record(input))
|
|
27
|
+
throw new Error("Expected an ExecutionPlan or the JSON output of Core's plan command");
|
|
28
|
+
let plan = input;
|
|
29
|
+
if (Object.hasOwn(input, "plan")) {
|
|
30
|
+
if (input.advisoryOnly !== true || typeof input.base !== "string" || !input.base
|
|
31
|
+
|| typeof input.head !== "string" || !input.head || !record(input.plan)
|
|
32
|
+
|| ["mode", "selectedTests", "skippedTests"].some((key) => Object.hasOwn(input, key))) {
|
|
33
|
+
throw new Error("Expected an unambiguous Core plan output with advisoryOnly, base, head and plan");
|
|
34
|
+
}
|
|
35
|
+
plan = input.plan;
|
|
36
|
+
}
|
|
37
|
+
if (plan.version !== "5.0.0")
|
|
38
|
+
throw new Error("Unsupported plan version; expected ExecutionPlan version 5.0.0");
|
|
39
|
+
if (plan.mode !== "SELECTIVE" && plan.mode !== "FULL")
|
|
40
|
+
throw new Error("Plan mode must be SELECTIVE or FULL");
|
|
41
|
+
if (!record(plan.safety) || typeof plan.safety.fallbackRequired !== "boolean"
|
|
42
|
+
|| plan.safety.fallbackRequired !== (plan.mode === "FULL")) {
|
|
43
|
+
throw new Error("Plan safety.fallbackRequired must agree with its mode");
|
|
44
|
+
}
|
|
45
|
+
const selected = planPaths(plan.selectedTests, "selectedTests");
|
|
46
|
+
const skipped = planPaths(plan.skippedTests, "skippedTests");
|
|
47
|
+
for (const path of selected) {
|
|
48
|
+
if (skipped.has(path))
|
|
49
|
+
throw new Error(`Plan selects and skips the same file: ${JSON.stringify(path)}`);
|
|
50
|
+
}
|
|
51
|
+
if (plan.mode === "FULL" && skipped.size > 0)
|
|
52
|
+
throw new Error("A FULL plan cannot contain skipped tests");
|
|
53
|
+
return { mode: plan.mode, selected, skipped };
|
|
54
|
+
}
|
|
55
|
+
function identity(test, file, repoRoot, root) {
|
|
56
|
+
const label = (value) => /\.[cm]?[jt]sx?$/.test(value) ? normalizeTestPath(value, repoRoot, root) || value : value;
|
|
57
|
+
// Keep stored keys bounded even when many passing cases inherit long describe names.
|
|
58
|
+
return createHash("sha256").update(JSON.stringify([file, test.suiteNames.map(label), label(test.classname ?? ""), test.name])).digest("hex");
|
|
59
|
+
}
|
|
60
|
+
function reporterRoot(value = ".") {
|
|
61
|
+
const path = value.replace(/\\/g, "/");
|
|
62
|
+
if (!path || /[\x00-\x1f\x7f]/.test(path) || path.startsWith("/")
|
|
63
|
+
|| /^[a-z][a-z\d+.-]*:/i.test(path) || path.split("/").includes("..")) {
|
|
64
|
+
throw new Error("Invalid --root: expected a directory inside the repository, without parent traversal");
|
|
65
|
+
}
|
|
66
|
+
return posix.normalize(path).replace(/\/$/, "") || ".";
|
|
67
|
+
}
|
|
68
|
+
/** This checks selection against supplied results, not suite success or report/revision provenance. */
|
|
69
|
+
export function checkPlan(planInput, junitXml, options = {}) {
|
|
70
|
+
const root = reporterRoot(options.root);
|
|
71
|
+
const repoRoot = options.repoRoot ?? process.cwd();
|
|
72
|
+
const baseRepoRoot = options.baseRepoRoot ?? repoRoot;
|
|
73
|
+
const plan = readSelection(planInput);
|
|
74
|
+
const known = new Set([...plan.selected, ...plan.skipped]);
|
|
75
|
+
const headCounts = new Map();
|
|
76
|
+
const baseCounts = new Map();
|
|
77
|
+
const baseline = new Set();
|
|
78
|
+
const basePassed = new Set();
|
|
79
|
+
function count(test, counts, checkout) {
|
|
80
|
+
const file = junitFile(test, checkout, root);
|
|
81
|
+
// Missing names and ambiguous file identities cannot establish an old failure.
|
|
82
|
+
if (!known.has(file) || !test.name)
|
|
83
|
+
return undefined;
|
|
84
|
+
const key = identity(test, file, checkout, root);
|
|
85
|
+
counts.set(key, (counts.get(key) ?? 0) + 1);
|
|
86
|
+
return key;
|
|
87
|
+
}
|
|
88
|
+
const results = readJunit(junitXml, options.baseResults === undefined ? undefined
|
|
89
|
+
: (test) => { count(test, headCounts, repoRoot); });
|
|
90
|
+
if (options.baseResults !== undefined) {
|
|
91
|
+
readJunit(options.baseResults, (test, failed, skipped) => {
|
|
92
|
+
const key = count(test, baseCounts, baseRepoRoot);
|
|
93
|
+
if (key !== undefined && failed)
|
|
94
|
+
baseline.add(key);
|
|
95
|
+
if (key !== undefined && !failed && !skipped)
|
|
96
|
+
basePassed.add(key);
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
const missed = [];
|
|
100
|
+
const possibleMisses = [];
|
|
101
|
+
const preExisting = [];
|
|
102
|
+
const selected = [];
|
|
103
|
+
const unattributed = [];
|
|
104
|
+
for (const failure of results.failures) {
|
|
105
|
+
const details = {
|
|
106
|
+
name: failure.name,
|
|
107
|
+
...(failure.classname === undefined ? {} : { classname: failure.classname }),
|
|
108
|
+
...(failure.suite === undefined ? {} : { suite: failure.suite }),
|
|
109
|
+
...(failure.file === undefined ? {} : { reportedFile: failure.file }),
|
|
110
|
+
};
|
|
111
|
+
const attribution = attributeJunitFile(failure, repoRoot, root);
|
|
112
|
+
if (!("file" in attribution)) {
|
|
113
|
+
unattributed.push({ ...details, ...attribution });
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
const file = attribution.file;
|
|
117
|
+
if (!known.has(file)) {
|
|
118
|
+
unattributed.push({ ...details, reason: "File is absent from the supplied plan; check --root and report scope", candidates: [file] });
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
const key = identity(failure, file, repoRoot, root);
|
|
122
|
+
if (baseline.has(key) && baseCounts.get(key) === 1 && headCounts.get(key) === 1) {
|
|
123
|
+
preExisting.push({ ...details, file });
|
|
124
|
+
}
|
|
125
|
+
else if (plan.mode === "SELECTIVE" && plan.skipped.has(file)) {
|
|
126
|
+
// Repeated or unnamed identities cannot tell whether the failing occurrence changed.
|
|
127
|
+
const uncertain = !basePassed.has(key) || (headCounts.get(key) ?? 0) > 1
|
|
128
|
+
|| (baseCounts.get(key) ?? 0) > 1
|
|
129
|
+
|| !failure.name;
|
|
130
|
+
(uncertain ? possibleMisses : missed).push({ ...details, file });
|
|
131
|
+
}
|
|
132
|
+
else
|
|
133
|
+
selected.push({ ...details, file });
|
|
134
|
+
}
|
|
135
|
+
return {
|
|
136
|
+
schemaVersion: 1,
|
|
137
|
+
mode: plan.mode,
|
|
138
|
+
fullFallback: plan.mode === "FULL",
|
|
139
|
+
baselineProvided: options.baseResults !== undefined,
|
|
140
|
+
summary: { failedTests: results.failures.length, missed: missed.length, possibleMisses: possibleMisses.length, preExisting: preExisting.length, selected: selected.length, unattributed: unattributed.length },
|
|
141
|
+
missed, possibleMisses, preExisting, selected, unattributed,
|
|
142
|
+
};
|
|
143
|
+
}
|