@@ -18,6 +18,7 @@ import (
1818
1919func TestIssueStatus (t * testing.T ) {
2020 initBlankContext ("" , "OWNER/REPO" , "master" )
21+ defer stubTerminal (true )()
2122 http := initFakeHTTP ()
2223 http .StubRepoResponse ("OWNER" , "REPO" )
2324 http .Register (
@@ -107,8 +108,31 @@ func TestIssueStatus_disabledIssues(t *testing.T) {
107108 }
108109}
109110
110- func TestIssueList (t * testing.T ) {
111+ func TestIssueList_nontty (t * testing.T ) {
111112 initBlankContext ("" , "OWNER/REPO" , "master" )
113+ defer stubTerminal (false )()
114+ http := initFakeHTTP ()
115+ http .StubRepoResponse ("OWNER" , "REPO" )
116+
117+ http .Register (
118+ httpmock .GraphQL (`query IssueList\b` ),
119+ httpmock .FileResponse ("../test/fixtures/issueList.json" ))
120+
121+ output , err := RunCommand ("issue list" )
122+ if err != nil {
123+ t .Errorf ("error running command `issue list`: %v" , err )
124+ }
125+
126+ eq (t , output .Stderr (), "" )
127+ test .ExpectLines (t , output .String (),
128+ `1[\t]+number won[\t]+label[\t]+\d+` ,
129+ `2[\t]+number too[\t]+label[\t]+\d+` ,
130+ `4[\t]+number fore[\t]+label[\t]+\d+` )
131+ }
132+
133+ func TestIssueList_tty (t * testing.T ) {
134+ initBlankContext ("" , "OWNER/REPO" , "master" )
135+ defer stubTerminal (true )()
112136 http := initFakeHTTP ()
113137 http .StubRepoResponse ("OWNER" , "REPO" )
114138 http .Register (
@@ -125,21 +149,14 @@ Showing 3 of 3 issues in OWNER/REPO
125149
126150` )
127151
128- expectedIssues := []* regexp.Regexp {
129- regexp .MustCompile (`(?m)^1\t.*won` ),
130- regexp .MustCompile (`(?m)^2\t.*too` ),
131- regexp .MustCompile (`(?m)^4\t.*fore` ),
132- }
133-
134- for _ , r := range expectedIssues {
135- if ! r .MatchString (output .String ()) {
136- t .Errorf ("output did not match regexp /%s/\n > output\n %s\n " , r , output )
137- return
138- }
139- }
152+ test .ExpectLines (t , output .String (),
153+ "number won" ,
154+ "number too" ,
155+ "number fore" )
140156}
141157
142- func TestIssueList_withFlags (t * testing.T ) {
158+ func TestIssueList_tty_withFlags (t * testing.T ) {
159+ defer stubTerminal (true )()
143160 initBlankContext ("" , "OWNER/REPO" , "master" )
144161 http := initFakeHTTP ()
145162 http .StubRepoResponse ("OWNER" , "REPO" )
@@ -296,7 +313,90 @@ func TestIssueView_web_numberArgWithHash(t *testing.T) {
296313 eq (t , url , "https://github.com/OWNER/REPO/issues/123" )
297314}
298315
299- func TestIssueView_Preview (t * testing.T ) {
316+ func TestIssueView_nontty_Preview (t * testing.T ) {
317+ defer stubTerminal (false )()
318+ tests := map [string ]struct {
319+ ownerRepo string
320+ command string
321+ fixture string
322+ expectedOutputs []string
323+ }{
324+ "Open issue without metadata" : {
325+ ownerRepo : "master" ,
326+ command : "issue view 123" ,
327+ fixture : "../test/fixtures/issueView_preview.json" ,
328+ expectedOutputs : []string {
329+ `title:\tix of coins` ,
330+ `state:\tOPEN` ,
331+ `comments:\t9` ,
332+ `author:\tmarseilles` ,
333+ `assignees:` ,
334+ `\*\*bold story\*\*` ,
335+ },
336+ },
337+ "Open issue with metadata" : {
338+ ownerRepo : "master" ,
339+ command : "issue view 123" ,
340+ fixture : "../test/fixtures/issueView_previewWithMetadata.json" ,
341+ expectedOutputs : []string {
342+ `title:\tix of coins` ,
343+ `assignees:\tmarseilles, monaco` ,
344+ `author:\tmarseilles` ,
345+ `state:\tOPEN` ,
346+ `comments:\t9` ,
347+ `labels:\tone, two, three, four, five` ,
348+ `projects:\tProject 1 \(column A\), Project 2 \(column B\), Project 3 \(column C\), Project 4 \(Awaiting triage\)\n` ,
349+ `milestone:\tuluru\n` ,
350+ `\*\*bold story\*\*` ,
351+ },
352+ },
353+ "Open issue with empty body" : {
354+ ownerRepo : "master" ,
355+ command : "issue view 123" ,
356+ fixture : "../test/fixtures/issueView_previewWithEmptyBody.json" ,
357+ expectedOutputs : []string {
358+ `title:\tix of coins` ,
359+ `state:\tOPEN` ,
360+ `author:\tmarseilles` ,
361+ `labels:\ttarot` ,
362+ },
363+ },
364+ "Closed issue" : {
365+ ownerRepo : "master" ,
366+ command : "issue view 123" ,
367+ fixture : "../test/fixtures/issueView_previewClosedState.json" ,
368+ expectedOutputs : []string {
369+ `title:\tix of coins` ,
370+ `state:\tCLOSED` ,
371+ `\*\*bold story\*\*` ,
372+ `author:\tmarseilles` ,
373+ `labels:\ttarot` ,
374+ `\*\*bold story\*\*` ,
375+ },
376+ },
377+ }
378+ for name , tc := range tests {
379+ t .Run (name , func (t * testing.T ) {
380+ initBlankContext ("" , "OWNER/REPO" , tc .ownerRepo )
381+ http := initFakeHTTP ()
382+ http .StubRepoResponse ("OWNER" , "REPO" )
383+
384+ http .Register (httpmock .GraphQL (`query IssueByNumber\b` ), httpmock .FileResponse (tc .fixture ))
385+
386+ output , err := RunCommand (tc .command )
387+ if err != nil {
388+ t .Errorf ("error running command `%v`: %v" , tc .command , err )
389+ }
390+
391+ eq (t , output .Stderr (), "" )
392+
393+ test .ExpectLines (t , output .String (), tc .expectedOutputs ... )
394+ })
395+ }
396+ }
397+
398+ func TestIssueView_tty_Preview (t * testing.T ) {
399+ defer stubTerminal (true )()
300400 tests := map [string ]struct {
301401 ownerRepo string
302402 command string
@@ -448,6 +548,28 @@ func TestIssueView_web_urlArg(t *testing.T) {
448548 eq (t , url , "https://github.com/OWNER/REPO/issues/123" )
449549}
450550
551+ func TestIssueCreate_nontty_error (t * testing.T ) {
552+ defer stubTerminal (false )()
553+ initBlankContext ("" , "OWNER/REPO" , "master" )
554+ http := initFakeHTTP ()
555+ http .StubRepoResponse ("OWNER" , "REPO" )
556+
557+ http .StubResponse (200 , bytes .NewBufferString (`
558+ { "data": { "repository": {
559+ "id": "REPOID",
560+ "hasIssuesEnabled": true
561+ } } }
562+ ` ))
563+
564+ _ , err := RunCommand (`issue create -t hello` )
565+ if err == nil {
566+ t .Fatal ("expected error running command `issue create`" )
567+ }
568+
569+ assert .Equal (t , "must provide --title and --body when not attached to a terminal" , err .Error ())
570+
571+ }
572+
451573func TestIssueCreate (t * testing.T ) {
452574 initBlankContext ("" , "OWNER/REPO" , "master" )
453575 http := initFakeHTTP ()
0 commit comments