@@ -12,8 +12,87 @@ import (
1212 "github.com/cli/cli/test"
1313 "github.com/google/shlex"
1414 "github.com/stretchr/testify/assert"
15+ "github.com/stretchr/testify/require"
1516)
1617
18+ func TestNewCmdClone (t * testing.T ) {
19+ testCases := []struct {
20+ name string
21+ args string
22+ wantOpts CloneOptions
23+ wantErr string
24+ }{
25+ {
26+ name : "no arguments" ,
27+ args : "" ,
28+ wantErr : "cannot clone: repository argument required" ,
29+ },
30+ {
31+ name : "repo argument" ,
32+ args : "OWNER/REPO" ,
33+ wantOpts : CloneOptions {
34+ Repository : "OWNER/REPO" ,
35+ GitArgs : []string {},
36+ },
37+ },
38+ {
39+ name : "directory argument" ,
40+ args : "OWNER/REPO mydir" ,
41+ wantOpts : CloneOptions {
42+ Repository : "OWNER/REPO" ,
43+ GitArgs : []string {"mydir" },
44+ },
45+ },
46+ {
47+ name : "git clone arguments" ,
48+ args : "OWNER/REPO -- --depth 1 --recurse-submodules" ,
49+ wantOpts : CloneOptions {
50+ Repository : "OWNER/REPO" ,
51+ GitArgs : []string {"--depth" , "1" , "--recurse-submodules" },
52+ },
53+ },
54+ {
55+ name : "unknown argument" ,
56+ args : "OWNER/REPO --depth 1" ,
57+ wantErr : "unknown flag: --depth\n Separate git clone flags with '--'." ,
58+ },
59+ }
60+ for _ , tt := range testCases {
61+ t .Run (tt .name , func (t * testing.T ) {
62+ io , stdin , stdout , stderr := iostreams .Test ()
63+ fac := & cmdutil.Factory {IOStreams : io }
64+
65+ var opts * CloneOptions
66+ cmd := NewCmdClone (fac , func (co * CloneOptions ) error {
67+ opts = co
68+ return nil
69+ })
70+
71+ argv , err := shlex .Split (tt .args )
72+ require .NoError (t , err )
73+ cmd .SetArgs (argv )
74+
75+ cmd .SetIn (stdin )
76+ cmd .SetOut (stdout )
77+ cmd .SetErr (stderr )
78+
79+ _ , err = cmd .ExecuteC ()
80+ if err != nil {
81+ assert .Equal (t , tt .wantErr , err .Error ())
82+ return
83+ } else if tt .wantErr != "" {
84+ t .Errorf ("expected error %q, got nil" , tt .wantErr )
85+ }
86+
87+ assert .Equal (t , "" , stdout .String ())
88+ assert .Equal (t , "" , stderr .String ())
89+
90+ assert .Equal (t , tt .wantOpts .Repository , opts .Repository )
91+ assert .Equal (t , tt .wantOpts .GitArgs , opts .GitArgs )
92+ })
93+ }
94+ }
95+
1796func runCloneCommand (httpClient * http.Client , cli string ) (* test.CmdOut , error ) {
1897 io , stdin , stdout , stderr := iostreams .Test ()
1998 fac := & cmdutil.Factory {
@@ -203,17 +282,3 @@ func Test_RepoClone_withoutUsername(t *testing.T) {
203282 assert .Equal (t , 1 , cs .Count )
204283 assert .Equal (t , "git clone https://github.com/OWNER/REPO.git" , strings .Join (cs .Calls [0 ].Args , " " ))
205284}
206-
207- func Test_RepoClone_flagError (t * testing.T ) {
208- _ , err := runCloneCommand (nil , "--depth 1 OWNER/REPO" )
209- if err == nil || err .Error () != "unknown flag: --depth\n Separate git clone flags with '--'." {
210- t .Errorf ("unexpected error %v" , err )
211- }
212- }
213-
214- func Test_RepoClone_noArgError (t * testing.T ) {
215- _ , err := runCloneCommand (nil , "" )
216- if err == nil || err .Error () != "cannot clone: no repository name given" {
217- t .Errorf ("unexpected error %v" , err )
218- }
219- }
0 commit comments