@@ -3,7 +3,6 @@ package create
33import (
44 "bytes"
55 "encoding/json"
6- "fmt"
76 "io/ioutil"
87 "net/http"
98 "testing"
@@ -98,6 +97,21 @@ func TestRepoCreate(t *testing.T) {
9897 Value : "PRIVATE" ,
9998 },
10099 })
100+
101+ as .Stub ([]* prompt.QuestionStub {
102+ {
103+ Name : "addGitIgnore" ,
104+ Value : false ,
105+ },
106+ })
107+
108+ as .Stub ([]* prompt.QuestionStub {
109+ {
110+ Name : "addLicense" ,
111+ Value : false ,
112+ },
113+ })
114+
101115 as .Stub ([]* prompt.QuestionStub {
102116 {
103117 Name : "confirmSubmit" ,
@@ -231,6 +245,21 @@ func TestRepoCreate_org(t *testing.T) {
231245 Value : "PRIVATE" ,
232246 },
233247 })
248+
249+ as .Stub ([]* prompt.QuestionStub {
250+ {
251+ Name : "addGitIgnore" ,
252+ Value : false ,
253+ },
254+ })
255+
256+ as .Stub ([]* prompt.QuestionStub {
257+ {
258+ Name : "addLicense" ,
259+ Value : false ,
260+ },
261+ })
262+
234263 as .Stub ([]* prompt.QuestionStub {
235264 {
236265 Name : "confirmSubmit" ,
@@ -307,6 +336,21 @@ func TestRepoCreate_orgWithTeam(t *testing.T) {
307336 Value : "PRIVATE" ,
308337 },
309338 })
339+
340+ as .Stub ([]* prompt.QuestionStub {
341+ {
342+ Name : "addGitIgnore" ,
343+ Value : false ,
344+ },
345+ })
346+
347+ as .Stub ([]* prompt.QuestionStub {
348+ {
349+ Name : "addLicense" ,
350+ Value : false ,
351+ },
352+ })
353+
310354 as .Stub ([]* prompt.QuestionStub {
311355 {
312356 Name : "confirmSubmit" ,
@@ -458,13 +502,6 @@ func TestRepoCreate_withoutNameArg(t *testing.T) {
458502 },
459503 })
460504
461- as .Stub ([]* prompt.QuestionStub {
462- {
463- Name : "addGitIgnoreLicense" ,
464- Value : false ,
465- },
466- })
467-
468505 as .Stub ([]* prompt.QuestionStub {
469506 {
470507 Name : "confirmSubmit" ,
@@ -504,7 +541,7 @@ func TestRepoCreate_withoutNameArg(t *testing.T) {
504541 }
505542}
506543
507- func TestRepoCreate_WithGitIgnoreLicense (t * testing.T ) {
544+ func TestRepoCreate_WithGitIgnore (t * testing.T ) {
508545 cs , cmdTeardown := run .Stub ()
509546 defer cmdTeardown (t )
510547
@@ -561,7 +598,7 @@ func TestRepoCreate_WithGitIgnoreLicense(t *testing.T) {
561598 httpmock .StringResponse (`{"name":"REPO", "owner":{"login": "OWNER"}, "html_url":"https://github.com/OWNER/REPO"}` ))
562599 httpClient := & http.Client {Transport : reg }
563600
564- output , err := runCommand (httpClient , "REPO" , true )
601+ output , err := runCommand (httpClient , "OWNER/ REPO" , true )
565602 if err != nil {
566603 t .Errorf ("error running command `repo create`: %v" , err )
567604 }
@@ -576,13 +613,111 @@ func TestRepoCreate_WithGitIgnoreLicense(t *testing.T) {
576613 LicenseTemplate string
577614 }
578615
579- if len (reg .Requests ) != 2 {
580- t .Fatalf ("expected 2 HTTP request, got %d" , len (reg .Requests ))
616+ if len (reg .Requests ) != 3 {
617+ t .Fatalf ("expected 3 HTTP request, got %d" , len (reg .Requests ))
581618 }
582619
583- bodyBytes , _ := ioutil .ReadAll (reg .Requests [1 ].Body )
620+ bodyBytes , _ := ioutil .ReadAll (reg .Requests [2 ].Body )
621+ _ = json .Unmarshal (bodyBytes , & reqBody )
622+ if repoName := reqBody .Name ; repoName != "REPO" {
623+ t .Errorf ("expected %q, got %q" , "REPO" , repoName )
624+ }
625+ if repoVisibility := reqBody .Visibility ; repoVisibility != "PRIVATE" {
626+ t .Errorf ("expected %q, got %q" , "PRIVATE" , repoVisibility )
627+ }
628+ if ownerId := reqBody .OwnerId ; ownerId != "OWNERID" {
629+ t .Errorf ("expected %q, got %q" , "OWNERID" , ownerId )
630+ }
631+ }
632+
633+ func TestRepoCreate_WithBothGitIgnoreLicense (t * testing.T ) {
634+ cs , cmdTeardown := run .Stub ()
635+ defer cmdTeardown (t )
636+
637+ cs .Register (`git remote add -f origin https://github\.com/OWNER/REPO\.git` , 0 , "" )
638+ cs .Register (`git rev-parse --show-toplevel` , 0 , "" )
639+
640+ as , surveyTearDown := prompt .InitAskStubber ()
641+ defer surveyTearDown ()
642+
643+ as .Stub ([]* prompt.QuestionStub {
644+ {
645+ Name : "repoVisibility" ,
646+ Value : "PRIVATE" ,
647+ },
648+ })
649+
650+ as .Stub ([]* prompt.QuestionStub {
651+ {
652+ Name : "addGitIgnore" ,
653+ Value : true ,
654+ },
655+ })
656+
657+ as .Stub ([]* prompt.QuestionStub {
658+ {
659+ Name : "chooseGitIgnore" ,
660+ Value : "Go" ,
661+ },
662+ })
663+
664+ as .Stub ([]* prompt.QuestionStub {
665+ {
666+ Name : "addLicense" ,
667+ Value : true ,
668+ },
669+ })
670+
671+ as .Stub ([]* prompt.QuestionStub {
672+ {
673+ Name : "chooseLicense" ,
674+ Value : "GNU Affero General Public License v3.0" ,
675+ },
676+ })
677+
678+ as .Stub ([]* prompt.QuestionStub {
679+ {
680+ Name : "confirmSubmit" ,
681+ Value : true ,
682+ },
683+ })
684+
685+ reg := & httpmock.Registry {}
686+ reg .Register (
687+ httpmock .REST ("GET" , "users/OWNER" ),
688+ httpmock .StringResponse (`{ "node_id": "OWNERID" }` ))
689+ reg .Register (
690+ httpmock .REST ("GET" , "gitignore/templates" ),
691+ httpmock .StringResponse (`["Actionscript","Android","AppceleratorTitanium","Autotools","Bancha","C","C++","Go"]` ))
692+ reg .Register (
693+ httpmock .REST ("GET" , "licenses" ),
694+ httpmock .StringResponse (`[{"key": "mit","name": "MIT License"},{"key": "lgpl-3.0","name": "GNU Lesser General Public License v3.0"}]` ))
695+ reg .Register (
696+ httpmock .REST ("POST" , "user/repos" ),
697+ httpmock .StringResponse (`{"name":"REPO", "owner":{"login": "OWNER"}, "html_url":"https://github.com/OWNER/REPO"}` ))
698+ httpClient := & http.Client {Transport : reg }
699+
700+ output , err := runCommand (httpClient , "OWNER/REPO" , true )
701+ if err != nil {
702+ t .Errorf ("error running command `repo create`: %v" , err )
703+ }
704+
705+ assert .Equal (t , "" , output .String ())
706+ assert .Equal (t , "✓ Created repository OWNER/REPO on GitHub\n ✓ Added remote https://github.com/OWNER/REPO.git\n " , output .Stderr ())
707+
708+ var reqBody struct {
709+ Name string
710+ Visibility string
711+ OwnerId string
712+ LicenseTemplate string
713+ }
714+
715+ if len (reg .Requests ) != 4 {
716+ t .Fatalf ("expected 4 HTTP request, got %d" , len (reg .Requests ))
717+ }
718+
719+ bodyBytes , _ := ioutil .ReadAll (reg .Requests [3 ].Body )
584720 _ = json .Unmarshal (bodyBytes , & reqBody )
585- fmt .Println (reqBody , "==========" )
586721 if repoName := reqBody .Name ; repoName != "REPO" {
587722 t .Errorf ("expected %q, got %q" , "REPO" , repoName )
588723 }
0 commit comments