Public: anyone can read this; the crew can change it.
The impact map counts a module a test used, not one it only loaded; a new file the diff leads to does not blind the plan #16975
Check · Review · Land · Live done — Landed and live in production.
impact-used → main 0 commits
4 files
changed +168
−13
scripts/test-shards.ts +3 −1
| @@ -618,6 +618,8 @@ async function drain( | ||
| 618 | 618 | } |
| 619 | 619 | |
| 620 | 620 | const measured = new Map<string, string[]>(); |
| 621 | // What each file imports, read once: `usedFiles` tells a module used from one only loaded by it. | |
| 622 | const readImports = importsReader(); | |
| 621 | 623 | const unmeasured = new Set<string>(); |
| 622 | 624 | // What a process that only loads the suite runs (`LOADS_ONLY`): a test's |
| 623 | 625 | // impact is what it runs beyond that. Without it, every executed file counts. |
| @@ -910,7 +912,7 @@ async function runShardIn( | ||
| 910 | 912 | try { |
| 911 | 913 | // The loads-only test's entry is what loading runs, set when it was covered. |
| 912 | 914 | if (loaded === undefined || slice[0]!.replace(/^\.\//, "") !== LOADS_ONLY) |
| 913 | measured.set(slice[0]!, usedFiles(readFileSync(join(covDir, "lcov.info"), "utf8"), loaded)); | |
| 915 | measured.set(slice[0]!, usedFiles(readFileSync(join(covDir, "lcov.info"), "utf8"), loaded, { test: slice[0]!.replace(/^\.\//, ""), importsOf: readImports })); | |
| 914 | 916 | } catch { |
| 915 | 917 | unmeasured.add(slice[0]!); |
| 916 | 918 | } |
src/checks/impact.ts +57 −11
| @@ -118,6 +118,23 @@ export function coverOf(lcov: string): Map<string, FileCover> { | ||
| 118 | 118 | return cover; |
| 119 | 119 | } |
| 120 | 120 | |
| 121 | /** | |
| 122 | * A module importing more than this many is a hub (the server, a registry): loading through it is | |
| 123 | * not use. | |
| 124 | */ | |
| 125 | export const HUB_IMPORTS = 30; | |
| 126 | ||
| 127 | /** | |
| 128 | * How a test's report reads a module beyond loading: `test` and `importsOf` let `usedFiles` tell | |
| 129 | * using a module from merely loading it. | |
| 130 | */ | |
| 131 | export interface UseReading { | |
| 132 | /** The test file, repository-relative. */ | |
| 133 | readonly test: string; | |
| 134 | /** A file's relative imports, repository-relative (`importsReader`). */ | |
| 135 | readonly importsOf: (file: string) => readonly string[]; | |
| 136 | } | |
| 137 | ||
| 121 | 138 | /** |
| 122 | 139 | * The `src/**` files a test used: those its report shows running more than |
| 123 | 140 | * `loaded` does — a file the suite's loading never touched, a line run more |
| @@ -125,36 +142,65 @@ export function coverOf(lcov: string): Map<string, FileCover> { | ||
| 125 | 142 | * way while loading is no more this test's than its neighbour's. With no |
| 126 | 143 | * `loaded` (no {@link LOADS_ONLY} in the tree, or it was red) the answer is |
| 127 | 144 | * {@link executedFiles}. |
| 145 | * | |
| 146 | * With `reading`, loading is not use either. A test that boots the server loads every page, | |
| 147 | * because the server imports them all, and their top-level lines run; on production that made | |
| 148 | * 840 files each reach more than 2,000 tests, so a one-page edit selected 62% of the suite. A file | |
| 149 | * loading never touched, that has functions, is this test's only when one of them ran, or when | |
| 150 | * the test imports it itself, or when a file this test did use imports it and is no hub (at most | |
| 151 | * {@link HUB_IMPORTS} imports: a page reading its words table, not the server loading every page). | |
| 152 | * A file with no functions (data, constants) counts when its lines ran, as before. A change that | |
| 153 | * breaks only a module's load is caught by the boot every fast tier runs (`release-boot.test.ts`, | |
| 154 | * `RULE_TESTS`). | |
| 128 | 155 | */ |
| 129 | export function usedFiles(lcov: string, loaded: Cover | undefined): string[] { | |
| 156 | export function usedFiles(lcov: string, loaded: Cover | undefined, reading?: UseReading): string[] { | |
| 130 | 157 | if (loaded === undefined) return executedFiles(lcov); |
| 131 | const used: string[] = []; | |
| 158 | type Seen = { more: boolean; called: boolean; functions: number; fresh: boolean }; | |
| 159 | const seen = new Map<string, Seen>(); | |
| 132 | 160 | let file: string | undefined; |
| 133 | 161 | let before: FileCover | undefined; |
| 134 | let more = false; | |
| 162 | let now: Seen = { more: false, called: false, functions: 0, fresh: true }; | |
| 135 | 163 | const end = (): void => { |
| 136 | if (file !== undefined && more && SOURCE.test(file)) used.push(own(file)); | |
| 164 | if (file !== undefined && SOURCE.test(file) && now.more) seen.set(own(file), now); | |
| 137 | 165 | file = undefined; |
| 138 | 166 | before = undefined; |
| 139 | more = false; | |
| 167 | now = { more: false, called: false, functions: 0, fresh: true }; | |
| 140 | 168 | }; |
| 141 | // Read as it comes, against what loading left: nothing is kept of the report. | |
| 169 | // Read as it comes, against what loading left: nothing is kept of the report but each file's | |
| 170 | // verdict. | |
| 142 | 171 | for (const line of lcov.split("\n")) { |
| 143 | 172 | if (line.startsWith("SF:")) { |
| 144 | 173 | end(); |
| 145 | 174 | file = line.slice(3).trim().replace(/^\.\//, ""); |
| 146 | 175 | before = loaded.get(file); |
| 147 | } else if (more || file === undefined) continue; | |
| 176 | now.fresh = before === undefined; | |
| 177 | } else if (file === undefined) continue; | |
| 148 | 178 | else if (line.startsWith("DA:")) { |
| 179 | if (now.more) continue; | |
| 149 | 180 | const comma = line.indexOf(","); |
| 150 | 181 | const count = Number(line.slice(comma + 1)); |
| 151 | if (count > 0 && count > (before?.lines.get(Number(line.slice(3, comma))) ?? 0)) more = true; | |
| 152 | } else if (line.startsWith("FNH:")) { | |
| 153 | if ((Number(line.slice(4)) || 0) > (before?.called ?? 0)) more = true; | |
| 182 | if (count > 0 && count > (before?.lines.get(Number(line.slice(3, comma))) ?? 0)) now.more = true; | |
| 183 | } else if (line.startsWith("FNF:")) now.functions = Number(line.slice(4)) || 0; | |
| 184 | else if (line.startsWith("FNH:")) { | |
| 185 | if ((Number(line.slice(4)) || 0) > (before?.called ?? 0)) { | |
| 186 | now.called = true; | |
| 187 | now.more = true; | |
| 188 | } | |
| 154 | 189 | } else if (line.startsWith("end_of_record")) end(); |
| 155 | 190 | } |
| 156 | 191 | end(); |
| 157 | return used.sort(); | |
| 192 | if (reading === undefined) return [...seen.keys()].sort(); | |
| 193 | // Loaded, not used: a file loading never touched, with functions, none of them run. | |
| 194 | const onlyLoaded = (s: Seen): boolean => s.fresh && s.functions > 0 && !s.called; | |
| 195 | const used = new Set([...seen].filter(([, s]) => !onlyLoaded(s)).map(([f]) => f)); | |
| 196 | const direct = new Set(reading.importsOf(reading.test)); | |
| 197 | const pulled = new Set<string>(); | |
| 198 | for (const f of used) { | |
| 199 | const imports = reading.importsOf(f); | |
| 200 | if (imports.length <= HUB_IMPORTS) for (const i of imports) pulled.add(i); | |
| 201 | } | |
| 202 | for (const [f, s] of seen) if (onlyLoaded(s) && (direct.has(f) || pulled.has(f))) used.add(f); | |
| 203 | return [...used].sort(); | |
| 158 | 204 | } |
| 159 | 205 | |
| 160 | 206 | /** |
src/checks/tiers.ts +13 −1
| @@ -275,7 +275,19 @@ export function fastSet(input: FastInput): FastPlan { | ||
| 275 | 275 | // outside src/ and test/ nothing bounds) makes the plan blind: every |
| 276 | 276 | // test is guessed against it, and every quick test runs. |
| 277 | 277 | if (reached !== undefined) { |
| 278 | for (const f of reached.unreached) blindBy.set(f, "no test executes it"); | |
| 278 | // A new file the map has not seen is judged through the files of this diff that import it, at | |
| 279 | // any depth, from one the map does reach: that file's tests already run. Only a new file | |
| 280 | // nothing here leads to blinds. | |
| 281 | const through = new Set(diffed.filter((f) => f.startsWith("src/") && !reached.unreached.includes(f))); | |
| 282 | for (let grew = true; grew; ) { | |
| 283 | grew = false; | |
| 284 | for (const f of reached.unreached) | |
| 285 | if (!through.has(f) && [...through].some((importer) => input.imports(importer).includes(f))) { | |
| 286 | through.add(f); | |
| 287 | grew = true; | |
| 288 | } | |
| 289 | } | |
| 290 | for (const f of reached.unreached) if (!through.has(f)) blindBy.set(f, "no test executes it"); | |
| 279 | 291 | for (const f of diffed) |
| 280 | 292 | if ( |
| 281 | 293 | !f.startsWith("src/") && |
test/impact-use-not-load.test.ts added +95
| @@ -0,0 +1,95 @@ | ||
| 1 | /** | |
| 2 | * The impact map tells a module a test used from one it only loaded (`usedFiles` with a | |
| 3 | * `UseReading`, src/checks/impact.ts). A test that boots the server loads every page, because the | |
| 4 | * server imports them all; on production that made 840 files each reach more than 2,000 tests, so | |
| 5 | * a one-page edit selected 62% of the suite. And a new file the map has never seen no longer | |
| 6 | * blinds the whole plan when a file of the same diff, which the map does reach, imports it. | |
| 7 | */ | |
| 8 | import { describe, expect, test } from "bun:test"; | |
| 9 | import { HUB_IMPORTS, coverOf, usedFiles } from "../src/checks/impact.ts"; | |
| 10 | import { RULE_TESTS, fastSet } from "../src/checks/tiers.ts"; | |
| 11 | ||
| 12 | describe("used, not loaded", () => { | |
| 13 | // What a process that only loads the suite runs: the preload's own files. | |
| 14 | const loaded = coverOf("SF:src/kernel/log.ts\nFNF:2\nFNH:1\nDA:1,1\nDA:5,2\nend_of_record\n"); | |
| 15 | const lcov = [ | |
| 16 | // A page the server loaded: its top level ran, none of its functions did. | |
| 17 | "SF:src/web/pages/system.ts\nFNF:4\nFNH:0\nDA:1,1\nDA:2,1\nend_of_record", | |
| 18 | // A page this test rendered: one of its functions ran. | |
| 19 | "SF:src/web/pages/home.ts\nFNF:3\nFNH:1\nDA:1,1\nDA:9,2\nend_of_record", | |
| 20 | // The words table the rendered page reads: loaded only, but its importer was used and is no | |
| 21 | // hub. | |
| 22 | "SF:src/web/pages/home_words.ts\nFNF:2\nFNH:0\nDA:1,1\nend_of_record", | |
| 23 | // A table the server alone imports: loaded through a hub. | |
| 24 | "SF:src/web/pages/hub_only.ts\nFNF:1\nFNH:0\nDA:1,1\nend_of_record", | |
| 25 | // A module the test imports itself, for a constant: used though no function ran. | |
| 26 | "SF:src/core/limits_table.ts\nFNF:2\nFNH:0\nDA:1,1\nend_of_record", | |
| 27 | // Data with no functions: its lines are its use, as before. | |
| 28 | "SF:src/design/palette.ts\nFNF:0\nFNH:0\nDA:1,1\nend_of_record", | |
| 29 | // The server: handled requests, so used; it imports every page. | |
| 30 | "SF:src/http/server.ts\nFNF:40\nFNH:12\nDA:1,1\nDA:30,4\nend_of_record", | |
| 31 | // Loaded by the preload and run further by this test. | |
| 32 | "SF:src/kernel/log.ts\nFNF:2\nFNH:2\nDA:1,1\nDA:5,6\nend_of_record", | |
| 33 | ].join("\n"); | |
| 34 | const pages = Array.from({ length: HUB_IMPORTS + 5 }, (_, i) => `src/web/pages/p${i}.ts`); | |
| 35 | const importsOf = (file: string): readonly string[] => | |
| 36 | ({ | |
| 37 | "test/home.test.ts": ["src/http/server.ts", "src/core/limits_table.ts"], | |
| 38 | "src/http/server.ts": [...pages, "src/web/pages/system.ts", "src/web/pages/hub_only.ts", "src/web/pages/home.ts"], | |
| 39 | "src/web/pages/home.ts": ["src/web/pages/home_words.ts"], | |
| 40 | })[file] ?? []; | |
| 41 | ||
| 42 | test("a module only loaded is not the test's; one it ran, imports, or reaches through a used non-hub is", () => { | |
| 43 | expect(usedFiles(lcov, loaded, { test: "test/home.test.ts", importsOf })).toEqual([ | |
| 44 | "src/core/limits_table.ts", | |
| 45 | "src/design/palette.ts", | |
| 46 | "src/http/server.ts", | |
| 47 | "src/kernel/log.ts", | |
| 48 | "src/web/pages/home.ts", | |
| 49 | "src/web/pages/home_words.ts", | |
| 50 | ]); | |
| 51 | }); | |
| 52 | ||
| 53 | test("without a reading, every file run beyond loading counts, as before", () => { | |
| 54 | expect(usedFiles(lcov, loaded)).toContain("src/web/pages/system.ts"); | |
| 55 | expect(usedFiles(lcov, loaded)).toContain("src/web/pages/hub_only.ts"); | |
| 56 | }); | |
| 57 | ||
| 58 | test("a change that breaks only a module's load is still caught: the boot runs in every fast tier", () => { | |
| 59 | expect(RULE_TESTS).toContain("test/release-boot.test.ts"); | |
| 60 | }); | |
| 61 | }); | |
| 62 | ||
| 63 | describe("a new file does not blind the plan when the diff leads to it", () => { | |
| 64 | const tests = ["test/system.test.ts", "test/other.test.ts", "test/quick.test.ts"]; | |
| 65 | const impact = new Map<string, string[]>([ | |
| 66 | ["test/system.test.ts", ["src/snapshot/service.ts"]], | |
| 67 | ["test/other.test.ts", ["src/core/other.ts"]], | |
| 68 | ["test/quick.test.ts", ["src/core/quick.ts"]], | |
| 69 | ]); | |
| 70 | const times = new Map([ | |
| 71 | ["test/quick.test.ts", 0.2], | |
| 72 | ["test/system.test.ts", 3], | |
| 73 | ["test/other.test.ts", 3], | |
| 74 | ]); | |
| 75 | const imports = (file: string): readonly string[] => | |
| 76 | ({ | |
| 77 | "src/snapshot/service.ts": ["src/snapshot/stream.ts"], | |
| 78 | "src/snapshot/stream.ts": ["src/snapshot/stream_words.ts"], | |
| 79 | })[file] ?? []; | |
| 80 | const plan = (changed: string[]) => fastSet({ changed, tests, imports, times, impact }); | |
| 81 | ||
| 82 | test("one a reached file of the diff imports, at any depth, is judged through that file's tests", () => { | |
| 83 | const p = plan(["src/snapshot/service.ts", "src/snapshot/stream.ts", "src/snapshot/stream_words.ts"]); | |
| 84 | expect(p.blindBy.size).toBe(0); | |
| 85 | expect(p.files).toContain("test/system.test.ts"); | |
| 86 | expect(p.files).not.toContain("test/other.test.ts"); | |
| 87 | expect(p.files).not.toContain("test/quick.test.ts"); | |
| 88 | }); | |
| 89 | ||
| 90 | test("one nothing in the diff leads to still blinds it, and every quick test runs", () => { | |
| 91 | const p = plan(["src/snapshot/service.ts", "src/snapshot/orphan.ts"]); | |
| 92 | expect([...p.blindBy.keys()]).toEqual(["src/snapshot/orphan.ts"]); | |
| 93 | expect(p.files).toContain("test/quick.test.ts"); | |
| 94 | }); | |
| 95 | }); |
new above
offline