Public: anyone can read this; the crew can change it.
A test file that fails in its shard is run once more alone; one that passes alone is named as 'fails only beside others' and does not fail the pull #2433
scout/forge:scout/2416-a-test-file-that-fails-in-its-shard-is-r → main 3 commits fork
5 files
changed +156
−6
scripts/shards.ts +50
| @@ -7,6 +7,8 @@ | ||
| 7 | 7 | * is disjoint and covering, and that one red shard makes the run red. |
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | import { resolve } from "node:path"; | |
| 11 | ||
| 10 | 12 | /** |
| 11 | 13 | * No more than this many `bun test` processes at once, whatever the host: |
| 12 | 14 | * a ceiling for a big shared machine, not the check cell's number. The cell |
| @@ -332,6 +334,8 @@ export interface ShardResult { | ||
| 332 | 334 | readonly files: readonly string[]; |
| 333 | 335 | readonly code: number; |
| 334 | 336 | readonly output: string; |
| 337 | /** Files that failed beside their shard-mates and passed run alone. */ | |
| 338 | readonly beside?: readonly string[]; | |
| 335 | 339 | } |
| 336 | 340 | |
| 337 | 341 | /** What the log ends with, and the exit code of the whole run. */ |
| @@ -343,6 +347,43 @@ export interface Tally { | ||
| 343 | 347 | const RAN = /^Ran (\d+) tests? across (\d+) files?\./m; |
| 344 | 348 | const FAIL = /^\(fail\) .*$/gm; |
| 345 | 349 | |
| 350 | /** The most files of one shard that are run again alone. */ | |
| 351 | export const RETRY_MAX = 3; | |
| 352 | ||
| 353 | const FILE_HEADER = /^(\S+\.test\.[cm]?[jt]sx?):$/; | |
| 354 | const SUMMARY = /^\s*\d+ pass$/; | |
| 355 | ||
| 356 | /** | |
| 357 | * Which of a shard's files failed, read from its `bun test` output: a | |
| 358 | * `(fail)` line, or an unhandled error between tests, under that file's | |
| 359 | * header. `sure` is false when some failure could not be given to one of | |
| 360 | * `files`, or when none was named: then nothing is run again and the | |
| 361 | * shard stays red. | |
| 362 | */ | |
| 363 | export function failedFiles( | |
| 364 | output: string, | |
| 365 | files: readonly string[], | |
| 366 | ): { failed: string[]; sure: boolean } { | |
| 367 | // bun prints each header relative to its cwd, which may climb with `..`. | |
| 368 | const norm = (path: string): string => resolve(path); | |
| 369 | const known = new Map(files.map((file) => [norm(file), file])); | |
| 370 | const failed = new Set<string>(); | |
| 371 | let sure = true; | |
| 372 | let current: string | undefined; | |
| 373 | for (const line of output.split("\n")) { | |
| 374 | if (SUMMARY.test(line)) break; | |
| 375 | const header = FILE_HEADER.exec(line); | |
| 376 | if (header) { | |
| 377 | current = known.get(norm(header[1]!)); | |
| 378 | continue; | |
| 379 | } | |
| 380 | if (!line.startsWith("(fail) ") && !line.startsWith("# Unhandled error")) continue; | |
| 381 | if (current !== undefined) failed.add(current); | |
| 382 | else sure = false; | |
| 383 | } | |
| 384 | return { failed: [...failed], sure: sure && failed.size > 0 }; | |
| 385 | } | |
| 386 | ||
| 346 | 387 | /** |
| 347 | 388 | * Reads every shard's `bun test` output into one ending. Green: one line, |
| 348 | 389 | * how many tests across how many files and how long. Red: every failing |
| @@ -354,12 +395,19 @@ export function tally(results: readonly ShardResult[], seconds: number): Tally { | ||
| 354 | 395 | let files = 0; |
| 355 | 396 | const failing: string[] = []; |
| 356 | 397 | const unnamed: string[] = []; |
| 398 | const besides: string[] = []; | |
| 357 | 399 | results.forEach((result, i) => { |
| 400 | for (const file of result.beside ?? []) | |
| 401 | besides.push( | |
| 402 | `fails only beside others: ${file} (shard-mates: ${result.files.filter((f) => f !== file).join(" ")})`, | |
| 403 | ); | |
| 358 | 404 | const ran = RAN.exec(result.output); |
| 359 | 405 | if (ran) { |
| 360 | 406 | tests += Number(ran[1]); |
| 361 | 407 | files += Number(ran[2]); |
| 362 | 408 | } |
| 409 | // A shard whose every failing file passed alone is green. | |
| 410 | if (result.code === 0) return; | |
| 363 | 411 | const named = result.output.match(FAIL) ?? []; |
| 364 | 412 | for (const line of new Set(named)) failing.push(`${line} [shard ${i + 1}]`); |
| 365 | 413 | if (result.code !== 0 && named.length === 0) |
| @@ -374,6 +422,7 @@ export function tally(results: readonly ShardResult[], seconds: number): Tally { | ||
| 374 | 422 | return { |
| 375 | 423 | code: 0, |
| 376 | 424 | lines: [ |
| 425 | ...besides, | |
| 377 | 426 | `Ran ${tests} tests across ${files} files in ${took} (${shards}).`, |
| 378 | 427 | ], |
| 379 | 428 | }; |
| @@ -383,6 +432,7 @@ export function tally(results: readonly ShardResult[], seconds: number): Tally { | ||
| 383 | 432 | `${red} of ${shards} failed:`, |
| 384 | 433 | ...failing, |
| 385 | 434 | ...unnamed, |
| 435 | ...besides, | |
| 386 | 436 | `Ran ${tests} tests across ${files} files in ${took}; ${failing.length} failed.`, |
| 387 | 437 | ], |
| 388 | 438 | }; |
scripts/test-shards.ts +33 −3
| @@ -23,7 +23,10 @@ | ||
| 23 | 23 | * finishes, so the log reads one shard at a time. Either way the tail |
| 24 | 24 | * names every failing test from every shard. The first red shard stops |
| 25 | 25 | * the rest (fail-fast) so a known fail does not burn the remaining wall |
| 26 | * clock. Exits non-zero if any shard does. | |
| 26 | * clock. A red shard of several files first runs each failing file | |
| 27 | * (at most 3, once) alone: if all pass alone the shard is green and the | |
| 28 | * tail says `fails only beside others: <file> (shard-mates: …)` (#2416). | |
| 29 | * Exits non-zero if any shard does. | |
| 27 | 30 | * |
| 28 | 31 | * The whole gate, unsliced (`--gate` on the full tier, as `check.run`), |
| 29 | 32 | * runs one file per `bun test`, heaviest first into whichever worker is |
| @@ -58,6 +61,8 @@ import { | ||
| 58 | 61 | splitFiles, |
| 59 | 62 | tally, |
| 60 | 63 | weigher, |
| 64 | failedFiles, | |
| 65 | RETRY_MAX, | |
| 61 | 66 | type ShardResult, |
| 62 | 67 | } from "./shards.ts"; |
| 63 | 68 | import { gateFiles, gateRefusal, presentTests } from "./gate.ts"; |
| @@ -437,9 +442,11 @@ async function runShard( | ||
| 437 | 442 | slice: string[], |
| 438 | 443 | index: number, |
| 439 | 444 | live: boolean, |
| 445 | /** Names a run that is not one of the slices: a file run again alone. */ | |
| 446 | label?: string, | |
| 440 | 447 | ): Promise<Timed> { |
| 441 | 448 | const started = performance.now(); |
| 442 | const where = `${index + 1}/${slices.length}`; | |
| 449 | const where = label ?? `${index + 1}/${slices.length}`; | |
| 443 | 450 | // Print before spawn so a mid-suite cgroup SIGKILL still leaves a breadcrumb. |
| 444 | 451 | // stderr: Actions often block-buffers stdout until exit; an OOM never flushes. |
| 445 | 452 | process.stderr.write(`test-shards: start ${where} ${slice.join(" ")}\n`); |
| @@ -509,6 +516,26 @@ async function runShard( | ||
| 509 | 516 | }; |
| 510 | 517 | } |
| 511 | 518 | |
| 519 | /** | |
| 520 | * A red shard of several files: each file that failed is run once more as | |
| 521 | * its own `bun test`. When every one of them (at most {@link RETRY_MAX}) | |
| 522 | * passes alone, the shard is green and names them as failing only beside | |
| 523 | * others; otherwise it stays as it was. Never for a shard of one file, the | |
| 524 | * perFile modes, or a failure no file can be given. | |
| 525 | */ | |
| 526 | async function retryAlone(result: Timed, index: number): Promise<Timed> { | |
| 527 | if (result.code === 0 || result.files.length < 2 || perFile) return result; | |
| 528 | const { failed, sure } = failedFiles(result.output, result.files); | |
| 529 | if (!sure || failed.length > RETRY_MAX) return result; | |
| 530 | const passed: string[] = []; | |
| 531 | for (const file of failed) { | |
| 532 | const alone = await runShard([file], index, false, `${index + 1}/${slices.length} again alone`); | |
| 533 | if (alone.code !== 0) return result; | |
| 534 | passed.push(file); | |
| 535 | } | |
| 536 | return { ...result, code: 0, beside: passed }; | |
| 537 | } | |
| 538 | ||
| 512 | 539 | /** Runs every slice, at most `width` at once, in the slices' order. |
| 513 | 540 | * Stops starting new slices after the first non-zero exit and signals |
| 514 | 541 | * in-flight children so a red shard does not burn the rest of the wall. */ |
| @@ -521,7 +548,10 @@ async function pool(width: number): Promise<Timed[]> { | ||
| 521 | 548 | if (stop) break; |
| 522 | 549 | const index = next++; |
| 523 | 550 | if (stop) break; |
| 524 | results[index] = await runShard(slices[index]!, index, width === 1); | |
| 551 | results[index] = await retryAlone( | |
| 552 | await runShard(slices[index]!, index, width === 1), | |
| 553 | index, | |
| 554 | ); | |
| 525 | 555 | if (results[index]!.code !== 0) { |
| 526 | 556 | stop = true; |
| 527 | 557 | for (const child of children) child.kill("SIGTERM"); |
test/test-shards-retry.test.ts added +70
| @@ -0,0 +1,70 @@ | ||
| 1 | import { afterAll, describe, expect, test } from "bun:test"; | |
| 2 | import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; | |
| 3 | import { tmpdir } from "node:os"; | |
| 4 | import { join } from "node:path"; | |
| 5 | import { failedFiles, tally } from "../scripts/shards.ts"; | |
| 6 | ||
| 7 | const dir = mkdtempSync(join(tmpdir(), "retry-")); | |
| 8 | afterAll(() => rmSync(dir, { recursive: true, force: true })); | |
| 9 | ||
| 10 | const write = (name: string, body: string): string => { | |
| 11 | const path = join(dir, name); | |
| 12 | writeFileSync(path, `import { test, expect } from "bun:test";\n${body}\n`); | |
| 13 | return path; | |
| 14 | }; | |
| 15 | ||
| 16 | /** A heavy file keeps itself in a shard alone; the two small ones share the other. */ | |
| 17 | const big = write("big.test.ts", `test("big", () => {});\n${"// padding\n".repeat(4000)}`); | |
| 18 | const leaker = write("a-leaker.test.ts", `test("sets a global", () => { (globalThis as any).__leak = 1; });`); | |
| 19 | const victim = write( | |
| 20 | "b-victim.test.ts", | |
| 21 | `test("sees the global", () => { expect((globalThis as any).__leak).toBeUndefined(); });`, | |
| 22 | ); | |
| 23 | const broken = write("c-broken.test.ts", `test("always red", () => { expect(1).toBe(2); });`); | |
| 24 | ||
| 25 | async function run(files: string[]): Promise<{ code: number; out: string }> { | |
| 26 | const child = Bun.spawn(["bun", "scripts/test-shards.ts", ...files], { | |
| 27 | stdout: "pipe", | |
| 28 | stderr: "pipe", | |
| 29 | env: { ...process.env, FORGE_CHECK_SHARDS: "2", FORGE_CHECK_IMPACT: "" }, | |
| 30 | }); | |
| 31 | const [out, err] = await Promise.all([new Response(child.stdout).text(), new Response(child.stderr).text()]); | |
| 32 | return { code: await child.exited, out: out + err }; | |
| 33 | } | |
| 34 | ||
| 35 | describe("a file that fails only beside others", () => { | |
| 36 | test("passes alone: the run is green and the file is named", async () => { | |
| 37 | const { code, out } = await run([big, leaker, victim]); | |
| 38 | expect(out).toContain(`fails only beside others: ${victim} (shard-mates: ${leaker})`); | |
| 39 | expect(code).toBe(0); | |
| 40 | }, 60_000); | |
| 41 | ||
| 42 | test("fails alone too: the run stays red", async () => { | |
| 43 | const { code, out } = await run([big, leaker, broken]); | |
| 44 | expect(out).not.toContain("fails only beside others"); | |
| 45 | expect(code).toBe(1); | |
| 46 | }, 60_000); | |
| 47 | }); | |
| 48 | ||
| 49 | describe("failedFiles", () => { | |
| 50 | const out = (body: string): string => `${body}\n\n 1 pass\n 1 fail\nRan 2 tests across 2 files.\n`; | |
| 51 | test("attributes a (fail) to the file header above it", () => { | |
| 52 | const got = failedFiles(out("test/a.test.ts:\n(pass) ok\n\ntest/b.test.ts:\n(fail) x [1ms]"), ["test/a.test.ts", "test/b.test.ts"]); | |
| 53 | expect(got).toEqual({ failed: ["test/b.test.ts"], sure: true }); | |
| 54 | }); | |
| 55 | test("a failure under no known file, or none named, is not sure", () => { | |
| 56 | expect(failedFiles(out("(fail) x [1ms]"), ["test/a.test.ts"]).sure).toBe(false); | |
| 57 | expect(failedFiles(out("test/a.test.ts:\n(pass) ok"), ["test/a.test.ts"]).sure).toBe(false); | |
| 58 | }); | |
| 59 | }); | |
| 60 | ||
| 61 | describe("tally with a file that passed alone", () => { | |
| 62 | test("names it and stays green", () => { | |
| 63 | const ending = tally( | |
| 64 | [{ files: ["a", "b"], code: 0, beside: ["b"], output: "(fail) t [1ms]\nRan 2 tests across 2 files.\n" }], | |
| 65 | 1, | |
| 66 | ); | |
| 67 | expect(ending.code).toBe(0); | |
| 68 | expect(ending.lines[0]).toBe("fails only beside others: b (shard-mates: a)"); | |
| 69 | }); | |
| 70 | }); |
test/toolbar-actions.test.ts +1 −1
| @@ -80,7 +80,7 @@ describe("toolbar actions", () => { | ||
| 80 | 80 | servers.stop(); |
| 81 | 81 | app.close(); |
| 82 | 82 | await rm(dataDir, { recursive: true, force: true }); |
| 83 | }); | |
| 83 | }, 30_000); | |
| 84 | 84 | |
| 85 | 85 | const url = (env: string) => |
| 86 | 86 | `/alice/notes/toolbar?env=${env}&page=${encodeURIComponent(PAGE)}`; |
test/tui-flags.test.ts +2 −2
| @@ -67,12 +67,12 @@ describe("flags in the terminal", () => { | ||
| 67 | 67 | } |
| 68 | 68 | forge.db.query("UPDATE user SET is_admin = 1, is_agent = 1 WHERE username = 'carol'").run(); |
| 69 | 69 | useFlagRegistry(reg); |
| 70 | }); | |
| 70 | }, 30_000); | |
| 71 | 71 | afterAll(async () => { |
| 72 | 72 | useFlagRegistry(undefined); |
| 73 | 73 | forge.close(); |
| 74 | 74 | await rm(dataDir, { recursive: true, force: true }); |
| 75 | }); | |
| 75 | }, 30_000); | |
| 76 | 76 | |
| 77 | 77 | const open = async (h: ReturnType<typeof boot>) => { |
| 78 | 78 | await h.ctrl("k"); |
new above
offline