forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

mutrun.py123 lines, 4.7 KiBHistoryRawDownload
1# usage: mutrun.py OUT NAMES_JSON TAG TIMEOUT_SECS FILTER_EXPR [--profile fast]
2# Applies each named mutant's own diff (from OUT/mutants.json) to the lane tree, runs
3# `cargo nextest run -p ec-h265 -E FILTER_EXPR`, restores the file, and records one row per mutant:
4# name <TAB> status <TAB> summary
5# status: KILLED (test failure or timeout), SURVIVED (all pass), UNVIABLE (compile error), NORUN (no test ran).
6# The tree is the lane's synced tree (cwd); only the one mutated file is touched, and it is restored.
7import json, os, subprocess, sys, re
8out, names_file, tag, timeout = sys.argv[1], sys.argv[2], sys.argv[3], int(sys.argv[4])
9expr = sys.argv[5]
10profile = sys.argv[7] if len(sys.argv) > 7 and sys.argv[6] == "--profile" else None
11names = json.load(open(names_file))
12muts = {m["name"]: m for m in json.load(open(f"{out}/mutants.json"))}
13
14def parse(diff):
15 lines = diff.split("\n")
16 fname = lines[0][4:].strip()
17 hunks = []
18 i = 1
19 while i < len(lines):
20 m = re.match(r"@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@", lines[i])
21 if not m:
22 i += 1
23 continue
24 start = int(m.group(1))
25 body = []
26 i += 1
27 while i < len(lines) and not lines[i].startswith("@@"):
28 if lines[i][:1] in (" ", "-", "+"):
29 body.append((lines[i][:1], lines[i][1:]))
30 i += 1
31 hunks.append((start, body))
32 return fname, hunks
33
34def apply(orig, hunks):
35 res, cur = [], 0
36 for start, body in hunks:
37 pos = start - 1
38 res.extend(orig[cur:pos])
39 cur = pos
40 for op, text in body:
41 if op in (" ", "-"):
42 if cur >= len(orig) or orig[cur] != text:
43 raise ValueError(f"context mismatch at line {cur + 1}")
44 if op == " ":
45 res.append(text)
46 cur += 1
47 else:
48 res.append(text)
49 res.extend(orig[cur:])
50 return res
51
52def sh(cmd, tmo):
53 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True,
54 start_new_session=True)
55 try:
56 output, _ = p.communicate(timeout=tmo)
57 return p.returncode, output, False
58 except subprocess.TimeoutExpired:
59 os.killpg(p.pid, 9)
60 output, _ = p.communicate()
61 return -1, output or "", True
62
63BUILD_TO = int(os.environ.get("BUILD_TO", "2400"))
64
65def run_once():
66 # release profile; build first (own generous timeout), then run with the measured-baseline timeout
67 cmd = ["cargo", "nextest", "run", "--release", "-p", "ec-h265", "--no-fail-fast", "-E", expr]
68 if os.environ.get("FAILFAST"):
69 cmd.remove("--no-fail-fast")
70 if profile:
71 cmd[-2:-2] = ["--profile", profile]
72 rc, out, to = sh([c for c in cmd if c != "--no-fail-fast"] + ["--no-run"], BUILD_TO)
73 if to or rc != 0:
74 return rc, out, "BUILDFAIL"
75 return sh(cmd, timeout)
76
77rows = []
78for name in names:
79 m = muts.get(name)
80 if m is None:
81 rows.append((name, "NORUN", "name not in mutants.json"))
82 continue
83 fname, hunks = parse(m["diff"])
84 raw = open(fname, "rb").read()
85 text = raw.decode()
86 orig = text.split("\n")
87 try:
88 new = apply(orig, hunks)
89 except ValueError as e:
90 rows.append((name, "NORUN", f"apply failed: {e}"))
91 continue
92 try:
93 open(fname, "w").write("\n".join(new))
94 rc, output, timed_out = run_once()
95 timed_out = timed_out
96 finally:
97 open(fname, "wb").write(raw)
98 summary = re.findall(r"Summary \[[^\]]*\] (\d+)(?:/\d+)? tests? run", output)
99 ran = int(summary[-1]) if summary else 0
100 if timed_out == "BUILDFAIL":
101 status = "UNVIABLE"
102 elif timed_out:
103 # A timeout is kill evidence only if the test run had started (build time alone is not).
104 status = "KILLED" if re.search(r"Starting \d+ tests?", output) else "TIMEOUT_NOSTART"
105 elif rc != 0 and re.search(r"EC_REQUIRE_FIXTURES|fixtures? (missing|not found)", output):
106 # A fixture-missing failure is not a kill.
107 status = "FIXTURE_FAIL"
108 elif "could not compile" in output or "error[E" in output:
109 status = "UNVIABLE"
110 elif rc == 0 and ran > 0:
111 status = "SURVIVED"
112 elif rc != 0 and ran > 0:
113 status = "KILLED"
114 else:
115 status = "NORUN"
116 err = next((l for l in output.splitlines() if "error[" in l or l.startswith("error")), "") if status == "UNVIABLE" else ""
117 rows.append((name, status, f"rc={rc} ran={ran} timeout={timed_out} {err}"))
118 print(status, name[:90], f"rc={rc} ran={ran}", flush=True)
119
120with open(f"{out}/res-{tag}.tsv", "w") as f:
121 for r in rows:
122 f.write("\t".join(r) + "\n")
123print("summary", {s: sum(1 for r in rows if r[1] == s) for s in ("KILLED", "SURVIVED", "UNVIABLE", "NORUN")})