From 749120d239f8219145087c2033436b9224cb0f83 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Tue, 9 Jun 2026 17:01:14 -0400 Subject: [PATCH] feat(audit): flag hardcoded workspace and project-root paths in tests --- scripts/check_test_toml_paths.py | 2 ++ tests/test_check_test_toml_paths.py | 48 +++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 tests/test_check_test_toml_paths.py diff --git a/scripts/check_test_toml_paths.py b/scripts/check_test_toml_paths.py index 24a2c69d..347421cb 100644 --- a/scripts/check_test_toml_paths.py +++ b/scripts/check_test_toml_paths.py @@ -20,6 +20,8 @@ PATTERNS = [ re.compile(rf'open\(["\'](?:{"|".join(TOML_BASENAMES)})\.toml["\']'), re.compile(rf'["\']\.{{1,2}}/(?:{"|".join(TOML_BASENAMES)})\.toml["\']'), re.compile(rf'Path\(["\']\.\./(?:{"|".join(TOML_BASENAMES)})\.toml["\']'), + re.compile(r'Path\(["\']tests/artifacts/'), + re.compile(r'Path\(["\']C:[/\\]+projects'), ] EXCLUDE_DIRS = {"artifacts", "logs", "__pycache__", "snapshots"} diff --git a/tests/test_check_test_toml_paths.py b/tests/test_check_test_toml_paths.py new file mode 100644 index 00000000..4cab0066 --- /dev/null +++ b/tests/test_check_test_toml_paths.py @@ -0,0 +1,48 @@ +"""Tests for scripts/check_test_toml_paths.py (Phase 8, Task 8.1).""" +import subprocess +import sys +from pathlib import Path + + +def test_audit_runs_without_error() -> None: + """The audit script runs and exits cleanly.""" + result = subprocess.run( + [sys.executable, "scripts/check_test_toml_paths.py"], + capture_output=True, text=True, cwd=str(Path(__file__).resolve().parent.parent) + ) + assert result.returncode in (0, 1), f"Unexpected exit code: {result.returncode}" + + +def test_audit_flags_hardcoded_workspace_path(tmp_path) -> None: + """A test file with Path('tests/artifacts/...') is flagged.""" + bad_file = tmp_path / "test_bad.py" + bad_file.write_text('workspace = Path("tests/artifacts/live_gui_workspace")\n', encoding="utf-8") + # Run a copy of the audit logic on tmp_path + import re + pattern = re.compile(r'Path\(["\']tests/artifacts/') + content = bad_file.read_text(encoding="utf-8") + assert pattern.search(content), "Pattern should match the hardcoded path" + + +def test_audit_flags_project_root_path(tmp_path) -> None: + """A test file with Path('C:/projects/...') is flagged.""" + bad_file = tmp_path / "test_bad.py" + bad_file.write_text('base_dir = Path("C:/projects/test")\n', encoding="utf-8") + import re + pattern = re.compile(r'Path\(["\']C:[/\\]+projects') + content = bad_file.read_text(encoding="utf-8") + assert pattern.search(content), "Pattern should match the project root path" + + +def test_audit_passes_clean_file(tmp_path) -> None: + """A test file with no hardcoded paths passes the audit patterns.""" + good_file = tmp_path / "test_good.py" + good_file.write_text("workspace = live_gui_workspace\n", encoding="utf-8") + import re + patterns = [ + re.compile(r'Path\(["\']tests/artifacts/'), + re.compile(r'Path\(["\']C:[/\\]+projects'), + ] + content = good_file.read_text(encoding="utf-8") + for p in patterns: + assert not p.search(content), f"Pattern {p} should not match clean file"