# our git hook tests escaped their temporary repositories

> Source: <https://dev.to/arian_gogani1/our-git-hook-tests-escaped-their-temporary-repositories-232k>
> Published: 2026-09-11 03:51:42+00:00

our pre-push check failed 13 tests. the bigger problem was that some of those tests had changed the repository they were supposed to protect.

this happened in Nobulex, the project i maintain. the investigation, fix and this write-up used AI assistance.

the tests create temporary Git repositories and run fixture commands inside them. we treated the subprocess working directory as the boundary. that assumption broke when the suite ran from a Git hook in an isolated worktree.

Git had exported `GIT_DIR` into the hook's environment. the test process inherited it. changing `cwd` to a temporary directory did not override the repository Git had already been told to use. fixture commands reached the calling repository's metadata instead.

the initial run affected our worktree branch and index, plus the shared `core.bare` setting. those were restored. no fixture commits were pushed publicly.

[Git's hook documentation](https://git-scm.com/docs/githooks#_description) describes this exact boundary: hooks inherit repository-local environment variables, and commands aimed at another repository or worktree should clear them.

the fix asks Git for that list with `git rev-parse --local-env-vars`, then removes those names from the selftest process environment before fixture imports or commands run. clearing them there preserves the parent hook's context for its own repository checks.

we then compared the old and fixed versions in disposable clones using the inherited hook environment:

`HEAD` changed.
the full pre-push guard also passed. [the patch is here](https://github.com/arian-gogani/nobulex-registry/commit/455cff3622d6638dbf4b9c6019eb0550c2407e69).

the useful check was the state comparison. a passing test count would not, by itself, tell us whether the caller had been left untouched. for tests that create repositories, the inherited environment belongs in the isolation check alongside the temporary directory.
