Keep a Rust test in a file of its own as planted in a task-bench fix

This commit is contained in:
2026-11-27 16:05:56 +01:00
parent 4a4a8526af
commit 86edf81d28
5 changed files with 195 additions and 23 deletions
+78 -1
View File
@@ -17,7 +17,7 @@ sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from task_bench import api_proxy, bench_folder, main, runs
from test_env import assert_not_root, isolated_env
from task_bench.runs import checked_tree, closed, Containers, docker_cli, fix_attempted, opened, ran_tests, TimedOut
from task_bench.runs import checked_tree, closed, Containers, docker_cli, fix_attempted, opened, ran_tests, rust_test_files, TimedOut
from task_bench.bugs.plant import drawn
from claude_code.entries import Entry, entry_from
from failure import Failure
@@ -613,6 +613,34 @@ class RunOutcomesTest(unittest.TestCase):
mutant = Mutant("lib.rs", 2, "lib.rs:2:7 replace + with -", (b" a + b",), (b" a - b",))
self.assertIsNone(main.planted(self.bench(), 1, mutant))
def rust_bench(self, files: dict[str, str], *runs: Ran | Failure) -> main.Bench:
for path, text in files.items():
(self.tree / path).parent.mkdir(parents=True, exist_ok=True)
(self.tree / path).write_text(text)
return self.bench(*runs)
ADD = "pub fn add(a: i32, b: i32) -> i32 {\n a + b\n}\n#[cfg(test)]\nmod tests;\n"
ADDING = Mutant("src/lib.rs", 2, "src/lib.rs:2:7 replace + with -", (b" a + b",), (b" a - b",))
def test_a_rust_mutant_in_a_test_file_of_its_own_is_passed_over(self):
bench = self.rust_bench({"src/lib.rs": self.ADD, "src/tests.rs": "fn t() { assert!(1 + 1 == 2); }\n"})
mutant = Mutant("src/tests.rs", 1, "src/tests.rs:1:20 replace + with -", (b"fn t() { assert!(1 + 1 == 2); }",), (b"fn t() { assert!(1 - 1 == 2); }",))
self.assertIsNone(main.planted(bench, 1, mutant))
def test_a_rust_mutant_in_a_tree_whose_declared_test_file_is_not_found_is_passed_over(self):
self.assertIsNone(main.planted(self.rust_bench({"src/lib.rs": self.ADD}), 1, self.ADDING))
def test_a_rust_mutant_in_code_beside_a_test_file_goes_on_to_the_tests(self):
broke = Failure("docker could not start: No such file or directory")
self.assertEqual(main.planted(self.rust_bench({"src/lib.rs": self.ADD, "src/tests.rs": ""}, broke), 1, self.ADDING), broke)
def test_a_rust_mutant_in_a_tree_whose_rust_file_cannot_be_read_fails_the_bench_naming_the_file(self):
assert_not_root(self)
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: ")
class ContainersTest(unittest.TestCase):
IN_TREE = ["--memory", "512m", "--memory-swap", "512m", "--user", "1000:1000", "--env", "HOME=/tmp", "--volume", "/tree:/work", "--workdir", "/work"]
@@ -772,6 +800,55 @@ class CheckedTreeTest(unittest.TestCase):
checked_tree(planted, copy, str(self.root / "check"))
self.assertEqual(self.held("check")["src/new.rs"], added)
DECLARING = "fn a() { 1 }\n#[cfg(test)]\nmod tests;\n"
def test_takes_a_rust_test_in_a_file_of_its_own_as_planted_and_the_rest_as_fixed(self):
planted = self.tree("planted", {"src/lib.rs": self.DECLARING, "src/tests.rs": "old tests", "src/other.rs": "bug"})
copy = self.tree("copy", {"src/lib.rs": "fn a() { 2 }\n#[cfg(test)]\nmod tests;\n", "src/tests.rs": "rewritten", "src/other.rs": "fixed"})
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_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"})
checked_tree(planted, copy, str(self.root / "check"))
self.assertEqual(self.held("check")["src/tests.rs"], "old tests")
def test_takes_a_rust_test_file_the_fix_added_as_fixed_since_the_planted_tree_declares_none(self):
planted = self.tree("planted", {"src/lib.rs": "fn a() { 1 }\n"})
copy = self.tree("copy", {"src/lib.rs": "fn a() { 2 }\n", "src/tests.rs": "mine"})
checked_tree(planted, copy, str(self.root / "check"))
self.assertEqual(self.held("check")["src/tests.rs"], "mine")
def test_a_task_with_acceptance_tests_takes_a_rust_test_file_as_fixed(self):
planted = self.tree("planted", {"src/lib.rs": self.DECLARING, "src/tests.rs": "old tests"})
copy = self.tree("copy", {"src/lib.rs": self.DECLARING, "src/tests.rs": "new tests"})
checked_tree(planted, copy, str(self.root / "check"), rust_tests_planted=False)
self.assertEqual(self.held("check")["src/tests.rs"], "new tests")
def test_finds_a_rust_test_file_where_the_module_system_puts_it(self):
cases = {
"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 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()),
}
for name, (files, found) in cases.items():
with self.subTest(name):
self.assertEqual(rust_test_files(self.tree(name.replace(" ", "-"), files)), found)
def test_cannot_find_a_rust_test_file_that_is_not_where_its_declaration_puts_it_or_cannot_be_read_from_it(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": ""},
"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():
with self.subTest(name):
self.assertIsNone(rust_test_files(self.tree(name.replace(" ", "-"), files)))
def test_leaves_a_rust_link_as_a_link_whatever_it_points_at(self):
planted = self.tree("planted", {"z.rs": "fn a() {}\n#[cfg(test)]\nmod tests {}\n", "a.txt": "text"})
copy = self.tree("copy", {"a.txt": "text"})