|
| 1 | +"""OX-02 mutation controls M1-M6 (docs/notes/owen-visual-studio-preregistration.md §12). |
| 2 | +
|
| 3 | +Each mutation edits ONE product source the way the bug it stands for would, runs the check |
| 4 | +that is registered to catch it, and requires that check to go red; the source is restored |
| 5 | +whatever happens. A mutation that leaves its check green is a FAIL of this script. |
| 6 | +
|
| 7 | + M1 the client publishes every answer, current or not -> LiveClientTests K4-stale-dropped |
| 8 | + M2 a service that cannot start is swallowed silently -> LiveClientTests M2-cannot-start |
| 9 | + M3 the coordinate contract is off by one -> LiveClientTests coord-* |
| 10 | + M4 the host routes only Typed Builder's generated sources -> owen_live_gate K8-two-extensions |
| 11 | + M5 live forms its own severity instead of the core's -> owen_live_gate P10-parity-* |
| 12 | + M6 the unsaved document is read from disk -> owen_live_gate K2 + extractor E4 |
| 13 | +
|
| 14 | +Run: python scripts/owen_live_mutations.py [--rust-core <own-cli>] [--only M1,M3] |
| 15 | +""" |
| 16 | + |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +import os |
| 20 | +import subprocess |
| 21 | +import sys |
| 22 | +import tempfile |
| 23 | + |
| 24 | +ROOT = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")) |
| 25 | +VS = os.path.join(ROOT, "frontend", "roslyn", "Owen.VisualStudio", "Live") |
| 26 | +CLI = os.path.join(ROOT, "frontend", "roslyn", "OwnSharp.Cli") |
| 27 | +EXTRACTOR = os.path.join(ROOT, "frontend", "roslyn", "OwnSharp.Extractor") |
| 28 | +TESTS = os.path.join(ROOT, "tests", "owen-live", "LiveClientTests") |
| 29 | + |
| 30 | +MUTATIONS: list[tuple[str, str, str, str, str, list[str]]] = [ |
| 31 | + ( |
| 32 | + "M1", |
| 33 | + os.path.join(VS, "LiveEngine.cs"), |
| 34 | + "if (!IgnoreVersions && !_gate.IsCurrent(snapshot.Key, response.Version))", |
| 35 | + "if (false && !_gate.IsCurrent(snapshot.Key, response.Version))", |
| 36 | + "client", |
| 37 | + ["K4-stale-dropped"], |
| 38 | + ), |
| 39 | + ( |
| 40 | + "M2", |
| 41 | + os.path.join(VS, "LiveEngine.cs"), |
| 42 | + " if (IgnoreVersions || _gate.IsCurrent(snapshot.Key, version))\n" |
| 43 | + " Publish(", |
| 44 | + " if (false)\n Publish(", |
| 45 | + "client", |
| 46 | + ["M2-cannot-start"], |
| 47 | + ), |
| 48 | + ( |
| 49 | + "M3", |
| 50 | + os.path.join(VS, "LiveScheduler.cs"), |
| 51 | + "return (start, end, start + 1);", |
| 52 | + "return (start + 1, end, start + 2);", |
| 53 | + "client", |
| 54 | + ["coord-no-column", "coord-column"], |
| 55 | + ), |
| 56 | + ( |
| 57 | + "M4", |
| 58 | + os.path.join(CLI, "OwenHost.cs"), |
| 59 | + "descriptors.SelectMany(d => d.Generators)", |
| 60 | + 'descriptors.Where(d => d.Id == "Owen.TypedBuilder").SelectMany(d => d.Generators)', |
| 61 | + "gate", |
| 62 | + ["K8-two-extensions"], |
| 63 | + ), |
| 64 | + ( |
| 65 | + "M5", |
| 66 | + os.path.join(CLI, "LiveAnalysis.cs"), |
| 67 | + 'level == "error" ? "error" : "warning",', |
| 68 | + '"error",', |
| 69 | + "gate", |
| 70 | + ["P10-parity-stale", "P10-parity-copy", "P10-parity-raw"], |
| 71 | + ), |
| 72 | + ( |
| 73 | + "M6", |
| 74 | + os.path.join(EXTRACTOR, "InProcess.cs"), |
| 75 | + "SourceOverlay is not null && " |
| 76 | + "SourceOverlay.TryGetValue(Path.GetFullPath(path), out var text)", |
| 77 | + "false && SourceOverlay!.TryGetValue(Path.GetFullPath(path), out var text)", |
| 78 | + "gate", |
| 79 | + ["K2-unsaved-own002"], |
| 80 | + ), |
| 81 | +] |
| 82 | + |
| 83 | + |
| 84 | +def run(cmd: list[str]) -> subprocess.CompletedProcess[str]: |
| 85 | + return subprocess.run( |
| 86 | + cmd, |
| 87 | + cwd=ROOT, |
| 88 | + capture_output=True, |
| 89 | + text=True, |
| 90 | + encoding="utf-8", |
| 91 | + errors="replace", |
| 92 | + check=False, |
| 93 | + ) |
| 94 | + |
| 95 | + |
| 96 | +def red(output: str, names: list[str]) -> list[str]: |
| 97 | + return [n for n in names if f"FAIL[{n}]" in output] |
| 98 | + |
| 99 | + |
| 100 | +def main() -> int: |
| 101 | + argv = sys.argv[1:] |
| 102 | + core = argv[argv.index("--rust-core") + 1] if "--rust-core" in argv else None |
| 103 | + only = set(argv[argv.index("--only") + 1].split(",")) if "--only" in argv else None |
| 104 | + failures = [] |
| 105 | + # the client tests' service half needs a real `owen serve` and a live request; a request |
| 106 | + # with no extension is enough (the service answers OWENB001), so build the host locally |
| 107 | + built = run(["dotnet", "build", CLI, "-nologo", "-v", "q"]) |
| 108 | + if built.returncode != 0: |
| 109 | + raise SystemExit(f"FAIL: OwnSharp.Cli does not build: {built.stdout[-800:]}") |
| 110 | + host = os.path.join(CLI, "bin", "Debug", "net8.0", "ownsharp.dll") |
| 111 | + scratch = tempfile.mkdtemp(prefix="owen-live-mutations-") |
| 112 | + request = os.path.join(scratch, "live.txt") |
| 113 | + with open(request, "w", encoding="utf-8") as f: |
| 114 | + f.write(f"project\t{os.path.join(scratch, 'Empty.csproj')}\nhost\t{host}\n") |
| 115 | + for name, path, before, after, where, expect in MUTATIONS: |
| 116 | + if only is not None and name not in only: |
| 117 | + continue |
| 118 | + with open(path, encoding="utf-8") as f: |
| 119 | + original = f.read() |
| 120 | + if original.count(before) != 1: |
| 121 | + failures.append( |
| 122 | + f"{name}: its anchor is not exactly once in {os.path.relpath(path, ROOT)}" |
| 123 | + ) |
| 124 | + continue |
| 125 | + try: |
| 126 | + with open(path, "w", encoding="utf-8", newline="") as f: |
| 127 | + f.write(original.replace(before, after)) |
| 128 | + if where == "client": |
| 129 | + built = run(["dotnet", "build", TESTS, "-c", "Release", "-nologo", "-v", "q"]) |
| 130 | + if built.returncode != 0: |
| 131 | + failures.append( |
| 132 | + f"{name}: LiveClientTests no longer builds: {built.stdout[-500:]}" |
| 133 | + ) |
| 134 | + continue |
| 135 | + done = run( |
| 136 | + [ |
| 137 | + "dotnet", |
| 138 | + os.path.join(TESTS, "bin", "Release", "net8.0", "LiveClientTests.dll"), |
| 139 | + host, |
| 140 | + request, |
| 141 | + ] |
| 142 | + ) |
| 143 | + else: |
| 144 | + cmd = [sys.executable, os.path.join(ROOT, "scripts", "owen_live_gate.py")] |
| 145 | + if core: |
| 146 | + cmd += ["--rust-core", core] |
| 147 | + done = run(cmd) |
| 148 | + caught = red(done.stdout, expect) |
| 149 | + if caught: |
| 150 | + print(f"ok[{name}]: caught by {caught} (exit {done.returncode})") |
| 151 | + else: |
| 152 | + failures.append(f"{name}: none of {expect} went red (exit {done.returncode})") |
| 153 | + print(done.stdout[-3000:]) |
| 154 | + finally: |
| 155 | + with open(path, "w", encoding="utf-8", newline="") as f: |
| 156 | + f.write(original) |
| 157 | + for failure in failures: |
| 158 | + print(f"FAIL[{failure.split(':')[0]}]: {failure}") |
| 159 | + print(f"owen live mutations: {len(failures)} not caught") |
| 160 | + return 1 if failures else 0 |
| 161 | + |
| 162 | + |
| 163 | +if __name__ == "__main__": |
| 164 | + raise SystemExit(main()) |
0 commit comments