Tell a Rust file's test modules apart as bytes and kill the surviving mutants
CI / tests (pull_request) Failing after 5m15s

This commit is contained in:
2026-11-26 00:00:33 +01:00
parent c794073312
commit dd15690903
5 changed files with 106 additions and 54 deletions
+44 -2
View File
@@ -6,7 +6,8 @@ import unittest
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from task_bench.rust_test_modules import modules, with_tests_of
from task_bench import rust_test_modules
from task_bench.rust_test_modules import tells_apart
LIB = textwrap.dedent("""\
pub fn add(a: i32, b: i32) -> i32 {
@@ -25,9 +26,17 @@ LIB = textwrap.dedent("""\
""")
def modules(source: str) -> dict[bytes, tuple[int, int]] | None:
return rust_test_modules.modules(source.encode())
def with_tests_of(planted: str, copy: str) -> str:
return rust_test_modules.with_tests_of(planted.encode(), copy.encode()).decode()
def spelt(source: str) -> dict[str, str] | None:
found = modules(source)
return None if found is None else {name: source[start:end] for name, (start, end) in found.items()}
return None if found is None else {name.decode(): source[start:end] for name, (start, end) in found.items()}
class ModulesTest(unittest.TestCase):
@@ -64,6 +73,26 @@ class ModulesTest(unittest.TestCase):
""")
self.assertEqual(spelt(source), {"tests": source[:source.index("}\nfn after")+1]})
def test_reads_a_string_that_ends_in_backslashes_or_in_an_escaped_quote(self):
source = textwrap.dedent("""\
#[cfg(test)]
mod tests {
const A: &str = r"\\";
const B: &str = "\\\\";
const C: &str = "a\\"";
const D: &str = "\\}\\}";
}
fn after() {}
""")
self.assertEqual(spelt(source), {"tests": source[:source.index("}\nfn after") + 1]})
def test_leaves_out_a_module_a_comment_or_a_char_stands_before(self):
self.assertEqual(modules("// helpers\nmod util {}\nfn a() { let c = 'x'; }\nmod more {}\n"), {})
def test_lists_a_test_module_inside_a_module_that_is_no_test(self):
source = "mod outer {\n #[cfg(test)]\n mod inner {}\n fn after() {}\n}\n"
self.assertEqual(spelt(source), {"inner": "#[cfg(test)]\n mod inner {}"})
def test_cannot_tell_the_modules_apart_where_two_hold_one_name(self):
self.assertIsNone(modules("#[cfg(test)]\nmod tests {}\nfn a() { #[cfg(test)] mod tests {} }\n"))
@@ -73,6 +102,14 @@ class ModulesTest(unittest.TestCase):
self.assertIsNone(modules(source))
class TellsApartTest(unittest.TestCase):
def test_tells_a_rust_file_apart_where_its_modules_can_be(self):
self.assertEqual([tells_apart("lib.rs", b"fn a() {}"), tells_apart("lib.rs", b"fn a() {")], [True, False])
def test_takes_a_file_of_another_language_whatever_it_holds(self):
self.assertTrue(tells_apart("calc.go", b"func a() {"))
class WithTestsOfTest(unittest.TestCase):
def test_takes_the_code_as_fixed_and_the_tests_as_planted(self):
planted = LIB.replace("a + b", "a - b")
@@ -94,6 +131,11 @@ class WithTestsOfTest(unittest.TestCase):
fixed = "fn a() { 2 }\n#[cfg(test)]\nmod y { fn changed() {} }\n#[cfg(test)]\nmod x { fn changed() {} }\nfn c() {}\n"
self.assertEqual(with_tests_of(planted, fixed), "fn a() { 2 }\n#[cfg(test)]\nmod y { fn u() {} }\n#[cfg(test)]\nmod x { fn t() {} }\nfn c() {}\n")
def test_puts_the_planted_modules_in_where_their_names_sort_in_file_order(self):
planted = "#[cfg(test)]\nmod a { 1 }\n#[cfg(test)]\nmod b { 22 }\nfn code() {}\n"
fixed = "#[cfg(test)]\nmod a { 333 }\n#[cfg(test)]\nmod b { 4444444 }\nfn fixed() {}\n"
self.assertEqual(with_tests_of(planted, fixed), "#[cfg(test)]\nmod a { 1 }\n#[cfg(test)]\nmod b { 22 }\nfn fixed() {}\n")
def test_takes_the_file_as_fixed_where_the_planted_modules_cannot_be_told_apart(self):
self.assertEqual(with_tests_of("#[cfg(test)]\nmod tests {\n", "fixed {}"), "fixed {}")