Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

feat(scripts): write nested test modules to the directory rustc implies

Steven Enamakelc870ef564853
diff --git a/scripts/__tests__/externalize-inline-tests.test.mjs b/scripts/__tests__/externalize-inline-tests.test.mjs
index b1d96c4958..7db22259c8 100644
--- a/scripts/__tests__/externalize-inline-tests.test.mjs
+++ b/scripts/__tests__/externalize-inline-tests.test.mjs
@@ -76,15 +76,16 @@ test("leaves cfg(not(test)) and plain inline modules alone", () => {
assert.equal(out.source, src);
});
-test("moves a module nested in an inline wrapper and climbs out of its directories", () => {
+test("writes a module nested in an inline wrapper to the directory rustc implies", () => {
const src = "#[cfg(target_os = \"macos\")]\nmod imp {\n fn a() {}\n\n #[cfg(test)]\n mod tests {\n use super::*;\n\n #[test]\n fn t() {}\n }\n}\n";
const plain = externalizeSource(src, "cf_type");
assert.equal(plain.moves[0].fileName, "cf_type_tests.rs");
+ assert.equal(plain.moves[0].relDir, "cf_type/imp");
assert.equal(plain.moves[0].body, "use super::*;\n\n#[test]\nfn t() {}\n");
- assert.match(plain.source, / #\[cfg\(test\)\]\n #\[path = "..\/..\/cf_type_tests.rs"\]\n mod tests;\n}\n$/);
+ assert.match(plain.source, / #\[cfg\(test\)\]\n #\[path = "cf_type_tests.rs"\]\n mod tests;\n}\n$/);
assert.deepEqual(plain.skipped, []);
- // A `mod.rs` or crate root owns its directory, so it climbs one level less.
- assert.match(externalizeSource(src, "mod").source, /#\[path = "..\/mod_tests.rs"\]/);
+ // A `mod.rs` or crate root owns its directory, so the wrapper's directory sits beside it.
+ assert.equal(externalizeSource(src, "mod").moves[0].relDir, "imp");
});
test("names a second nested module after its wrapper when the file name is taken", () => {
@@ -92,6 +93,7 @@ test("names a second nested module after its wrapper when the file name is taken
`mod ${wrapper} {\n #[cfg(test)]\n mod tests {\n fn a() {}\n }\n}\n`;
const out = externalizeSource(`${block("imp")}\n${block("raise")}`, "ops");
assert.deepEqual(out.moves.map((m) => m.fileName), ["ops_tests.rs", "ops_raise_tests.rs"]);
+ assert.deepEqual(out.moves.map((m) => m.relDir), ["ops/imp", "ops/raise"]);
assert.deepEqual(out.skipped, []);
});
@@ -99,8 +101,7 @@ test("puts a bin crate root's tests in a subdirectory Cargo will not build as a
const src = "fn main() {}\n\n#[cfg(test)]\nmod tests {\n fn a() {}\n}\n";
const out = externalizeSource(src, "tool", new Set(), { subdir: "tool" });
assert.match(out.source, /#\[path = "tool\/tool_tests.rs"\]\nmod tests;/);
- const nested = "mod imp {\n #[cfg(test)]\n mod tests {\n fn a() {}\n }\n}\n";
- assert.match(externalizeSource(nested, "tool", new Set(), { subdir: "tool" }).source, /#\[path = "..\/tool\/tool_tests.rs"\]/);
+ assert.equal(out.moves[0].relDir, "tool");
});
test("reports a test module nested inside a function instead of moving it", () => {
diff --git a/scripts/externalize-inline-tests.mjs b/scripts/externalize-inline-tests.mjs
index bf32fb75bc..9b5d489f4d 100644
--- a/scripts/externalize-inline-tests.mjs
+++ b/scripts/externalize-inline-tests.mjs
@@ -149,13 +149,15 @@ const MOD_RS_STEMS = new Set(["mod", "lib", "main"]);
*
* `stem` is the source file's name without `.rs`; `taken` holds sibling file
* names that already exist. A module nested in other inline modules (the
- * `mod imp { ... }` wrapper around platform code) gets a `#[path]` that climbs
- * out of the directories those modules imply. `subdir` puts the test files in
- * a subdirectory of the source file's own, which a crate root in `src/bin/`
- * needs: Cargo would build any `.rs` file placed directly there as a binary.
- * Returns the rewritten source,
- * the moved bodies (`{ name, fileName, body, line }`) and `skipped` modules
- * that need a human.
+ * `mod imp { ... }` wrapper around platform code) is written to the directory
+ * rustc implies for it (`foo/imp/` for `foo.rs`), so its `#[path]` is a bare
+ * file name. `subdir` puts a top-level module's file in a subdirectory of the
+ * source file's own, which a crate root in `src/bin/` needs: Cargo builds any
+ * `.rs` file placed directly there as a binary.
+ *
+ * Returns the rewritten source, the moved bodies
+ * (`{ name, fileName, relDir, body, line }`, `relDir` relative to the source
+ * file) and `skipped` modules that need a human.
*/
export function externalizeSource(src, stem, taken = new Set(), { subdir = "" } = {}) {
const code = codeMask(src);