Skip to content

Commit 9898dc8

Browse files
committed
Clear all token env vars in resolve tests
Follow-up to #105. TestTokenForDomain still failed if FORGE_TOKEN was set in the shell because the first assertion expects an empty result for example.com. Add a clearTokenEnv helper that blanks every token var TokenForDomainEnv reads, and call it at the top of each token test so each test only sets the vars it actually exercises.
1 parent 3385a9a commit 9898dc8

1 file changed

Lines changed: 17 additions & 4 deletions

File tree

internal/resolve/resolve_test.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,28 @@ func TestMapSSHHostNoConfig(t *testing.T) {
5555
}
5656
}
5757

58+
func clearTokenEnv(t *testing.T) {
59+
t.Helper()
60+
for _, v := range []string{
61+
"GITHUB_TOKEN", "GH_TOKEN",
62+
"GITLAB_TOKEN", "GLAB_TOKEN",
63+
"GITEA_TOKEN", "BITBUCKET_TOKEN",
64+
"FORGE_TOKEN",
65+
} {
66+
t.Setenv(v, "")
67+
}
68+
}
69+
5870
func TestTokenForDomain(t *testing.T) {
71+
clearTokenEnv(t)
72+
5973
// With no env vars set, should return empty
6074
got := TokenForDomain("example.com")
6175
if got != "" {
6276
t.Errorf("expected empty token, got %q", got)
6377
}
6478

6579
// FORGE_TOKEN is a fallback for any domain
66-
t.Setenv("GITHUB_TOKEN", "")
67-
t.Setenv("GH_TOKEN", "")
6880
t.Setenv("FORGE_TOKEN", "forge-tok")
6981
got = TokenForDomain("github.com")
7082
if got != "forge-tok" {
@@ -97,6 +109,7 @@ func TestTokenForDomain(t *testing.T) {
97109
}
98110

99111
func TestTokenForDomainEnvSpecificOverridesFallback(t *testing.T) {
112+
clearTokenEnv(t)
100113
t.Setenv("GITHUB_TOKEN", "gh-specific")
101114
t.Setenv("FORGE_TOKEN", "forge-fallback")
102115

@@ -107,8 +120,7 @@ func TestTokenForDomainEnvSpecificOverridesFallback(t *testing.T) {
107120
}
108121

109122
func TestTokenForDomainEnvFallbackToForgeToken(t *testing.T) {
110-
t.Setenv("GITHUB_TOKEN", "")
111-
t.Setenv("GH_TOKEN", "")
123+
clearTokenEnv(t)
112124
t.Setenv("FORGE_TOKEN", "forge-fallback")
113125

114126
got := TokenForDomainEnv("github.com")
@@ -118,6 +130,7 @@ func TestTokenForDomainEnvFallbackToForgeToken(t *testing.T) {
118130
}
119131

120132
func TestTokenForDomainEnvFallbackForUnknownDomain(t *testing.T) {
133+
clearTokenEnv(t)
121134
t.Setenv("FORGE_TOKEN", "forge-fallback")
122135

123136
got := TokenForDomainEnv("custom.example.com")

0 commit comments

Comments
 (0)