diff --git a/AGENTS.md b/AGENTS.md index 22dc331bf46..5882fc4cf4b 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -137,9 +137,10 @@ coverage must be at least 80 percent. starts with `use super::*;` and carries no `#[cfg(test)]` of its own. Never name one `test.rs`, `tests.rs` or `_test.rs`, and never write an inline `#[cfg(test)] mod tests { ... }` (`pnpm rust:layout` fails on an inline module - and on `test.rs`/`tests.rs`; it does not yet catch `_test.rs`). The same - rule binds every `vendor/` submodule: `node scripts/externalize-inline-tests.mjs - --write` converts one mechanically, and a crate root directly in + and on `test.rs`/`tests.rs`/`_test.rs`). The same rule binds every + `vendor/` submodule: `node scripts/externalize-inline-tests.mjs + --write` converts one mechanically (add `--rename-legacy` for `test.rs` and + `_test.rs`), and a crate root directly in `src/bin/` keeps its tests in `src/bin//` because Cargo builds any `.rs` placed straight in `src/bin/` as a binary. - Rust domain tests live beside their modules. Use diff --git a/scripts/__tests__/check-openhuman-rust-layout.test.mjs b/scripts/__tests__/check-openhuman-rust-layout.test.mjs index 53d76a63c03..fb9925c21cf 100644 --- a/scripts/__tests__/check-openhuman-rust-layout.test.mjs +++ b/scripts/__tests__/check-openhuman-rust-layout.test.mjs @@ -63,6 +63,16 @@ test("fails one line over the limit", () => { assert.match(out, /a\.rs: 751 lines \(limit 750\)/); }); +test("rejects test.rs, tests.rs and the singular _test.rs", () => { + for (const name of ["test.rs", "tests.rs", "thing_test.rs"]) { + const { status, out } = run({ [name]: 1 }); + assert.equal(status, 1, name); + assert.match(out, /must use a descriptive \*_tests\.rs filename/); + } + const ok = run({ "thing_tests.rs": 1 }); + assert.equal(ok.status, 0, ok.out); +}); + test("warns, without failing, on a file inside the warn band", () => { const { status, out } = run({ "near.rs": 740, "far.rs": 725 }); assert.equal(status, 0, out); diff --git a/scripts/__tests__/externalize-inline-tests.test.mjs b/scripts/__tests__/externalize-inline-tests.test.mjs index 7db22259c80..98c8847e947 100644 --- a/scripts/__tests__/externalize-inline-tests.test.mjs +++ b/scripts/__tests__/externalize-inline-tests.test.mjs @@ -1,7 +1,7 @@ import assert from "node:assert/strict"; import { test } from "node:test"; -import { codeMask, externalizeSource } from "../externalize-inline-tests.mjs"; +import { codeMask, externalizeSource, planLegacyRenames } from "../externalize-inline-tests.mjs"; const DECL = '#[cfg(test)]\n#[path = "lib_tests.rs"]\nmod tests;\n'; @@ -132,3 +132,57 @@ test("codeMask treats lifetimes as code and char literals as data", () => { assert.equal(mask[src.indexOf("'a")], 1); assert.equal(mask[src.indexOf("'{'") + 1], 0); }); + +test("renames legacy test files and repoints their declarations with #[path]", () => { + const sources = new Map([ + ["src/config/mod.rs", "#[cfg(test)]\nmod test;\nmod types;\n"], + ["src/config/test.rs", "use super::*;\n"], + ["src/foo.rs", "pub fn f() {}\n\n#[cfg(test)]\nmod foo_test;\n"], + ["src/foo/foo_test.rs", "use super::*;\n"], + ["src/bar.rs", '#[cfg(test)]\n#[path = "bar/x_test.rs"]\nmod x;\n'], + ["src/bar/x_test.rs", "use super::*;\n"], + ["src/lib.rs", "mod config;\nmod foo;\nmod bar;\n"], + ]); + const plan = planLegacyRenames(sources); + assert.deepEqual( + plan.renames.map((r) => `${r.from}->${r.to}`).sort(), + ["src/bar/x_test.rs->src/bar/x_tests.rs", "src/config/test.rs->src/config/mod_tests.rs", "src/foo/foo_test.rs->src/foo/foo_tests.rs"], + ); + assert.equal(plan.edits.get("src/config/mod.rs"), '#[cfg(test)]\n#[path = "mod_tests.rs"]\nmod test;\nmod types;\n'); + assert.match(plan.edits.get("src/foo.rs"), /#\[path = "foo\/foo_tests.rs"\]\nmod foo_test;/); + assert.match(plan.edits.get("src/bar.rs"), /#\[path = "bar\/x_tests.rs"\]\nmod x;/); + assert.deepEqual(plan.manual, []); +}); + +test("names test.rs after a non-mod.rs declarer and refuses a taken name", () => { + const nested = planLegacyRenames(new Map([ + ["src/foo.rs", "#[cfg(test)]\nmod test;\n"], + ["src/foo/test.rs", "use super::*;\n"], + ])); + assert.deepEqual(nested.renames, [{ from: "src/foo/test.rs", to: "src/foo/foo_tests.rs" }]); + assert.match(nested.edits.get("src/foo.rs"), /#\[path = "foo\/foo_tests.rs"\]/); + + const taken = planLegacyRenames(new Map([ + ["src/lib.rs", "mod a_test;\n"], + ["src/a_test.rs", "x\n"], + ["src/a_tests.rs", "y\n"], + ])); + assert.equal(taken.renames.length, 0); + assert.match(taken.manual[0].reason, /already exists/); +}); + +test("leaves a legacy file alone when its own child modules depend on its name", () => { + const plan = planLegacyRenames(new Map([ + ["src/lib.rs", "mod a_test;\n"], + ["src/a_test.rs", "mod helper;\n"], + ["src/a_test/helper.rs", "x\n"], + ])); + assert.equal(plan.renames.length, 0); + assert.match(plan.manual[0].reason, /depends on this file's own name/); +}); + +test("reports a legacy file nothing declares", () => { + const plan = planLegacyRenames(new Map([["src/orphan_test.rs", "x\n"]])); + assert.equal(plan.renames.length, 0); + assert.match(plan.manual[0].reason, /no `mod` declaration/); +}); diff --git a/scripts/ci/check-openhuman-rust-layout.mjs b/scripts/ci/check-openhuman-rust-layout.mjs index f9f4acd0dd9..fd2eca6faed 100644 --- a/scripts/ci/check-openhuman-rust-layout.mjs +++ b/scripts/ci/check-openhuman-rust-layout.mjs @@ -108,7 +108,10 @@ for (const file of allRustFiles(ROOT)) { for (const file of allRustFiles(CRATES_ROOT)) { const source = fs.readFileSync(file, "utf8"); - if (["tests.rs", "test.rs"].includes(path.basename(file))) { + if ( + ["tests.rs", "test.rs"].includes(path.basename(file)) || + path.basename(file).endsWith("_test.rs") + ) { failures.push( `${file}: test modules must use a descriptive *_tests.rs filename`, ); diff --git a/scripts/ci/module-pin-exemptions.json b/scripts/ci/module-pin-exemptions.json index 9720a45edb0..3f2626c9100 100644 --- a/scripts/ci/module-pin-exemptions.json +++ b/scripts/ci/module-pin-exemptions.json @@ -9,7 +9,68 @@ "is a deliberate edit to this file, which is a reviewable diff.", "", "Delete an entry the moment the pins are reconciled. An exemption that has", - "stopped being true fails the gate too \u2014 `expect` must still match." + "stopped being true fails the gate too — `expect` must still match." ], - "exemptions": [] + "exemptions": [ + { + "id": "tinycomputer", + "expect": "v0.9.0-1-g8b73151a", + "reason": "Pin carries the legacy test-file rename commits (#6903) past the release tag; re-pin to the next release of this module once its rename PR is released." + }, + { + "id": "tinysearch", + "expect": "v0.3.4-2-g2a71ea9", + "reason": "Pin carries the legacy test-file rename commits (#6903) past the release tag; re-pin to the next release of this module once its rename PR is released." + }, + { + "id": "tinydocs", + "expect": "v0.1.19-3-g25290eb", + "reason": "Pin carries the legacy test-file rename commits (#6903) past the release tag; re-pin to the next release of this module once its rename PR is released." + }, + { + "id": "tinywallet", + "expect": "v0.7.2-2-g5b18a06", + "reason": "Pin carries the legacy test-file rename commits (#6903) past the release tag; re-pin to the next release of this module once its rename PR is released." + }, + { + "id": "tinymemory", + "expect": "v1.22.3-3-ge5e2cbbc", + "reason": "Pin carries the legacy test-file rename commits (#6903) past the release tag; re-pin to the next release of this module once its rename PR is released." + }, + { + "id": "tinyjuice", + "expect": "v0.5.1-2-gb2f7172", + "reason": "Pin carries the legacy test-file rename commits (#6903) past the release tag; re-pin to the next release of this module once its rename PR is released." + }, + { + "id": "tinyvoice", + "expect": "v0.1.10-2-gb8594d5", + "reason": "Pin carries the legacy test-file rename commits (#6903) past the release tag; re-pin to the next release of this module once its rename PR is released." + }, + { + "id": "tinymcp", + "expect": "v0.3.6-8-g8a25ea4", + "reason": "Pin carries the legacy test-file rename commits (#6903) past the release tag; re-pin to the next release of this module once its rename PR is released." + }, + { + "id": "tinyconnectors", + "expect": "v0.12.2-2-g8d54a88", + "reason": "Pin carries the legacy test-file rename commits (#6903) past the release tag; re-pin to the next release of this module once its rename PR is released." + }, + { + "id": "tinybox", + "expect": "v0.1.12-2-g9106bf3", + "reason": "Pin carries the legacy test-file rename commits (#6903) past the release tag; re-pin to the next release of this module once its rename PR is released." + }, + { + "id": "tinychannels", + "expect": "v0.1.8-2-gb5efe58", + "reason": "Pin carries the legacy test-file rename commits (#6903) past the release tag; re-pin to the next release of this module once its rename PR is released." + }, + { + "id": "tinyhosts", + "expect": "v0.2.1-2-gd39dc16", + "reason": "Pin carries the legacy test-file rename commits (#6903) past the release tag; re-pin to the next release of this module once its rename PR is released." + } + ] } diff --git a/scripts/externalize-inline-tests.mjs b/scripts/externalize-inline-tests.mjs index 9b5d489f4de..b415e49938b 100644 --- a/scripts/externalize-inline-tests.mjs +++ b/scripts/externalize-inline-tests.mjs @@ -12,6 +12,10 @@ // the core crate or any vendored submodule checkout: // // node scripts/externalize-inline-tests.mjs ... [--write] [--no-fmt] +// node scripts/externalize-inline-tests.mjs ... --rename-legacy [--write] +// +// `--rename-legacy` renames `test.rs` and `_test.rs` to `*_tests.rs` and +// points each module declaration at the new file with `#[path]`. // // Without `--write` it only reports. It exits 1 while any inline test module // remains, including the ones it refuses to move and lists as "manual". @@ -393,13 +397,154 @@ function run(roots, { write, fmt }) { return write ? manual : manual + movedTotal; } +const LEGACY_FILE = /(^|\/)(test|[A-Za-z0-9_]+_test)\.rs$/; +const ROOT_STEMS = new Set(["mod", "lib", "main", "build"]); +const MOD_DECL = /^([ \t]*)(?:pub(?:\([^)]*\))?[ \t]+)?mod[ \t]+(\w+)[ \t]*;/gm; +const PATH_ATTR = /^[ \t]*#\[path[ \t]*=[ \t]*"([^"]+)"[ \t]*\]/; + +/** A `mod.rs`, crate root or `src/bin/*.rs` looks for child modules beside itself, not in `/`. */ +function ownsDir(rel) { + const stem = path.posix.basename(rel, ".rs"); + return ROOT_STEMS.has(stem) || /(^|\/)src\/bin$/.test(path.posix.dirname(rel)); +} + +/** + * Plan the rename of legacy test files (`test.rs`, `_test.rs`) to + * `*_tests.rs`. `sources` maps repo-relative posix paths to file contents. + * + * The module keeps its identifier, so references to it keep resolving; its + * declaration gains (or retargets) a `#[path]` to the new file. `test.rs` + * becomes `_tests.rs` (`mod_tests.rs` beside a `mod.rs`). + * Returns `renames` (`{ from, to }`), `edits` (path -> rewritten source) and + * `manual` files the plan refuses to touch. + */ +export function planLegacyRenames(sources) { + const legacy = new Set([...sources.keys()].filter((f) => LEGACY_FILE.test(f))); + const found = new Map(); + const manual = []; + for (const [file, src] of sources) { + if (!/\bmod\s+\w+\s*;/.test(src)) continue; + const skel = skeleton(src, codeMask(src)); + const lines = src.split("\n"); + for (const m of skel.matchAll(MOD_DECL)) { + const line = (skel.slice(0, m.index).match(/\n/g) ?? []).length; + let pathLine = -1; + let pathValue = null; + for (let k = line - 1; k >= 0; k -= 1) { + const text = lines[k].trim(); + if (text.startsWith("#[")) { + const hit = PATH_ATTR.exec(lines[k]); + if (hit) { + pathLine = k; + pathValue = hit[1]; + } + } else if (!text.startsWith("//")) { + break; + } + } + const dir = path.posix.dirname(file); + const base = ownsDir(file) ? dir : path.posix.join(dir, path.posix.basename(file, ".rs")); + const targets = pathValue + ? [path.posix.join(dir, pathValue)] + : [path.posix.join(base, `${m[2]}.rs`), path.posix.join(base, m[2], "mod.rs")]; + const target = targets.find((t) => legacy.has(t)); + if (legacy.has(file) && !pathValue && !ownsDir(file)) { + manual.push({ file, reason: `declares \`mod ${m[2]};\` whose location depends on this file's own name` }); + } + if (!target) continue; + found.set(target, [...(found.get(target) ?? []), { file, indent: m[1], line, pathLine }]); + } + } + + const planned = new Map(); + const taken = new Set(sources.keys()); + for (const file of legacy) { + const decls = found.get(file); + if (!decls) { + manual.push({ file, reason: "no `mod` declaration found (include!, a Cargo target, or a macro?)" }); + continue; + } + if (decls.some((d) => d.indent !== "")) { + manual.push({ file, reason: "declared inside an inline module; move it by hand" }); + continue; + } + const name = path.posix.basename(file); + const declStem = path.posix.basename(decls[0].file, ".rs"); + const newName = name === "test.rs" ? `${declStem}_tests.rs` : name.replace(/_test\.rs$/, "_tests.rs"); + const to = path.posix.join(path.posix.dirname(file), newName); + if (taken.has(to)) { + manual.push({ file, reason: `${to} already exists` }); + continue; + } + taken.add(to); + planned.set(file, { to, decls }); + } + for (const x of manual) planned.delete(x.file); + + const edits = new Map(); + const perFile = new Map(); + for (const { to, decls } of planned.values()) { + for (const d of decls) { + const rel = path.posix.relative(path.posix.dirname(d.file), to); + perFile.set(d.file, [...(perFile.get(d.file) ?? []), { ...d, rel }]); + } + } + for (const [file, list] of perFile) { + const lines = sources.get(file).split("\n"); + for (const d of list.sort((a, b) => b.line - a.line)) { + const attr = `#[path = "${d.rel}"]`; + if (d.pathLine >= 0) lines[d.pathLine] = attr; + else lines.splice(d.line, 0, attr); + } + edits.set(file, lines.join("\n")); + } + return { + renames: [...planned].map(([from, { to }]) => ({ from, to })), + edits, + manual, + }; +} + +function renameLegacy(roots, { write }) { + let manualTotal = 0; + let renameTotal = 0; + for (const rootArg of roots) { + const root = path.resolve(rootArg); + const sources = new Map(); + for (const rel of trackedRustFiles(root)) { + if (fs.existsSync(path.join(root, rel))) sources.set(rel, fs.readFileSync(path.join(root, rel), "utf8")); + } + const plan = planLegacyRenames(sources); + for (const x of plan.manual) console.log(`manual ${path.relative(process.cwd(), path.join(root, x.file))}: ${x.reason}`); + if (write) { + for (const [file, text] of plan.edits) fs.writeFileSync(path.join(root, file), text); + for (const r of plan.renames) { + try { + execFileSync("git", ["-C", root, "mv", r.from, r.to], { stdio: "pipe" }); + } catch { + fs.renameSync(path.join(root, r.from), path.join(root, r.to)); + } + } + } else { + for (const r of plan.renames) console.log(`rename ${r.from} -> ${path.posix.basename(r.to)}`); + } + console.log(`${root}: ${plan.renames.length} legacy test file(s) ${write ? "renamed" : "renamable"}, ${plan.manual.length} manual`); + manualTotal += plan.manual.length; + renameTotal += plan.renames.length; + } + return write ? manualTotal : manualTotal + renameTotal; +} + if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { const args = process.argv.slice(2); const roots = args.filter((a) => !a.startsWith("--")); if (roots.length === 0) { - console.error("usage: externalize-inline-tests.mjs ... [--write] [--no-fmt]"); + console.error("usage: externalize-inline-tests.mjs ... [--write] [--no-fmt] [--rename-legacy]"); process.exit(2); } - const remaining = run(roots, { write: args.includes("--write"), fmt: !args.includes("--no-fmt") }); + const write = args.includes("--write"); + const remaining = args.includes("--rename-legacy") + ? renameLegacy(roots, { write }) + : run(roots, { write, fmt: !args.includes("--no-fmt") }); process.exit(remaining > 0 ? 1 : 0); } diff --git a/vendor/tinybox b/vendor/tinybox index 48faeede7c9..9106bf33397 160000 --- a/vendor/tinybox +++ b/vendor/tinybox @@ -1 +1 @@ -Subproject commit 48faeede7c9728ff0315c36ebfc7da8c300803e4 +Subproject commit 9106bf333973100ba69510e6853fd963d20acae3 diff --git a/vendor/tinybus b/vendor/tinybus index dc8c92f58b5..75b8a3f5a2c 160000 --- a/vendor/tinybus +++ b/vendor/tinybus @@ -1 +1 @@ -Subproject commit dc8c92f58b5dcb8b8af4a5b3a81aafe3333fa1e4 +Subproject commit 75b8a3f5a2cfee98c9d0e3bf08df7a3d163ffbd8 diff --git a/vendor/tinychannels b/vendor/tinychannels index 9f674d2d329..b5efe5881c3 160000 --- a/vendor/tinychannels +++ b/vendor/tinychannels @@ -1 +1 @@ -Subproject commit 9f674d2d329292755b3b8403193e25f087da2c62 +Subproject commit b5efe5881c3dcd082227c08eb20f5a7f45754a17 diff --git a/vendor/tinycomputer b/vendor/tinycomputer index eb1415246ca..8b73151ae97 160000 --- a/vendor/tinycomputer +++ b/vendor/tinycomputer @@ -1 +1 @@ -Subproject commit eb1415246ca56e0f58b9e558073c5d9e3740ce8f +Subproject commit 8b73151ae9712f78aeaa27a6a48dd2b06cf6a830 diff --git a/vendor/tinyconnectors b/vendor/tinyconnectors index 93de07bd7db..8d54a880542 160000 --- a/vendor/tinyconnectors +++ b/vendor/tinyconnectors @@ -1 +1 @@ -Subproject commit 93de07bd7dbefbf450e089e482ba4d3e481082e2 +Subproject commit 8d54a880542dc2edb0462dc2af2d52e7fba3c612 diff --git a/vendor/tinydocs b/vendor/tinydocs index 255fa638bcb..25290eb964d 160000 --- a/vendor/tinydocs +++ b/vendor/tinydocs @@ -1 +1 @@ -Subproject commit 255fa638bcb75a8fd5fe56a265aadccefdfa363d +Subproject commit 25290eb964d38bf00ad2a6b735282792e751291f diff --git a/vendor/tinyhosts b/vendor/tinyhosts index 50b3808f41a..d39dc1645b7 160000 --- a/vendor/tinyhosts +++ b/vendor/tinyhosts @@ -1 +1 @@ -Subproject commit 50b3808f41ade2744c7c833f731af50899f927eb +Subproject commit d39dc1645b74ec7333ac5612e73c39f0eede83ee diff --git a/vendor/tinyjuice b/vendor/tinyjuice index 8b70ce7a235..b2f71723882 160000 --- a/vendor/tinyjuice +++ b/vendor/tinyjuice @@ -1 +1 @@ -Subproject commit 8b70ce7a235d4f92c42ca0dc1dc0dc934c7c612c +Subproject commit b2f71723882cc90c5be7534b3b64d0a66bf16fac diff --git a/vendor/tinymcp b/vendor/tinymcp index 3a9a27ac381..8a25ea4fb1e 160000 --- a/vendor/tinymcp +++ b/vendor/tinymcp @@ -1 +1 @@ -Subproject commit 3a9a27ac381d6e43315ee52c73ec34d518fc41f4 +Subproject commit 8a25ea4fb1e8ecfe98e4e9406ff2cf3162d2575a diff --git a/vendor/tinymemory b/vendor/tinymemory index c1004ae8465..e5e2cbbc315 160000 --- a/vendor/tinymemory +++ b/vendor/tinymemory @@ -1 +1 @@ -Subproject commit c1004ae8465a843392f0f7d60edb1b74dae16f4a +Subproject commit e5e2cbbc315cfbe1fd439948f359f758316b4ddc diff --git a/vendor/tinysearch b/vendor/tinysearch index 7aff0a92f45..2a71ea9da68 160000 --- a/vendor/tinysearch +++ b/vendor/tinysearch @@ -1 +1 @@ -Subproject commit 7aff0a92f4519c218cbbc61889c7e89b9decd4d4 +Subproject commit 2a71ea9da68e43ec3da12c83f5091390f33fe616 diff --git a/vendor/tinyvoice b/vendor/tinyvoice index 772c1134f2d..b8594d54393 160000 --- a/vendor/tinyvoice +++ b/vendor/tinyvoice @@ -1 +1 @@ -Subproject commit 772c1134f2d0455501b18e5ec156364c254d4db8 +Subproject commit b8594d54393ff3378dc6d731d7d933678abd6290 diff --git a/vendor/tinywallet b/vendor/tinywallet index 994e2ac09ac..5b18a068066 160000 --- a/vendor/tinywallet +++ b/vendor/tinywallet @@ -1 +1 @@ -Subproject commit 994e2ac09ac15624eecf6ce6bab7941a8cf3ae5e +Subproject commit 5b18a06806648329ac6c890125095d5cce508ff8