Skip to content

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

fix(scripts): avoid name collision for nested test modules

Steven Enamakel8a5f8e7fec29
diff --git a/scripts/__tests__/externalize-inline-tests.test.mjs b/scripts/__tests__/externalize-inline-tests.test.mjs
index 4cf9aaad7e..ff5a298378 100644
--- a/scripts/__tests__/externalize-inline-tests.test.mjs
+++ b/scripts/__tests__/externalize-inline-tests.test.mjs
@@ -87,6 +87,14 @@ test("moves a module nested in an inline wrapper and climbs out of its directori
assert.match(externalizeSource(src, "mod").source, /#\[path = "..\/mod_tests.rs"\]/);
});
+test("names a second nested module after its wrapper when the file name is taken", () => {
+ const block = (wrapper) =>
+ `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.skipped, []);
+});
+
test("reports a test module nested inside a function instead of moving it", () => {
const src = "fn f() {\n #[cfg(test)]\n mod tests {\n fn a() {}\n }\n}\n";
const out = externalizeSource(src, "lib");
diff --git a/scripts/externalize-inline-tests.mjs b/scripts/externalize-inline-tests.mjs
index c1ead4f524..e2521334d7 100644
--- a/scripts/externalize-inline-tests.mjs
+++ b/scripts/externalize-inline-tests.mjs
@@ -254,7 +254,11 @@ export function externalizeSource(src, stem, taken = new Set()) {
reject("declares out-of-line modules whose paths would shift");
continue;
}
- const fileName = testFileName(stem, m[3]);
+ let fileName = testFileName(stem, m[3]);
+ // Two wrappers can each hold a \`tests\`; the second is named after its wrapper.
+ if (used.has(fileName) && chain.length > 0) {
+ fileName = testFileName(`${stem}_${chain[chain.length - 1].m[3]}`, m[3]);
+ }
if (used.has(fileName)) {
reject(`${fileName} already exists`);
continue;