feat(tests): add FR1 Python runtime sandbox via sys.addaudithook

This commit is contained in:
ed
2026-06-19 07:36:59 -04:00
parent 1329723c20
commit e733e5247f
2 changed files with 147 additions and 6 deletions
+65 -2
View File
@@ -1,10 +1,15 @@
"""Tests for scripts/audit_test_sandbox_violations.py (Phase 2, FR4)."""
"""Tests for scripts/audit_test_sandbox_violations.py (Phase 2, FR4) and
the Python audit guard in tests/conftest.py (Phase 3, FR1).
"""
from __future__ import annotations
import os
import re
import subprocess
import sys
from pathlib import Path
import pytest
def test_audit_runs_without_error() -> None:
"""The audit script runs and exits cleanly."""
@@ -84,4 +89,62 @@ def test_audit_subprocess_bad_dir_exits_one() -> None:
assert result.returncode == 1, f"Expected exit 1, got {result.returncode}"
finally:
bad.unlink(missing_ok=True)
tmp_dir.rmdir()
tmp_dir.rmdir()
def test_sandbox_blocks_writes_outside_tests_dir() -> None:
"""A write to <project_root>/manual_slop.toml raises TEST_SANDBOX_VIOLATION."""
bad_path = Path(__file__).resolve().parent.parent / "manual_slop.toml"
if bad_path.exists():
original = bad_path.read_bytes()
existed = True
else:
existed = False
original = b""
try:
with pytest.raises(RuntimeError, match="TEST_SANDBOX_VIOLATION"):
bad_path.write_text("corrupt", encoding="utf-8")
finally:
if existed:
bad_path.write_bytes(original)
elif bad_path.exists():
bad_path.unlink()
def test_sandbox_allows_writes_inside_tests_dir(tmp_path) -> None:
"""A write to tmp_path (which lives under tests/artifacts/_pytest_tmp) succeeds."""
target = tmp_path / "foo.txt"
target.write_text("ok", encoding="utf-8")
assert target.read_text(encoding="utf-8") == "ok"
def test_sandbox_allows_writes_inside_tests_artifacts() -> None:
"""A write to tests/artifacts/_sandbox_test_allows/foo.txt succeeds."""
p = Path("tests/artifacts/_sandbox_test_allows/foo.txt")
p.parent.mkdir(parents=True, exist_ok=True)
try:
p.write_text("ok", encoding="utf-8")
assert p.read_text(encoding="utf-8") == "ok"
finally:
p.unlink(missing_ok=True)
if p.parent.exists():
p.parent.rmdir()
def test_sandbox_does_not_block_reads() -> None:
"""A read of <project_root>/pyproject.toml succeeds (reads are always allowed)."""
pyproject = Path(__file__).resolve().parent.parent / "pyproject.toml"
content = pyproject.read_text(encoding="utf-8")
assert "[tool.pytest.ini_options]" in content
def test_sandbox_allows_pytest_cache_write() -> None:
"""Writes under .pytest_cache are allowed (pytest internal cache)."""
cache_root = Path(__file__).resolve().parent.parent / ".pytest_cache"
probe = cache_root / "_sandbox_probe.txt"
cache_root.mkdir(parents=True, exist_ok=True)
try:
probe.write_text("ok", encoding="utf-8")
assert probe.read_text(encoding="utf-8") == "ok"
finally:
probe.unlink(missing_ok=True)