Skip to content

Commit 5b1aec5

Browse files
cpcloudclaude
andauthored
Isolate tests from global git config to prevent gpg-agent/pinentry (#531)
## Summary - Add `GIT_CONFIG_GLOBAL=/dev/null`, `GIT_CONFIG_NOSYSTEM=1`, and `GIT_TERMINAL_PROMPT=0` to `testenv.RunIsolatedMain()` so tests never read global/system git config or prompt for input — prevents `commit.gpgsign=true` from triggering gpg-agent/pinentry during test commits - Add `TestMain` calling `RunIsolatedMain()` to `internal/git`, `internal/prompt`, and `internal/worktree` packages that create git commits in tests but were missing it - Fix `TestEnsureAbsoluteHooksPath` subtest that simulates global config by explicitly setting `GIT_CONFIG_GLOBAL` to its fake gitconfig file (since `/dev/null` override would otherwise shadow it) 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent aab8089 commit 5b1aec5

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

internal/git/git_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@ import (
88
"strings"
99
"testing"
1010

11+
"github.com/roborev-dev/roborev/internal/testenv"
1112
"github.com/stretchr/testify/assert"
1213
"github.com/stretchr/testify/require"
1314
)
1415

16+
func TestMain(m *testing.M) {
17+
os.Exit(testenv.RunIsolatedMain(m))
18+
}
19+
1520
type TestRepo struct {
1621
T *testing.T
1722
Dir string
@@ -370,6 +375,7 @@ func TestEnsureAbsoluteHooksPath(t *testing.T) {
370375
fakeHome, ".config",
371376
))
372377
globalCfg := filepath.Join(fakeHome, ".gitconfig")
378+
t.Setenv("GIT_CONFIG_GLOBAL", globalCfg)
373379
err := os.WriteFile(globalCfg, []byte(
374380
"[core]\n\thooksPath = .githooks\n",
375381
), 0644)

internal/prompt/prompt_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,16 @@ import (
99
"github.com/roborev-dev/roborev/internal/config"
1010
gitpkg "github.com/roborev-dev/roborev/internal/git"
1111
"github.com/roborev-dev/roborev/internal/storage"
12+
"github.com/roborev-dev/roborev/internal/testenv"
1213
"github.com/roborev-dev/roborev/internal/testutil"
1314
"github.com/stretchr/testify/assert"
1415
"github.com/stretchr/testify/require"
1516
)
1617

18+
func TestMain(m *testing.M) {
19+
os.Exit(testenv.RunIsolatedMain(m))
20+
}
21+
1722
func TestBuildPromptWithoutContext(t *testing.T) {
1823
repoPath, commits := setupTestRepo(t)
1924
targetSHA := commits[len(commits)-1]

internal/testenv/testenv.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ func RunIsolatedMain(m *testing.M) int {
2323
}
2424
defer os.RemoveAll(tmpDir)
2525

26+
// Prevent global/system git config from leaking into tests.
27+
// Without this, commit.gpgsign=true in global config triggers
28+
// gpg-agent/pinentry during test commits. os.DevNull is
29+
// cross-platform (/dev/null on Unix, NUL on Windows).
30+
os.Setenv("GIT_CONFIG_GLOBAL", os.DevNull)
31+
os.Setenv("GIT_CONFIG_NOSYSTEM", "1")
32+
// Never allow git to prompt for input (passwords, passphrases, etc).
33+
// If something unexpected tries to prompt, fail fast instead of blocking.
34+
os.Setenv("GIT_TERMINAL_PROMPT", "0")
35+
2636
origEnv, hasEnv := os.LookupEnv("ROBOREV_DATA_DIR")
2737
os.Setenv("ROBOREV_DATA_DIR", tmpDir)
2838
defer func() {

internal/worktree/worktree_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@ package worktree
22

33
import (
44
"fmt"
5-
"github.com/stretchr/testify/assert"
6-
"github.com/stretchr/testify/require"
75
"os"
86
"os/exec"
97
"path/filepath"
108
"strings"
119
"testing"
10+
11+
"github.com/roborev-dev/roborev/internal/testenv"
12+
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/require"
1214
)
1315

16+
func TestMain(m *testing.M) {
17+
os.Exit(testenv.RunIsolatedMain(m))
18+
}
19+
1420
func runGit(t *testing.T, dir string, args ...string) string {
1521
t.Helper()
1622
cmd := exec.Command("git", append([]string{"-C", dir}, args...)...)

0 commit comments

Comments
 (0)