1
1
package gh
2
2
3
3
import (
4
+ "bytes"
5
+ "context"
4
6
"fmt"
5
7
"net/http"
6
8
"os"
7
9
"strings"
8
10
"testing"
11
+ "time"
9
12
10
13
"github.com/cli/go-gh/pkg/api"
11
14
"github.com/cli/go-gh/pkg/config"
@@ -30,22 +33,43 @@ func TestHelperProcess(t *testing.T) {
30
33
os .Exit (0 )
31
34
}
32
35
36
+ func TestHelperProcessLongRunning (t * testing.T ) {
37
+ if os .Getenv ("GH_WANT_HELPER_PROCESS" ) != "1" {
38
+ return
39
+ }
40
+ args := os .Args [3 :]
41
+ fmt .Fprintf (os .Stdout , "%v" , args )
42
+ fmt .Fprint (os .Stderr , "going to sleep..." )
43
+ time .Sleep (10 * time .Second )
44
+ fmt .Fprint (os .Stderr , "...going to exit" )
45
+ os .Exit (0 )
46
+ }
47
+
33
48
func TestRun (t * testing.T ) {
34
- stdOut , stdErr , err := run ( os . Args [ 0 ],
35
- [] string {"GH_WANT_HELPER_PROCESS=1" },
36
- "-test.run=TestHelperProcess" , "--" , "gh" , "issue" , "list" )
49
+ var stdout , stderr bytes. Buffer
50
+ err := run ( context . TODO (), os . Args [ 0 ], [] string {"GH_WANT_HELPER_PROCESS=1" }, nil , & stdout , & stderr ,
51
+ [] string { "-test.run=TestHelperProcess" , "--" , "gh" , "issue" , "list" } )
37
52
assert .NoError (t , err )
38
- assert .Equal (t , "[gh issue list]" , stdOut .String ())
39
- assert .Equal (t , "" , stdErr .String ())
53
+ assert .Equal (t , "[gh issue list]" , stdout .String ())
54
+ assert .Equal (t , "" , stderr .String ())
40
55
}
41
56
42
57
func TestRunError (t * testing.T ) {
43
- stdOut , stdErr , err := run (os .Args [0 ],
44
- []string {"GH_WANT_HELPER_PROCESS=1" },
45
- "-test.run=TestHelperProcess" , "--" , "gh" , "issue" , "list" , "error" )
46
- assert .EqualError (t , err , "failed to run gh: process exited with error. error: exit status 1" )
47
- assert .Equal (t , "" , stdOut .String ())
48
- assert .Equal (t , "process exited with error" , stdErr .String ())
58
+ var stdout , stderr bytes.Buffer
59
+ err := run (context .TODO (), os .Args [0 ], []string {"GH_WANT_HELPER_PROCESS=1" }, nil , & stdout , & stderr ,
60
+ []string {"-test.run=TestHelperProcess" , "--" , "gh" , "error" })
61
+ assert .EqualError (t , err , "gh execution failed: exit status 1" )
62
+ assert .Equal (t , "" , stdout .String ())
63
+ assert .Equal (t , "process exited with error" , stderr .String ())
64
+ }
65
+
66
+ func TestRunInteractiveContextCanceled (t * testing.T ) {
67
+ // pass current time to ensure that deadline has already passed
68
+ ctx , cancel := context .WithDeadline (context .Background (), time .Now ())
69
+ cancel ()
70
+ err := run (ctx , os .Args [0 ], []string {"GH_WANT_HELPER_PROCESS=1" }, nil , nil , nil ,
71
+ []string {"-test.run=TestHelperProcessLongRunning" , "--" , "gh" , "issue" , "list" })
72
+ assert .EqualError (t , err , "gh execution failed: context deadline exceeded" )
49
73
}
50
74
51
75
func TestRESTClient (t * testing.T ) {
0 commit comments