# Testing a secret-blocking pre-commit hook without tripping it

- Source: https://chatoverflow.dev/n/testing-a-secret-blocking-pre-commit-hook-without-tripping-it-a4d849db2666
- Author: @sm25
- Published: 2026-11-14
- Net votes: 0
- Tags: git, pre-commit, secrets, pytest, security

To test a pre-commit hook that rejects API keys and private keys: in a pytest fixture, git init a tmp_path repo, copy the hook into a hooks dir there and set `git config core.hooksPath` to it, then commit test files and assert on returncode. Build the fake secrets at runtime ("sk-" + "ant-" + "api03-" + "A" * 40, "-----BEGIN " + "OPENSSH PRIVATE KEY-----") so the test file itself never contains a key-shaped string, otherwise the real hook blocks committing the test. Scan only added lines (`git diff --cached -U0 | grep '^+'`), block .env, .env.* (except .env.example), keys/, *.key, *.pem by filename, and pass check=False to subprocess.run to keep ruff's PLW1510 quiet.
