Fix GitProtocol signature and rework test descriptions

This commit is contained in:
William Martin 2023-10-19 12:48:30 +02:00
parent b9cbee0462
commit 7d8c1af009
4 changed files with 36 additions and 27 deletions

View file

@ -107,7 +107,7 @@ func TestTokenFromKeyringNonExistent(t *testing.T) {
}
func TestHasEnvTokenWithoutAnyEnvToken(t *testing.T) {
// Given an empty hosts configuration
// Given we have no env set
authCfg := newTestAuthConfig(t)
// When we check if it has an env token
@ -118,7 +118,8 @@ func TestHasEnvTokenWithoutAnyEnvToken(t *testing.T) {
}
func TestHasEnvTokenWithEnvToken(t *testing.T) {
// Given an empty hosts configuration but a token set in the env var
// Given we have an env token set
// Note that any valid env var for tokens will do, not just GH_ENTERPRISE_TOKEN
authCfg := newTestAuthConfig(t)
t.Setenv("GH_ENTERPRISE_TOKEN", "test-token")
@ -158,17 +159,28 @@ func TestUserNotLoggedIn(t *testing.T) {
}
func TestGitProtocolNotLoggedInDefaults(t *testing.T) {
// Given a host configuration without a git protocol
// Given we have not logged in
authCfg := newTestAuthConfig(t)
// When we get the git protocol
gitProtocol, err := authCfg.GitProtocol("github.com")
gitProtocol := authCfg.GitProtocol("github.com")
// Then it returns success, using the default
require.NoError(t, err)
// Then it returns the default
require.Equal(t, "https", gitProtocol)
}
func TestHostsIncludesEnvVar(t *testing.T) {
// Given the GH_HOST env var is set
authCfg := newTestAuthConfig(t)
t.Setenv("GH_HOST", "ghe.io")
// When we get the hosts
hosts := authCfg.Hosts()
// Then the host in the env var is included
require.Contains(t, hosts, "ghe.io")
}
func TestDefaultHostFromEnvVar(t *testing.T) {
// Given the GH_HOST env var is set
authCfg := newTestAuthConfig(t)
@ -284,31 +296,28 @@ func TestLoginSetsUserForProvidedHost(t *testing.T) {
}
func TestLoginSetsGitProtocolForProvidedHost(t *testing.T) {
// Given we are not logged in
// Given we are loggedin
authCfg := newTestAuthConfig(t)
// When we login
_, err := authCfg.Login("github.com", "test-user", "test-token", "ssh", false)
// Then it returns success and the git protocol is set
require.NoError(t, err)
gitProtocol, err := authCfg.GitProtocol("github.com")
require.NoError(t, err)
// When we get the git protocol
gitProtocol := authCfg.GitProtocol("github.com")
// Then it returns the git protocol we provided on login
require.Equal(t, "ssh", gitProtocol)
}
func TestLoginAddsHostIfNotAlreadyAdded(t *testing.T) {
// Given we are not logged in
// Given we are logged in
authCfg := newTestAuthConfig(t)
// When we login
_, err := authCfg.Login("github.com", "test-user", "test-token", "ssh", false)
// Then it returns success and a host is added
require.NoError(t, err)
// When we get the hosts
hosts := authCfg.Hosts()
// Then it includes our logged in host
require.Contains(t, hosts, "github.com")
}

View file

@ -164,19 +164,19 @@ func (c *AuthConfig) User(hostname string) (string, error) {
// GitProtocol will retrieve the git protocol for the logged in user at the given hostname.
// If none is set it will return the default value.
// TODO: although this returns an error, it actually has no path to error.
func (c *AuthConfig) GitProtocol(hostname string) (string, error) {
func (c *AuthConfig) GitProtocol(hostname string) string {
key := "git_protocol"
val, err := c.cfg.Get([]string{hosts, hostname, key})
if err == nil {
return val, err
if val, err := c.cfg.Get([]string{hosts, hostname, key}); err == nil {
return val
}
if val, ok := defaultFor(key); ok {
return val, nil
return val
}
return "", nil
// This should not happen, as we know there is a default value for this key.
// Perhaps it says something about our current default abstraction being not quite right?
return ""
}
func (c *AuthConfig) Hosts() []string {

View file

@ -178,7 +178,7 @@ func refreshRun(opts *RefreshOptions) error {
Prompter: opts.Prompter,
GitClient: opts.GitClient,
}
gitProtocol, _ := authCfg.GitProtocol(hostname)
gitProtocol := authCfg.GitProtocol(hostname)
if opts.Interactive && gitProtocol == "https" {
if err := credentialFlow.Prompt(hostname); err != nil {
return err

View file

@ -139,7 +139,7 @@ func statusRun(opts *StatusOptions) error {
}
addMsg("%s Logged in to %s as %s (%s)", cs.SuccessIcon(), hostname, cs.Bold(username), tokenSource)
proto, _ := authCfg.GitProtocol(hostname)
proto := authCfg.GitProtocol(hostname)
if proto != "" {
addMsg("%s Git operations for %s configured to use %s protocol.",
cs.SuccessIcon(), hostname, cs.Bold(proto))