Answer stability round 1: 3 🔴, 5 🟡 (0 misread); run only a Stryker the project installed, tell mutmut's setup fault from no test, pin cargo-mutants' diff prefixes, keep -x, end the tool and report its scratch folder on SIGTERM, and test option-like names; with product-owner's and prose's code fixes
This commit is contained in:
@@ -1,7 +1,14 @@
|
||||
"""Start a mutation tool, through --prefix's shell text where the run gives one, and wait for it."""
|
||||
"""Start a mutation tool, through --prefix's shell text where the run gives one, and wait for it; and make and remove its scratch folder."""
|
||||
import contextlib
|
||||
import dataclasses
|
||||
import os
|
||||
import pathlib
|
||||
import shlex
|
||||
import shutil
|
||||
import signal
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
|
||||
from failure import Failure
|
||||
|
||||
@@ -18,12 +25,39 @@ def run_tool(argv: list[str], cwd: str, prefix: str | None) -> Ran | Failure:
|
||||
With a prefix, sh runs `<prefix> "$@"` with argv as its arguments, so the prefix keeps its own quoting and each of argv stays one word."""
|
||||
command = ["sh", "-c", f'{prefix} "$@"', "sh", *argv] if prefix else argv
|
||||
try:
|
||||
ran = subprocess.run(command, cwd=cwd, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, encoding="utf-8", errors="replace")
|
||||
# In a session of its own, the tool and each process it starts share one process group, which ends whole below.
|
||||
process = subprocess.Popen(command, cwd=cwd, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, encoding="utf-8", errors="replace", start_new_session=True)
|
||||
except OSError as failure:
|
||||
return Failure(f"{shlex.join(argv)} could not start: {failure.strerror}")
|
||||
return Ran(ran.returncode, ran.stdout)
|
||||
try:
|
||||
output = process.communicate()[0]
|
||||
except BaseException:
|
||||
# The run is ending, as on Ctrl-C or the SIGTERM the launcher turns into SystemExit, so end what the tool started too.
|
||||
with contextlib.suppress(ProcessLookupError):
|
||||
os.killpg(process.pid, signal.SIGKILL)
|
||||
process.wait()
|
||||
raise
|
||||
return Ran(process.returncode, output)
|
||||
|
||||
|
||||
def failed(name: str, ran: Ran) -> Failure:
|
||||
"""A tool's exit code and the last 40 lines of its output, which hold its error."""
|
||||
return Failure("\n".join([f"{name} exited {ran.code}:", *ran.output.splitlines()[-40:]]))
|
||||
|
||||
|
||||
def scratch_folder(top: str) -> str | Failure:
|
||||
"""A new folder in top for one tool's files. It holds a .gitignore of `*`, as pytest's cache folder does, so git lists none of them."""
|
||||
try:
|
||||
scratch = tempfile.mkdtemp(prefix=".mutate-changes-", dir=top)
|
||||
pathlib.Path(scratch, ".gitignore").write_text("*\n", encoding="utf-8")
|
||||
except OSError as failure:
|
||||
return Failure(f"could not make a scratch folder in {top}: {failure}")
|
||||
return scratch
|
||||
|
||||
|
||||
def remove(scratch: str) -> None:
|
||||
"""Remove a scratch folder, saying on stderr where it can't: the run's report holds, so the run doesn't fail."""
|
||||
try:
|
||||
shutil.rmtree(scratch)
|
||||
except OSError as failure:
|
||||
print(f"could not remove {scratch}: {failure}", file=sys.stderr)
|
||||
|
||||
Reference in New Issue
Block a user