|
| 1 | +package clone |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/cli/cli/internal/config" |
| 9 | + "github.com/cli/cli/pkg/cmdutil" |
| 10 | + "github.com/cli/cli/pkg/httpmock" |
| 11 | + "github.com/cli/cli/pkg/iostreams" |
| 12 | + "github.com/cli/cli/test" |
| 13 | + "github.com/google/shlex" |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | +) |
| 16 | + |
| 17 | +func runCloneCommand(httpClient *http.Client, cli string) (*test.CmdOut, error) { |
| 18 | + io, stdin, stdout, stderr := iostreams.Test() |
| 19 | + fac := &cmdutil.Factory{ |
| 20 | + IOStreams: io, |
| 21 | + HttpClient: func() (*http.Client, error) { |
| 22 | + return httpClient, nil |
| 23 | + }, |
| 24 | + Config: func() (config.Config, error) { |
| 25 | + return config.NewBlankConfig(), nil |
| 26 | + }, |
| 27 | + } |
| 28 | + |
| 29 | + cmd := NewCmdClone(fac, nil) |
| 30 | + |
| 31 | + argv, err := shlex.Split(cli) |
| 32 | + cmd.SetArgs(argv) |
| 33 | + |
| 34 | + cmd.SetIn(stdin) |
| 35 | + cmd.SetOut(stdout) |
| 36 | + cmd.SetErr(stderr) |
| 37 | + |
| 38 | + if err != nil { |
| 39 | + panic(err) |
| 40 | + } |
| 41 | + |
| 42 | + _, err = cmd.ExecuteC() |
| 43 | + |
| 44 | + if err != nil { |
| 45 | + return nil, err |
| 46 | + } |
| 47 | + |
| 48 | + return &test.CmdOut{OutBuf: stdout, ErrBuf: stderr}, nil |
| 49 | +} |
| 50 | + |
| 51 | +func Test_GistClone(t *testing.T) { |
| 52 | + tests := []struct { |
| 53 | + name string |
| 54 | + args string |
| 55 | + want string |
| 56 | + }{ |
| 57 | + { |
| 58 | + name: "shorthand", |
| 59 | + args: "GIST", |
| 60 | + want: "git clone https://gist.github.com/GIST.git", |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "shorthand with directory", |
| 64 | + args: "GIST target_directory", |
| 65 | + want: "git clone https://gist.github.com/GIST.git target_directory", |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "clone arguments", |
| 69 | + args: "GIST -- -o upstream --depth 1", |
| 70 | + want: "git clone -o upstream --depth 1 https://gist.github.com/GIST.git", |
| 71 | + }, |
| 72 | + { |
| 73 | + name: "clone arguments with directory", |
| 74 | + args: "GIST target_directory -- -o upstream --depth 1", |
| 75 | + want: "git clone -o upstream --depth 1 https://gist.github.com/GIST.git target_directory", |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "HTTPS URL", |
| 79 | + args: "https://gist.github.com/OWNER/GIST", |
| 80 | + want: "git clone https://gist.github.com/OWNER/GIST", |
| 81 | + }, |
| 82 | + { |
| 83 | + name: "SSH URL", |
| 84 | + args: "git@gist.github.com:GIST.git", |
| 85 | + want: "git clone git@gist.github.com:GIST.git", |
| 86 | + }, |
| 87 | + } |
| 88 | + for _, tt := range tests { |
| 89 | + t.Run(tt.name, func(t *testing.T) { |
| 90 | + reg := &httpmock.Registry{} |
| 91 | + |
| 92 | + httpClient := &http.Client{Transport: reg} |
| 93 | + |
| 94 | + cs, restore := test.InitCmdStubber() |
| 95 | + defer restore() |
| 96 | + |
| 97 | + cs.Stub("") // git clone |
| 98 | + |
| 99 | + output, err := runCloneCommand(httpClient, tt.args) |
| 100 | + if err != nil { |
| 101 | + t.Fatalf("error running command `gist clone`: %v", err) |
| 102 | + } |
| 103 | + |
| 104 | + assert.Equal(t, "", output.String()) |
| 105 | + assert.Equal(t, "", output.Stderr()) |
| 106 | + assert.Equal(t, 1, cs.Count) |
| 107 | + assert.Equal(t, tt.want, strings.Join(cs.Calls[0].Args, " ")) |
| 108 | + reg.Verify(t) |
| 109 | + }) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +func Test_GistClone_flagError(t *testing.T) { |
| 114 | + _, err := runCloneCommand(nil, "--depth 1 GIST") |
| 115 | + if err == nil || err.Error() != "unknown flag: --depth\nSeparate git clone flags with '--'." { |
| 116 | + t.Errorf("unexpected error %v", err) |
| 117 | + } |
| 118 | +} |
0 commit comments