cli: isolate the token store in tests so spawned binaries skip the keychain · Entire
cli: isolate the token store in tests so spawned binaries skip the keychain
6a2ed44→main·
Soph·1mo ago·1 file·+25 added/-5 removed
The cli package TestMain mocked the in-process zalando keyring, but several tests spawn the real entire binary (or a git hook that invokes it). testing.Testing() is false in that child, so neither the in-memory keyring mock nor the internal/testdirs fallback applies, and the child's tokenstore default backend reaches the developer's real OS keychain — triggering a macOS unlock prompt during go test ./cmd/entire/cli/.
Set ENTIRE_TOKEN_STORE=file (plus an isolated token/auth/config/cache path) process-wide in TestMain before m.Run, so spawned children inherit file-backed isolation. Mirrors the integration and e2e TestMains. The in-memory keyring mock stays for in-process legacy auth.NewStore paths.
Co-Authored-By: Claude Opus 4.8 (1M context) noreply@anthropic.com
Sessions
66eeae1e1eceView transcript
Changes
1
cmd/entire/cli
Mglobal_test.go+25/-5
2 unmodified lines
3
4
5
6
7
8
9
10 unmodified lines
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
25
44
45
27
28
29
46
47
48
49
32
50
51
52
53
2 unmodified lines
import (
"fmt"
"os"
"path/filepath"
"testing"
"github.com/go-git/go-git/v6/x/plugin"
10 unmodified lines
// auth subpackage's TestMain.
keyring.MockInit()
// keyring.MockInit only covers in-process credential access. Several tests
// in this package spawn the real entire binary (or a git hook that invokes
// it), and testing.Testing() is false in that child — so the internal
// testdirs fallback and the in-memory keyring mock don't apply there, and
// the child's tokenstore default backend reaches the developer's real OS
// keychain. Set the file-backed token store and isolated config/cache dirs
// process-wide so spawned children inherit them. Mirrors the integration
// and e2e TestMains.
isolationDir, err := os.MkdirTemp("", "entire-cli-test-*")
if err != nil {
panic(fmt.Errorf("failed to create test isolation dir: %w", err))
}
os.Setenv("ENTIRE_TOKEN_STORE", "file")
os.Setenv("ENTIRE_TOKEN_STORE_PATH", filepath.Join(isolationDir, "tokenstore.json"))
os.Setenv("ENTIRE_TEST_AUTH_STORE_FILE", filepath.Join(isolationDir, "auth-tokens.json"))
os.Setenv("ENTIRE_CONFIG_DIR", filepath.Join(isolationDir, "config"))
os.Setenv("XDG_CACHE_HOME", filepath.Join(isolationDir, "cache"))
// Register a default ConfigSource so tests that call ConfigScoped
// (directly or indirectly via Commit/CreateTag) don't fail with
// "no config loader registered".
err := plugin.Register(plugin.ConfigLoader(), func() plugin.ConfigSource {
if regErr := plugin.Register(plugin.ConfigLoader(), func() plugin.ConfigSource {
return config.NewEmpty()
})
if err != nil {
panic(fmt.Errorf("failed to register config storers: %w", err))
}); regErr != nil {
panic(fmt.Errorf("failed to register config storers: %w", regErr))
}
os.Exit(m.Run())
code := m.Run()
_ = os.RemoveAll(isolationDir)
os.Exit(code)
}
Mcmd/entire/cli/global_test.go+25/-5