| 1 | #!/usr/bin/env python3 |
| 2 | """Verify that the previous registry implementation refuses v3 without rewriting it.""" |
| 3 | import json |
| 4 | from pathlib import Path |
| 5 | import subprocess |
| 6 | import tempfile |
| 7 | import sys |
| 8 | |
| 9 | root = Path(__file__).resolve().parents[2] |
| 10 | baseline = sys.argv[1] if len(sys.argv) > 1 else "4c8cec3b9f69f979c34769eb197908b7f549c578" |
| 11 | with tempfile.TemporaryDirectory(prefix="reasonix-registry-previous-") as temporary: |
| 12 | checkout = Path(temporary) |
| 13 | package = checkout / "internal/workspacestate" |
| 14 | package.mkdir(parents=True) |
| 15 | files = subprocess.check_output(["git", "ls-tree", "-r", "--name-only", baseline, |
| 16 | "desktop/internal/workspacestate"], cwd=root, text=True).splitlines() |
| 17 | for name in files: |
| 18 | if name.endswith(".go") and not name.endswith("_test.go"): |
| 19 | (package / Path(name).name).write_bytes(subprocess.check_output(["git", "show", f"{baseline}:{name}"], cwd=root)) |
| 20 | manifest = (root / "desktop/go.mod").read_text().replace("replace reasonix => ../", f"replace reasonix => {root}") |
| 21 | manifest = manifest.replace("=> ./third_party/systray", f"=> {root}/desktop/third_party/systray") |
| 22 | (checkout / "go.mod").write_text(manifest) |
| 23 | (checkout / "go.sum").write_bytes((root / "desktop/go.sum").read_bytes()) |
| 24 | (package / "previous_writer_test.go").write_text(r'''package workspacestate |
| 25 | import ("bytes"; "errors"; "os"; "path/filepath"; "testing") |
| 26 | func TestPreviousWriterRejectsV3(t *testing.T) { |
| 27 | path := filepath.Join(t.TempDir(), "registry.json") |
| 28 | body := []byte(`{"version":3,"generation":9,"workspaceIds":["global"],"workspaces":{"global":{"id":"global","sessionIds":["a","b"],"organization":{"revision":2,"order":["ref\u0000local\u0000b","ref\u0000local\u0000a"],"futureField":"preserved"}}},"sessionStates":{},"futureRoot":true}`) |
| 29 | if err := os.WriteFile(path, body, 0600); err != nil { t.Fatal(err) } |
| 30 | s := NewStore(path) |
| 31 | if _, err := s.Load(t.Context()); !errors.Is(err, ErrUnsupportedVersion) { t.Fatalf("read: %v", err) } |
| 32 | if err := s.RenameWorkspace(t.Context(), "global", "old writer"); !errors.Is(err, ErrUnsupportedVersion) { t.Fatalf("write: %v", err) } |
| 33 | after, err := os.ReadFile(path) |
| 34 | if err != nil || !bytes.Equal(body,after) { t.Fatal("old writer modified v3 registry") } |
| 35 | } |
| 36 | ''') |
| 37 | result = subprocess.run(["go", "test", "./internal/workspacestate", "-run", "TestPreviousWriterRejectsV3", "-count=1"], |
| 38 | cwd=checkout, text=True, capture_output=True) |
| 39 | print(result.stdout, end="") |
| 40 | print(result.stderr, end="", file=sys.stderr) |
| 41 | evidence = {"baseline": baseline, "passed": result.returncode == 0, |
| 42 | "scope": "actual baseline registry reader and writer; v3 bytes unchanged", |
| 43 | "stdout": result.stdout, "stderr": result.stderr} |
| 44 | (Path(tempfile.gettempdir()) / "reasonix-independent-registry-compatibility.json").write_text(json.dumps(evidence, indent=2)) |
| 45 | sys.exit(result.returncode) |
| 46 |