@@ -3,6 +3,7 @@ package create
33import (
44 "bytes"
55 "encoding/json"
6+ "errors"
67 "io/ioutil"
78 "net/http"
89 "os/exec"
@@ -20,10 +21,10 @@ import (
2021 "github.com/stretchr/testify/assert"
2122)
2223
23- func runCommand (httpClient * http.Client , cli string ) (* test.CmdOut , error ) {
24+ func runCommand (httpClient * http.Client , cli string , isTTY bool ) (* test.CmdOut , error ) {
2425 io , _ , stdout , stderr := iostreams .Test ()
25- io .SetStdoutTTY (true )
26- io .SetStdinTTY (true )
26+ io .SetStdoutTTY (isTTY )
27+ io .SetStdinTTY (isTTY )
2728 fac := & cmdutil.Factory {
2829 IOStreams : io ,
2930 HttpClient : func () (* http.Client , error ) {
@@ -106,7 +107,7 @@ func TestRepoCreate(t *testing.T) {
106107 },
107108 })
108109
109- output , err := runCommand (httpClient , "REPO" )
110+ output , err := runCommand (httpClient , "REPO" , true )
110111 if err != nil {
111112 t .Errorf ("error running command `repo create`: %v" , err )
112113 }
@@ -143,6 +144,83 @@ func TestRepoCreate(t *testing.T) {
143144 }
144145}
145146
147+ func TestRepoCreate_outsideGitWorkDir (t * testing.T ) {
148+ reg := & httpmock.Registry {}
149+ reg .Register (
150+ httpmock .GraphQL (`mutation RepositoryCreate\b` ),
151+ httpmock .StringResponse (`
152+ { "data": { "createRepository": {
153+ "repository": {
154+ "id": "REPOID",
155+ "url": "https://github.com/OWNER/REPO",
156+ "name": "REPO",
157+ "owner": {
158+ "login": "OWNER"
159+ }
160+ }
161+ } } }` ))
162+
163+ httpClient := & http.Client {Transport : reg }
164+
165+ var seenCmds []* exec.Cmd
166+ cmdOutputs := []test.OutputStub {
167+ {
168+ Error : errors .New ("Not a git repository" ),
169+ },
170+ {},
171+ {},
172+ }
173+ restoreCmd := run .SetPrepareCmd (func (cmd * exec.Cmd ) run.Runnable {
174+ if len (cmdOutputs ) == 0 {
175+ t .Fatal ("Too many calls to git command" )
176+ }
177+ out := cmdOutputs [0 ]
178+ cmdOutputs = cmdOutputs [1 :]
179+ seenCmds = append (seenCmds , cmd )
180+ return & out
181+ })
182+ defer restoreCmd ()
183+
184+ output , err := runCommand (httpClient , "REPO --private --confirm" , false )
185+ if err != nil {
186+ t .Errorf ("error running command `repo create`: %v" , err )
187+ }
188+
189+ assert .Equal (t , "https://github.com/OWNER/REPO\n " , output .String ())
190+ assert .Equal (t , "" , output .Stderr ())
191+
192+ if len (seenCmds ) != 3 {
193+ t .Fatal ("expected three commands to run" )
194+ }
195+
196+ assert .Equal (t , "git rev-parse --show-toplevel" , strings .Join (seenCmds [0 ].Args , " " ))
197+ assert .Equal (t , "git init REPO" , strings .Join (seenCmds [1 ].Args , " " ))
198+ assert .Equal (t , "git -C REPO remote add origin https://github.com/OWNER/REPO.git" , strings .Join (seenCmds [2 ].Args , " " ))
199+
200+ var reqBody struct {
201+ Query string
202+ Variables struct {
203+ Input map [string ]interface {}
204+ }
205+ }
206+
207+ if len (reg .Requests ) != 1 {
208+ t .Fatalf ("expected 1 HTTP request, got %d" , len (reg .Requests ))
209+ }
210+
211+ bodyBytes , _ := ioutil .ReadAll (reg .Requests [0 ].Body )
212+ _ = json .Unmarshal (bodyBytes , & reqBody )
213+ if repoName := reqBody .Variables .Input ["name" ].(string ); repoName != "REPO" {
214+ t .Errorf ("expected %q, got %q" , "REPO" , repoName )
215+ }
216+ if repoVisibility := reqBody .Variables .Input ["visibility" ].(string ); repoVisibility != "PRIVATE" {
217+ t .Errorf ("expected %q, got %q" , "PRIVATE" , repoVisibility )
218+ }
219+ if _ , ownerSet := reqBody .Variables .Input ["ownerId" ]; ownerSet {
220+ t .Error ("expected ownerId not to be set" )
221+ }
222+ }
223+
146224func TestRepoCreate_org (t * testing.T ) {
147225 reg := & httpmock.Registry {}
148226 reg .Register (
@@ -188,7 +266,7 @@ func TestRepoCreate_org(t *testing.T) {
188266 },
189267 })
190268
191- output , err := runCommand (httpClient , "ORG/REPO" )
269+ output , err := runCommand (httpClient , "ORG/REPO" , true )
192270 if err != nil {
193271 t .Errorf ("error running command `repo create`: %v" , err )
194272 }
@@ -270,7 +348,7 @@ func TestRepoCreate_orgWithTeam(t *testing.T) {
270348 },
271349 })
272350
273- output , err := runCommand (httpClient , "ORG/REPO --team monkeys" )
351+ output , err := runCommand (httpClient , "ORG/REPO --team monkeys" , true )
274352 if err != nil {
275353 t .Errorf ("error running command `repo create`: %v" , err )
276354 }
@@ -353,7 +431,7 @@ func TestRepoCreate_template(t *testing.T) {
353431 },
354432 })
355433
356- output , err := runCommand (httpClient , "REPO --template='OWNER/REPO'" )
434+ output , err := runCommand (httpClient , "REPO --template='OWNER/REPO'" , true )
357435 if err != nil {
358436 t .Errorf ("error running command `repo create`: %v" , err )
359437 }
@@ -441,7 +519,7 @@ func TestRepoCreate_withoutNameArg(t *testing.T) {
441519 },
442520 })
443521
444- output , err := runCommand (httpClient , "" )
522+ output , err := runCommand (httpClient , "" , true )
445523 if err != nil {
446524 t .Errorf ("error running command `repo create`: %v" , err )
447525 }
0 commit comments