Use tighter interface in setup-git

This commit is contained in:
William Martin 2024-05-16 13:33:02 +02:00
parent 177cf7d35b
commit c16836bcf7
2 changed files with 18 additions and 28 deletions

View file

@ -6,23 +6,22 @@ import (
"github.com/MakeNowJust/heredoc"
"github.com/cli/cli/v2/internal/gh"
"github.com/cli/cli/v2/pkg/cmd/auth/shared"
"github.com/cli/cli/v2/pkg/cmd/auth/shared/gitcredentials"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/spf13/cobra"
)
type gitConfigurator interface {
Setup(hostname, username, authToken string) error
type gitCredentialsConfigurer interface {
ConfigureOurs(hostname string) error
}
type SetupGitOptions struct {
IO *iostreams.IOStreams
Config func() (gh.Config, error)
Hostname string
Force bool
gitConfigure gitConfigurator
IO *iostreams.IOStreams
Config func() (gh.Config, error)
Hostname string
Force bool
CredentialsHelperConfig gitCredentialsConfigurer
}
func NewCmdSetupGit(f *cmdutil.Factory, runF func(*SetupGitOptions) error) *cobra.Command {
@ -53,14 +52,9 @@ func NewCmdSetupGit(f *cmdutil.Factory, runF func(*SetupGitOptions) error) *cobr
$ gh auth setup-git --hostname enterprise.internal
`),
RunE: func(cmd *cobra.Command, args []string) error {
opts.gitConfigure = &shared.GitCredentialFlow{
HelperConfig: &gitcredentials.HelperConfig{
SelfExecutablePath: f.Executable(),
GitClient: f.GitClient,
},
Updater: &gitcredentials.Updater{
GitClient: f.GitClient,
},
opts.CredentialsHelperConfig = &gitcredentials.HelperConfig{
SelfExecutablePath: f.Executable(),
GitClient: f.GitClient,
}
if opts.Hostname == "" && opts.Force {
return cmdutil.FlagErrorf("`--force` must be used in conjunction with `--hostname`")
@ -98,7 +92,7 @@ func setupGitRun(opts *SetupGitOptions) error {
)
}
if err := opts.gitConfigure.Setup(opts.Hostname, "", ""); err != nil {
if err := opts.CredentialsHelperConfig.ConfigureOurs(opts.Hostname); err != nil {
return fmt.Errorf("failed to set up git credential helper: %s", err)
}
@ -117,8 +111,7 @@ func setupGitRun(opts *SetupGitOptions) error {
}
for _, hostname := range hostnames {
// TODO: Use gitcredentials.HelperConfigurer here
if err := opts.gitConfigure.Setup(hostname, "", ""); err != nil {
if err := opts.CredentialsHelperConfig.ConfigureOurs(hostname); err != nil {
return fmt.Errorf("failed to set up git credential helper: %s", err)
}
}

View file

@ -15,19 +15,16 @@ import (
"github.com/stretchr/testify/require"
)
type mockGitConfigurer struct {
type gitCredentialsConfigurerSpy struct {
hosts []string
setupErr error
}
func (gf *mockGitConfigurer) SetupFor(hostname string) []string {
return gf.hosts
}
func (gf *mockGitConfigurer) Setup(hostname, username, authToken string) error {
func (gf *gitCredentialsConfigurerSpy) ConfigureOurs(hostname string) error {
gf.hosts = append(gf.hosts, hostname)
return gf.setupErr
}
func TestNewCmdSetupGit(t *testing.T) {
tests := []struct {
name string
@ -183,8 +180,8 @@ func Test_setupGitRun(t *testing.T) {
}
}
gcSpy := &mockGitConfigurer{setupErr: tt.setupErr}
tt.opts.gitConfigure = gcSpy
credentialsConfigurerSpy := &gitCredentialsConfigurerSpy{setupErr: tt.setupErr}
tt.opts.CredentialsHelperConfig = credentialsConfigurerSpy
err := setupGitRun(tt.opts)
if tt.expectedErr != nil {
@ -194,7 +191,7 @@ func Test_setupGitRun(t *testing.T) {
}
if tt.expectedHostsSetup != nil {
require.Equal(t, tt.expectedHostsSetup, gcSpy.hosts)
require.Equal(t, tt.expectedHostsSetup, credentialsConfigurerSpy.hosts)
}
require.Equal(t, tt.expectedErrOut, stderr.String())