1#!/usr/bin/env python3
2"""Exercise routing with real Jujutsu repositories and a local SSH stand-in."""
3
4import hashlib
5import fcntl
6import json
7import os
8from pathlib import Path
9import shutil
10import subprocess
11import sys
12import tempfile
13import threading
14import time
15import unittest
16import client
17
18
19HERE = Path(__file__).resolve().parent
20
21
22class RoutingTest(unittest.TestCase):
23 def setUp(self):
24 temporary = tempfile.TemporaryDirectory(prefix="project-routing-")
25 self.addCleanup(temporary.cleanup)
26 self.root = Path(temporary.name).resolve()
27 self.projects = self.root / "project"
28 self.projects.mkdir()
29 self.bin = self.root / "bin"
30 self.bin.mkdir()
31 for name in ("jj", "pnpm", "project", "client.py"):
32 shutil.copy2(HERE / name, self.bin / name)
33 native = Path(shutil.which("jj"))
34 if native.with_name("jj-real").exists():
35 native = native.with_name("jj-real")
36 (self.bin / "jj-real").symlink_to(native)
37 self.requests = self.root / "requests"
38 self.native = self.root / "native"
39 (self.bin / "pnpm-real").write_text(f'''#!{sys.executable}
40import json, pathlib, sys
41pathlib.Path({str(self.native)!r}).write_text(json.dumps(sys.argv[1:]))
42print("native output")
43sys.exit(23)
44''')
45 (self.bin / "pnpm-real").chmod(0o755)
46 (self.bin / "ssh").write_text(f'''#!{sys.executable}
47import json, os, pathlib, subprocess, sys
48request = json.load(sys.stdin)
49with open({str(self.requests)!r}, "a") as log: log.write(json.dumps(request) + "\\n")
50if request["command"] == "pnpm":
51 print(json.dumps({{"ready": True}} if request.get("prepare") else {{"installed": False, "reason": "fixture miss"}}))
52 sys.exit(0)
53sys.exit(subprocess.run([{sys.executable!r}, {str(HERE.parent / "project-store.py")!r}, {str(self.projects)!r}],
54 input=json.dumps(request), text=True, env=dict(os.environ, PROJECT_STORE_SSH="")).returncode)
55''')
56 (self.bin / "ssh").chmod(0o755)
57 config = self.root / "jj.toml"
58 config.write_text('[user]\nname="Guest Identity"\nemail="guest@example.invalid"\n'
59 '[signing]\nbehavior="drop"\n[ui]\ndiff-formatter=":git"\n')
60 self.env = dict(os.environ, PATH=str(self.bin) + os.pathsep + os.environ["PATH"],
61 JJ_CONFIG=str(config), PROJECT_ROOT=str(self.projects), PROJECT_STORE_SSH="fixture",
62 GIT_CONFIG_NOSYSTEM="1", GIT_CONFIG_GLOBAL=str(self.root / "absent"))
63 for key in list(self.env):
64 if key.lower().startswith(("npm_config_", "pnpm_config_")) or key in (
65 "NODE_ENV", "NODE_OPTIONS", "NODE_PATH", "VP_PACKAGE_MANAGER"):
66 self.env.pop(key)
67 self.main = self.projects / "Example/main"
68 self.main.parent.mkdir()
69 self.run_tool("jj-real", "git", "init", "--no-colocate", str(self.main), cwd=self.root)
70 (self.main / "source").write_text("base\n")
71 self.run_tool("jj-real", "describe", "-m", "base")
72
73 def run_tool(self, tool, *args, cwd=None, check=True):
74 result = subprocess.run([str(self.bin / tool), *args], cwd=cwd or self.main,
75 env=self.env, text=True, capture_output=True, timeout=15)
76 if check:
77 self.assertEqual(result.returncode, 0, result.stderr)
78 return result
79
80 def test_workspace_and_mutations_keep_paths_identity_and_dirty_content(self):
81 destination = self.main.parent / "agent space"
82 result = self.run_tool("jj", "workspace", "add", "--", str(destination))
83 self.assertIn("agent space", result.stderr)
84 (destination / "dirty").write_text("unsaved agent work\n")
85 self.run_tool("jj", "new", "-m", "child", cwd=destination)
86 self.run_tool("jj", "rebase", "-s", "@-", "-d", "root()", cwd=destination)
87 self.assertEqual((destination / "dirty").read_text(), "unsaved agent work\n")
88 author = self.run_tool("jj", "log", "-r", "@", "--no-graph", "-T", "author.email()", cwd=destination)
89 self.assertEqual(author.stdout, "guest@example.invalid")
90 self.assertEqual(len(self.requests.read_text().splitlines()), 4)
91 self.assertFalse(Path((destination / ".jj/repo").read_text()).is_absolute())
92
93 def test_paths_cannot_escape_share_even_through_symlinks(self):
94 outside = self.root / "outside"
95 outside.mkdir()
96 (self.main / "escape").symlink_to(outside)
97 for args in [("workspace", "add", str(outside / "new")),
98 ("workspace", "add", "../../../../escape"),
99 ("workspace", "add", "escape/new"),
100 ("-R", str(outside), "status"),
101 ("file", "show", str(outside / "file"))]:
102 with self.subTest(args=args):
103 result = self.run_tool("jj", *args, check=False)
104 self.assertNotEqual(result.returncode, 0)
105 self.assertIn("outside the project share", result.stderr)
106 self.assertFalse(self.requests.exists())
107
108 def test_remote_errors_preserve_exit_status_without_local_retry(self):
109 result = self.run_tool("jj", "rebase", "-r", "missing-revision", "-d", "root()", check=False)
110 self.assertNotEqual(result.returncode, 0)
111 self.assertIn("missing-revision", result.stderr)
112 self.assertEqual(len(self.requests.read_text().splitlines()), 1)
113
114 def test_relative_repository_selection_from_outside_the_share(self):
115 destination = self.main.parent / "selected"
116 result = self.run_tool("jj", "-R", str(self.main.relative_to(self.root)),
117 "--config", 'user.email="override@example.invalid"',
118 "workspace", "add", "-r", "@", str(destination), cwd=self.root)
119 self.assertEqual(result.returncode, 0)
120 author = self.run_tool("jj", "log", "--no-graph", "-r", "@", "-T", "author.email()", cwd=destination)
121 self.assertEqual(author.stdout, "override@example.invalid")
122
123 def test_local_repositories_and_interactive_commands_stay_local(self):
124 local = self.root / "local"
125 self.run_tool("jj-real", "git", "init", "--no-colocate", str(local))
126 self.run_tool("jj", "status", cwd=local)
127 self.env["JJ_EDITOR"] = "true"
128 self.run_tool("jj", "describe")
129 self.assertFalse(self.requests.exists())
130
131 def test_pnpm_scripts_and_unknown_options_keep_native_argv_and_exit(self):
132 for args in [("run", "build"), ("exec", "node", "-p", "process.platform"),
133 ("build",), ("install", "--config.ignore-scripts=true"),
134 ("--version",), ("run", "install")]:
135 with self.subTest(args=args):
136 result = self.run_tool("pnpm", *args, check=False)
137 self.assertEqual(result.returncode, 23)
138 self.assertEqual(result.stdout, "native output\n")
139 prefix = ["--config.verify-deps-before-run=false"] if args[0] in ("run", "exec") else []
140 self.assertEqual(json.loads(self.native.read_text()), prefix + list(args))
141 requests = [json.loads(line) for line in self.requests.read_text().splitlines()]
142 self.assertTrue(all(request["prepare"] and request["args"] == [] for request in requests))
143
144 def test_script_shortcut_checks_dependencies_remotely_then_runs_natively(self):
145 (self.main / "package.json").write_text(json.dumps({"scripts": {"build": "never run on Linux"}}))
146 result = self.run_tool("pnpm", "build", "--flag", check=False)
147 self.assertEqual(result.returncode, 23)
148 self.assertEqual(json.loads(self.requests.read_text())["args"], [])
149 self.assertEqual(json.loads(self.native.read_text()), ["--config.verify-deps-before-run=false", "build", "--flag"])
150
151 def test_handoff_waits_for_the_servers_modules_manifest(self):
152 modules = self.main / "node_modules/.modules.yaml"
153 modules.parent.mkdir()
154 modules.write_text('{"pendingBuilds":[]}')
155 content = b'{"pendingBuilds":["new-native-package"]}'
156 def publish():
157 time.sleep(0.15)
158 temporary = modules.with_suffix(".next")
159 temporary.write_bytes(content)
160 temporary.replace(modules)
161 publisher = threading.Thread(target=publish)
162 publisher.start()
163 self.addCleanup(publisher.join)
164 client.finish_install(self.main, self.bin / "pnpm-real", ignore_scripts=True,
165 modules_hash=hashlib.sha256(content).hexdigest())
166 self.assertEqual(modules.read_bytes(), content)
167
168 def test_workspace_handoff_waits_for_metadata_visibility(self):
169 destination = self.main.parent / "new workspace"
170 def publish():
171 time.sleep(0.15)
172 (destination / ".jj/working_copy").mkdir(parents=True)
173 publisher = threading.Thread(target=publish)
174 publisher.start()
175 self.addCleanup(publisher.join)
176 client.wait_workspace(destination)
177 self.assertTrue((destination / ".jj/working_copy").is_dir())
178
179 def test_install_miss_falls_back_once_with_exact_arguments(self):
180 self.env.update(npm_config_prefix=str(self.root / "local"), npm_config_cache=str(self.root / "cache"),
181 PNPM_CONFIG_UPDATE_NOTIFIER="false")
182 args = ["--dir", str(self.main), "install", "--frozen-lockfile", "--offline"]
183 result = self.run_tool("pnpm", *args, check=False)
184 self.assertEqual(result.returncode, 23)
185 self.assertIn("fixture miss", result.stderr)
186 self.assertEqual(json.loads(self.native.read_text()), args)
187 self.assertEqual(json.loads(self.requests.read_text())["args"], ["install", "--frozen-lockfile", "--offline"])
188
189 def test_dependency_changes_and_filters_use_storage_before_native_hooks(self):
190 for args in [("add", "-D", "is-number@7.0.0"), ("update", "is-number"),
191 ("remove", "is-number"), ("--filter", "app", "install", "--lockfile-only")]:
192 with self.subTest(args=args):
193 result = self.run_tool("pnpm", *args, check=False)
194 self.assertEqual(result.returncode, 23)
195 request = json.loads(self.requests.read_text().splitlines()[-1])
196 self.assertEqual(request["args"], list(args))
197 self.assertRegex(request["engine"], r";node\d+\.\d+\.\d+$")
198
199 def test_install_miss_keeps_the_existing_store(self):
200 modules = self.main / "node_modules/.modules.yaml"
201 modules.parent.mkdir()
202 store = self.root / "cache/v10"
203 modules.write_text(json.dumps({"storeDir": str(store)}))
204 result = self.run_tool("pnpm", "install", check=False)
205 self.assertEqual(result.returncode, 23)
206 self.assertEqual(json.loads(self.native.read_text()), ["install", "--store-dir=" + str(store.parent)])
207
208 def test_custom_install_environment_stays_native(self):
209 for key, value in (("npm_config_ignore_scripts", "true"),
210 ("PNPM_CONFIG_STORE_DIR", str(self.root / "custom-store")),
211 ("VP_PACKAGE_MANAGER", "pnpm@12.15.0"),
212 ("NODE_OPTIONS", "--require=/private/agent-hook.cjs"),
213 ("NODE_PATH", "/private/agent-modules")):
214 with self.subTest(key=key):
215 self.env[key] = value
216 result = self.run_tool("pnpm", "install", check=False)
217 self.assertEqual(result.returncode, 23)
218 self.assertEqual(json.loads(self.native.read_text()), ["install"])
219 self.assertIn("Using Mac installation: custom environment (" + key + ")", result.stderr)
220 self.assertNotIn(value, result.stderr)
221 self.env.pop(key)
222 self.assertFalse(self.requests.exists())
223
224 def test_inherited_metadata_and_memory_limits_still_offload(self):
225 self.env["npm_config_user_agent"] = "pnpm/12.14.0 npm/? node/v26.12.0 darwin arm64"
226 for options in ("--max-old-space-size=8192", "--max_old_space_size=8192",
227 "--max-old-space-size=8192 --max-semi-space-size=32"):
228 with self.subTest(options=options):
229 self.env["NODE_OPTIONS"] = options
230 result = self.run_tool("pnpm", "install", "--frozen-lockfile", check=False)
231 self.assertEqual(result.returncode, 23)
232 self.assertIn("fixture miss", result.stderr)
233 self.assertNotIn("custom environment", result.stderr)
234 request = json.loads(self.requests.read_text().splitlines()[-1])
235 self.assertEqual(request["args"], ["install", "--frozen-lockfile"])
236
237 def test_production_environment_reaches_pnpm_without_changing_arguments(self):
238 self.env["NODE_ENV"] = "production"
239 for args in (("install",), ("install", "--dev"), ("add", "is-number@7.0.0"), ("exec", "node")):
240 with self.subTest(args=args):
241 result = self.run_tool("pnpm", *args, check=False)
242 self.assertEqual(result.returncode, 23)
243 request = json.loads(self.requests.read_text().splitlines()[-1])
244 self.assertEqual(request["node_env"], "production")
245 self.assertEqual(request["args"], [] if args[0] == "exec" else list(args))
246
247 def test_storage_reads_preserve_paths_output_and_ignore_files_without_snapshotting(self):
248 (self.main / "file with spaces").write_text("needle\n")
249 (self.main / ".gitignore").write_text("node_modules/\n")
250 (self.main / "node_modules").mkdir()
251 (self.main / "node_modules/ignored").write_text("needle\n")
252 with (self.main / ".jj/repo/command.lock").open("w") as lock:
253 fcntl.flock(lock, fcntl.LOCK_EX)
254 listed = self.run_tool("project", "read", "rg", "--files")
255 self.assertIn("file with spaces", listed.stdout.splitlines())
256 self.assertNotIn("node_modules/ignored", listed.stdout)
257 result = self.run_tool("project", "read", "rg", "--json", "needle", "file with spaces")
258 matches = [json.loads(line) for line in result.stdout.splitlines()]
259 self.assertEqual(next(row for row in matches if row["type"] == "match")
260 ["data"]["path"]["text"], "file with spaces")
261 for pattern, status in (("not present", 1), ("[", 2)):
262 result = self.run_tool("project", "read", "rg", pattern, check=False)
263 self.assertEqual(result.returncode, status)
264 requests = [json.loads(line) for line in self.requests.read_text().splitlines()]
265 self.assertTrue(all(request["command"] == "read" for request in requests))
266
267 def test_storage_hashes_match_content_and_reject_external_paths_and_commands(self):
268 content = b"fixture" * 1000
269 (self.main / "archive with spaces.zip").write_bytes(content)
270 result = self.run_tool("project", "read", "sha256sum", "archive with spaces.zip")
271 self.assertEqual(result.stdout, hashlib.sha256(content).hexdigest() + " archive with spaces.zip\n")
272 outside = self.root / "private"
273 outside.write_text("not shared")
274 (self.main / "escape").symlink_to(outside)
275 for args in (("sha256sum", str(outside)), ("sha256sum", "escape"),
276 ("sha256sum", "-"), ("sha256sum", "--check", "archive with spaces.zip"),
277 ("rg", "--pre=touch marker", "needle"), ("sh", "-c", "touch marker")):
278 with self.subTest(args=args):
279 self.assertNotEqual(self.run_tool("project", "read", *args, check=False).returncode, 0)
280 self.assertFalse((self.main / "marker").exists())
281
282
283if __name__ == "__main__":
284 unittest.main()