From 71842a6a85559fd34a11614b751ae4bd051e492f Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Fri, 27 Nov 2026 17:05:37 +0100 Subject: [PATCH] =?UTF-8?q?Answer=20stability=20round=201:=201=20?= =?UTF-8?q?=F0=9F=94=B4,=201=20=F0=9F=9F=A1=20(0=20misread);=20a=20fix=20t?= =?UTF-8?q?hat=20drops=20a=20test=20file's=20declaration=20gets=20the=20pl?= =?UTF-8?q?anted=20file,=20and=20any=20file=20may=20stand=20beside=20or=20?= =?UTF-8?q?under=20its=20declarer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/task_bench/runs.py | 13 ++++++------ bin/task_bench/rust_test_modules.py | 27 ++++++++++++------------ bin/task_bench/test_rust_test_modules.py | 23 ++++++++++++++++---- bin/task_bench/test_task_bench.py | 15 +++++++++---- 4 files changed, 50 insertions(+), 28 deletions(-) diff --git a/bin/task_bench/runs.py b/bin/task_bench/runs.py index e6316e06..3d94c29a 100644 --- a/bin/task_bench/runs.py +++ b/bin/task_bench/runs.py @@ -168,11 +168,12 @@ def checked_tree(planted: str, copy: str, into: str, rust_tests_planted: bool = """Makes at `into` the tree the tests check a fix in: `planted`, the code with the bug as Haiku got it, with each file that is no test as `copy` holds it after the fix. So a test Haiku changed, removed or added doesn't count, and neither does a file under a `testdata` folder, where Go's tests keep the output they expect. Where rust_tests_planted, a Rust file's inline `#[cfg(test)]` - modules stand as the planted file has them too, as `with_tests_of` says, and so does each Rust file that `rust_test_files` finds. + modules stand as the planted file has them too, as `with_tests_of` says, and so does each file that `rust_test_files` finds in + the planted tree. Raises OSError where a file cannot be copied.""" shutil.copytree(planted, into, symlinks=True) - # `rust_test_files` is None for a tree whose declarations it cannot resolve, which the bench passes over where it plants the bug. + # `rust_test_files` is None for a tree with a declaration whose file it can't find, and the bench plants no bug in such a tree. rust_tests = (rust_test_files(planted) or set()) if rust_tests_planted else set() before, after = set(entries(planted)), set(entries(copy)) for path in before - after: @@ -209,8 +210,8 @@ def of_tests(path: str, rust_tests: typing.AbstractSet[str] = frozenset()) -> bo def rust_test_files(tree: str) -> set[str] | None: - """The Rust files under tree that a `#[cfg(test)] mod name;` declaration names, by their paths from tree. None where a - declaration names no file there, or `declared` cannot tell a file's declarations. + """The Rust files under tree that a `#[cfg(test)] mod name;` declaration names, by their paths from tree. None where a declaration + names no file there, or a string or comment of a Rust file is never closed: the tree may hold a test file this can't find. Raises OSError where a file cannot be read.""" files = set(entries(tree)) @@ -222,10 +223,10 @@ def rust_test_files(tree: str) -> set[str] | None: if names is None: return None for name in names: - standing = [home for home in homes(path, name) if home in files] + standing = files.intersection(homes(path, name)) if not standing: return None - found.add(standing[0]) + found |= standing return found diff --git a/bin/task_bench/rust_test_modules.py b/bin/task_bench/rust_test_modules.py index 2ebed22c..1588b8b7 100644 --- a/bin/task_bench/rust_test_modules.py +++ b/bin/task_bench/rust_test_modules.py @@ -1,6 +1,6 @@ """The `#[cfg(test)]` modules of a Rust file, which stand in the file of the code they test or, as `mod name;` declares, in a file of -their own. Here are where each inline one is, which files hold the others, and a file with the modules of another file put in place -of its own. A file is bytes: a Rust file is UTF-8, and a module is found by ASCII text alone.""" +their own. Here are where each inline one is, which modules a file declares and where their files may stand, and a file with the +modules of another file put in place of its own. A file is bytes: a Rust file is UTF-8, and a module is found by ASCII text alone.""" import pathlib import re @@ -20,9 +20,7 @@ COMMENT_EDGE = re.compile(rb"/\*|\*/") # What follows the attribute that starts an inline module: any other attributes, an optional visibility, then `mod name {`. MODULE = re.compile(rb"\s*(?:#\[[^\]]*\]\s*)*(?:pub(?:\([^)]*\))?\s+)?mod\s+(?P\w+)\s*\{") # What follows the attribute that starts a module kept in a file of its own: any other attributes, an optional visibility, then `mod name;`. -DECLARATION = re.compile(rb"\s*(?P(?:#\[[^\]]*\]\s*)*)(?:pub(?:\([^)]*\))?\s+)?mod\s+(?P\w+)\s*;") -PATH_ATTRIBUTE = re.compile(rb"#\[\s*path\b") -KEEP_BESIDE = ("lib.rs", "main.rs", "mod.rs") +DECLARATION = re.compile(rb"\s*(?:#\[[^\]]*\]\s*)*(?:pub(?:\([^)]*\))?\s+)?mod\s+(?P\w+)\s*;") def tells_apart(path: str, source: bytes) -> bool: @@ -59,8 +57,8 @@ def modules(source: bytes) -> dict[bytes, tuple[int, int]] | None: def declared(source: bytes) -> list[str] | None: - """The name of each `#[cfg(test)] mod name;` of source, a test module kept in a file of its own, in the order they stand. - None where a `#[path]` attribute moves such a file, or a string or comment is never closed.""" + """The name of each `#[cfg(test)] mod name;` of source, a test module kept in a file of its own, in the order they stand. None where + a string or comment is never closed.""" names = [] position = 0 while match := SCAN.search(source, position): @@ -68,18 +66,16 @@ def declared(source: bytes) -> list[str] | None: return None position = after if match.group() == b"#[cfg(test)]" and (declaration := DECLARATION.match(source, position)): - if PATH_ATTRIBUTE.search(declaration.group("attributes")): - return None names.append(declaration.group("name").decode()) return names def homes(path: str, name: str) -> list[str]: - """Where the file of the module `name`, declared in the Rust file at path, may stand, by its path from the same top. A crate root - (`lib.rs`, `main.rs`) and a `mod.rs` keep their modules beside them; any other file keeps them in a folder of its own name.""" + """Each place the file of the module `name`, declared in the Rust file at path, may stand, by its path from the same top. Rust keeps + it in the folder of the declaring file, or in a folder of that file's name where it is a module file, `a.rs` for `a/`; which of the + two the file is depends on what the crate's root is, so both count.""" declaring = pathlib.PurePosixPath(path) - folder = declaring.parent if declaring.name in KEEP_BESIDE else declaring.parent / declaring.stem - return [str(folder / f"{name}.rs"), str(folder / name / "mod.rs")] + return [str(folder / part) for folder in (declaring.parent, declaring.parent / declaring.stem) for part in (f"{name}.rs", f"{name}/mod.rs")] def after_token(source: bytes, match: re.Match[bytes]) -> int | None: @@ -121,7 +117,10 @@ def with_tests_of(planted: bytes, copy: bytes) -> bytes: Where planted's modules cannot be told apart, or planted has none and copy's cannot be told apart, copy is returned as it is. Where planted has some and copy's cannot be told apart, or copy lacks one of planted's, planted is returned: Haiku's file has no module - to put them in.""" + to put them in. The same holds for the `mod name;` declarations of planted, whose files stand as planted: a fix that drops one + would leave its tests uncompiled.""" + if not set(declared(planted) or []) <= set(declared(copy) or []): + return planted before, after = modules(planted), modules(copy) # Haiku's file stands where planted's modules can't be told apart. The bench never plants a bug in such a file, and a guess at # which module is which could swap one test for another. Haiku's file is also what the check took before the bench put back diff --git a/bin/task_bench/test_rust_test_modules.py b/bin/task_bench/test_rust_test_modules.py index cfd977c5..6acb2e27 100644 --- a/bin/task_bench/test_rust_test_modules.py +++ b/bin/task_bench/test_rust_test_modules.py @@ -7,7 +7,7 @@ import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from task_bench import rust_test_modules -from task_bench.rust_test_modules import tells_apart +from task_bench.rust_test_modules import homes, tells_apart LIB = textwrap.dedent("""\ pub fn add(a: i32, b: i32) -> i32 { @@ -118,15 +118,20 @@ class DeclaredTest(unittest.TestCase): source = '// #[cfg(test)]\n// mod a;\n/* #[cfg(test)] mod b; */\nconst C: &str = "#[cfg(test)] mod c;";\nconst D: &str = r#"#[cfg(test)] mod d;"#;\n' self.assertEqual(declared(source), []) - def test_cannot_tell_where_a_declaration_moves_its_file_with_a_path_attribute(self): - self.assertIsNone(declared('#[cfg(test)]\n#[path = "elsewhere.rs"]\nmod tests;\n')) - def test_cannot_tell_where_a_string_or_comment_is_never_closed(self): for source in ("#[cfg(test)]\nmod tests;\nconst A: &str = \"\n", "/* open\n#[cfg(test)]\nmod tests;\n"): with self.subTest(source=source): self.assertIsNone(declared(source)) +class HomesTest(unittest.TestCase): + def test_lists_where_the_file_of_a_declared_module_may_stand(self): + self.assertEqual(homes("src/pick.rs", "tests"), ["src/tests.rs", "src/tests/mod.rs", "src/pick/tests.rs", "src/pick/tests/mod.rs"]) + + def test_lists_the_places_by_the_path_from_the_top_of_a_file_there(self): + self.assertEqual(homes("lib.rs", "t"), ["t.rs", "t/mod.rs", "lib/t.rs", "lib/t/mod.rs"]) + + class TellsApartTest(unittest.TestCase): def test_tells_the_modules_of_a_rust_file_apart_where_they_can_be(self): self.assertEqual([tells_apart("lib.rs", b"fn a() {}"), tells_apart("lib.rs", b"fn a() {")], [True, False]) @@ -136,6 +141,16 @@ class TellsApartTest(unittest.TestCase): class WithTestsOfTest(unittest.TestCase): + def test_takes_the_planted_file_where_the_fix_dropped_a_declaration_of_a_test_file(self): + planted = "fn a() { 1 }\n#[cfg(test)]\nmod tests;\n#[cfg(test)]\nmod more;\n" + for name, fixed in {"dropped": "fn a() { 2 }\n", "one of two kept": "fn a() { 2 }\n#[cfg(test)]\nmod more;\n", "string never closed": 'fn a() { "\n#[cfg(test)]\nmod tests;\n#[cfg(test)]\nmod more;\n'}.items(): + with self.subTest(name): + self.assertEqual(with_tests_of(planted, fixed), planted) + + def test_takes_the_fixed_file_where_it_keeps_each_declaration_and_adds_one(self): + fixed = "fn a() { 2 }\n#[cfg(test)]\nmod more;\n#[cfg(test)]\nmod tests;\n" + self.assertEqual(with_tests_of("fn a() { 1 }\n#[cfg(test)]\nmod tests;\n", fixed), fixed) + def test_takes_the_code_as_fixed_and_the_tests_as_planted(self): planted = LIB.replace("a + b", "a - b") fixed = LIB.replace("assert_eq!(add(1, 2), 3);", "assert_eq!(add(1, 2), 0);") diff --git a/bin/task_bench/test_task_bench.py b/bin/task_bench/test_task_bench.py index 5e36a5aa..199569c7 100644 --- a/bin/task_bench/test_task_bench.py +++ b/bin/task_bench/test_task_bench.py @@ -338,7 +338,7 @@ class BenchTest(unittest.TestCase): # Passing tests with the bug planted: no message to tell Haiku. (self.repo / "calc_test.go").write_text("exit 0\n") self.commit("Tests that pass with the bug") - self.assertEqual(self.bench(), Failure("None of the 2 mutants the tests kill becomes a bug: each is on a line the head changed, or the test it fails doesn't print a message.")) + self.assertEqual(self.bench(), Failure("None of the 2 mutants the tests kill becomes a bug: each is on a line the head changed, the test it fails doesn't print a message, or, in a Rust file, a `#[cfg(test)] mod name;` of the tree names a file the bench can't find.")) def test_a_run_the_api_refuses_fails_the_bench_and_still_removes_what_it_made(self): self.env["FAKE_REFUSED_FROM"] = "1" @@ -639,7 +639,7 @@ class RunOutcomesTest(unittest.TestCase): bench = self.rust_bench({"src/lib.rs": self.ADD, "src/tests.rs": "", "src/other.rs": ""}) (self.tree / "src/other.rs").chmod(0) self.addCleanup((self.tree / "src/other.rs").chmod, 0o644) - self.assertFailsWith(main.planted(bench, 1, self.ADDING), "could not find the Rust test files beside src/lib.rs: ") + self.assertFailsWith(main.planted(bench, 1, self.ADDING), "could not read the Rust files for the mutant in src/lib.rs: ") class ContainersTest(unittest.TestCase): @@ -808,6 +808,12 @@ class CheckedTreeTest(unittest.TestCase): checked_tree(planted, copy, str(self.root / "check")) self.assertEqual(self.held("check"), {"src/lib.rs": "fn a() { 2 }\n#[cfg(test)]\nmod tests;\n", "src/tests.rs": "old tests", "src/other.rs": "fixed"}) + def test_takes_the_planted_rust_file_where_the_fix_dropped_the_declaration_of_a_test_file(self): + planted = self.tree("planted", {"src/lib.rs": self.DECLARING, "src/tests.rs": "old tests"}) + copy = self.tree("copy", {"src/lib.rs": "fn a() { 2 }\n", "src/tests.rs": "old tests"}) + checked_tree(planted, copy, str(self.root / "check")) + self.assertEqual(self.held("check"), {"src/lib.rs": self.DECLARING, "src/tests.rs": "old tests"}) + def test_takes_a_rust_test_file_the_fix_deleted_as_planted(self): planted = self.tree("planted", {"src/lib.rs": self.DECLARING, "src/tests.rs": "old tests"}) copy = self.tree("copy", {"src/lib.rs": "fn a() { 2 }\n#[cfg(test)]\nmod tests;\n"}) @@ -831,6 +837,7 @@ class CheckedTreeTest(unittest.TestCase): "beside a crate root": ({"src/lib.rs": self.DECLARING, "src/tests.rs": ""}, {"src/tests.rs"}), "as a folder with a mod.rs": ({"src/main.rs": self.DECLARING, "src/tests/mod.rs": ""}, {"src/tests/mod.rs"}), "in the folder of a module file": ({"src/pick.rs": self.DECLARING, "src/pick/tests.rs": ""}, {"src/pick/tests.rs"}), + "beside another crate root": ({"src/bin/tool.rs": self.DECLARING, "src/bin/tests.rs": ""}, {"src/bin/tests.rs"}), "beside a mod.rs": ({"src/pick/mod.rs": self.DECLARING, "src/pick/tests.rs": ""}, {"src/pick/tests.rs"}), "declared by no file": ({"src/lib.rs": "fn a() {}\n", "src/tests.rs": ""}, set()), } @@ -843,11 +850,11 @@ class CheckedTreeTest(unittest.TestCase): os.symlink("missing.rs", os.path.join(tree, "src/ghost.rs")) self.assertEqual(rust_test_files(tree), {"src/tests.rs"}) - def test_cannot_find_a_rust_test_file_that_is_not_where_its_declaration_puts_it_or_cannot_be_read_from_it(self): + def test_cannot_find_a_rust_test_file_that_is_not_where_its_declaration_puts_it_or_cannot_be_read_from_the_tree(self): cases = { "missing": {"src/lib.rs": self.DECLARING}, - "beside a module file, where only a crate root has its modules": {"src/pick.rs": self.DECLARING, "src/tests.rs": ""}, "moved by a path attribute": {"src/lib.rs": '#[cfg(test)]\n#[path = "t.rs"]\nmod tests;\n', "src/t.rs": ""}, + "inside an inline module": {"src/lib.rs": "mod a {\n#[cfg(test)]\nmod tests;\n}\n", "src/a/tests.rs": ""}, "in a declaring file whose string is never closed": {"src/lib.rs": '#[cfg(test)]\nmod tests;\nconst A: &str = "\n', "src/tests.rs": ""}, } for name, files in cases.items():