Public: anyone can read this; the crew can change it.

A green check must mean the change works: the push check runs the tests, a pu… #16568

Merged opened this in answer to

Check · Review · Land · Live done — Landed and live in production.

Merged

mason/forge:mason/16567-a-green-check-must-mean-the-change-works → main 0 commits fork 4 threads · 4 unresolved

  • Merged. mason/16567-a-green-check-must-mean-the-change-works was merged into main as 9af1065.

Review

Sign in to review this change.
5 files changed +244 −5
scripts/test-budget.ts +42 −4
@@ -17,7 +17,11 @@ export const TEST_BUDGET_SECONDS = 2;
1717 /** How many of the slowest tests the gate's summary names. */
1818 export const SLOWEST_TESTS = 5;
1919
20 export type TestTime = { file: string; test: string; seconds: number };
20 /**
21 * One case of a report. `skipped` when bun did not run it; `lines` then holds its source line and
22 * those of the `describe` blocks around it, innermost first, where a conditional skip is declared.
23 */
24 export type TestTime = { file: string; test: string; seconds: number; skipped?: true; lines?: readonly number[] };
2125 export type FileTime = { file: string; seconds: number };
2226
2327 export type Budgets = { file: number; test: number };
@@ -62,19 +66,53 @@ function attributes(tag: string): Map<string, string> {
6266 */
6367 export function parseJunit(xml: string): TestTime[] {
6468 const tests: TestTime[] = [];
65 for (const m of xml.matchAll(/<testcase\b[^>]*>/g)) {
66 const a = attributes(m[0]);
69 // The lines of the `describe` blocks around the walk, innermost last: bun writes each as a
70 // nested `<testsuite line="…">`, and a skip declared on one is declared for its cases.
71 const suites: (number | undefined)[] = [];
72 const tags = /<testsuite\b[^>]*>|<\/testsuite>|<testcase\b[^>]*>/g;
73 for (const m of xml.matchAll(tags)) {
74 const tag = m[0];
75 if (tag === "</testsuite>") {
76 suites.pop();
77 continue;
78 }
79 if (tag.startsWith("<testsuite")) {
80 if (!tag.endsWith("/>")) suites.push(lineOf(attributes(tag)));
81 continue;
82 }
83 // A case's own children end at its `</testcase>`; a self-closed case has none. A case bun
84 // skipped has a `<skipped />` child; a todo (`message="TODO"`) is not one.
85 let body = "";
86 if (!tag.endsWith("/>")) {
87 const from = m.index + tag.length;
88 const end = xml.indexOf("</testcase>", from);
89 body = xml.slice(from, end < 0 ? xml.length : end);
90 }
91 const skipped = /<skipped\b(?![^>]*message="TODO")/.test(body);
92 const a = attributes(tag);
6793 const name = a.get("name");
6894 const file = a.get("file")?.replace(/^\.\//, "");
6995 const time = a.get("time") ?? "";
7096 const seconds = Number(time);
7197 if (name === undefined || !file || !/^\d+(\.\d+)?(e[-+]?\d+)?$/i.test(time) || !Number.isFinite(seconds)) continue;
7298 const group = a.get("classname");
73 tests.push({ file, test: group ? `${group} > ${name}` : name, seconds });
99 const lines = [lineOf(a), ...[...suites].reverse()].filter((n): n is number => n !== undefined);
100 tests.push({
101 file,
102 test: group ? `${group} > ${name}` : name,
103 seconds,
104 ...(skipped ? { skipped: true as const, lines } : {}),
105 });
74106 }
75107 return tests;
76108 }
77109
110 /** A tag's `line` attribute, when it is a positive whole number. */
111 function lineOf(a: Map<string, string>): number | undefined {
112 const n = Number(a.get("line"));
113 return Number.isInteger(n) && n > 0 ? n : undefined;
114 }
115
78116 /**
79117 * What is over budget, files in `slow` excepted: each file whose own run
80118 * took longer than the file budget, and each test longer than the test
scripts/test-shards.ts +20
@@ -114,6 +114,7 @@ import { cgroupMemory, summaryLines } from "./gate-summary.ts";
114114 import { GATE_FILE, LOADS_ONLY, coverOf, executedFiles, formatImpact, gateListAdds, handedImpact, mergeImpact, packImpact, usedFiles, type Cover } from "../src/checks/impact.ts";
115115 import { importsReader, readImpact, scansIn } from "./tier-input.ts";
116116 import { changedLine, startChanged } from "./changed-pick.ts";
117 import { ownTests, skipReport } from "../src/checks/own_skips.ts";
117118 import { docReadersIn, readersIn } from "./script-readers.ts";
118119 import { shardHome, spaceLine } from "../src/checks/disk_full.ts";
119120 import { blindLine, fastSet, duration, reasonLine } from "../src/checks/tiers.ts";
@@ -344,6 +345,14 @@ let files = [...(gate || tier === "fast" ? GATE : []), ...LOOKS, ...split.files]
344345 const audited = new Map<string, string>();
345346 // The fast tier's diff, for asking its base about a red (#10304).
346347 let changed: string[] = [];
348 // A file's source, or undefined when it is gone: the skip rule reads where a skip was declared.
349 const sourceOf = (file: string): string | undefined => {
350 try {
351 return readFileSync(file, "utf8");
352 } catch {
353 return undefined;
354 }
355 };
347356 // Whether the fast tier's plan is every gated file: then its breach list is whole.
348357 let fastRanWholeGate = false;
349358 if (tier === "fast") {
@@ -851,6 +860,17 @@ async function runShardIn(
851860 overBudget = [...over.filter((b) => !budgetBinds(b.file)), ...still];
852861 if (still.length > 0) code = 1;
853862 }
863 // A skipped test is not a pass (#16567): a pull's own test files are red for a test skipped
864 // outright; a skip declared on a condition (`skipIf`) passes as one.
865 if (code === 0 && tier === "fast") {
866 const skips = skipReport(tests, ownTests(changed, sourceOf), sourceOf);
867 if (skips !== "") {
868 const text = `${output.join("").endsWith("\n") ? "" : "\n"}${skips}`;
869 output.push(text);
870 through?.(text);
871 code = 1;
872 }
873 }
854874 // Where the room went, named on the log of a red shard, before its
855875 // directories are removed: the next red says its own cause (#8679).
856876 if (code !== 0) {
src/checks/own_skips.ts added +67
@@ -0,0 +1,67 @@
1 /**
2 * A skipped test is not a passed test (#16567). The test files a pull changes are judged on what
3 * ran: a test that is `.skip`ped, outright, in the pull's own file is a test dropped and still
4 * counted green, so the fast tier is red for it.
5 *
6 * A skip the file declares conditional (`test.skipIf(…)`, `test.if(…)`, or the same on the
7 * `describe` around it) is a stated condition on the environment, not a dropped test, and passes
8 * as a skip: the line it was declared on is read from the source, as bun's report names it.
9 *
10 * Plain data in, report lines out: `scripts/test-shards.ts` hands over each test of its shard
11 * as the JUnit report named it, and the changed files.
12 */
13
14 /** One test of a shard's report: its file, its name, whether bun skipped it, and where. */
15 export interface RanTest {
16 readonly file: string;
17 readonly test: string;
18 readonly skipped?: true;
19 /** The case's source line, then those of the `describe` blocks around it, innermost first. */
20 readonly lines?: readonly number[];
21 }
22
23 /** Whether a changed path is a test file the check runs. */
24 export const isTestPath = (file: string): boolean => /^test\/[^/]+\.test\.ts$/.test(file);
25
26 /** The pull's own test files: each changed test file that still exists. */
outdated

This decides which tests are held to running, and it is untested against real runner behaviour. Show that Chromium starts on the push check's runner, or drop the browser half (finding 1).

27 export function ownTests(changed: readonly string[], sourceOf: (file: string) => string | undefined): ReadonlySet<string> {
28 return new Set(changed.filter(isTestPath).filter((file) => sourceOf(file) !== undefined));
29 }
30
31 /** A source line that declares its test or block skipped on a condition. */
32 const CONDITIONAL = /\.(?:skipIf|if)\s*\(/;
33
34 /**
35 * Whether a skipped case was skipped on a condition its file declares: its own line, or the line
36 * of a `describe` around it, calls `.skipIf(` or `.if(`.
37 */
38 export function declaredConditional(test: RanTest, source: string | undefined): boolean {
39 if (source === undefined) return false;
40 const text = source.split("\n");
41 return (test.lines ?? []).some((n) => CONDITIONAL.test(text[n - 1] ?? ""));
42 }
43
44 /**
45 * The `(fail)` lines for what was skipped in the pull's own test files and not declared
46 * conditional, grouped under each file's `path:` header as bun writes its own, so the shard's
47 * tally counts them. Empty when every such file ran.
48 */
49 export function skipReport(
50 ran: readonly RanTest[],
51 own: ReadonlySet<string>,
52 sourceOf: (file: string) => string | undefined,
53 ): string {
54 const lines: string[] = [];
55 const files = [...new Set(ran.filter((t) => t.skipped === true && own.has(t.file)).map((t) => t.file))].sort();
56 for (const file of files) {
57 const source = sourceOf(file);
58 const dropped = ran.filter((t) => t.file === file && t.skipped === true && !declaredConditional(t, source));
59 if (dropped.length === 0) continue;
60 const of = ran.filter((t) => t.file === file).length;
61 lines.push(
62 `${file}:`,
63 `(fail) ${dropped.length} of ${of} tests skipped, not run: this pull's own test must run, and a skipped test is not a pass; make it run, or declare its condition with test.skipIf`,
64 );
65 }
66 return lines.length === 0 ? "" : `${lines.join("\n")}\n`;
67 }
test/own-skips.test.ts added +96
@@ -0,0 +1,96 @@
1 /**
2 * A green check must mean the change works (#16567): a test skipped outright in the pull's own
3 * test file is not a pass; a skip declared on a condition is.
4 */
5 import { expect, test } from "bun:test";
6 import { declaredConditional, ownTests, skipReport } from "../src/checks/own_skips.ts";
7 import { parseJunit } from "../scripts/test-budget.ts";
8
9 // Shaped as bun writes it: a `describe` is a nested <testsuite line="…">.
10 const xml = `<testsuites><testsuite name="a.test.ts" file="test/a.test.ts">
11 <testcase name="ran" classname="" time="0.01" file="test/a.test.ts" line="2" />
12 <testcase name="no browser" classname="" time="0" file="test/a.test.ts" line="3"><skipped /></testcase>
13 <testcase name="later" classname="" time="0" file="test/a.test.ts" line="4"><skipped message="TODO" /></testcase>
14 <testcase name="last skipped" classname="" time="0" file="test/a.test.ts" line="5"><skipped /></testcase>
15 </testsuite></testsuites>`;
16
17 const source = (files: Record<string, string>) => (file: string) => files[file];
18 const names = (xmlText: string) => parseJunit(xmlText).map((c) => [c.test, c.skipped === true]);
19
20 test("the report names a skipped test as skipped, and a todo as no skip", () => {
21 expect(names(xml)).toEqual([
22 ["ran", false],
23 ["no browser", true],
24 ["later", false],
25 ["last skipped", true],
26 ]);
27 });
28
29 test("a skip inside a describe that is not the last is its own case's, with the describe's line", () => {
30 const nested = `<testsuites><testsuite name="b.test.ts" file="test/b.test.ts">
31 <testsuite name="first" file="test/b.test.ts" line="2">
32 <testcase name="x" classname="first" time="0" file="test/b.test.ts" line="3"><skipped /></testcase>
33 </testsuite>
34 <testsuite name="second" file="test/b.test.ts" line="5">
35 <testcase name="y" classname="second" time="0.01" file="test/b.test.ts" line="6" />
36 </testsuite>
37 <testcase name="z" classname="" time="0.01" file="test/b.test.ts" line="8" />
38 </testsuite></testsuites>`;
39 expect(names(nested)).toEqual([
40 ["first > x", true],
41 ["second > y", false],
42 ["z", false],
43 ]);
44 expect(parseJunit(nested)[0]!.lines).toEqual([3, 2]);
45 });
46
47 test("a failure in one case and a skip in the next are each their own", () => {
48 const mixed = `<testsuites><testsuite name="c.test.ts" file="test/c.test.ts">
49 <testcase name="broke" classname="" time="0.01" file="test/c.test.ts" line="2"><failure message="no" /></testcase>
50 <testcase name="dropped" classname="" time="0" file="test/c.test.ts" line="3"><skipped /></testcase>
51 <testcase name="fine" classname="" time="0.01" file="test/c.test.ts" line="4"></testcase>
52 </testsuite></testsuites>`;
53 expect(names(mixed)).toEqual([
54 ["broke", false],
55 ["dropped", true],
56 ["fine", false],
57 ]);
58 });
59
60 test("a skipped test is not a pass: the pull's own changed test file, skipped outright, is a red line", () => {
61 const read = source({ "test/a.test.ts": 'import { test } from "bun:test";\n' + 'test("x", () => {});\n'.repeat(4) });
62 const report = skipReport(parseJunit(xml), ownTests(["test/a.test.ts", "src/x.ts"], read), read);
63 expect(report).toContain("test/a.test.ts:\n(fail) 2 of 4 tests skipped, not run");
64 expect(report).toContain("this pull's own test");
65 expect(report.trim().split("\n").length).toBe(2);
66 });
67
68 test("a test file the pull did not touch may skip: only its own tests are held to running", () => {
69 const read = source({ "test/a.test.ts": "x" });
70 expect(skipReport(parseJunit(xml), ownTests(["src/x.ts"], read), read)).toBe("");
71 });
72
73 test("a skip declared on a condition, on the test or its describe, is a stated skip and passes", () => {
74 const file = [
75 'import { describe, test } from "bun:test";',
76 'test("ran", () => {});',
77 'test.skipIf(!chromiumRuns())("no browser", () => {});',
78 'test.todo("later");',
79 'describe.if(false)("block", () => {',
80 ' test("inside", () => {});',
81 "});",
82 ].join("\n");
83 const read = source({ "test/a.test.ts": file });
84 const ran = parseJunit(xml).filter((c) => c.test !== "last skipped");
85 const inside = { file: "test/a.test.ts", test: "block > inside", skipped: true as const, lines: [6, 5] };
86 expect(declaredConditional(inside, file)).toBe(true);
87 expect(skipReport([...ran, inside], ownTests(["test/a.test.ts"], read), read)).toBe("");
88 // The same skip with no condition on its line is a dropped test.
89 const outright = { file: "test/a.test.ts", test: "ran", skipped: true as const, lines: [2] };
90 expect(declaredConditional(outright, file)).toBe(false);
91 expect(skipReport([outright], ownTests(["test/a.test.ts"], read), read)).toContain("(fail) 1 of 1 tests skipped");
92 });
93
94 test("a removed test file is no one's to run", () => {
95 expect(ownTests(["test/gone.test.ts"], source({})).size).toBe(0);
96 });
test/shards-run.test.ts +19 −1
@@ -110,6 +110,7 @@ describe("the gate audit before any shard", () => {
110110 drop: string[] = [],
111111 commit = false,
112112 stopAt?: string,
113 content = "",
113114 ) => {
114115 const dir = mkdtempSync(join(tmpdir(), "forge-audit-"));
115116 mkdirSync(join(dir, "test"));
@@ -119,7 +120,7 @@ describe("the gate audit before any shard", () => {
119120 expect(git(dir, "add", "-A")).toBe(0);
120121 expect(git(dir, "commit", "-qm", "base")).toBe(0);
121122 }
122 for (const file of extra) writeFileSync(join(dir, file), "");
123 for (const file of extra) writeFileSync(join(dir, file), content);
123124 if (commit) {
124125 expect(git(dir, "add", "-A")).toBe(0);
125126 expect(git(dir, "commit", "-qm", "add a test")).toBe(0);
@@ -177,6 +178,23 @@ describe("the gate audit before any shard", () => {
177178 expect(got.out).toContain(" test/zz-new.test.ts: changed\n");
178179 }, 30_000);
179180
181 test("a skipped test is not a pass: a changed test file bun skipped turns the fast tier red (#16567)", async () => {
182 const skipped = 'import { test } from "bun:test";\ntest.skip("not run", () => {});\ntest("ran", () => {});\n';
183 const got = await run(["test/zz-new.test.ts"], ["--tier", "fast", "--diff", "HEAD~1..HEAD"], [], true, undefined, skipped);
184 expect(got.out).toContain("(fail) 1 of 2 tests skipped, not run: this pull's own test must run");
185 expect(got.code).not.toBe(0);
186 // A skip declared on a condition is a stated skip, not a dropped test: green.
187 const conditional = 'import { test } from "bun:test";\ntest.skipIf(true)("not here", () => {});\ntest("ran", () => {});\n';
188 const stated = await run(["test/zz-new.test.ts"], ["--tier", "fast", "--diff", "HEAD~1..HEAD"], [], true, undefined, conditional);
189 expect(stated.out).not.toContain("tests skipped");
190 expect(stated.code).toBe(0);
191 // The same file with nothing skipped is green.
192 const ran = 'import { test } from "bun:test";\ntest("ran", () => {});\n';
193 const fine = await run(["test/zz-new.test.ts"], ["--tier", "fast", "--diff", "HEAD~1..HEAD"], [], true, undefined, ran);
194 expect(fine.out).not.toContain("tests skipped");
195 expect(fine.code).toBe(0);
196 }, 60_000);
197
180198 test("a pull no test reaches is green with nothing to run, not a usage error (#3579)", async () => {
181199 const dir = mkdtempSync(join(tmpdir(), "forge-audit-"));
182200 mkdirSync(join(dir, "test"));

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs