Skip to content

fix(git-guards): mock _is_on_main_branch() via GIT_GUARD_BRANCH_OVERRIDE#263

Open
JacobPEvans wants to merge 4 commits intomainfrom
fix/issue-261-test-branch-detection
Open

fix(git-guards): mock _is_on_main_branch() via GIT_GUARD_BRANCH_OVERRIDE#263
JacobPEvans wants to merge 4 commits intomainfrom
fix/issue-261-test-branch-detection

Conversation

@JacobPEvans
Copy link
Copy Markdown
Owner

@JacobPEvans JacobPEvans commented Apr 28, 2026

Closes #261

Summary

Three tests in git-guards/scripts/test_permission_guard.py fail on the main branch because _is_on_main_branch() returns true (GitHub Actions CI has a detached HEAD), causing the main-branch guard to block test logic before hook-bypass detection can run. This PR adds an environment variable escape hatch so tests can override branch detection without touching git.

Changes

  • git-permission-guard.py: Added GIT_GUARD_BRANCH_OVERRIDE env-var check at the top of _is_on_main_branch(). When set, returns (override == "main") directly without calling git branch --show-current.
  • test_permission_guard.py: Wrapped 3 affected tests with a branch_override() context manager (using @contextmanager from contextlib) that safely saves/restores the env-var. Tests now run with GIT_GUARD_BRANCH_OVERRIDE=feature to simulate non-main branch.

Test Plan


Generated by Issue Solver — prompt source: https://github.com/JacobPEvans/claude-code-routines/blob/main/routines/issue-solver.prompt.md

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a GIT_GUARD_BRANCH_OVERRIDE environment variable to the _is_on_main_branch function, allowing the branch status to be mocked. The test suite has been updated to use this override, ensuring consistent test results regardless of the actual git environment. I have no feedback to provide.

@JacobPEvans JacobPEvans marked this pull request as ready for review April 28, 2026 14:00
Copilot AI review requested due to automatic review settings April 28, 2026 14:00
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses CI failures in git-guards/scripts/test_permission_guard.py when tests run on the main branch by introducing an environment-variable override to make _is_on_main_branch() deterministic in CI.

Changes:

  • Added GIT_GUARD_BRANCH_OVERRIDE handling in _is_on_main_branch() to bypass git-based branch detection when set.
  • Updated three permission-guard tests to set/unset the override so they validate hook-bypass/force-push logic rather than the main-branch block.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
git-guards/scripts/git-permission-guard.py Adds env-var branch override path to _is_on_main_branch() to avoid CI branch-state coupling.
git-guards/scripts/test_permission_guard.py Sets GIT_GUARD_BRANCH_OVERRIDE=feature around three cases that otherwise get blocked on main.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread git-guards/scripts/test_permission_guard.py Outdated
Comment thread git-guards/scripts/test_permission_guard.py Outdated
Comment thread git-guards/scripts/git-permission-guard.py
Comment thread git-guards/scripts/test_permission_guard.py Outdated
…H_OVERRIDE

- Add branch_override() context manager to preserve/restore any
  pre-existing GIT_GUARD_BRANCH_OVERRIDE value around each test call
- Replace three raw set/pop blocks with 'with branch_override("feature"):'
- Add docstring note to _is_on_main_branch() documenting the override
  path and its CI/test-only intent
@JacobPEvans JacobPEvans changed the title 🤖 Fix #261: make _is_on_main_branch() mockable via env-var for CI fix(git-guards): mock _is_on_main_branch() via GIT_GUARD_BRANCH_OVERRIDE Apr 29, 2026
JacobPEvans added a commit that referenced this pull request Apr 29, 2026
* chore(git-standards): forbid emoji in commits and PR titles

Adds an explicit "Commit & PR Title Style" section to the pr-standards
skill (loaded on demand whenever an agent drafts a PR), and a one-line
cross-reference bullet in git-workflow-standards.

Companion to the soul.md flip in ai-assistant-instructions. Together
these eliminate the gap that caused recent automated AI-fix PRs (#263,
#257) to land with `🤖` prefixes — soul.md was endorsing gitmoji and
neither plugin skill said otherwise.

Applies to human, AI-assisted, and bot-authored PRs alike. Conventional-
commit prefixes carry the signal.

(claude)

* fix(git-standards): clarify emoji-ban scope and conventional-commit phrasing

Addresses copilot review feedback (threads VOoy, VOpG, VOpT) on PR #266.

Changes:
- Rename section header from "Commit & PR Title Style" to
  "Commit, PR Title & PR Description Style" (header now matches body scope)
- Split rule into two parts: emoji ban (commits + titles + descriptions
  + release notes) and conventional-commit prefix requirement (commit
  subjects + PR titles only)
- Replace ambiguous "Use conventional-commit prefixes only" with "are
  required at the start of commit subjects and PR titles only" — the
  original "only" was meant to scope where prefixes apply, but read as
  if the prefix should be the entire message
- Add release notes to the ban for parity with the soul.md update in
  ai-assistant-instructions PR #604

(claude)
Replace the hand-rolled branch_override @contextmanager with a thin wrapper
around unittest.mock.patch.dict — stdlib already provides the exact
save-and-restore semantics (including key-deletion when previously unset).

(claude)
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.

CI Failure DoctorCI Failure: test_permission_guard.py fails on main branch (BLOCKED_ON_MAIN intercepts tests)

2 participants