Clear all possible token env vars for token accessor test. (#3347)

Signed-off-by: Spencer Schrock <sschrock@google.com>
This commit is contained in:
Spencer Schrock 2023-08-03 21:31:01 -07:00 committed by GitHub
parent b1f3519d6e
commit f30ff23d82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 7 deletions

View File

@ -23,6 +23,9 @@ import (
// githubAuthServer is the RPC URL for the token server.
const githubAuthServer = "GITHUB_AUTH_SERVER"
// env variables from which GitHub auth tokens are read, in order of precedence.
var githubAuthTokenEnvVars = []string{"GITHUB_AUTH_TOKEN", "GITHUB_TOKEN", "GH_TOKEN", "GH_AUTH_TOKEN"}
// TokenAccessor interface defines a `retrieve-once` data structure.
// Implementations of this interface must be thread-safe.
type TokenAccessor interface {
@ -31,8 +34,7 @@ type TokenAccessor interface {
}
func readGitHubTokens() (string, bool) {
githubAuthTokens := []string{"GITHUB_AUTH_TOKEN", "GITHUB_TOKEN", "GH_TOKEN", "GH_AUTH_TOKEN"}
for _, name := range githubAuthTokens {
for _, name := range githubAuthTokenEnvVars {
if token, exists := os.LookupEnv(name); exists && token != "" {
return token, exists
}
@ -45,7 +47,7 @@ func MakeTokenAccessor() TokenAccessor {
if value, exists := readGitHubTokens(); exists {
return makeRoundRobinAccessor(strings.Split(value, ","))
}
if value, exists := os.LookupEnv(githubAuthServer); exists {
if value, exists := os.LookupEnv(githubAuthServer); exists && value != "" {
return makeRPCAccessor(value)
}
return nil

View File

@ -40,16 +40,17 @@ func TestMakeTokenAccessor(t *testing.T) {
useServer: true,
},
}
t.Setenv("GITHUB_AUTH_TOKEN", "")
t.Setenv("GITHUB_TOKEN", "")
// clear all env variables devs may have defined, or the test will fail locally
for _, envVar := range githubAuthTokenEnvVars {
t.Setenv(envVar, "")
}
t.Setenv(githubAuthServer, "")
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
switch {
case tt.useGitHubToken:
t.Helper()
testToken(t)
case tt.useServer:
t.Helper()
testServer(t)
default:
got := MakeTokenAccessor()