diff --git a/.claude/hooks/test.sh b/.claude/hooks/test.sh index 33c61b301..1a472a2dc 100755 --- a/.claude/hooks/test.sh +++ b/.claude/hooks/test.sh @@ -12,6 +12,9 @@ if ! command -v uv &> /dev/null; then fi echo "=== Running tests ===" +# pre-push runs this script, and git exports these to hooks. Tests that run git +# in a throwaway repo would otherwise act on this one. +unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE # Note: Using timeout to prevent hanging tests from blocking indefinitely (5 min max) # Matches .github/workflows/test.yml exactly to catch CI failures before push PYTHONPATH=src:envs timeout 300 uv run pytest tests/ \ diff --git a/tests/scripts/test_lint_hook.py b/tests/scripts/test_lint_hook.py index ed4a05ea5..19b680c01 100644 --- a/tests/scripts/test_lint_hook.py +++ b/tests/scripts/test_lint_hook.py @@ -25,6 +25,7 @@ REPO_ROOT = pathlib.Path(__file__).resolve().parents[2] LINT_HOOK = REPO_ROOT / ".claude" / "hooks" / "lint.sh" +TEST_HOOK = REPO_ROOT / ".claude" / "hooks" / "test.sh" # The stub stands in for `uv`. In check mode it reports whether any target file # still carries the marker; otherwise it rewrites them, standing in for a @@ -108,6 +109,10 @@ def repo(tmp_path: pathlib.Path) -> pathlib.Path: stub = bin_dir / "uv" stub.write_text(STUB_UV) stub.chmod(0o755) + # Stock macOS has no `timeout`, which test.sh calls. + timeout = bin_dir / "timeout" + timeout.write_text('#!/bin/bash\nshift\nexec "$@"\n') + timeout.chmod(0o755) return work @@ -313,3 +318,28 @@ def test_git_hooks_leave_uncommitted_work_alone(repo: pathlib.Path, hook: str) - f"The {hook} hook changed the working tree or the index. It must leave " "formatting to lint.sh, which only reports." ) + # src/mod.py is still unformatted, so the hook must still block. + assert result.returncode != 0 + + +def test_test_hook_clears_git_hook_environment(repo: pathlib.Path) -> None: + """test.sh must not pass the git hook environment on to the tests. + + pre-push runs test.sh, and git exports `GIT_DIR` to hooks in a linked + worktree. Tests that run git in a throwaway repo then commit to and + configure the developer's repository instead. + """ + (repo.parent / "stubbin" / "uv").write_text("#!/bin/bash\nenv\n") + git_env = { + "GIT_DIR": str(repo / ".git"), + "GIT_WORK_TREE": str(repo), + "GIT_INDEX_FILE": str(repo / ".git" / "index"), + } + + result = _run_hook(repo, TEST_HOOK, **git_env) + + assert result.returncode == 0, result.stdout + result.stderr + passed_on = [ + line for line in result.stdout.splitlines() if line.split("=")[0] in git_env + ] + assert passed_on == [], f"test.sh passed {passed_on} on to pytest."