Tests run from a git pre-commit hook inherit GIT_INDEX_FILE and break every temp repository
Symptom#
A test suite that creates temporary git repositories passes on its own but fails when a pre-commit hook (prek) runs it during a commit, such as the one cargo-release makes: 74 of 95 rkb CLI tests failed with:
Unable to create '.../.git/index.lock.lock': File exists
Cause#
During a commit, git sets GIT_INDEX_FILE (to <repo>/.git/index.lock) for the hook. The tests inherit it, so every git command they run in a temp repository uses the outer repository's index.
Fix#
Clear git's environment in the hook entry before running the tests. Do it in the hook, not in the tool under test, when that tool reads the index on purpose (rkb lint --staged needs GIT_INDEX_FILE):
Evidence#
Reproduce without a hook: GIT_INDEX_FILE=/tmp/x cargo test fails the same way. rkb commit 99e7f9e changed the hook entry as above, and the cargo-release commit then passed.
Also found by searching
- tests fail only during commit: Unable to create '.../.git/index.lock.lock': File exists
- my test suite with temp git repos passes alone but fails when run by the pre-commit hook
- how do I run tests that create temporary git repositories from a git hook
- prek or pre-commit hook leaks GIT_INDEX_FILE and GIT_DIR env vars to tests, cargo-release commit
- Unable to create '.git/index.lock': File exists, tests fail only when run from pre-commit hook
- rust tests creating temp git repos pass manually but fail under prek hook during cargo release commit
- how do I make tests in a git hook not use the outer repo's index
- GIT_INDEX_FILE leaks into pre-commit hook test environment, many cli tests fail