Skip to content

Commit

Permalink
Add default host as known host when environment auth token exists (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
samcoe committed May 9, 2022
1 parent 234a47d commit 33e81eb
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
3 changes: 3 additions & 0 deletions internal/config/config.go
Expand Up @@ -72,6 +72,9 @@ func (c config) Hosts() []string {
if host := os.Getenv(ghHost); host != "" {
hosts.Add(host)
}
if token, _ := c.AuthToken(defaultHost); token != "" {
hosts.Add(defaultHost)
}
entries := c.hosts.keys()
hosts.AddValues(entries)
return hosts.ToSlice()
Expand Down
59 changes: 59 additions & 0 deletions internal/config/config_test.go
Expand Up @@ -398,6 +398,65 @@ func TestConfigHost(t *testing.T) {
}
}

func TestConfigHosts(t *testing.T) {
tests := []struct {
name string
cfg Config
ghHost string
ghToken string
wantHosts []string
}{
{
name: "no known hosts",
cfg: testLoadedNoHostConfig(),
wantHosts: []string{},
},
{
name: "includes GH_HOST",
cfg: testLoadedNoHostConfig(),
ghHost: "test.com",
wantHosts: []string{"test.com"},
},
{
name: "includes authenticated hosts",
cfg: testLoadedConfig(),
wantHosts: []string{"github.com", "enterprise.com"},
},
{
name: "includes default host if environment auth token",
cfg: testLoadedNoHostConfig(),
ghToken: "TOKEN",
wantHosts: []string{"github.com"},
},
{
name: "deduplicates hosts",
cfg: testLoadedConfig(),
ghHost: "test.com",
ghToken: "TOKEN",
wantHosts: []string{"test.com", "github.com", "enterprise.com"},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.ghHost != "" {
k := "GH_HOST"
old := os.Getenv(k)
os.Setenv(k, tt.ghHost)
defer os.Setenv(k, old)
}
if tt.ghToken != "" {
k := "GH_TOKEN"
old := os.Getenv(k)
os.Setenv(k, tt.ghToken)
defer os.Setenv(k, old)
}
hosts := tt.cfg.Hosts()
assert.Equal(t, tt.wantHosts, hosts)
})
}
}

func TestConfigAuthToken(t *testing.T) {
orig_GITHUB_TOKEN := os.Getenv("GITHUB_TOKEN")
orig_GITHUB_ENTERPRISE_TOKEN := os.Getenv("GITHUB_ENTERPRISE_TOKEN")
Expand Down
1 change: 1 addition & 0 deletions internal/set/string_set.go
Expand Up @@ -10,6 +10,7 @@ type stringSet struct {
func NewStringSet() *stringSet {
s := &stringSet{}
s.m = make(map[string]struct{})
s.v = []string{}
return s
}

Expand Down

0 comments on commit 33e81eb

Please sign in to comment.