@@ -102,6 +102,56 @@ func TestRepoFork_nontty(t *testing.T) {
102102 reg .Verify (t )
103103}
104104
105+ func TestRepoFork_existing_remote_error (t * testing.T ) {
106+ defer stubSince (2 * time .Second )()
107+ reg := & httpmock.Registry {}
108+ defer reg .StubWithFixturePath (200 , "./forkResult.json" )()
109+ httpClient := & http.Client {Transport : reg }
110+
111+ _ , err := runCommand (httpClient , nil , false , "--remote" )
112+ if err == nil {
113+ t .Fatal ("expected error running command `repo fork`" )
114+ }
115+
116+ assert .Equal (t , "a remote called 'origin' already exists. You can rerun this command with --remote-name to specify a different remote name." , err .Error ())
117+
118+ reg .Verify (t )
119+ }
120+
121+ func TestRepoFork_no_existing_remote (t * testing.T ) {
122+ remotes := []* context.Remote {
123+ {
124+ Remote : & git.Remote {
125+ Name : "upstream" ,
126+ FetchURL : & url.URL {},
127+ },
128+ Repo : ghrepo .New ("OWNER" , "REPO" ),
129+ },
130+ }
131+ defer stubSince (2 * time .Second )()
132+ reg := & httpmock.Registry {}
133+ defer reg .StubWithFixturePath (200 , "./forkResult.json" )()
134+ httpClient := & http.Client {Transport : reg }
135+
136+ cs , restore := test .InitCmdStubber ()
137+ defer restore ()
138+
139+ cs .Stub ("" ) // git remote rename
140+ cs .Stub ("" ) // git remote add
141+
142+ output , err := runCommand (httpClient , remotes , false , "--remote" )
143+ if err != nil {
144+ t .Fatalf ("error running command `repo fork`: %v" , err )
145+ }
146+
147+ assert .Equal (t , 1 , len (cs .Calls ))
148+ assert .Equal (t , "git remote add -f origin https://github.com/someone/REPO.git" , strings .Join (cs .Calls [0 ].Args , " " ))
149+
150+ assert .Equal (t , "" , output .String ())
151+ assert .Equal (t , "" , output .Stderr ())
152+ reg .Verify (t )
153+ }
154+
105155func TestRepoFork_in_parent_nontty (t * testing.T ) {
106156 defer stubSince (2 * time .Second )()
107157 reg := & httpmock.Registry {}
@@ -114,14 +164,13 @@ func TestRepoFork_in_parent_nontty(t *testing.T) {
114164 cs .Stub ("" ) // git remote rename
115165 cs .Stub ("" ) // git remote add
116166
117- output , err := runCommand (httpClient , nil , false , "--remote" )
167+ output , err := runCommand (httpClient , nil , false , "--remote --remote-name=fork " )
118168 if err != nil {
119169 t .Fatalf ("error running command `repo fork`: %v" , err )
120170 }
121171
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 , " " ))
172+ assert .Equal (t , 1 , len (cs .Calls ))
173+ assert .Equal (t , "git remote add -f fork https://github.com/someone/REPO.git" , strings .Join (cs .Calls [0 ].Args , " " ))
125174
126175 assert .Equal (t , "" , output .String ())
127176 assert .Equal (t , "" , output .Stderr ())
@@ -285,25 +334,20 @@ func TestRepoFork_in_parent_yes(t *testing.T) {
285334 return & test.OutputStub {}
286335 })()
287336
288- output , err := runCommand (httpClient , nil , true , "--remote" )
337+ output , err := runCommand (httpClient , nil , true , "--remote --remote-name=fork " )
289338 if err != nil {
290339 t .Errorf ("error running command `repo fork`: %v" , err )
291340 }
292341
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- }
342+ assert .Equal (t , 1 , len (seenCmds ))
343+ expectedCmd := "git remote add -f fork https://github.com/someone/REPO.git"
344+ assert .Equal (t , expectedCmd , strings .Join (seenCmds [0 ].Args , " " ))
301345
302346 assert .Equal (t , "" , output .String ())
303347
304348 test .ExpectLines (t , output .Stderr (),
305349 "Created fork.*someone/REPO" ,
306- "Added remote.*origin " )
350+ "Added remote.*fork " )
307351 reg .Verify (t )
308352}
309353
@@ -414,26 +458,20 @@ func TestRepoFork_in_parent_survey_yes(t *testing.T) {
414458
415459 defer prompt .StubConfirm (true )()
416460
417- output , err := runCommand (httpClient , nil , true , "" )
461+ output , err := runCommand (httpClient , nil , true , "--remote-name=fork " )
418462 if err != nil {
419463 t .Errorf ("error running command `repo fork`: %v" , err )
420464 }
421465
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- }
466+ assert .Equal (t , 1 , len (seenCmds ))
467+ expectedCmd := "git remote add -f fork https://github.com/someone/REPO.git"
468+ assert .Equal (t , expectedCmd , strings .Join (seenCmds [0 ].Args , " " ))
430469
431470 assert .Equal (t , "" , output .String ())
432471
433472 test .ExpectLines (t , output .Stderr (),
434473 "Created fork.*someone/REPO" ,
435- "Renamed.*origin.*remote to.*upstream" ,
436- "Added remote.*origin" )
474+ "Added remote.*fork" )
437475 reg .Verify (t )
438476}
439477
@@ -491,25 +529,20 @@ func TestRepoFork_in_parent_match_protocol(t *testing.T) {
491529 },
492530 }
493531
494- output , err := runCommand (httpClient , remotes , true , "--remote" )
532+ output , err := runCommand (httpClient , remotes , true , "--remote --remote-name=fork " )
495533 if err != nil {
496534 t .Errorf ("error running command `repo fork`: %v" , err )
497535 }
498536
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- }
537+ assert .Equal (t , 1 , len (seenCmds ))
538+ expectedCmd := "git remote add -f fork git@github.com:someone/REPO.git"
539+ assert .Equal (t , expectedCmd , strings .Join (seenCmds [0 ].Args , " " ))
507540
508541 assert .Equal (t , "" , output .String ())
509542
510543 test .ExpectLines (t , output .Stderr (),
511544 "Created fork.*someone/REPO" ,
512- "Added remote.*origin " )
545+ "Added remote.*fork " )
513546 reg .Verify (t )
514547}
515548
0 commit comments