@@ -3,8 +3,10 @@ package create
33import (
44 "bytes"
55 "encoding/json"
6+ "fmt"
67 "io/ioutil"
78 "net/http"
9+ "os"
810 "os/exec"
911 "reflect"
1012 "strings"
@@ -13,6 +15,7 @@ import (
1315 "github.com/cli/cli/internal/config"
1416 "github.com/cli/cli/internal/ghrepo"
1517 "github.com/cli/cli/internal/run"
18+ prShared "github.com/cli/cli/pkg/cmd/pr/shared"
1619 "github.com/cli/cli/pkg/cmdutil"
1720 "github.com/cli/cli/pkg/httpmock"
1821 "github.com/cli/cli/pkg/iostreams"
@@ -126,7 +129,7 @@ func TestIssueCreate(t *testing.T) {
126129 eq (t , output .String (), "https://github.com/OWNER/REPO/issues/12\n " )
127130}
128131
129- func TestIssueCreate_JSON (t * testing.T ) {
132+ func TestIssueCreate_recover (t * testing.T ) {
130133 http := & httpmock.Registry {}
131134 defer http .Verify (t )
132135
@@ -136,32 +139,72 @@ func TestIssueCreate_JSON(t *testing.T) {
136139 "hasIssuesEnabled": true
137140 } } }
138141 ` ))
139- http .StubResponse (200 , bytes .NewBufferString (`
142+ http .Register (
143+ httpmock .GraphQL (`query RepositoryResolveMetadataIDs\b` ),
144+ httpmock .StringResponse (`
145+ { "data": {
146+ "u000": { "login": "MonaLisa", "id": "MONAID" },
147+ "repository": {
148+ "l000": { "name": "bug", "id": "BUGID" },
149+ "l001": { "name": "TODO", "id": "TODOID" }
150+ }
151+ } }
152+ ` ))
153+ http .Register (
154+ httpmock .GraphQL (`mutation IssueCreate\b` ),
155+ httpmock .GraphQLMutation (`
140156 { "data": { "createIssue": { "issue": {
141157 "URL": "https://github.com/OWNER/REPO/issues/12"
142158 } } } }
143- ` ))
159+ ` , func (inputs map [string ]interface {}) {
160+ eq (t , inputs ["title" ], "recovered title" )
161+ eq (t , inputs ["body" ], "recovered body" )
162+ eq (t , inputs ["labelIds" ], []interface {}{"BUGID" , "TODOID" })
163+ }))
144164
145- output , err := runCommand (http , true , `-j'{"title":"cool", "body":"issue"}'` )
146- if err != nil {
147- t .Errorf ("error running command `issue create`: %v" , err )
165+ as , teardown := prompt .InitAskStubber ()
166+ defer teardown ()
167+
168+ as .Stub ([]* prompt.QuestionStub {
169+ {
170+ Name : "Title" ,
171+ Default : true ,
172+ },
173+ })
174+ as .Stub ([]* prompt.QuestionStub {
175+ {
176+ Name : "Body" ,
177+ Default : true ,
178+ },
179+ })
180+ as .Stub ([]* prompt.QuestionStub {
181+ {
182+ Name : "confirmation" ,
183+ Value : 0 ,
184+ },
185+ })
186+
187+ tmpfile , err := ioutil .TempFile (os .TempDir (), "testrecover*" )
188+ assert .NoError (t , err )
189+
190+ state := prShared.IssueMetadataState {
191+ Title : "recovered title" ,
192+ Body : "recovered body" ,
193+ Labels : []string {"bug" , "TODO" },
148194 }
149195
150- bodyBytes , _ := ioutil .ReadAll (http .Requests [1 ].Body )
151- reqBody := struct {
152- Variables struct {
153- Input struct {
154- RepositoryID string
155- Title string
156- Body string
157- }
158- }
159- }{}
160- _ = json .Unmarshal (bodyBytes , & reqBody )
196+ data , err := json .Marshal (state )
197+ assert .NoError (t , err )
161198
162- eq (t , reqBody .Variables .Input .RepositoryID , "REPOID" )
163- eq (t , reqBody .Variables .Input .Title , "cool" )
164- eq (t , reqBody .Variables .Input .Body , "issue" )
199+ _ , err = tmpfile .Write (data )
200+ assert .NoError (t , err )
201+
202+ args := fmt .Sprintf ("-e '%s'" , tmpfile .Name ())
203+
204+ output , err := runCommandWithRootDirOverridden (http , true , args , "" )
205+ if err != nil {
206+ t .Errorf ("error running command `issue create`: %v" , err )
207+ }
165208
166209 eq (t , output .String (), "https://github.com/OWNER/REPO/issues/12\n " )
167210}
0 commit comments