github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/pkg/cmd/gist/clone/clone_test.go (about) 1 package clone 2 3 import ( 4 "net/http" 5 "strings" 6 "testing" 7 8 "github.com/ungtb10d/cli/v2/git" 9 "github.com/ungtb10d/cli/v2/internal/config" 10 "github.com/ungtb10d/cli/v2/internal/run" 11 "github.com/ungtb10d/cli/v2/pkg/cmdutil" 12 "github.com/ungtb10d/cli/v2/pkg/httpmock" 13 "github.com/ungtb10d/cli/v2/pkg/iostreams" 14 "github.com/ungtb10d/cli/v2/test" 15 "github.com/google/shlex" 16 "github.com/stretchr/testify/assert" 17 ) 18 19 func runCloneCommand(httpClient *http.Client, cli string) (*test.CmdOut, error) { 20 ios, stdin, stdout, stderr := iostreams.Test() 21 fac := &cmdutil.Factory{ 22 IOStreams: ios, 23 HttpClient: func() (*http.Client, error) { 24 return httpClient, nil 25 }, 26 Config: func() (config.Config, error) { 27 return config.NewBlankConfig(), nil 28 }, 29 GitClient: &git.Client{GitPath: "some/path/git"}, 30 } 31 32 cmd := NewCmdClone(fac, nil) 33 34 argv, err := shlex.Split(cli) 35 cmd.SetArgs(argv) 36 37 cmd.SetIn(stdin) 38 cmd.SetOut(stderr) 39 cmd.SetErr(stderr) 40 41 if err != nil { 42 panic(err) 43 } 44 45 _, err = cmd.ExecuteC() 46 47 if err != nil { 48 return nil, err 49 } 50 51 return &test.CmdOut{OutBuf: stdout, ErrBuf: stderr}, nil 52 } 53 54 func Test_GistClone(t *testing.T) { 55 tests := []struct { 56 name string 57 args string 58 want string 59 }{ 60 { 61 name: "shorthand", 62 args: "GIST", 63 want: "git clone https://gist.github.com/GIST.git", 64 }, 65 { 66 name: "shorthand with directory", 67 args: "GIST target_directory", 68 want: "git clone https://gist.github.com/GIST.git target_directory", 69 }, 70 { 71 name: "clone arguments", 72 args: "GIST -- -o upstream --depth 1", 73 want: "git clone -o upstream --depth 1 https://gist.github.com/GIST.git", 74 }, 75 { 76 name: "clone arguments with directory", 77 args: "GIST target_directory -- -o upstream --depth 1", 78 want: "git clone -o upstream --depth 1 https://gist.github.com/GIST.git target_directory", 79 }, 80 { 81 name: "HTTPS URL", 82 args: "https://gist.github.com/OWNER/GIST", 83 want: "git clone https://gist.github.com/OWNER/GIST", 84 }, 85 { 86 name: "SSH URL", 87 args: "git@gist.github.com:GIST.git", 88 want: "git clone git@gist.github.com:GIST.git", 89 }, 90 } 91 for _, tt := range tests { 92 t.Run(tt.name, func(t *testing.T) { 93 reg := &httpmock.Registry{} 94 defer reg.Verify(t) 95 96 httpClient := &http.Client{Transport: reg} 97 98 cs, restore := run.Stub() 99 defer restore(t) 100 cs.Register(`git clone`, 0, "", func(s []string) { 101 assert.Equal(t, tt.want, strings.Join(s, " ")) 102 }) 103 104 output, err := runCloneCommand(httpClient, tt.args) 105 if err != nil { 106 t.Fatalf("error running command `gist clone`: %v", err) 107 } 108 109 assert.Equal(t, "", output.String()) 110 assert.Equal(t, "", output.Stderr()) 111 }) 112 } 113 } 114 115 func Test_GistClone_flagError(t *testing.T) { 116 _, err := runCloneCommand(nil, "--depth 1 GIST") 117 if err == nil || err.Error() != "unknown flag: --depth\nSeparate git clone flags with '--'." { 118 t.Errorf("unexpected error %v", err) 119 } 120 } 121 122 func Test_formatRemoteURL(t *testing.T) { 123 type args struct { 124 hostname string 125 gistID string 126 protocol string 127 } 128 tests := []struct { 129 name string 130 args args 131 want string 132 }{ 133 { 134 name: "github.com HTTPS", 135 args: args{ 136 hostname: "github.com", 137 protocol: "https", 138 gistID: "ID", 139 }, 140 want: "https://gist.github.com/ID.git", 141 }, 142 { 143 name: "github.com SSH", 144 args: args{ 145 hostname: "github.com", 146 protocol: "ssh", 147 gistID: "ID", 148 }, 149 want: "git@gist.github.com:ID.git", 150 }, 151 { 152 name: "Enterprise HTTPS", 153 args: args{ 154 hostname: "acme.org", 155 protocol: "https", 156 gistID: "ID", 157 }, 158 want: "https://acme.org/gist/ID.git", 159 }, 160 { 161 name: "Enterprise SSH", 162 args: args{ 163 hostname: "acme.org", 164 protocol: "ssh", 165 gistID: "ID", 166 }, 167 want: "git@acme.org:gist/ID.git", 168 }, 169 } 170 for _, tt := range tests { 171 t.Run(tt.name, func(t *testing.T) { 172 if got := formatRemoteURL(tt.args.hostname, tt.args.gistID, tt.args.protocol); got != tt.want { 173 t.Errorf("formatRemoteURL() = %v, want %v", got, tt.want) 174 } 175 }) 176 } 177 }