Answer stability round 1: 0 🔴, 5 🟡 (0 misread); run both tools in a scratch copy, read Infection's two sources, end PHPUnit's list, spell glob characters
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
"""task-bench's PHP planter: which files Infection mutates, how its log's diffs become mutants and what PHPUnit's output tells Haiku.
|
||||
"""task-bench's PHP planter: which files Infection mutates, how its log's mutants become lines and what PHPUnit's output tells Haiku.
|
||||
|
||||
PICK's diffs and FAILING are what Infection 0.35.12 and PHPUnit 12.5.45 wrote for a small project on PHP 8.5.13, and each logged
|
||||
mutant holds the keys the planter reads. The other diffs and outputs are made up in the same form."""
|
||||
The four mutants of PICK and FAILING are what Infection 0.35.12 and PHPUnit 12.5.45 wrote for a small project on PHP 8.5.13. Each
|
||||
logged mutant is cut down to the keys the planter reads. The other mutants and outputs are made up in the same form."""
|
||||
import json
|
||||
import pathlib
|
||||
import sys
|
||||
@@ -42,29 +42,25 @@ final class Pick
|
||||
}
|
||||
"""
|
||||
LINES = PICK.split("\n")
|
||||
OPERATOR_DIFF = "@@ @@\n \t{\n \t\t$sum = 0;\n \t\tforeach ($items as $item) {\n-\t\t\tif ($item >= $floor) {\n+\t\t\tif ($item > $floor) {\n \t\t\t\t$sum += $item;\n \t\t\t}\n \t\t}"
|
||||
|
||||
|
||||
def with_lines(first: int, count: int, *now: str) -> str:
|
||||
"""PICK with `count` lines from line `first` on replaced by `now`: the mutated source Infection logged for such a mutant."""
|
||||
return "\n".join([*LINES[:first - 1], *now, *LINES[first - 1 + count:]])
|
||||
|
||||
|
||||
OPERATOR = with_lines(12, 1, "\t\t\tif ($item > $floor) {")
|
||||
# Infection prints the lines of a statement it rewrote with its own indent, and leaves the ones between them as they were.
|
||||
FOREACH_DIFF = (
|
||||
"@@ @@\n \tpublic static function sum(array $items, int $floor): int\n \t{\n \t\t$sum = 0;\n-\t\tforeach ($items as $item) {\n-\t\t\tif ($item >= $floor) {\n"
|
||||
"+\t\tforeach ([] as $item) {\n+ if ($item >= $floor) {\n \t\t\t\t$sum += $item;\n \t\t\t}\n-\t\t}\n+ }\n \t\treturn $sum;\n \t}"
|
||||
)
|
||||
TERNARY_DIFF = (
|
||||
"@@ @@\n\n \tpublic static function label(int $count): string\n \t{\n-\t\treturn $count === 1\n-\t\t\t? 'one item'\n-\t\t\t: $count . ' items';\n"
|
||||
"+\t\treturn $count === 1 ? $count . ' items' : 'one item';\n \t}\n }"
|
||||
)
|
||||
# An empty line of the file stands in the diff without the space that opens an unchanged line.
|
||||
VISIBILITY_DIFF = (
|
||||
"@@ @@\n \t\treturn $sum;\n \t}\n\n-\tpublic static function label(int $count): string\n+\tprotected static function label(int $count): string\n"
|
||||
" \t{\n \t\treturn $count === 1\n \t\t\t? 'one item'"
|
||||
)
|
||||
FOREACH = with_lines(11, 5, "\t\tforeach ([] as $item) {", " if ($item >= $floor) {", "\t\t\t\t$sum += $item;", "\t\t\t}", " }")
|
||||
TERNARY = with_lines(21, 3, "\t\treturn $count === 1 ? $count . ' items' : 'one item';")
|
||||
VISIBILITY = with_lines(19, 1, "\tprotected static function label(int $count): string")
|
||||
OPERATOR_NOW = (b"\t\t\tif ($item > $floor) {",)
|
||||
|
||||
|
||||
def logged(mutator: str, line: int, diff: str, path: str = "/w/src/Pick.php", source: str = PICK) -> dict:
|
||||
return {"mutator": {"mutatorName": mutator, "originalSourceCode": source, "originalFilePath": path, "originalStartLine": line}, "diff": diff}
|
||||
def logged(mutator: str, line: int, mutated_source: str, path: str = "/w/src/Pick.php", source: str = PICK) -> dict:
|
||||
return {"mutator": {"mutatorName": mutator, "originalSourceCode": source, "mutatedSourceCode": mutated_source, "originalFilePath": path, "originalStartLine": line}}
|
||||
|
||||
|
||||
# Three failed tests.
|
||||
FAILING = """PHPUnit 12.5.45 by Sebastian Bergmann and contributors.
|
||||
|
||||
Runtime: PHP 8.5.13
|
||||
@@ -116,45 +112,36 @@ def failing(test: str, *lines: str) -> str:
|
||||
|
||||
class ChangedTest(unittest.TestCase):
|
||||
def test_is_the_line_an_operator_mutant_changes(self):
|
||||
self.assertEqual(changed(OPERATOR_DIFF, LINES, 12), (12, (b"\t\t\tif ($item >= $floor) {",), OPERATOR_NOW))
|
||||
self.assertEqual(changed(PICK, OPERATOR), (12, (b"\t\t\tif ($item >= $floor) {",), OPERATOR_NOW))
|
||||
|
||||
def test_is_every_line_from_the_first_changed_one_to_the_last(self):
|
||||
was = tuple(line.encode() for line in LINES[10:15])
|
||||
now = (b"\t\tforeach ([] as $item) {", b" if ($item >= $floor) {", b"\t\t\t\t$sum += $item;", b"\t\t\t}", b" }")
|
||||
self.assertEqual(changed(FOREACH_DIFF, LINES, 11), (11, was, now))
|
||||
self.assertEqual(changed(PICK, FOREACH), (11, was, now))
|
||||
|
||||
def test_a_mutant_may_have_fewer_lines_than_it_replaces(self):
|
||||
was = (b"\t\treturn $count === 1", b"\t\t\t? 'one item'", b"\t\t\t: $count . ' items';")
|
||||
self.assertEqual(changed(TERNARY_DIFF, LINES, 21), (21, was, (b"\t\treturn $count === 1 ? $count . ' items' : 'one item';",)))
|
||||
self.assertEqual(changed(PICK, TERNARY), (21, was, (b"\t\treturn $count === 1 ? $count . ' items' : 'one item';",)))
|
||||
|
||||
def test_reads_an_empty_line_of_the_diff_as_an_unchanged_empty_line(self):
|
||||
self.assertEqual(changed(VISIBILITY_DIFF, LINES, 19), (19, (b"\tpublic static function label(int $count): string",), (b"\tprotected static function label(int $count): string",)))
|
||||
def test_a_mutant_may_only_remove_a_line_or_only_add_one(self):
|
||||
self.assertEqual(changed("a\nb\nc", "a\nc"), (2, (b"b",), ()))
|
||||
self.assertEqual(changed("a\nc", "a\nb\nc"), (2, (), (b"b",)))
|
||||
|
||||
def test_finds_the_lines_around_the_mutants_start_line_where_the_file_holds_them_twice(self):
|
||||
twice = ["\t\t$a = 1;", "\t\treturn $a >= 2;", "\t}", "", "\t\t$a = 1;", "\t\treturn $a >= 2;", "\t}"]
|
||||
diff = "@@ @@\n \t\t$a = 1;\n-\t\treturn $a >= 2;\n+\t\treturn $a > 2;\n \t}"
|
||||
self.assertEqual(changed(diff, twice, 2), (2, (b"\t\treturn $a >= 2;",), (b"\t\treturn $a > 2;",)))
|
||||
self.assertEqual(changed(diff, twice, 6), (6, (b"\t\treturn $a >= 2;",), (b"\t\treturn $a > 2;",)))
|
||||
def test_a_line_counts_as_unchanged_before_the_change_or_after_it_never_both(self):
|
||||
self.assertEqual(changed("a\nb\nb", "a\nb"), (3, (b"b",), ()))
|
||||
self.assertEqual(changed("b\nb\nc", "b\nc"), (2, (b"b",), ()))
|
||||
|
||||
def test_is_none_where_the_diffs_lines_do_not_stand_around_the_start_line(self):
|
||||
self.assertIsNone(changed(OPERATOR_DIFF, LINES, 20))
|
||||
self.assertIsNone(changed(OPERATOR_DIFF.replace("$sum = 0", "$sum = 1"), LINES, 12))
|
||||
self.assertIsNone(changed(OPERATOR_DIFF, LINES, 0))
|
||||
def test_reaches_the_first_line_and_the_last(self):
|
||||
self.assertEqual(changed("a\nb", "c\nb"), (1, (b"a",), (b"c",)))
|
||||
self.assertEqual(changed("a\nb", "a\nc"), (2, (b"b",), (b"c",)))
|
||||
|
||||
def test_is_none_where_the_diff_holds_no_change_or_no_hunk(self):
|
||||
self.assertIsNone(changed("@@ @@\n \t{\n \t\t$sum = 0;", LINES, 10))
|
||||
self.assertIsNone(changed("", LINES, 10))
|
||||
|
||||
def test_is_none_where_the_diff_holds_a_second_hunk(self):
|
||||
self.assertIsNone(changed(OPERATOR_DIFF + "\n@@ @@\n \t\t\t}\n-\t\t}\n+\t\t};\n \t\treturn $sum;", LINES, 15))
|
||||
def test_is_none_where_the_mutant_changes_no_line(self):
|
||||
self.assertIsNone(changed(PICK, PICK))
|
||||
|
||||
def test_what_it_returns_plants_as_the_mutant(self):
|
||||
for diff, start in ((OPERATOR_DIFF, 12), (FOREACH_DIFF, 11), (TERNARY_DIFF, 21), (VISIBILITY_DIFF, 19)):
|
||||
with self.subTest(start=start):
|
||||
line, was, now = changed(diff, LINES, start)
|
||||
planted = mutated(PICK.encode(), Mutant("src/Pick.php", line, "a name", was, now))
|
||||
self.assertEqual(planted.split(b"\n")[line - 1], now[0])
|
||||
self.assertEqual(len(planted.split(b"\n")), len(LINES) - len(was) + len(now))
|
||||
for source in (OPERATOR, FOREACH, TERNARY, VISIBILITY):
|
||||
line, was, now = changed(PICK, source)
|
||||
self.assertEqual(mutated(PICK.encode(), Mutant("src/Pick.php", line, "a name", was, now)), source.encode())
|
||||
|
||||
|
||||
class KilledMutantsTest(unittest.TestCase):
|
||||
@@ -162,28 +149,30 @@ class KilledMutantsTest(unittest.TestCase):
|
||||
folder = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(folder.cleanup)
|
||||
self.top = pathlib.Path(folder.name)
|
||||
self.ran: list[tuple[list[str], str]] = []
|
||||
(self.top / "src").mkdir()
|
||||
(self.top / "src/Pick.php").write_text(PICK, encoding="utf-8")
|
||||
self.ran: list[tuple[list[str], str, str]] = []
|
||||
|
||||
def infection(self, log: dict | str | None, code: int = 0):
|
||||
"""A run that writes Infection's log where its --logger-json names, or none where log is None."""
|
||||
"""A run that writes Infection's log where its --logger-json names, or none where log is None, and another log beside it."""
|
||||
def run(argv: list[str], cwd: str) -> Ran:
|
||||
self.ran.append((argv, cwd))
|
||||
self.ran.append((argv, cwd, pathlib.Path(cwd, "src/Pick.php").read_text(encoding="utf-8")))
|
||||
pathlib.Path(cwd, "infection.html").write_text("every mutant", encoding="utf-8")
|
||||
if log is not None:
|
||||
path = next(argument.partition("=")[2] for argument in argv if argument.startswith("--logger-json="))
|
||||
pathlib.Path(cwd, path).write_text(log if isinstance(log, str) else json.dumps(log), encoding="utf-8")
|
||||
return Ran(code, "the tool's last words\n")
|
||||
return run
|
||||
|
||||
def test_runs_infection_over_the_files_at_the_top_with_a_json_log_in_a_scratch_folder(self):
|
||||
def test_runs_infection_over_the_files_in_a_copy_of_the_tree_with_a_json_log_and_no_score_to_reach(self):
|
||||
killed_mutants(str(self.top), ["src/Pick.php", "src/Last.php"], self.infection({"killed": []}), 3)
|
||||
(argv, cwd), = self.ran
|
||||
self.assertEqual(cwd, str(self.top))
|
||||
self.assertEqual(argv[:4], ["vendor/bin/infection", "--filter=src/Pick.php,src/Last.php", "--threads=3", "--no-progress"])
|
||||
self.assertRegex(argv[4], r"^--logger-json=\.mutate-changes-[^/]+/log\.json$")
|
||||
self.assertEqual(len(argv), 5)
|
||||
(argv, cwd, source), = self.ran
|
||||
self.assertRegex(cwd, rf"^{self.top}/\.mutate-changes-[^/]+/copy$")
|
||||
self.assertEqual(source, PICK)
|
||||
self.assertEqual(argv, ["vendor/bin/infection", "--filter=src/Pick.php,src/Last.php", "--threads=3", "--no-progress", "--min-msi=0", "--min-covered-msi=0", "--logger-json=infection-log.json"])
|
||||
|
||||
def test_is_each_killed_mutant_as_the_lines_it_changes(self):
|
||||
log = {"killed": [logged("GreaterThanOrEqualTo", 12, OPERATOR_DIFF), logged("Ternary", 21, TERNARY_DIFF)], "escaped": [logged("PublicVisibility", 19, VISIBILITY_DIFF)]}
|
||||
log = {"killed": [logged("GreaterThanOrEqualTo", 12, OPERATOR), logged("Ternary", 21, TERNARY)], "escaped": [logged("PublicVisibility", 19, VISIBILITY)]}
|
||||
found = killed_mutants(str(self.top), ["src/Pick.php"], self.infection(log), 1)
|
||||
ternary = (b"\t\treturn $count === 1", b"\t\t\t? 'one item'", b"\t\t\t: $count . ' items';")
|
||||
self.assertEqual(found, [
|
||||
@@ -191,19 +180,23 @@ class KilledMutantsTest(unittest.TestCase):
|
||||
Mutant("src/Pick.php", 21, "src/Pick.php:21 Ternary", ternary, (b"\t\treturn $count === 1 ? $count . ' items' : 'one item';",)),
|
||||
])
|
||||
|
||||
def test_names_a_mutant_by_the_line_infection_logs_for_it(self):
|
||||
found = killed_mutants(str(self.top), ["src/Pick.php"], self.infection({"killed": [logged("Foreach_", 11, FOREACH), logged("Ternary", 20, TERNARY)]}), 1)
|
||||
self.assertEqual([(mutant.line, mutant.name) for mutant in found], [(11, "src/Pick.php:11 Foreach_"), (21, "src/Pick.php:20 Ternary")])
|
||||
|
||||
def test_names_a_file_by_its_path_from_the_top_wherever_infection_saw_the_tree(self):
|
||||
log = {"killed": [logged("GreaterThanOrEqualTo", 12, OPERATOR_DIFF, path) for path in ("/w/src/Pick.php", "src/Pick.php", "/w/lib/src/Pick.php", "/w/src/Other.php", "/w/src/NotPick.php")]}
|
||||
log = {"killed": [logged("GreaterThanOrEqualTo", 12, OPERATOR, path) for path in ("/w/src/Pick.php", "src/Pick.php", "/w/lib/src/Pick.php", "/w/src/Other.php", "/w/src/NotPick.php")]}
|
||||
found = killed_mutants(str(self.top), ["src/Pick.php", "lib/src/Pick.php", "Pick.php"], self.infection(log), 1)
|
||||
self.assertEqual([mutant.path for mutant in found], ["src/Pick.php", "src/Pick.php", "lib/src/Pick.php"])
|
||||
|
||||
def test_leaves_out_a_mutant_whose_diff_does_not_fit_its_source(self):
|
||||
log = {"killed": [logged("GreaterThanOrEqualTo", 12, OPERATOR_DIFF, source="<?php\n")]}
|
||||
def test_leaves_out_a_mutant_that_changes_no_line(self):
|
||||
log = {"killed": [logged("GreaterThanOrEqualTo", 12, PICK)]}
|
||||
self.assertEqual(killed_mutants(str(self.top), ["src/Pick.php"], self.infection(log), 1), [])
|
||||
|
||||
def test_removes_the_scratch_folder(self):
|
||||
def test_leaves_the_tree_as_it_was_whatever_infection_wrote(self):
|
||||
for code in (0, 1):
|
||||
killed_mutants(str(self.top), ["src/Pick.php"], self.infection({"killed": []}, code), 1)
|
||||
self.assertEqual(list(self.top.iterdir()), [])
|
||||
self.assertEqual(sorted(str(path.relative_to(self.top)) for path in self.top.rglob("*")), ["src", "src/Pick.php"])
|
||||
|
||||
def test_fails_where_infection_exits_nonzero(self):
|
||||
found = killed_mutants(str(self.top), ["src/Pick.php"], self.infection({"killed": []}, code=1), 1)
|
||||
@@ -214,11 +207,17 @@ class KilledMutantsTest(unittest.TestCase):
|
||||
self.assertIs(killed_mutants(str(self.top), ["src/Pick.php"], lambda argv, cwd: failure, 1), failure)
|
||||
|
||||
def test_fails_where_infection_wrote_no_log_or_one_it_cannot_read(self):
|
||||
for log in (None, "not json", "{}", '{"killed": [{"diff": ""}]}', '{"killed": [7]}'):
|
||||
unreadable = (None, "not json", "{}", '{"killed": [{}]}', '{"killed": [7]}', json.dumps({"killed": [{"mutator": {**logged("Ternary", 21, TERNARY)["mutator"], "mutatedSourceCode": 7}}]}))
|
||||
for log in unreadable:
|
||||
with self.subTest(log=log):
|
||||
found = killed_mutants(str(self.top), ["src/Pick.php"], self.infection(log), 1)
|
||||
self.assertIsInstance(found, Failure)
|
||||
self.assertIn("could not read Infection's log in ", found.reason)
|
||||
self.assertIn("could not read Infection's log, infection-log.json in ", found.reason)
|
||||
|
||||
def test_fails_where_the_tree_cannot_be_copied(self):
|
||||
found = killed_mutants(str(self.top / "missing"), ["src/Pick.php"], self.infection({"killed": []}), 1)
|
||||
self.assertIsInstance(found, Failure)
|
||||
self.assertEqual(self.ran, [])
|
||||
|
||||
|
||||
class TaskTextTest(unittest.TestCase):
|
||||
@@ -251,6 +250,17 @@ class TaskTextTest(unittest.TestCase):
|
||||
def test_is_none_where_the_message_does_not_say_anything_beyond_the_test(self):
|
||||
self.assertIsNone(task_text(failing("Probe\\Tests\\PickTest::testNamesOne", "testNamesOne failed", "", "/w/tests/PickTest.php:17")))
|
||||
|
||||
def test_reads_no_test_of_another_list_as_a_failed_one(self):
|
||||
named = failing("Probe\\Tests\\PickTest::testSum", "PickTest failed", "", "/w/tests/PickTest.php:9").partition("FAILURES!")[0]
|
||||
for heading in ("There was 1 risky test:", "There were 2 risky tests:", "1 test triggered 1 deprecation:", "2 tests triggered 3 PHPUnit warnings:"):
|
||||
with self.subTest(heading=heading):
|
||||
output = "\n".join([named, "--", "", heading, "", "1) Probe\\Tests\\LastTest::testLabels", "This test did not perform any assertions", "", "/w/tests/LastTest.php:20", "", "FAILURES!"])
|
||||
self.assertIsNone(task_text(output))
|
||||
|
||||
def test_reads_the_failures_listed_after_another_list(self):
|
||||
output = "\n".join(["There was 1 risky test:", "", "1) LastTest::testLabels", "This test did not perform any assertions", "", "--", "", "There was 1 failure:", "", "1) PickTest::testSum", "Failed asserting that 3 is identical to 5.", "", "/w/tests/PickTest.php:9", "", "FAILURES!"])
|
||||
self.assertEqual(task_text(output), "Failed asserting that 3 is identical to 5.")
|
||||
|
||||
def test_takes_the_first_failed_test_that_has_a_message(self):
|
||||
output = "There were 2 failures:\n\n1) PickTest::testAdds\ntestAdds\n\n/w/tests/PickTest.php:12\n\n2) PickTest::testLabels\nno label\n\n/w/tests/PickTest.php:17\n\nFAILURES!\n"
|
||||
self.assertEqual(task_text(output), "no label")
|
||||
|
||||
Reference in New Issue
Block a user