Skip to content

Commit

Permalink
Unsetting local env vars in tests.
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Heidrich <[email protected]>
  • Loading branch information
aunovis-heidrich committed Jan 9, 2025
1 parent b52c11b commit 91d202c
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions clients/githubrepo/roundtripper/tokens/accessor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ package tokens
import (
"net/http/httptest"
"net/rpc"
"os"
"strings"
"testing"
)

//nolint:paralleltest // test uses t.Setenv
//nolint:paralleltest // test uses t.Setenv indirectly
func TestMakeTokenAccessor(t *testing.T) {
tests := []struct {
name string
Expand All @@ -40,10 +41,7 @@ func TestMakeTokenAccessor(t *testing.T) {
useServer: true,
},
}
// clear all env variables devs may have defined, or the test will fail locally
for _, envVar := range githubAuthTokenEnvVars {
t.Setenv(envVar, "")
}
unsetTokens(t)
t.Setenv(githubAuthServer, "")
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -92,7 +90,10 @@ func testServer(t *testing.T) {
}
}

//nolint:paralleltest // test uses t.Setenv indirectly
func TestClashingTokensDisplayWarning(t *testing.T) {
unsetTokens(t)

someToken := "test_token"
otherToken := "clashing_token"
t.Setenv("GITHUB_AUTH_TOKEN", someToken)
Expand All @@ -118,7 +119,10 @@ func TestClashingTokensDisplayWarning(t *testing.T) {
}
}

//nolint:paralleltest // test uses t.Setenv indirectly
func TestConsistentTokensDoNotDisplayWarning(t *testing.T) {
unsetTokens(t)

someToken := "test_token"
t.Setenv("GITHUB_AUTH_TOKEN", someToken)
t.Setenv("GITHUB_TOKEN", someToken)
Expand All @@ -143,7 +147,10 @@ func TestConsistentTokensDoNotDisplayWarning(t *testing.T) {
}
}

//nolint:paralleltest // test uses t.Setenv indirectly
func TestNoTokensDoNoDisplayWarning(t *testing.T) {
unsetTokens(t)

warningCalled := false
originalLogWarning := logDuplicateTokenWarning
logDuplicateTokenWarning = func(firstName string, clashingName string) {
Expand All @@ -163,3 +170,14 @@ func TestNoTokensDoNoDisplayWarning(t *testing.T) {
t.Errorf("Expected logWarning to not have been called for no set tokens, but it was not.")
}
}

// temporarily unset all of the github token env vars,
// as tests may otherwise fail depending on the local environment.
func unsetTokens(t *testing.T) {
t.Helper()
for _, name := range githubAuthTokenEnvVars {
// equivalent to t.Unsetenv (which does not exist)
t.Setenv(name, "")
os.Unsetenv(name)
}
}

0 comments on commit 91d202c

Please sign in to comment.