@@ -2,12 +2,14 @@ package fork
22
33import (
44 "bytes"
5+ "io/ioutil"
56 "net/http"
67 "net/url"
78 "regexp"
89 "testing"
910 "time"
1011
12+ "github.com/MakeNowJust/heredoc"
1113 "github.com/cli/cli/context"
1214 "github.com/cli/cli/git"
1315 "github.com/cli/cli/internal/config"
@@ -72,8 +74,9 @@ func TestNewCmdFork(t *testing.T) {
7274 name : "blank nontty" ,
7375 cli : "" ,
7476 wants : ForkOptions {
75- RemoteName : "origin" ,
76- Rename : true ,
77+ RemoteName : "origin" ,
78+ Rename : true ,
79+ Organization : "" ,
7780 },
7881 },
7982 {
@@ -85,6 +88,7 @@ func TestNewCmdFork(t *testing.T) {
8588 PromptClone : true ,
8689 PromptRemote : true ,
8790 Rename : true ,
91+ Organization : "" ,
8892 },
8993 },
9094 {
@@ -104,6 +108,16 @@ func TestNewCmdFork(t *testing.T) {
104108 Rename : true ,
105109 },
106110 },
111+ {
112+ name : "to org" ,
113+ cli : "--org batmanshome" ,
114+ wants : ForkOptions {
115+ RemoteName : "origin" ,
116+ Remote : false ,
117+ Rename : false ,
118+ Organization : "batmanshome" ,
119+ },
120+ },
107121 }
108122
109123 for _ , tt := range tests {
@@ -141,6 +155,7 @@ func TestNewCmdFork(t *testing.T) {
141155 assert .Equal (t , tt .wants .Remote , gotOpts .Remote )
142156 assert .Equal (t , tt .wants .PromptRemote , gotOpts .PromptRemote )
143157 assert .Equal (t , tt .wants .PromptClone , gotOpts .PromptClone )
158+ assert .Equal (t , tt .wants .Organization , gotOpts .Organization )
144159 })
145160 }
146161}
@@ -289,6 +304,7 @@ func TestRepoFork_in_parent_tty(t *testing.T) {
289304 assert .Equal (t , "✓ Created fork someone/REPO\n ✓ Added remote origin\n " , output .Stderr ())
290305 reg .Verify (t )
291306}
307+
292308func TestRepoFork_in_parent_nontty (t * testing.T ) {
293309 defer stubSince (2 * time .Second )()
294310 reg := & httpmock.Registry {}
@@ -409,37 +425,65 @@ func TestRepoFork_in_parent(t *testing.T) {
409425
410426func TestRepoFork_outside (t * testing.T ) {
411427 tests := []struct {
412- name string
413- args string
428+ name string
429+ args string
430+ postBody string
431+ responseBody string
432+ wantStderr string
414433 }{
415434 {
416- name : "url arg" ,
417- args : "--clone=false http://github.com/OWNER/REPO.git" ,
435+ name : "url arg" ,
436+ args : "--clone=false http://github.com/OWNER/REPO.git" ,
437+ postBody : "{}\n " ,
438+ responseBody : `{"name":"REPO", "owner":{"login":"monalisa"}}` ,
439+ wantStderr : heredoc .Doc (`
440+ ✓ Created fork monalisa/REPO
441+ ` ),
418442 },
419443 {
420- name : "full name arg" ,
421- args : "--clone=false OWNER/REPO" ,
444+ name : "full name arg" ,
445+ args : "--clone=false OWNER/REPO" ,
446+ postBody : "{}\n " ,
447+ responseBody : `{"name":"REPO", "owner":{"login":"monalisa"}}` ,
448+ wantStderr : heredoc .Doc (`
449+ ✓ Created fork monalisa/REPO
450+ ` ),
451+ },
452+ {
453+ name : "fork to org without clone" ,
454+ args : "--clone=false OWNER/REPO --org batmanshome" ,
455+ postBody : "{\" organization\" :\" batmanshome\" }\n " ,
456+ responseBody : `{"name":"REPO", "owner":{"login":"BatmansHome"}}` ,
457+ wantStderr : heredoc .Doc (`
458+ ✓ Created fork BatmansHome/REPO
459+ ` ),
422460 },
423461 }
424462 for _ , tt := range tests {
425463 t .Run (tt .name , func (t * testing.T ) {
426464 defer stubSince (2 * time .Second )()
465+
427466 reg := & httpmock.Registry {}
428- defer reg .StubWithFixturePath (200 , "./forkResult.json" )()
429- httpClient := & http.Client {Transport : reg }
467+ reg .Register (
468+ httpmock .REST ("POST" , "repos/OWNER/REPO/forks" ),
469+ func (req * http.Request ) (* http.Response , error ) {
470+ bb , err := ioutil .ReadAll (req .Body )
471+ if err != nil {
472+ return nil , err
473+ }
474+ assert .Equal (t , tt .postBody , string (bb ))
475+ return & http.Response {
476+ Request : req ,
477+ StatusCode : 200 ,
478+ Body : ioutil .NopCloser (bytes .NewBufferString (tt .responseBody )),
479+ }, nil
480+ })
430481
482+ httpClient := & http.Client {Transport : reg }
431483 output , err := runCommand (httpClient , nil , true , tt .args )
432- if err != nil {
433- t .Errorf ("error running command `repo fork`: %v" , err )
434- }
435-
484+ assert .NoError (t , err )
436485 assert .Equal (t , "" , output .String ())
437-
438- r := regexp .MustCompile (`Created fork.*someone/REPO` )
439- if ! r .MatchString (output .Stderr ()) {
440- t .Errorf ("output did not match regexp /%s/\n > output\n %s\n " , r , output )
441- return
442- }
486+ assert .Equal (t , tt .wantStderr , output .Stderr ())
443487 reg .Verify (t )
444488 })
445489 }
0 commit comments