github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/pkg/cmd/auth/setupgit/setupgit_test.go (about)

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