mirror of
https://github.com/ossf/scorecard.git
synced 2024-09-11 08:55:27 +03:00
Clear all possible token env vars for token accessor test. (#3347)
Signed-off-by: Spencer Schrock <sschrock@google.com>
This commit is contained in:
parent
b1f3519d6e
commit
f30ff23d82
@ -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
|
||||
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user