diff --git a/bin/mutate_changes/in_diff_tools.py b/bin/mutate_changes/in_diff_tools.py index 7717ca75..f282aaad 100644 --- a/bin/mutate_changes/in_diff_tools.py +++ b/bin/mutate_changes/in_diff_tools.py @@ -45,18 +45,24 @@ def outcome_lines(path: pathlib.Path) -> list[str]: # Stryker and Infection also exit nonzero on a mutation score below a threshold the project sets, which their exit code doesn't # tell from a run that broke, so either fails the run. def run_stryker(top: str, changed: dict[str, frozenset[int]], prefix: str | None, jobs: int) -> list[str] | Failure: - """For each folder Stryker runs in, a line naming it, then Stryker's own report of the mutants on the changed lines. + """For each folder Stryker runs in, a line naming it, then Stryker's own report of the mutants on the changed lines. Last, a line + naming the files with no package.json above them, if any. - Stryker runs in the nearest folder above each file that holds a package.json, else in top. Through `npx --no`, it runs only where - the project installed it.""" + Stryker runs in the nearest folder above each file that holds a package.json. Through `npx --no`, it runs only where the project + installed it.""" report: list[str] = [] + unplaced: list[str] = [] for folder, files in sorted(by_folder(top, changed, ("package.json",)).items()): + # by_folder falls back to top, where no package.json means there is no Stryker to run. + if not os.path.isfile(os.path.join(top, folder, "package.json")): + unplaced = sorted(files) + continue mutate = ",".join(f"{path}:{first}-{last}" for path, lines in sorted(files.items()) for first, last in runs(lines)) ran = run_tool(["npx", "--no", "stryker", "run", "--mutate", mutate, "--concurrency", str(jobs), "--reporters", "clear-text"], os.path.join(top, folder), prefix) if isinstance(ran, Failure) or ran.code: return ran if isinstance(ran, Failure) else failed(f"Stryker in {label(folder)}", ran) report.extend([f"Stryker in {label(folder)}:", *ran.output.splitlines()]) - return report + return [*report, *([f"Not mutated, as no package.json is above them: {', '.join(unplaced)}."] if unplaced else [])] def run_infection(top: str, base: str, prefix: str | None, jobs: int) -> list[str] | Failure: diff --git a/bin/mutate_changes/main.py b/bin/mutate_changes/main.py index e83bcae8..42691b02 100644 --- a/bin/mutate_changes/main.py +++ b/bin/mutate_changes/main.py @@ -14,8 +14,8 @@ or __tests__/ folder. process reaches, such as a tool a test starts, shows as `no tests`. A pyproject.toml with a [tool.mutmut] section fails the run. - .rs: cargo-mutants, as `cargo mutants --in-diff`, in the checkout's top. - .cjs, .cts, .js, .jsx, .mjs, .mts, .ts, .tsx: Stryker, as `npx --no stryker run --mutate` on the changed lines, in the nearest - folder above the file that holds a package.json, else in the checkout's top. Through --no, npx runs only a Stryker the project - installed. + folder above the file that holds a package.json. A file with no package.json above it isn't mutated. Through --no, npx runs only + a Stryker the project installed. - .php: Infection, as `vendor/bin/infection --git-diff-lines`, in the checkout's top. --prefix is shell text that runs each tool: sh runs ` "$@"` in the tool's folder, which $PWD names, with the tool's command @@ -33,6 +33,7 @@ It prints: - for cargo-mutants, a line with the mutant count, then a line per mutant it missed or that timed out, as `::: : `; - for Stryker and Infection, a line naming the folder the tool ran in, then the tool's own report; +- a line naming the changed JavaScript and TypeScript files with no package.json above them; - a line naming the changed files in a language no tool covers, then one naming the changed test files, none of which it mutates; - last, the wall time. diff --git a/bin/mutate_changes/test_mutate_changes_runs.py b/bin/mutate_changes/test_mutate_changes_runs.py index c00ce2b6..308c26a5 100644 --- a/bin/mutate_changes/test_mutate_changes_runs.py +++ b/bin/mutate_changes/test_mutate_changes_runs.py @@ -149,9 +149,10 @@ class MutateChangesRuns(unittest.TestCase): def change_rust_php_and_typescript(self): """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. Also change line 1 of a TypeScript file in web/, which holds a package.json.""" + not UTF-8, and TypeScript's lines 1 and 4. Also change line 1 of a TypeScript file in web/. The top and web/ each hold a package.json.""" (self.checkout / "src").mkdir() (self.checkout / "web" / "src").mkdir(parents=True) + (self.checkout / "package.json").write_text("{}\n") (self.checkout / "web" / "package.json").write_text("{}\n") (self.checkout / "web" / "src" / "app.ts").write_text("export const two = 2;\n") (self.checkout / "src" / "lib.rs").write_text("pub fn add(a: i32, b: i32) -> i32 {\n\ta + b\n}\n") @@ -329,7 +330,16 @@ class MutateChangesRuns(unittest.TestCase): self.assertEqual(infection, f"\nvendor/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, "\nnpx --no stryker run --mutate src/calc.ts:1-1,src/calc.ts:4-4 --concurrency 3 --reporters clear-text\n") self.assertEqual(web, "/web\nnpx --no stryker run --mutate src/app.ts:1-1 --concurrency 3 --reporters clear-text\n") - self.assertEqual(sorted(path.name for path in self.checkout.iterdir()), [".git", "README.md", "pkg", "src", "web"]) + self.assertEqual(sorted(path.name for path in self.checkout.iterdir()), [".git", "README.md", "package.json", "pkg", "src", "web"]) + + def test_a_javascript_file_with_no_package_json_above_it_is_named_and_not_mutated(self): + (self.checkout / "presentation").mkdir() + (self.checkout / "presentation" / "render.js").write_text("export const one = 1;\n") + self.git("add", "presentation/render.js") + ran = self.run_tool(*self.faked("passes")) + skipped = "Not mutated, as no package.json is above them: presentation/render.js." + self.assertEqual((ran.returncode, ran.stdout.splitlines()[:-1], ran.stderr), (0, [*PASSED, skipped, *NOT_MUTATED], "")) + self.assertNotIn("npx", self.log.read_text()) def test_a_stryker_or_infection_run_that_exits_nonzero_fails_the_run(self): self.change_rust_php_and_typescript() @@ -376,7 +386,7 @@ class MutateChangesRuns(unittest.TestCase): ran = self.run_tool("--base", "HEAD", "--prefix", f"python3 {self.temp / 'fake_tools.py'} {self.log} 0", env={"CAUGHT_UNREADABLE": "1"}) self.assertEqual(ran.returncode, 1) self.assertRegex(ran.stderr, rf"^could not hand cargo-mutants the diff or read its outcomes in {re.escape(str(self.checkout.resolve()))}/\.mutate-changes-\w+: ") - self.assertEqual(sorted(path.name for path in self.checkout.iterdir()), [".git", "README.md", "pkg", "src", "web"]) + self.assertEqual(sorted(path.name for path in self.checkout.iterdir()), [".git", "README.md", "package.json", "pkg", "src", "web"]) def test_a_branch_that_adds_no_line_a_tool_mutates_says_so_and_names_what_it_changes(self): # A file the branch deletes is no longer there to mutate, so the report doesn't name it.