| 1 | #!/usr/bin/env python3 |
| 2 | """Disposable workspace tests; PROJECT_STORE_REAL_REFLINK=1 requires Linux/ZFS.""" |
| 3 | |
| 4 | import importlib.util |
| 5 | import os |
| 6 | from pathlib import Path |
| 7 | import shutil |
| 8 | import subprocess |
| 9 | import tempfile |
| 10 | import unittest |
| 11 | from unittest.mock import patch |
| 12 | |
| 13 | spec = importlib.util.spec_from_file_location("project_store", Path(__file__).with_name("project-store.py")) |
| 14 | store = importlib.util.module_from_spec(spec) |
| 15 | spec.loader.exec_module(store) |
| 16 | |
| 17 | |
| 18 | class WorkspaceTest(unittest.TestCase): |
| 19 | def setUp(self): |
| 20 | self.tmp = tempfile.TemporaryDirectory(prefix="project-store-test-") |
| 21 | self.addCleanup(self.tmp.cleanup) |
| 22 | self.root = Path(self.tmp.name).resolve() |
| 23 | config = self.root / "jj.toml" |
| 24 | config.write_text('[user]\nname="Fixture"\nemail="fixture@example.invalid"\n[signing]\nbehavior="drop"\n') |
| 25 | self.env = patch.dict(os.environ, {"JJ_CONFIG": str(config)}) |
| 26 | self.env.start() |
| 27 | self.addCleanup(self.env.stop) |
| 28 | if not os.environ.get("PROJECT_STORE_REAL_REFLINK"): |
| 29 | self.copy = patch.object(store.fcntl, "ioctl", side_effect=self.copy_bytes) |
| 30 | self.copy.start() |
| 31 | self.addCleanup(self.copy.stop) |
| 32 | self.main = self.root / "Example/main" |
| 33 | self.main.parent.mkdir() |
| 34 | self.jj("git", "init", "--no-colocate", str(self.main)) |
| 35 | (self.main / ".gitignore").write_text("target/\n") |
| 36 | (self.main / "source.txt").write_text("committed\n") |
| 37 | (self.main / "unchanged.txt").write_text("same\n") |
| 38 | (self.main / "link").symlink_to("source.txt") |
| 39 | self.jj("-R", str(self.main), "describe", "-m", "fixture") |
| 40 | self.revision = self.jj("-R", str(self.main), "log", "--no-graph", "-r", "@", "-T", "commit_id").strip() |
| 41 | (self.main / "target/debug").mkdir(parents=True) |
| 42 | (self.main / "target/debug/artifact").write_bytes(b"warm artifact" * 10000) |
| 43 | (self.main / "target/debug/.cargo-lock").touch() |
| 44 | self.request = dict(project="Example", source="main", destination="agent", |
| 45 | revision=self.revision, targets=["target"]) |
| 46 | |
| 47 | @staticmethod |
| 48 | def copy_bytes(destination, command, source): |
| 49 | with os.fdopen(os.dup(source), "rb") as src, os.fdopen(os.dup(destination), "wb") as dst: |
| 50 | shutil.copyfileobj(src, dst) |
| 51 | |
| 52 | def jj(self, *args): |
| 53 | return subprocess.run(["jj", *args], text=True, capture_output=True, check=True).stdout |
| 54 | |
| 55 | def test_host_checkout_without_cache_preserves_donor(self): |
| 56 | (self.main / "source.txt").write_text("active agent edit\n") |
| 57 | request = dict(project="Example", destination="cold", revision=self.revision, |
| 58 | engine="darwin;arm64;node26", install=True) |
| 59 | self.assertEqual(store.seed(self.root, request), {"workspace": "cold", "installed": False, "pnpm": False}) |
| 60 | self.assertEqual((self.main.parent / "cold/source.txt").read_text(), "committed\n") |
| 61 | self.assertEqual((self.main / "source.txt").read_text(), "active agent edit\n") |
| 62 | with self.assertRaises(ValueError): |
| 63 | store.seed(self.root, request) |
| 64 | for name in ("../escape", "main", "default"): |
| 65 | with self.assertRaises(ValueError): |
| 66 | store.seed(self.root, dict(request, destination=name)) |
| 67 | |
| 68 | def test_cache_inputs_track_dependency_changes_only(self): |
| 69 | (self.main / "package.json").write_text('{"packageManager":"pnpm@10.32.1"}') |
| 70 | before = store.dependency_inputs(self.main) |
| 71 | operation = self.jj("--ignore-working-copy", "-R", str(self.main), "op", "log", "--no-graph", "-n", "1", "-T", "id") |
| 72 | (self.main / "source.txt").write_text("new source\n") |
| 73 | self.assertEqual(store.dependency_inputs(self.main), before) |
| 74 | (self.main / "package.json").write_text('{"packageManager":"pnpm@12.14.0"}') |
| 75 | self.assertNotEqual(store.dependency_inputs(self.main), before) |
| 76 | self.assertEqual(self.jj("--ignore-working-copy", "-R", str(self.main), "op", "log", "--no-graph", "-n", "1", "-T", "id"), operation) |
| 77 | (self.main / "package.json").unlink() |
| 78 | (self.main / "package.json").symlink_to("source.txt") |
| 79 | with self.assertRaises(ValueError): |
| 80 | store.dependency_inputs(self.main) |
| 81 | |
| 82 | def test_clone_restores_revision_and_preserves_warm_files(self): |
| 83 | (self.main / "source.txt").write_text("donor edit\n") |
| 84 | (self.main / "donor-only.txt").write_text("uncommitted\n") |
| 85 | original = self.main / "target/debug/artifact" |
| 86 | mtime = (self.main / "unchanged.txt").stat().st_mtime_ns |
| 87 | self.assertEqual(store.seed(self.root, self.request), {"workspace": "agent"}) |
| 88 | agent = self.main.parent / "agent" |
| 89 | self.assertEqual((agent / "source.txt").read_text(), "committed\n") |
| 90 | self.assertFalse((agent / "donor-only.txt").exists()) |
| 91 | self.assertEqual((self.main / "source.txt").read_text(), "donor edit\n") |
| 92 | self.assertEqual((agent / "unchanged.txt").stat().st_mtime_ns, mtime) |
| 93 | artifact = agent / "target/debug/artifact" |
| 94 | self.assertEqual(artifact.read_bytes(), original.read_bytes()) |
| 95 | self.assertEqual(artifact.stat().st_mtime_ns, original.stat().st_mtime_ns) |
| 96 | self.assertNotEqual(artifact.stat().st_ino, original.stat().st_ino) |
| 97 | artifact.write_bytes(b"modified") |
| 98 | self.assertNotEqual(artifact.read_bytes(), original.read_bytes()) |
| 99 | self.assertEqual(os.readlink(agent / "link"), "source.txt") |
| 100 | self.assertEqual(self.jj("-R", str(agent), "diff", "--summary"), "") |
| 101 | self.assertFalse((agent / ".jj/repo").is_dir()) |
| 102 | self.assertFalse(Path((agent / ".jj/repo").read_text()).is_absolute()) |
| 103 | |
| 104 | def test_rejects_paths_and_existing_destination(self): |
| 105 | for key, value in (("source", "../outside"), ("destination", "main"), |
| 106 | ("project", "/tmp"), ("targets", ["../outside"])): |
| 107 | with self.subTest(key=key), self.assertRaises(ValueError): |
| 108 | store.seed(self.root, dict(self.request, **{key: value})) |
| 109 | (self.main.parent / "agent").mkdir() |
| 110 | with self.assertRaises(ValueError): |
| 111 | store.seed(self.root, self.request) |
| 112 | |
| 113 | def test_rejects_symlink_workspace(self): |
| 114 | (self.main.parent / "linked").symlink_to(self.main) |
| 115 | with self.assertRaises(OSError): |
| 116 | store.seed(self.root, dict(self.request, source="linked")) |
| 117 | |
| 118 | def test_failed_reflink_removes_workspace_registration_and_files(self): |
| 119 | with patch.object(store.fcntl, "ioctl", side_effect=OSError("reflink unavailable")): |
| 120 | with self.assertRaises(OSError): |
| 121 | store.seed(self.root, self.request) |
| 122 | self.assertFalse((self.main.parent / "agent").exists()) |
| 123 | self.assertNotIn("agent:", self.jj("-R", str(self.main), "workspace", "list")) |
| 124 | self.assertEqual((self.main / "source.txt").read_text(), "committed\n") |
| 125 | |
| 126 | |
| 127 | if __name__ == "__main__": |
| 128 | unittest.main() |