diff --git a/.github/workflows/markdown-validation.yml b/.github/workflows/markdown-validation.yml index b176517..688dd9d 100644 --- a/.github/workflows/markdown-validation.yml +++ b/.github/workflows/markdown-validation.yml @@ -102,6 +102,9 @@ jobs: git fetch --no-tags --depth=1 origin "${BASE_SHA}" python3 scripts/apm-external-skills.py check --base "${BASE_SHA}" + - name: Check test fixtures leave a commit hook's repo alone + run: PYTHONDONTWRITEBYTECODE=1 python3 -m unittest discover -s tests -p 'test_hook_git_env.py' -v + - name: Test SkillSpector gate and report run: PYTHONDONTWRITEBYTECODE=1 python3 -m unittest discover -s tests -p 'test_skillspector*.py' -v diff --git a/.lefthook.toml b/.lefthook.toml index 05bb6ca..2b98d70 100644 --- a/.lefthook.toml +++ b/.lefthook.toml @@ -34,6 +34,10 @@ run = "PYTHONDONTWRITEBYTECODE=1 sh scripts/py.sh -m unittest discover -s tests [pre-commit.commands.apm-external-skills] run = "PYTHONDONTWRITEBYTECODE=1 sh scripts/py.sh -m unittest discover -s tests -p 'test_apm_external_skills.py'" +# Fixture repos must not touch the repo a commit hook points at (core.bare; #251). +[pre-commit.commands.hook-git-env] +run = "PYTHONDONTWRITEBYTECODE=1 sh scripts/py.sh -m unittest discover -s tests -p 'test_hook_git_env.py'" + # SkillSpector gate and report: severity policy, reviewed acceptances, PR comment. [pre-commit.commands.skillspector-gate] run = "PYTHONDONTWRITEBYTECODE=1 sh scripts/py.sh -m unittest discover -s tests -p 'test_skillspector*.py'" diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 4b819ec..6b46c38 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -121,6 +121,12 @@ What runs on **pre-commit**: — checks `apm.yml` doesn't list `ai-toolkit` as its own dependency. - `PYTHONDONTWRITEBYTECODE=1 sh scripts/py.sh -m unittest discover -s tests -p 'test_apm_overrides.py'` — checks vendored agents keep their `apm-overrides.json` pins. +- `PYTHONDONTWRITEBYTECODE=1 sh scripts/py.sh -m unittest discover -s tests -p 'test_apm_external_skills.py'` + — checks every locked external dependency still deploys its files. +- `PYTHONDONTWRITEBYTECODE=1 sh scripts/py.sh -m unittest discover -s tests -p 'test_hook_git_env.py'` + — runs the git-fixture tests with the `GIT_DIR` / `GIT_INDEX_FILE` a commit + in a linked worktree exports, and checks they don't touch that repo (such a + leak once wrote `core.bare = true` to the main checkout's config). - `PYTHONDONTWRITEBYTECODE=1 sh scripts/py.sh -m unittest discover -s tests -p 'test_skillspector*.py'` — checks the SkillSpector severity gate, reviewed acceptances and report rendering. - `bash scripts/stage-index.sh` — regenerates `INDEX.md` / `README.md` from the diff --git a/tests/test_hook_git_env.py b/tests/test_hook_git_env.py new file mode 100644 index 0000000..fcae752 --- /dev/null +++ b/tests/test_hook_git_env.py @@ -0,0 +1,67 @@ +"""Test fixtures must leave alone the repo a commit hook points at (#251). + +In a linked worktree, `git commit` exports GIT_DIR=
/.git/worktrees/ +and GIT_INDEX_FILE to the hooks. A fixture `git init` that inherits them +re-initialises that git dir, takes it for a bare repo (its path doesn't end in +.git) and writes `core.bare = true` to the shared
/.git/config, which +breaks the main checkout; fixture commits land on its HEAD. Run every suite +that creates git fixtures with such an environment, pointed at a throwaway +main repo and worktree, and check that nothing there changed. +""" + +import os +from pathlib import Path +import shutil +import subprocess +import sys +import tempfile +import unittest + + +TESTS = Path(__file__).resolve().parent +SUITES = ["test_apm_external_skills.WorkTreeTests", "test_renovate_prs.RepositoryScriptTests"] +if os.name != "nt" and shutil.which("jq"): + # The push-hook tests need a POSIX shell and jq, which stock macOS lacks. + SUITES.append("test_hook_scripts.FeatureBranchPushTest") + + +def without_git_env(): + return {k: v for k, v in os.environ.items() if not k.startswith("GIT_")} + + +def git(*args): + return subprocess.run(["git", "-c", "user.name=t", "-c", "user.email=t@t", "-c", "commit.gpgsign=false", *args], + env=without_git_env(), check=True, capture_output=True, encoding="utf-8", timeout=60).stdout + + +def snapshot(main, gitdir): + files = {str(path): path.read_bytes() for path in (main / ".git/HEAD", gitdir / "HEAD", gitdir / "index")} + return git("-C", str(main), "for-each-ref", "--format=%(refname) %(objectname)"), files + + +class HookGitEnvironmentTests(unittest.TestCase): + def test_git_fixtures_leave_the_hook_repo_alone(self): + with tempfile.TemporaryDirectory(prefix="hook-git-env-") as tmp: + main, worktree = Path(tmp) / "main", Path(tmp) / "worktree" + git("init", "-q", "-b", "main", str(main)) + (main / "README.md").write_text("x\n", encoding="utf-8") + git("-C", str(main), "add", "README.md") + git("-C", str(main), "commit", "-qm", "init") + git("-C", str(main), "worktree", "add", "-q", "-b", "feature", str(worktree)) + gitdir = Path(git("-C", str(worktree), "rev-parse", "--absolute-git-dir").strip()) + config = (main / ".git/config").read_text(encoding="utf-8") + before = snapshot(main, gitdir) + + # What `git commit` in the worktree hands its pre-commit hooks. + hook_env = {**without_git_env(), "PYTHONDONTWRITEBYTECODE": "1", "PYTHONPATH": str(TESTS), + "GIT_DIR": gitdir.as_posix(), "GIT_INDEX_FILE": (gitdir / "index").as_posix()} + result = subprocess.run([sys.executable, "-m", "unittest", *SUITES], cwd=worktree, env=hook_env, + capture_output=True, text=True, timeout=600, check=False) + + self.assertEqual(result.returncode, 0, result.stderr[-4000:]) + self.assertEqual((main / ".git/config").read_text(encoding="utf-8"), config) + self.assertEqual(snapshot(main, gitdir), before, "a fixture changed the hook repo's refs or index") + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_hook_scripts.py b/tests/test_hook_scripts.py index c583c74..98fe3e0 100644 --- a/tests/test_hook_scripts.py +++ b/tests/test_hook_scripts.py @@ -71,8 +71,13 @@ def hook_scripts(): return sorted(scripts) +def without_git_env(): + # Drop the GIT_DIR / GIT_INDEX_FILE a commit hook exports (test_hook_git_env.py). + return {k: v for k, v in os.environ.items() if not k.startswith("GIT_")} + + def run_hook(script, payload, **env): - merged = {**os.environ, "SKIP_PUSH_APPROVAL": "", "SKIP_TOOL_GUARD": "", **env} + merged = {**without_git_env(), "SKIP_PUSH_APPROVAL": "", "SKIP_TOOL_GUARD": "", **env} return subprocess.run( [HOOK_BASH, str(script)], input=json.dumps(payload), @@ -244,9 +249,10 @@ def setUpClass(cls): gitconfig = Path(cls.tmp.name) / "gitconfig" gitconfig.write_text("", encoding="utf-8") cls.env = {"GIT_CONFIG_GLOBAL": str(gitconfig), "GIT_CONFIG_NOSYSTEM": "1"} + # Fixture git runs without the GIT_DIR / GIT_INDEX_FILE a commit hook exports. + cls.git_env = {**without_git_env(), **cls.env} cls.repo = str(Path(cls.tmp.name) / "repo") - subprocess.run(["git", "init", "-q", "-b", "main", cls.repo], check=True, - env={**os.environ, **cls.env}) + subprocess.run(["git", "init", "-q", "-b", "main", cls.repo], check=True, env=cls.git_env) for args in ( ["-c", "user.name=t", "-c", "user.email=t@example.com", "commit", "-q", "--allow-empty", "-m", "init"], ["remote", "add", "origin", "https://example.invalid/repo.git"], @@ -269,8 +275,7 @@ def tearDownClass(cls): @classmethod def git(cls, *args): - subprocess.run(["git", "-C", cls.repo, *args], check=True, capture_output=True, - env={**os.environ, **cls.env}) + subprocess.run(["git", "-C", cls.repo, *args], check=True, capture_output=True, env=cls.git_env) def setUp(self): self.git("checkout", "-q", "feat")