Skip to content

Commit

Permalink
improvement as per review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rajhawaldar committed Jan 16, 2024
1 parent 0d1697e commit 76a008a
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 43 deletions.
32 changes: 15 additions & 17 deletions pkg/cmd/auth/setupgit/setupgit.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ func NewCmdSetupGit(f *cmdutil.Factory, runF func(*SetupGitOptions) error) *cobr
Executable: f.Executable(),
GitClient: f.GitClient,
}

if opts.Hostname == "" && opts.Force {
return cmdutil.FlagErrorf("cannot use `--force` without `--hostname`")
}
if runF != nil {
return runF(opts)
}
Expand All @@ -65,7 +67,7 @@ func NewCmdSetupGit(f *cmdutil.Factory, runF func(*SetupGitOptions) error) *cobr
}

cmd.Flags().StringVarP(&opts.Hostname, "hostname", "h", "", "The hostname to configure git for")
cmd.Flags().BoolVarP(&opts.Force, "force", "f", false, "Add credential helper for unknown host")
cmd.Flags().BoolVarP(&opts.Force, "force", "f", false, "Force setup even if the host is not known. Must be used in conjunction with `--hostname`")

return cmd
}
Expand All @@ -81,21 +83,17 @@ func setupGitRun(opts *SetupGitOptions) error {

stderr := opts.IO.ErrOut
cs := opts.IO.ColorScheme()
if opts.Force && opts.Hostname == "" {
fmt.Fprintf(
stderr,
"You must use the `--force` flag in conjunction with the `--hostname` flag.\n",
)
return cmdutil.SilentError
}
if len(hostnames) == 0 {
fmt.Fprintf(
stderr,
"You are not logged into any GitHub hosts. Run %s to authenticate.\n",
cs.Bold("gh auth login"),
)

return cmdutil.SilentError

if !opts.Force {
if len(hostnames) == 0 {
fmt.Fprintf(
stderr,
"You are not logged into any GitHub hosts. Run %s to authenticate.\n",
cs.Bold("gh auth login"),
)

return cmdutil.SilentError
}
}

hostnamesToSetup := hostnames
Expand Down
87 changes: 61 additions & 26 deletions pkg/cmd/auth/setupgit/setupgit_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package setupgit

import (
"bytes"
"fmt"
"testing"

"github.com/cli/cli/v2/internal/config"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/google/shlex"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand All @@ -22,7 +26,56 @@ func (gf *mockGitConfigurer) Setup(hostname, username, authToken string) error {
gf.hosts = append(gf.hosts, hostname)
return gf.setupErr
}
func TestNewCmdSetupGit(t *testing.T) {
tests := []struct {
name string
cli string
wantsErr bool
errMsg string
}{
{
name: "--force without hostname",
cli: "--force",
wantsErr: true,
errMsg: "cannot use `--force` without `--hostname`",
},
{
name: "no error when --force used with hostname",
cli: "--force --hostname ghe.io",
wantsErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f := &cmdutil.Factory{}

argv, err := shlex.Split(tt.cli)
assert.NoError(t, err)

cmd := NewCmdSetupGit(f, func(opts *SetupGitOptions) error {
return nil
})

// TODO cobra hack-around
cmd.Flags().BoolP("help", "x", false, "")

cmd.SetArgs(argv)
cmd.SetIn(&bytes.Buffer{})
cmd.SetOut(&bytes.Buffer{})
cmd.SetErr(&bytes.Buffer{})

_, err = cmd.ExecuteC()
if tt.wantsErr {
assert.Error(t, err)
assert.Equal(t, err.Error(), tt.errMsg)
return
} else {
assert.NoError(t, err)
}

})
}
}
func Test_setupGitRun(t *testing.T) {
tests := []struct {
name string
Expand All @@ -48,14 +101,6 @@ func Test_setupGitRun(t *testing.T) {
expectedErr: "SilentError",
expectedErrOut: "You are not logged into any GitHub hosts. Run gh auth login to authenticate.\n",
},
{
name: "force flag must be use with hostname flag",
opts: &SetupGitOptions{
Force: true,
},
expectedErr: "SilentError",
expectedErrOut: "You must use the `--force` flag in conjunction with the `--hostname` flag.\n",
},
{
name: "not authenticated with the hostname given as flag",
opts: &SetupGitOptions{
Expand All @@ -67,24 +112,6 @@ func Test_setupGitRun(t *testing.T) {
expectedErr: "You are not logged into the GitHub host \"ghe.io\"\n",
expectedErrOut: "",
},
{
name: "add credential helper for unknown host",
opts: &SetupGitOptions{
Hostname: "ghe.io",
Force: true,
},
cfgStubs: func(t *testing.T, cfg config.Config) {
login(t, cfg, "github.com", "test-user", "gho_ABCDEFG", "https", false)
},
},
{
name: "setup credential helper for unknown host",
opts: &SetupGitOptions{},
cfgStubs: func(t *testing.T, cfg config.Config) {
login(t, cfg, "ghe.io", "test-user", "gho_ABCDEFG", "https", false)
},
expectedHostsSetup: []string{"ghe.io"},
},
{
name: "error setting up git for hostname",
opts: &SetupGitOptions{},
Expand Down Expand Up @@ -114,6 +141,14 @@ func Test_setupGitRun(t *testing.T) {
},
expectedHostsSetup: []string{"ghe.io"},
},
{
name: "when the force flag is provided, it sets up the credential helper even if there are no known hosts",
opts: &SetupGitOptions{
Hostname: "ghe.io",
Force: true,
},
expectedHostsSetup: []string{"ghe.io"},
},
}

for _, tt := range tests {
Expand Down

0 comments on commit 76a008a

Please sign in to comment.