Skip to content

test(guard-pack): isolate the env-passthrough test from the runner's git branch - #49

Merged
karanb192 merged 1 commit into
mainfrom
fix/guard-pack-test-branch-isolation
Aug 20, 2026
Merged

test(guard-pack): isolate the env-passthrough test from the runner's git branch#49
karanb192 merged 1 commit into
mainfrom
fix/guard-pack-test-branch-isolation

Conversation

@karanb192

Copy link
Copy Markdown
Owner

The symptom

Tests on main (push) has been red on every push to main (e.g. the run after #48 merged, and identically on Aug 18 before it). The PR checks were always green, so it only ever showed up after merge.

Root cause

One test fails: guard-pack.test.js -> Integration: env passthrough -> "HOOK_SAFETY_LEVEL=strict tightens every guard in the pack". It runs git push --force origin feature-branch and asserts default mode returns {} (a force-push to a feature branch is only a strict-tier concern).

The actual output on main-push runs is a deny: [push-on-protected] Pushing from main is not allowed. That is git-safety's push-on-protected guard, which is branchOnly and resolves the branch with git branch --show-current in process.cwd(). runHook spawned the hook child without a cwd, so it inherited the runner's working directory:

  • push to main: HEAD is main -> guard fires -> test sees a deny instead of {} -> red.
  • pull_request: GitHub checks out a detached merge ref -> branch is not main -> guard stays quiet -> {} -> green.

So the test was coupled to whatever branch CI happened to be sitting on. Reproduced locally by running the suite with HEAD on main.

The fix

Spawn the hook child in the already-isolated TMP_HOME (created with mkdtemp, not a git repo). getCurrentBranch() then returns '', so branchOnly guards behave deterministically regardless of the runner's branch. This also makes the child's cwd match the cwd the payload already advertises.

Test-only change. git-safety's production behavior is unchanged: in real use the hook process runs from the project directory, so reading the branch from process.cwd() is correct.

Verified

Full suite 1584 pass / 0 fail with HEAD on main, the exact condition that was failing. The other test files that spawn the hook were checked and pass on main (their branch-dependent assertions pass an explicit branch to checkCommand).

The env-passthrough test asserts a default-mode force-push to a feature
branch returns {} (allowed), but git-safety's push-on-protected guard reads
the current branch via git in process.cwd(). runHook spawned the child
without a cwd, so it inherited the runner's working dir. On push-to-main CI
runs HEAD is main, the guard fired, and the test saw a deny instead of {}.
PR runs checked out a detached merge ref, so they stayed green and the
failure only ever showed on pushes to main.

Spawn the child in the isolated TMP_HOME (not a git repo), so branchOnly
guards read '' for the branch regardless of where CI is sitting. Test-only;
git-safety's production behavior is unchanged (in real use the hook process
cwd is the project dir). Full suite 1584/0 with HEAD on main, the exact
condition that was failing.
@karanb192
karanb192 merged commit 811aeb7 into main Aug 20, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant