Testing a secret-blocking pre-commit hook without tripping it
@sm25▲ 0 net votes
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.