|
3 | 3 | import argparse |
4 | 4 | import json |
5 | 5 | import os |
| 6 | +from pathlib import Path |
6 | 7 | import subprocess |
7 | 8 | import sys |
8 | 9 | import time |
@@ -124,6 +125,55 @@ def test_real_detached_worker_finishes_after_its_observer_exits(tmp_path): |
124 | 125 | assert payload["result"]["plan"]["apply_supported"] is False |
125 | 126 |
|
126 | 127 |
|
| 128 | +@pytest.mark.parametrize("command", ["update", "upgrade"]) |
| 129 | +@pytest.mark.parametrize("shadow_pythonpath", [False, True]) |
| 130 | +def test_detached_worker_keeps_its_installation_in_a_different_checkout( |
| 131 | + tmp_path, monkeypatch, command, shadow_pythonpath, |
| 132 | +): |
| 133 | + """The requesting installation owns the worker, not the caller's workspace.""" |
| 134 | + foreign = tmp_path / "another checkout" |
| 135 | + shadow = foreign / "loopx" |
| 136 | + shadow.mkdir(parents=True) |
| 137 | + imported = tmp_path / "foreign-imported" |
| 138 | + (shadow / "__init__.py").write_text( |
| 139 | + f"from pathlib import Path\nPath({str(imported)!r}).touch()\n" |
| 140 | + "raise RuntimeError('foreign installation must not run the update')\n" |
| 141 | + ) |
| 142 | + monkeypatch.chdir(foreign) |
| 143 | + monkeypatch.setenv("LOOPX_UPDATE_STATE_DIR", str(tmp_path / "state")) |
| 144 | + monkeypatch.setenv("LOOPX_BIN_DIR", str(tmp_path / "bin")) |
| 145 | + monkeypatch.delenv("LOOPX_RELEASE_ROOT", raising=False) |
| 146 | + # Exercise the real checkout's supported refusal, never an installed user's |
| 147 | + # archive/package-manager mutation. The worker still runs the owning CLI. |
| 148 | + own_root = Path(operations.__file__).resolve().parents[2] |
| 149 | + monkeypatch.setenv("PATH", str(own_root / "scripts") + os.pathsep + os.environ["PATH"]) |
| 150 | + if shadow_pythonpath: |
| 151 | + monkeypatch.setenv("PYTHONPATH", str(foreign)) |
| 152 | + else: |
| 153 | + monkeypatch.delenv("PYTHONPATH", raising=False) |
| 154 | + registry, runtime = tmp_path / "registry.json", tmp_path / "runtime" |
| 155 | + if command == "upgrade": |
| 156 | + registry, runtime = Path("registry.json"), Path("runtime") |
| 157 | + args = argparse.Namespace(command=command, runtime_root=str(runtime), |
| 158 | + timeout_seconds=30, repo=None, ref="main", archive_url=None, |
| 159 | + source=True, allow_downgrade=False) |
| 160 | + started = operations.start_or_observe(args, registry, registry_was_supplied=True) |
| 161 | + operation_id = started["operation_id"] |
| 162 | + for _ in range(100): |
| 163 | + state = operations.read_operation(operation_id) |
| 164 | + if imported.exists() or state["status"] not in {"starting", "running"}: |
| 165 | + break |
| 166 | + time.sleep(.1) |
| 167 | + assert not imported.exists(), "the detached process imported the foreign checkout" |
| 168 | + assert state["status"] == "failed", state |
| 169 | + assert state["result"]["changes_applied"] is False |
| 170 | + assert state["result"]["plan"]["apply_supported"] is False |
| 171 | + request = json.loads((operations.operation_root() / operation_id / "request.json").read_text()) |
| 172 | + argv = request["argv"] |
| 173 | + assert argv[argv.index("--registry") + 1] == str(registry) |
| 174 | + assert argv[argv.index("--runtime-root") + 1] == str(runtime) |
| 175 | + |
| 176 | + |
127 | 177 | @pytest.mark.parametrize("switched", [True, False]) |
128 | 178 | def test_material_failure_reports_an_observed_switch_but_never_invents_one(monkeypatch, switched): |
129 | 179 | from loopx.self_update import execute_update_plan |
|
0 commit comments