|
| 1 | +package setupgit |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/cli/cli/v2/internal/config" |
| 8 | + "github.com/cli/cli/v2/pkg/iostreams" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +type mockGitConfigurer struct { |
| 14 | + setupErr error |
| 15 | +} |
| 16 | + |
| 17 | +func (gf *mockGitConfigurer) Setup(hostname, username, authToken string) error { |
| 18 | + return gf.setupErr |
| 19 | +} |
| 20 | + |
| 21 | +func Test_setupGitRun(t *testing.T) { |
| 22 | + tests := []struct { |
| 23 | + name string |
| 24 | + opts *SetupGitOptions |
| 25 | + expectedErr string |
| 26 | + expectedErrOut string |
| 27 | + }{ |
| 28 | + { |
| 29 | + name: "opts.Config returns an error", |
| 30 | + opts: &SetupGitOptions{ |
| 31 | + Config: func() (config.Config, error) { |
| 32 | + return nil, fmt.Errorf("oops") |
| 33 | + }, |
| 34 | + }, |
| 35 | + expectedErr: "oops", |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "no authenticated hostnames", |
| 39 | + opts: &SetupGitOptions{}, |
| 40 | + expectedErr: "SilentError", |
| 41 | + expectedErrOut: "You are not logged into any GitHub hosts. Run gh auth login to authenticate.\n", |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "not authenticated with the hostname given as flag", |
| 45 | + opts: &SetupGitOptions{ |
| 46 | + Hostname: "foo", |
| 47 | + Config: func() (config.Config, error) { |
| 48 | + cfg := config.NewBlankConfig() |
| 49 | + require.NoError(t, cfg.Set("bar", "", "")) |
| 50 | + return cfg, nil |
| 51 | + }, |
| 52 | + }, |
| 53 | + expectedErr: "You are not logged into the GitHub host \"foo\"\n", |
| 54 | + expectedErrOut: "", |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "error setting up git for hostname", |
| 58 | + opts: &SetupGitOptions{ |
| 59 | + gitConfigure: &mockGitConfigurer{ |
| 60 | + setupErr: fmt.Errorf("broken"), |
| 61 | + }, |
| 62 | + Config: func() (config.Config, error) { |
| 63 | + cfg := config.NewBlankConfig() |
| 64 | + require.NoError(t, cfg.Set("bar", "", "")) |
| 65 | + return cfg, nil |
| 66 | + }, |
| 67 | + }, |
| 68 | + expectedErr: "failed to set up git credential helper: broken", |
| 69 | + expectedErrOut: "", |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "no hostname option given. Setup git for each hostname in config", |
| 73 | + opts: &SetupGitOptions{ |
| 74 | + gitConfigure: &mockGitConfigurer{}, |
| 75 | + Config: func() (config.Config, error) { |
| 76 | + cfg := config.NewBlankConfig() |
| 77 | + require.NoError(t, cfg.Set("bar", "", "")) |
| 78 | + return cfg, nil |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + { |
| 83 | + name: "setup git for the hostname given via options", |
| 84 | + opts: &SetupGitOptions{ |
| 85 | + Hostname: "yes", |
| 86 | + gitConfigure: &mockGitConfigurer{}, |
| 87 | + Config: func() (config.Config, error) { |
| 88 | + cfg := config.NewBlankConfig() |
| 89 | + require.NoError(t, cfg.Set("bar", "", "")) |
| 90 | + require.NoError(t, cfg.Set("yes", "", "")) |
| 91 | + return cfg, nil |
| 92 | + }, |
| 93 | + }, |
| 94 | + }, |
| 95 | + } |
| 96 | + |
| 97 | + for _, tt := range tests { |
| 98 | + t.Run(tt.name, func(t *testing.T) { |
| 99 | + if tt.opts.Config == nil { |
| 100 | + tt.opts.Config = func() (config.Config, error) { |
| 101 | + return config.NewBlankConfig(), nil |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + io, _, _, stderr := iostreams.Test() |
| 106 | + |
| 107 | + io.SetStdinTTY(true) |
| 108 | + io.SetStderrTTY(true) |
| 109 | + io.SetStdoutTTY(true) |
| 110 | + tt.opts.IO = io |
| 111 | + |
| 112 | + err := setupGitRun(tt.opts) |
| 113 | + if tt.expectedErr != "" { |
| 114 | + assert.EqualError(t, err, tt.expectedErr) |
| 115 | + } else { |
| 116 | + assert.NoError(t, err) |
| 117 | + } |
| 118 | + |
| 119 | + assert.Equal(t, tt.expectedErrOut, stderr.String()) |
| 120 | + }) |
| 121 | + } |
| 122 | +} |
0 commit comments