@@ -24,16 +24,30 @@ func eq(t *testing.T, got interface{}, expected interface{}) {
2424 }
2525}
2626
27- func RunCommand (cmd * cobra.Command , args string ) (string , error ) {
27+ type cmdOut struct {
28+ outBuf , errBuf * bytes.Buffer
29+ }
30+
31+ func (c cmdOut ) String () string {
32+ return c .outBuf .String ()
33+ }
34+
35+ func (c cmdOut ) Stderr () string {
36+ return c .errBuf .String ()
37+ }
38+
39+ func RunCommand (cmd * cobra.Command , args string ) (* cmdOut , error ) {
2840 rootCmd := cmd .Root ()
2941 argv , err := shlex .Split (args )
3042 if err != nil {
31- return "" , err
43+ return nil , err
3244 }
3345 rootCmd .SetArgs (argv )
3446
3547 outBuf := bytes.Buffer {}
3648 cmd .SetOut (& outBuf )
49+ errBuf := bytes.Buffer {}
50+ cmd .SetErr (& errBuf )
3751
3852 // Reset flag values so they don't leak between tests
3953 cmd .Flags ().VisitAll (func (f * pflag.Flag ) {
@@ -48,7 +62,10 @@ func RunCommand(cmd *cobra.Command, args string) (string, error) {
4862 })
4963
5064 _ , err = rootCmd .ExecuteC ()
51- return outBuf .String (), err
65+ cmd .SetOut (nil )
66+ cmd .SetErr (nil )
67+
68+ return & cmdOut {& outBuf , & errBuf }, err
5269}
5370
5471func TestPRStatus (t * testing.T ) {
@@ -72,7 +89,7 @@ func TestPRStatus(t *testing.T) {
7289 }
7390
7491 for _ , r := range expectedPrs {
75- if ! r .MatchString (output ) {
92+ if ! r .MatchString (output . String () ) {
7693 t .Errorf ("output did not match regexp /%s/" , r )
7794 }
7895 }
@@ -91,7 +108,7 @@ func TestPRList(t *testing.T) {
91108 t .Fatal (err )
92109 }
93110
94- eq (t , output , `32 New feature feature
111+ eq (t , output . String () , `32 New feature feature
9511229 Fixed bad bug hubot:bug-fix
9611328 Improve documentation docs
97114` )
@@ -104,11 +121,14 @@ func TestPRList_filtering(t *testing.T) {
104121 respBody := bytes .NewBufferString (`{ "data": {} }` )
105122 http .StubResponse (200 , respBody )
106123
107- _ , err := RunCommand (prListCmd , `pr list -s all -l one,two -l three` )
124+ output , err := RunCommand (prListCmd , `pr list -s all -l one,two -l three` )
108125 if err != nil {
109126 t .Fatal (err )
110127 }
111128
129+ eq (t , output .String (), "" )
130+ eq (t , output .Stderr (), "No pull requests match your search\n " )
131+
112132 bodyBytes , _ := ioutil .ReadAll (http .Requests [0 ].Body )
113133 reqBody := struct {
114134 Variables struct {
@@ -183,9 +203,8 @@ func TestPRView_currentBranch(t *testing.T) {
183203 t .Errorf ("error running command `pr view`: %v" , err )
184204 }
185205
186- if output == "" {
187- t .Errorf ("command output expected got an empty string" )
188- }
206+ eq (t , output .String (), "" )
207+ eq (t , output .Stderr (), "Opening https://github.com/OWNER/REPO/pull/10 in your browser.\n " )
189208
190209 if seenCmd == nil {
191210 t .Fatal ("expected a command to run" )
@@ -248,9 +267,7 @@ func TestPRView_numberArg(t *testing.T) {
248267 t .Errorf ("error running command `pr view`: %v" , err )
249268 }
250269
251- if output == "" {
252- t .Errorf ("command output expected got an empty string" )
253- }
270+ eq (t , output .String (), "" )
254271
255272 if seenCmd == nil {
256273 t .Fatal ("expected a command to run" )
@@ -281,9 +298,7 @@ func TestPRView_urlArg(t *testing.T) {
281298 t .Errorf ("error running command `pr view`: %v" , err )
282299 }
283300
284- if output == "" {
285- t .Errorf ("command output expected got an empty string" )
286- }
301+ eq (t , output .String (), "" )
287302
288303 if seenCmd == nil {
289304 t .Fatal ("expected a command to run" )
@@ -316,9 +331,7 @@ func TestPRView_branchArg(t *testing.T) {
316331 t .Errorf ("error running command `pr view`: %v" , err )
317332 }
318333
319- if output == "" {
320- t .Errorf ("command output expected got an empty string" )
321- }
334+ eq (t , output .String (), "" )
322335
323336 if seenCmd == nil {
324337 t .Fatal ("expected a command to run" )
@@ -352,9 +365,7 @@ func TestPRView_branchWithOwnerArg(t *testing.T) {
352365 t .Errorf ("error running command `pr view`: %v" , err )
353366 }
354367
355- if output == "" {
356- t .Errorf ("command output expected got an empty string" )
357- }
368+ eq (t , output .String (), "" )
358369
359370 if seenCmd == nil {
360371 t .Fatal ("expected a command to run" )
0 commit comments