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