Parse each mutants/ copy once when reporting mutmut's mutants, and kill more survivors with tests of by_folder, the in-diff tools' folder and their start failures
This commit is contained in:
@@ -43,17 +43,18 @@ else:
|
||||
print("Stopping early, because we could not find any test case for any mutant.")
|
||||
sys.exit(1)
|
||||
"""
|
||||
# cargo-mutants, Infection and Stryker, which log how they were called and exit with the code the test gives. cargo-mutants also
|
||||
# logs the diff it is given and writes its outcome files, all but unviable.txt, as a run without an unviable mutant does.
|
||||
FAKE_TOOLS = r"""import pathlib, sys
|
||||
# cargo-mutants, Infection and Stryker, which log the folder they run in and how they were called, and exit with the code the test
|
||||
# gives. cargo-mutants also logs the diff it is given and writes its outcome files, all but unviable.txt, as a run without an
|
||||
# unviable mutant does.
|
||||
FAKE_TOOLS = r"""import os, pathlib, sys
|
||||
log, code, args = pathlib.Path(sys.argv[1]), int(sys.argv[2]), sys.argv[3:]
|
||||
with log.open("a") as written:
|
||||
written.write(" ".join(args) + "\n")
|
||||
written.write(f"{os.getcwd()}\n{' '.join(args)}\n")
|
||||
if args[0] == "cargo":
|
||||
written.write(pathlib.Path(args[args.index("--in-diff") + 1]).read_text())
|
||||
outcomes = pathlib.Path(args[args.index("--output") + 1], "mutants.out")
|
||||
outcomes.mkdir()
|
||||
(outcomes / "caught.txt").write_text("src/lib.rs:2:7: replace + with - in add\n")
|
||||
(outcomes / "caught.txt").write_bytes(b"src/lib.rs:2:7: replace + with - in add, \xff not being UTF-8\n")
|
||||
(outcomes / "missed.txt").write_text("src/lib.rs:2:5: replace add -> i32 with 0\n")
|
||||
(outcomes / "timeout.txt").write_text("src/lib.rs:2:7: replace + with * in add\n")
|
||||
print(f"{args[0]} reports")
|
||||
@@ -129,15 +130,24 @@ class MutateChangesRuns(unittest.TestCase):
|
||||
return ["--prefix", f"python3 {self.temp / 'fake_mutmut.py'} {self.log} {mode}"]
|
||||
|
||||
def change_rust_php_and_typescript(self):
|
||||
"""Commit a Rust, a PHP and a TypeScript file, then change line 2 of each in the working tree, PHP's with a byte that is not UTF-8."""
|
||||
"""Commit a Rust, a PHP and a TypeScript file, then change them: Rust's line 2 in the index, PHP's line 2 with a byte that is
|
||||
not UTF-8, and TypeScript's lines 1 and 4."""
|
||||
(self.checkout / "src").mkdir()
|
||||
(self.checkout / "src" / "lib.rs").write_text("pub fn add(a: i32, b: i32) -> i32 {\n\ta + b\n}\n")
|
||||
(self.checkout / "src" / "Calc.php").write_text("<?php\nfunction add($a, $b) { return $a + $b; }\n")
|
||||
(self.checkout / "src" / "calc.ts").write_text("export function add(a: number, b: number) {\n\treturn a + b;\n}\n")
|
||||
(self.checkout / "src" / "calc.ts").write_text("export function add(a: number, b: number) {\n\treturn a + b;\n}\nexport const one = 1;\n")
|
||||
self.commit()
|
||||
(self.checkout / "src" / "lib.rs").write_text("pub fn add(a: i32, b: i32) -> i32 {\n\tb + a\n}\n")
|
||||
self.git("add", "src/lib.rs")
|
||||
(self.checkout / "src" / "Calc.php").write_bytes(b"<?php\nfunction add($a, $b) { return $b + $a; } // caf\xe9\n")
|
||||
(self.checkout / "src" / "calc.ts").write_text("export function add(a: number, b: number) {\n\treturn b + a;\n}\n")
|
||||
(self.checkout / "src" / "calc.ts").write_text("export function add(b: number, a: number) {\n\treturn a + b;\n}\nexport const one = 2;\n")
|
||||
|
||||
def path_with_git(self):
|
||||
"""A PATH that finds git and no mutation tool."""
|
||||
tools = self.temp / "tools"
|
||||
tools.mkdir()
|
||||
(tools / "git").symlink_to(shutil.which("git"))
|
||||
return tools
|
||||
|
||||
def test_prints_each_mutant_on_a_changed_line_that_its_tests_do_not_kill(self):
|
||||
ran = self.run_tool(*self.faked("passes"), "--jobs", "2")
|
||||
@@ -197,9 +207,7 @@ class MutateChangesRuns(unittest.TestCase):
|
||||
self.assertEqual(ran.stderr, "mutmut run in pkg exited 1:\n")
|
||||
|
||||
def test_without_a_prefix_each_tool_runs_from_path(self):
|
||||
tools = self.temp / "tools"
|
||||
tools.mkdir()
|
||||
(tools / "git").symlink_to(shutil.which("git"))
|
||||
tools = self.path_with_git()
|
||||
ran = self.run_tool(env={"PATH": str(tools)})
|
||||
self.assertEqual(ran.returncode, 1)
|
||||
self.assertRegex(ran.stderr, "^mutmut run .* could not start: ")
|
||||
@@ -208,18 +216,30 @@ class MutateChangesRuns(unittest.TestCase):
|
||||
ran = self.run_tool(env={"PATH": str(tools)})
|
||||
self.assertEqual((ran.returncode, ran.stdout.splitlines()[:2]), (0, PASSED), ran.stderr)
|
||||
|
||||
def test_rust_php_and_typescript_go_to_cargo_mutants_infection_and_stryker(self):
|
||||
def test_rust_php_and_typescript_go_to_cargo_mutants_infection_and_stryker_at_the_checkouts_top(self):
|
||||
self.change_rust_php_and_typescript()
|
||||
ran = self.run_tool("--base", "HEAD", "--jobs=3", "--prefix", f"python3 {self.temp / 'fake_tools.py'} {self.log} 2")
|
||||
ran = self.run_tool("--base", "HEAD", "--jobs=3", "--prefix", f"python3 {self.temp / 'fake_tools.py'} {self.log} 2", cwd=self.checkout / "src")
|
||||
report = ["cargo-mutants: 3 mutants on changed lines, 2 not caught.", "src/lib.rs:2:5: missed: replace add -> i32 with 0", "src/lib.rs:2:7: timeout: replace + with * in add"]
|
||||
self.assertEqual((ran.returncode, ran.stdout.splitlines()[:-1]), (0, [*report, "Infection exited 2; its report:", "vendor/bin/infection reports", "Stryker exited 2; its report:", "npx reports"]), ran.stderr)
|
||||
calls = self.log.read_text().splitlines()
|
||||
self.assertRegex(calls[0], r"^cargo mutants --in-diff (\.mutate-changes-\w+)/branch\.diff --output \1 --jobs 3$")
|
||||
self.assertEqual([line for line in calls[1:-2] if line[:1] in "+-"], ["--- a/src/lib.rs", "+++ b/src/lib.rs", "-\ta + b", "+\tb + a"])
|
||||
infection = f"vendor/bin/infection --git-diff-lines --git-diff-base={self.git('rev-parse', 'HEAD').strip()} --threads=3 --show-mutations=max --no-progress"
|
||||
self.assertEqual(calls[-2:], [infection, "npx stryker run --mutate src/calc.ts:2-2 --concurrency 3 --reporters clear-text"])
|
||||
cargo, infection, stryker = self.log.read_text().split(f"{self.checkout.resolve()}\n")[1:]
|
||||
command, diff = cargo.split("\n", 1)
|
||||
self.assertRegex(command, r"^cargo mutants --in-diff (\.mutate-changes-\w+)/branch\.diff --output \1 --jobs 3$")
|
||||
self.assertEqual([line for line in diff.splitlines() if line[:1] in "+-"], ["--- a/src/lib.rs", "+++ b/src/lib.rs", "-\ta + b", "+\tb + a"])
|
||||
self.assertEqual(infection, f"vendor/bin/infection --git-diff-lines --git-diff-base={self.git('rev-parse', 'HEAD').strip()} --threads=3 --show-mutations=max --no-progress\n")
|
||||
self.assertEqual(stryker, "npx stryker run --mutate src/calc.ts:1-1,src/calc.ts:4-4 --concurrency 3 --reporters clear-text\n")
|
||||
self.assertEqual(sorted(path.name for path in self.checkout.iterdir()), [".git", "README.md", "pkg", "src"])
|
||||
|
||||
def test_a_tool_that_cannot_start_fails_the_run(self):
|
||||
self.change_rust_php_and_typescript()
|
||||
path = str(self.path_with_git())
|
||||
# The tools run in the order of their names, so undoing the change of the one that failed lets the next one run.
|
||||
for tool, undone in (("cargo mutants", "src/lib.rs"), ("vendor/bin/infection", "src/Calc.php"), ("npx stryker", "src/calc.ts")):
|
||||
with self.subTest(tool):
|
||||
ran = self.run_tool("--base", "HEAD", env={"PATH": path})
|
||||
self.assertEqual(ran.returncode, 1)
|
||||
self.assertRegex(ran.stderr, f"^{re.escape(tool)} .* could not start: ")
|
||||
self.git("checkout", "HEAD", "--", undone)
|
||||
|
||||
def test_cargo_mutants_fails_the_run_unless_it_finished(self):
|
||||
self.change_rust_php_and_typescript()
|
||||
for code, failed in ((0, False), (1, True), (3, False), (4, True)):
|
||||
|
||||
Reference in New Issue
Block a user