@@ -3,9 +3,7 @@ package fork
33import (
44 "net/http"
55 "net/url"
6- "os/exec"
76 "regexp"
8- "strings"
97 "testing"
108 "time"
119
@@ -85,21 +83,21 @@ func runCommand(httpClient *http.Client, remotes []*context.Remote, isTTY bool,
8583func TestRepoFork_nontty (t * testing.T ) {
8684 defer stubSince (2 * time .Second )()
8785 reg := & httpmock.Registry {}
86+ defer reg .Verify (t )
8887 defer reg .StubWithFixturePath (200 , "./forkResult.json" )()
8988 httpClient := & http.Client {Transport : reg }
9089
91- cs , restore := test . InitCmdStubber ()
92- defer restore ()
90+ _ , restore := run . Stub ()
91+ defer restore (t )
9392
9493 output , err := runCommand (httpClient , nil , false , "" )
9594 if err != nil {
9695 t .Fatalf ("error running command `repo fork`: %v" , err )
9796 }
9897
99- assert .Equal (t , 0 , len (cs .Calls ))
10098 assert .Equal (t , "" , output .String ())
10199 assert .Equal (t , "" , output .Stderr ())
102- reg . Verify ( t )
100+
103101}
104102
105103func TestRepoFork_in_parent_nontty (t * testing.T ) {
@@ -108,21 +106,17 @@ func TestRepoFork_in_parent_nontty(t *testing.T) {
108106 defer reg .StubWithFixturePath (200 , "./forkResult.json" )()
109107 httpClient := & http.Client {Transport : reg }
110108
111- cs , restore := test . InitCmdStubber ()
112- defer restore ()
109+ cs , restore := run . Stub ()
110+ defer restore (t )
113111
114- cs .Stub ( "" ) // git remote rename
115- cs .Stub ( "" ) // git remote add
112+ cs .Register ( ` git remote rename origin upstream` , 0 , "" )
113+ cs .Register ( `git remote add -f origin https://github\.com/someone/REPO\. git` , 0 , "" )
116114
117115 output , err := runCommand (httpClient , nil , false , "--remote" )
118116 if err != nil {
119117 t .Fatalf ("error running command `repo fork`: %v" , err )
120118 }
121119
122- assert .Equal (t , 2 , len (cs .Calls ))
123- assert .Equal (t , "git remote rename origin upstream" , strings .Join (cs .Calls [0 ].Args , " " ))
124- assert .Equal (t , "git remote add -f origin https://github.com/someone/REPO.git" , strings .Join (cs .Calls [1 ].Args , " " ))
125-
126120 assert .Equal (t , "" , output .String ())
127121 assert .Equal (t , "" , output .Stderr ())
128122 reg .Verify (t )
@@ -131,27 +125,24 @@ func TestRepoFork_in_parent_nontty(t *testing.T) {
131125func TestRepoFork_outside_parent_nontty (t * testing.T ) {
132126 defer stubSince (2 * time .Second )()
133127 reg := & httpmock.Registry {}
128+ reg .Verify (t )
134129 defer reg .StubWithFixturePath (200 , "./forkResult.json" )()
135130 httpClient := & http.Client {Transport : reg }
136131
137- cs , restore := test . InitCmdStubber ()
138- defer restore ()
132+ cs , restore := run . Stub ()
133+ defer restore (t )
139134
140- cs .Stub ( "" ) // git clone
141- cs .Stub ( "" ) // git remote add
135+ cs .Register ( `git clone https://github.com/someone/REPO\. git` , 0 , "" )
136+ cs .Register ( `git -C REPO remote add -f upstream https://github\.com/OWNER/REPO\.git` , 0 , "" )
142137
143138 output , err := runCommand (httpClient , nil , false , "--clone OWNER/REPO" )
144139 if err != nil {
145140 t .Errorf ("error running command `repo fork`: %v" , err )
146141 }
147142
148143 assert .Equal (t , "" , output .String ())
149-
150- assert .Equal (t , "git clone https://github.com/someone/REPO.git" , strings .Join (cs .Calls [0 ].Args , " " ))
151- assert .Equal (t , "git -C REPO remote add -f upstream https://github.com/OWNER/REPO.git" , strings .Join (cs .Calls [1 ].Args , " " ))
152-
153144 assert .Equal (t , output .Stderr (), "" )
154- reg . Verify ( t )
145+
155146}
156147
157148func TestRepoFork_already_forked (t * testing.T ) {
@@ -160,16 +151,14 @@ func TestRepoFork_already_forked(t *testing.T) {
160151 defer reg .StubWithFixturePath (200 , "./forkResult.json" )()
161152 httpClient := & http.Client {Transport : reg }
162153
163- cs , restore := test . InitCmdStubber ()
164- defer restore ()
154+ _ , restore := run . Stub ()
155+ defer restore (t )
165156
166157 output , err := runCommand (httpClient , nil , true , "--remote=false" )
167158 if err != nil {
168159 t .Errorf ("got unexpected error: %v" , err )
169160 }
170161
171- assert .Equal (t , 0 , len (cs .Calls ))
172-
173162 r := regexp .MustCompile (`someone/REPO.*already exists` )
174163 if ! r .MatchString (output .Stderr ()) {
175164 t .Errorf ("output did not match regexp /%s/\n > output\n %s\n " , r , output .Stderr ())
@@ -213,16 +202,15 @@ func TestRepoFork_in_parent(t *testing.T) {
213202 defer reg .StubWithFixturePath (200 , "./forkResult.json" )()
214203 httpClient := & http.Client {Transport : reg }
215204
216- cs , restore := test . InitCmdStubber ()
217- defer restore ()
205+ _ , restore := run . Stub ()
206+ defer restore (t )
218207 defer stubSince (2 * time .Second )()
219208
220209 output , err := runCommand (httpClient , nil , true , "--remote=false" )
221210 if err != nil {
222211 t .Errorf ("error running command `repo fork`: %v" , err )
223212 }
224213
225- assert .Equal (t , 0 , len (cs .Calls ))
226214 assert .Equal (t , "" , output .String ())
227215
228216 r := regexp .MustCompile (`Created fork.*someone/REPO` )
@@ -279,28 +267,18 @@ func TestRepoFork_in_parent_yes(t *testing.T) {
279267 defer reg .StubWithFixturePath (200 , "./forkResult.json" )()
280268 httpClient := & http.Client {Transport : reg }
281269
282- var seenCmds [] * exec. Cmd
283- defer run . SetPrepareCmd ( func ( cmd * exec. Cmd ) run. Runnable {
284- seenCmds = append ( seenCmds , cmd )
285- return & test. OutputStub {}
286- })( )
270+ cs , restore := run . Stub ()
271+ defer restore ( t )
272+
273+ cs . Register ( `git remote rename origin upstream` , 0 , "" )
274+ cs . Register ( `git remote add -f origin https://github\.com/someone/REPO\.git` , 0 , "" )
287275
288276 output , err := runCommand (httpClient , nil , true , "--remote" )
289277 if err != nil {
290278 t .Errorf ("error running command `repo fork`: %v" , err )
291279 }
292280
293- expectedCmds := []string {
294- "git remote rename origin upstream" ,
295- "git remote add -f origin https://github.com/someone/REPO.git" ,
296- }
297-
298- for x , cmd := range seenCmds {
299- assert .Equal (t , expectedCmds [x ], strings .Join (cmd .Args , " " ))
300- }
301-
302281 assert .Equal (t , "" , output .String ())
303-
304282 test .ExpectLines (t , output .Stderr (),
305283 "Created fork.*someone/REPO" ,
306284 "Added remote.*origin" )
@@ -314,22 +292,18 @@ func TestRepoFork_outside_yes(t *testing.T) {
314292 defer reg .StubWithFixturePath (200 , "./forkResult.json" )()
315293 httpClient := & http.Client {Transport : reg }
316294
317- cs , restore := test . InitCmdStubber ()
318- defer restore ()
295+ cs , restore := run . Stub ()
296+ defer restore (t )
319297
320- cs .Stub ( "" ) // git clone
321- cs .Stub ( "" ) // git remote add
298+ cs .Register ( `git clone https://github\.com/someone/REPO\. git` , 0 , "" )
299+ cs .Register ( `git -C REPO remote add -f upstream https://github\.com/OWNER/REPO\.git` , 0 , "" )
322300
323301 output , err := runCommand (httpClient , nil , true , "--clone OWNER/REPO" )
324302 if err != nil {
325303 t .Errorf ("error running command `repo fork`: %v" , err )
326304 }
327305
328306 assert .Equal (t , "" , output .String ())
329-
330- assert .Equal (t , "git clone https://github.com/someone/REPO.git" , strings .Join (cs .Calls [0 ].Args , " " ))
331- assert .Equal (t , "git -C REPO remote add -f upstream https://github.com/OWNER/REPO.git" , strings .Join (cs .Calls [1 ].Args , " " ))
332-
333307 test .ExpectLines (t , output .Stderr (),
334308 "Created fork.*someone/REPO" ,
335309 "Cloned fork" )
@@ -343,11 +317,11 @@ func TestRepoFork_outside_survey_yes(t *testing.T) {
343317 defer reg .StubWithFixturePath (200 , "./forkResult.json" )()
344318 httpClient := & http.Client {Transport : reg }
345319
346- cs , restore := test . InitCmdStubber ()
347- defer restore ()
320+ cs , restore := run . Stub ()
321+ defer restore (t )
348322
349- cs .Stub ( "" ) // git clone
350- cs .Stub ( "" ) // git remote add
323+ cs .Register ( `git clone https://github\.com/someone/REPO\. git` , 0 , "" )
324+ cs .Register ( `git -C REPO remote add -f upstream https://github\.com/OWNER/REPO\.git` , 0 , "" )
351325
352326 defer prompt .StubConfirm (true )()
353327
@@ -357,10 +331,6 @@ func TestRepoFork_outside_survey_yes(t *testing.T) {
357331 }
358332
359333 assert .Equal (t , "" , output .String ())
360-
361- assert .Equal (t , "git clone https://github.com/someone/REPO.git" , strings .Join (cs .Calls [0 ].Args , " " ))
362- assert .Equal (t , "git -C REPO remote add -f upstream https://github.com/OWNER/REPO.git" , strings .Join (cs .Calls [1 ].Args , " " ))
363-
364334 test .ExpectLines (t , output .Stderr (),
365335 "Created fork.*someone/REPO" ,
366336 "Cloned fork" )
@@ -374,11 +344,8 @@ func TestRepoFork_outside_survey_no(t *testing.T) {
374344 defer reg .StubWithFixturePath (200 , "./forkResult.json" )()
375345 httpClient := & http.Client {Transport : reg }
376346
377- cmdRun := false
378- defer run .SetPrepareCmd (func (cmd * exec.Cmd ) run.Runnable {
379- cmdRun = true
380- return & test.OutputStub {}
381- })()
347+ _ , restore := run .Stub ()
348+ defer restore (t )
382349
383350 defer prompt .StubConfirm (false )()
384351
@@ -389,8 +356,6 @@ func TestRepoFork_outside_survey_no(t *testing.T) {
389356
390357 assert .Equal (t , "" , output .String ())
391358
392- assert .Equal (t , false , cmdRun )
393-
394359 r := regexp .MustCompile (`Created fork.*someone/REPO` )
395360 if ! r .MatchString (output .Stderr ()) {
396361 t .Errorf ("output did not match regexp /%s/\n > output\n %s\n " , r , output )
@@ -406,11 +371,11 @@ func TestRepoFork_in_parent_survey_yes(t *testing.T) {
406371 httpClient := & http.Client {Transport : reg }
407372 defer stubSince (2 * time .Second )()
408373
409- var seenCmds [] * exec. Cmd
410- defer run . SetPrepareCmd ( func ( cmd * exec. Cmd ) run. Runnable {
411- seenCmds = append ( seenCmds , cmd )
412- return & test. OutputStub {}
413- })( )
374+ cs , restore := run . Stub ()
375+ defer restore ( t )
376+
377+ cs . Register ( `git remote rename origin upstream` , 0 , "" )
378+ cs . Register ( `git remote add -f origin https://github\.com/someone/REPO\.git` , 0 , "" )
414379
415380 defer prompt .StubConfirm (true )()
416381
@@ -419,15 +384,6 @@ func TestRepoFork_in_parent_survey_yes(t *testing.T) {
419384 t .Errorf ("error running command `repo fork`: %v" , err )
420385 }
421386
422- expectedCmds := []string {
423- "git remote rename origin upstream" ,
424- "git remote add -f origin https://github.com/someone/REPO.git" ,
425- }
426-
427- for x , cmd := range seenCmds {
428- assert .Equal (t , expectedCmds [x ], strings .Join (cmd .Args , " " ))
429- }
430-
431387 assert .Equal (t , "" , output .String ())
432388
433389 test .ExpectLines (t , output .Stderr (),
@@ -440,15 +396,13 @@ func TestRepoFork_in_parent_survey_yes(t *testing.T) {
440396func TestRepoFork_in_parent_survey_no (t * testing.T ) {
441397 stubSpinner ()
442398 reg := & httpmock.Registry {}
399+ defer reg .Verify (t )
443400 defer reg .StubWithFixturePath (200 , "./forkResult.json" )()
444401 httpClient := & http.Client {Transport : reg }
445402 defer stubSince (2 * time .Second )()
446403
447- cmdRun := false
448- defer run .SetPrepareCmd (func (cmd * exec.Cmd ) run.Runnable {
449- cmdRun = true
450- return & test.OutputStub {}
451- })()
404+ _ , restore := run .Stub ()
405+ defer restore (t )
452406
453407 defer prompt .StubConfirm (false )()
454408
@@ -459,28 +413,26 @@ func TestRepoFork_in_parent_survey_no(t *testing.T) {
459413
460414 assert .Equal (t , "" , output .String ())
461415
462- assert .Equal (t , false , cmdRun )
463-
464416 r := regexp .MustCompile (`Created fork.*someone/REPO` )
465417 if ! r .MatchString (output .Stderr ()) {
466418 t .Errorf ("output did not match regexp /%s/\n > output\n %s\n " , r , output )
467419 return
468420 }
469- reg .Verify (t )
470421}
471422
472423func TestRepoFork_in_parent_match_protocol (t * testing.T ) {
473424 stubSpinner ()
474425 defer stubSince (2 * time .Second )()
475426 reg := & httpmock.Registry {}
427+ defer reg .Verify (t )
476428 defer reg .StubWithFixturePath (200 , "./forkResult.json" )()
477429 httpClient := & http.Client {Transport : reg }
478430
479- var seenCmds [] * exec. Cmd
480- defer run . SetPrepareCmd ( func ( cmd * exec. Cmd ) run. Runnable {
481- seenCmds = append ( seenCmds , cmd )
482- return & test. OutputStub {}
483- })( )
431+ cs , restore := run . Stub ()
432+ defer restore ( t )
433+
434+ cs . Register ( `git remote rename origin upstream` , 0 , "" )
435+ cs . Register ( `git remote add -f origin git@github\.com:someone/REPO\.git` , 0 , "" )
484436
485437 remotes := []* context.Remote {
486438 {
@@ -496,21 +448,11 @@ func TestRepoFork_in_parent_match_protocol(t *testing.T) {
496448 t .Errorf ("error running command `repo fork`: %v" , err )
497449 }
498450
499- expectedCmds := []string {
500- "git remote rename origin upstream" ,
501- "git remote add -f origin git@github.com:someone/REPO.git" ,
502- }
503-
504- for x , cmd := range seenCmds {
505- assert .Equal (t , expectedCmds [x ], strings .Join (cmd .Args , " " ))
506- }
507-
508451 assert .Equal (t , "" , output .String ())
509452
510453 test .ExpectLines (t , output .Stderr (),
511454 "Created fork.*someone/REPO" ,
512455 "Added remote.*origin" )
513- reg .Verify (t )
514456}
515457
516458func stubSpinner () {
0 commit comments