Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions tests/test_sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,19 +507,29 @@ def test_real_sandbox_tmp_subpath_bind_stays_visible(git_repo, tmp_path):
@pytest.mark.skipif(
not _real_bwrap_works() or shutil.which("uv") is None, reason="needs real bwrap and uv"
)
def test_real_sandbox_uv_run_offline(git_repo):
home = Path.home() # the sandbox home inside `loopzero check`: bind the real toolchain
uv_cache = os.environ.get("UV_CACHE_DIR", f"{home}/.cache/uv")
uv_pythons = os.environ.get("UV_PYTHON_INSTALL_DIR", f"{home}/.local/share/uv/python")
def test_real_sandbox_uv_run_offline(git_repo, tmp_path):
uv_cache = tmp_path / "uv-cache"
uv_cache.mkdir()
uv = shutil.which("uv")
assert uv is not None
env = {
"UV_CACHE_DIR": str(uv_cache),
"UV_NO_CONFIG": "1",
"UV_OFFLINE": "1",
"UV_PYTHON": str(Path(sys._base_executable).resolve()),
"UV_PYTHON_DOWNLOADS": "never",
}
pyproject = '[project]\nname = "t"\nversion = "0"\nrequires-python = ">=3.12"\n'
(git_repo / "pyproject.toml").write_text(pyproject + "[dependency-groups]\ndev = []\n")
subprocess.run(["uv", "lock", "-q"], cwd=git_repo, check=True) # projects commit uv.lock
lock_env = {"HOME": str(tmp_path), "PATH": os.defpath, **env}
subprocess.run([uv, "lock", "-q"], cwd=git_repo, env=lock_env, check=True)
git(git_repo, "add", "-A")
git(git_repo, "commit", "-q", "-m", "uv project")
cfg = make_config(
checks=("uv run --group dev python -c 1", ".venv/bin/python -c 1", "test ! -w ."),
env=(("UV_CACHE_DIR", uv_cache), ("UV_PYTHON_INSTALL_DIR", uv_pythons)),
sandbox_ro=(str(Path(shutil.which("uv")).parent), uv_pythons), writable=(uv_cache,),
env=tuple(env.items()),
sandbox_ro=(str(Path(uv).parent), sys.base_prefix),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Bound interpreter prefix is not resolved, but UV_PYTHON is

UV_PYTHON is set to Path(sys._base_executable).resolve(), but the sandbox read-only bind uses the unresolved sys.base_prefix. The two can point to different trees. One case is a uv-managed interpreter under ~/.local/share/uv/python, where newer uv versions use a minor-version symlink directory (cpython-3.12-… → cpython-3.12.N-…) as the venv home. bwrap --ro-bind-try would mount that content at the symlink path, while the resolved executable path points into the patch directory, and the sandbox does not expose that directory because /home is hidden. uv run would then fail to find the interpreter inside the sandbox. This is a host-dependent false failure, not a leak. The smallest fix is to derive both values from the same resolved path, for example py = Path(sys._base_executable).resolve() and then bind str(py.parent.parent), or bind str(Path(sys.base_prefix).resolve()).

writable=(str(uv_cache),),
)
report = sandbox.run_checks(cfg, git_repo)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Cache isolation is not asserted by the test

The acceptance criteria say the inherited cache and sentinel stay unchanged. That was only checked by hand. In the test, isolation follows from overriding UV_CACHE_DIR and HOME and from the private sandbox home, but nothing guards against a regression. An example would be a future refactor that drops env=lock_env, which would let uv lock write to the builder's cache again. A cheap guard: monkeypatch the inherited UV_CACHE_DIR and XDG_CACHE_HOME to an empty sentinel directory under tmp_path, then assert it is still empty after uv lock and run_checks. Also assert that uv_cache is non-empty, to show the disposable cache was actually used.

assert report.ok, report
Expand Down
Loading