@@ -3,17 +3,12 @@ package comment
33import (
44 "bytes"
55 "net/http"
6- "os/exec"
76 "testing"
87
9- "github.com/cli/cli/internal/config"
108 "github.com/cli/cli/internal/ghrepo"
11- "github.com/cli/cli/internal/run"
129 "github.com/cli/cli/pkg/cmdutil"
1310 "github.com/cli/cli/pkg/httpmock"
1411 "github.com/cli/cli/pkg/iostreams"
15- "github.com/cli/cli/pkg/prompt"
16- "github.com/cli/cli/test"
1712 "github.com/google/shlex"
1813 "github.com/stretchr/testify/assert"
1914)
@@ -155,69 +150,89 @@ func TestNewCmdComment(t *testing.T) {
155150
156151func Test_commentRun (t * testing.T ) {
157152 tests := []struct {
158- name string
159- input * CommentOptions
160- testInputType int
161- stdout string
162- stderr string
163- wantsErr bool
164- errMsg string
153+ name string
154+ input * CommentOptions
155+ httpStubs func (* testing.T , * httpmock.Registry )
156+ stdout string
157+ stderr string
165158 }{
166159 {
167- name : "interactive web" ,
168- testInputType : web ,
160+ name : "interactive web" ,
169161 input : & CommentOptions {
170162 SelectorArg : "123" ,
171163 Interactive : true ,
172164 InputType : 0 ,
173165 Body : "" ,
166+
167+ InputTypeSurvey : func () (int , error ) { return web , nil },
168+ OpenInBrowser : func (string ) error { return nil },
169+ },
170+ httpStubs : func (t * testing.T , reg * httpmock.Registry ) {
171+ mockIssueFromNumber (t , reg )
174172 },
175173 stderr : "Opening github.com/OWNER/REPO/issues/123 in your browser.\n " ,
176174 },
177175 {
178- name : "interactive editor" ,
179- testInputType : editor ,
176+ name : "interactive editor" ,
180177 input : & CommentOptions {
181178 SelectorArg : "123" ,
182179 Interactive : true ,
183180 InputType : 0 ,
184181 Body : "" ,
185- Edit : func (string ) (string , error ) { return "comment body" , nil },
182+
183+ EditSurvey : func () (string , error ) { return "comment body" , nil },
184+ InputTypeSurvey : func () (int , error ) { return editor , nil },
185+ ConfirmSubmitSurvey : func () (bool , error ) { return true , nil },
186+ },
187+ httpStubs : func (t * testing.T , reg * httpmock.Registry ) {
188+ mockIssueFromNumber (t , reg )
189+ mockCommentCreate (t , reg )
186190 },
187191 stdout : "? Body <Received>\n https://github.com/OWNER/REPO/issues/123#issuecomment-456\n " ,
188192 },
189193 {
190- name : "non-interactive web" ,
191- testInputType : web ,
194+ name : "non-interactive web" ,
192195 input : & CommentOptions {
193196 SelectorArg : "123" ,
194197 Interactive : false ,
195198 InputType : web ,
196199 Body : "" ,
200+
201+ OpenInBrowser : func (string ) error { return nil },
202+ },
203+ httpStubs : func (t * testing.T , reg * httpmock.Registry ) {
204+ mockIssueFromNumber (t , reg )
197205 },
198206 stderr : "Opening github.com/OWNER/REPO/issues/123 in your browser.\n " ,
199207 },
200208 {
201- name : "non-interactive editor" ,
202- testInputType : editor ,
209+ name : "non-interactive editor" ,
203210 input : & CommentOptions {
204211 SelectorArg : "123" ,
205212 Interactive : false ,
206213 InputType : editor ,
207214 Body : "" ,
208- Edit : func (string ) (string , error ) { return "comment body" , nil },
215+
216+ EditSurvey : func () (string , error ) { return "comment body" , nil },
217+ },
218+ httpStubs : func (t * testing.T , reg * httpmock.Registry ) {
219+ mockIssueFromNumber (t , reg )
220+ mockCommentCreate (t , reg )
209221 },
210222 stdout : "https://github.com/OWNER/REPO/issues/123#issuecomment-456\n " ,
211223 },
212224 {
213- name : "non-interactive inline" ,
214- testInputType : inline ,
225+ name : "non-interactive inline" ,
215226 input : & CommentOptions {
216227 SelectorArg : "123" ,
217228 Interactive : false ,
218229 InputType : inline ,
219230 Body : "comment body" ,
220231 },
232+ httpStubs : func (t * testing.T , reg * httpmock.Registry ) {
233+ mockIssueFromNumber (t , reg )
234+ mockCommentCreate (t , reg )
235+ },
221236 stdout : "https://github.com/OWNER/REPO/issues/123#issuecomment-456\n " ,
222237 },
223238 }
@@ -227,57 +242,28 @@ func Test_commentRun(t *testing.T) {
227242 io .SetStdinTTY (true )
228243 io .SetStderrTTY (true )
229244
230- client := & httpmock.Registry {}
231- defer client .Verify (t )
232- mockIssueFromNumber (client )
233- if tt .testInputType != web {
234- mockCommentCreate (t , client )
235- }
245+ reg := & httpmock.Registry {}
246+ defer reg .Verify (t )
247+ tt .httpStubs (t , reg )
236248
237249 tt .input .IO = io
238250 tt .input .HttpClient = func () (* http.Client , error ) {
239- return & http.Client {Transport : client }, nil
240- }
241- tt .input .Config = func () (config.Config , error ) {
242- return config .NewBlankConfig (), nil
251+ return & http.Client {Transport : reg }, nil
243252 }
244253 tt .input .BaseRepo = func () (ghrepo.Interface , error ) {
245254 return ghrepo .New ("OWNER" , "REPO" ), nil
246255 }
247256
248- if tt .input .Interactive {
249- as , teardown := prompt .InitAskStubber ()
250- defer teardown ()
251- // Input type select
252- as .StubOne (tt .testInputType )
253- if tt .testInputType == editor {
254- // Confirm submit
255- as .StubOne (true )
256- }
257- }
258-
259- if tt .testInputType == web {
260- // Stub browser open
261- restoreCmd := run .SetPrepareCmd (func (cmd * exec.Cmd ) run.Runnable {
262- return & test.OutputStub {}
263- })
264- defer restoreCmd ()
265- }
266-
267257 t .Run (tt .name , func (t * testing.T ) {
268258 err := commentRun (tt .input )
269- if tt .wantsErr {
270- assert .EqualError (t , err , tt .errMsg )
271- return
272- }
273259 assert .NoError (t , err )
274260 assert .Equal (t , tt .stdout , stdout .String ())
275261 assert .Equal (t , tt .stderr , stderr .String ())
276262 })
277263 }
278264}
279265
280- func mockIssueFromNumber (reg * httpmock.Registry ) {
266+ func mockIssueFromNumber (_ * testing. T , reg * httpmock.Registry ) {
281267 reg .Register (
282268 httpmock .GraphQL (`query IssueByNumber\b` ),
283269 httpmock .StringResponse (`
0 commit comments