ReMe/plugins/home/tests/test_auto_bash_seatbelt.py
jinliyl 54fa3381d7
Some checks failed
CI and Release / Docker / Build and test / amd64 (push) Has been cancelled
CI and Release / Docker / Build and test / arm64 (push) Has been cancelled
Security / CodeQL / Analyze python (push) Has been cancelled
CI / Python quality / Pre-commit (push) Has been cancelled
CI / Python tests / Unit Tests - py3.12 (push) Has been cancelled
CI / Python tests / Unit Tests - py3.13 (push) Has been cancelled
CI / Python tests / Unit Tests - py3.14 (push) Has been cancelled
CI / Python packages / Build and verify distributions (push) Has been cancelled
CI / Python quality / GitHub Actions (push) Has been cancelled
CI / Python tests / Hermes embedded - ubuntu-latest (push) Has been cancelled
CI / Python tests / Hermes embedded - windows-latest (push) Has been cancelled
CI / Python tests / Unit Tests - py3.11 (push) Has been cancelled
CI / ReMe Studio / Studio checks (push) Has been cancelled
Deploy / Documentation / Build documentation (push) Has been cancelled
CI / Windows / CLI smoke - py3.11 (push) Has been cancelled
Security / CodeQL / Analyze javascript-typescript (push) Has been cancelled
CI and Release / Docker / Publish multi-platform tags (push) Has been cancelled
Deploy / Documentation / deploy (push) Has been cancelled
refactor(core): reorganize the ReMe runtime and shared helpers (#769)
* refactor(core): reorganize the ReMe runtime and shared helpers

* test(status): import the base component from its canonical module

* test: remove redundant unit checks

* refactor(core): use canonical runtime imports and shared CLI discovery

Restore evolve behavior to the base branch and update its imports. Use Application as the runtime entry point and encode SSE in the HTTP service.

* fix(cli): honor caller config when service replay is incomplete

Use partial startup overrides for port probes and resolve named configs before requesting the service process working directory.
2026-11-20 22:06:46 +08:00

95 lines
4.4 KiB
Python

"""Real macOS execution, deliberately separate from the mocked command tests."""
import os
import shlex
import subprocess
import sys
import pytest
from reme_home.auto_bash import AutoBash, AutoBashStep
from reme.core.context import ApplicationContext
from reme.steps.common.python_execute import PythonExecuteStep
from reme.utils.file_sandbox.path_seatbelt_backend import PathSeatbeltBackend
pytestmark = pytest.mark.skipif(sys.platform != "darwin", reason="Seatbelt requires macOS")
@pytest.mark.asyncio
async def test_auto_bash_git_preserves_sandbox(tmp_path, monkeypatch):
"""Apple Git works under the default sandbox without granting external access."""
root = tmp_path.resolve()
workspace = root / "workspace"
workspace.mkdir()
# Use the Apple dispatcher rather than a developer's Homebrew Git.
subprocess.run(["/usr/bin/git", "init", "-q", str(workspace)], check=True)
(workspace / "note.txt").write_text("hello", encoding="utf-8")
outside = root / "outside.txt"
outside.write_text("private", encoding="utf-8")
monkeypatch.setenv("REME_SANDBOX_TEST_SECRET", "must-not-inherit")
context = ApplicationContext(workspace_dir=str(workspace))
step = AutoBashStep(app_context=context)
# The default PATH may select Homebrew Git. Explicitly exercise Apple's
# dispatcher argv too, which the backend resolves to trusted developer tools.
backend = AutoBash(cwd=workspace)._backend # pylint: disable=protected-access
assert isinstance(backend, PathSeatbeltBackend)
for command, expected in (("status", "?? note.txt"), ("rev-parse", str(workspace))):
flag = "--short" if command == "status" else "--show-toplevel"
result = await backend.exec_shell(["/usr/bin/git", command, flag], timeout=10)
assert result.exit_code == 0, result.stderr.decode(errors="replace")
assert result.stderr == b""
assert expected in result.stdout.decode()
for read_only in (True, False):
status = await step(command="git status --short", is_read_only=read_only)
assert status.success, status.answer
assert "?? note.txt" in status.answer
toplevel = await step(command="git rev-parse --show-toplevel", is_read_only=read_only)
assert toplevel.success, toplevel.answer
assert str(workspace) in toplevel.answer
denied = await step(command=f"cat {shlex.quote(str(outside))}")
assert not denied.success
denied = await step(command=f"echo changed > {shlex.quote(str(outside))}", is_read_only=False)
assert not denied.success
assert outside.read_text(encoding="utf-8") == "private"
denied = await step(command="echo changed > note.txt", is_read_only=False)
assert not denied.success
assert (workspace / "note.txt").read_text(encoding="utf-8") == "hello"
denied = await step(command="true", is_read_only=False, writable_paths=[str(root)])
assert not denied.success
assert "must be configured on the Step" in denied.answer
# Both public executors share the backend affected by the runtime grants.
code = (
"import os; from pathlib import Path; "
"assert 'REME_SANDBOX_TEST_SECRET' not in os.environ; "
f"Path({str(outside)!r}).read_text()"
)
python = PythonExecuteStep(app_context=context)
denied = await python(code=code)
assert not denied.success
assert "PermissionError" in denied.metadata["stderr"]
clean = await step(
command=shlex.join([sys.executable, "-c", "import os; print(os.getenv('REME_SANDBOX_TEST_SECRET', 'clean'))"]),
is_read_only=False,
)
assert clean.success, clean.answer
assert clean.answer.strip() == "clean"
# Make the trace destination writable so absence of output proves that
# environment cleanup works independently of filesystem denials.
trace = workspace / "trace"
trace.mkdir()
marker = trace / "git-trace"
monkeypatch.setenv("GIT_TRACE", str(marker))
step = AutoBashStep(app_context=context, writable_paths=[str(trace)])
for read_only in (True, False):
result = await step(command="git rev-parse --show-toplevel", is_read_only=read_only)
assert result.success, result.answer
assert not marker.exists()
assert os.environ["GIT_TRACE"] == str(marker)
assert os.environ["REME_SANDBOX_TEST_SECRET"] == "must-not-inherit"
subprocess.run(["/usr/bin/git", "rev-parse", "--show-toplevel"], cwd=workspace, check=True, capture_output=True)
assert marker.stat().st_size > 0