@@ -2,6 +2,7 @@ package command
22
33import (
44 "os"
5+ "os/exec"
56 "regexp"
67 "testing"
78
@@ -63,8 +64,12 @@ func TestPRView(t *testing.T) {
6364 defer jsonFile .Close ()
6465 http .StubResponse (200 , jsonFile )
6566
66- teardown , callCount := mockOpenInBrowser ()
67- defer teardown ()
67+ var seenCmd * exec.Cmd
68+ restoreCmd := utils .SetPrepareCmd (func (cmd * exec.Cmd ) utils.Runnable {
69+ seenCmd = cmd
70+ return & outputStub {}
71+ })
72+ defer restoreCmd ()
6873
6974 output , err := test .RunCommand (RootCmd , "pr view" )
7075 if err != nil {
@@ -75,9 +80,25 @@ func TestPRView(t *testing.T) {
7580 t .Errorf ("command output expected got an empty string" )
7681 }
7782
78- if * callCount != 1 {
79- t .Errorf ( "OpenInBrowser should be called 1 time but was called %d time(s)" , * callCount )
83+ if seenCmd == nil {
84+ t .Fatal ( "expected a command to run" )
8085 }
86+ url := seenCmd .Args [len (seenCmd .Args )- 1 ]
87+ if url != "https://github.com/OWNER/REPO/pull/10" {
88+ t .Errorf ("got: %q" , url )
89+ }
90+ }
91+
92+ type outputStub struct {
93+ contents []byte
94+ }
95+
96+ func (s outputStub ) Output () ([]byte , error ) {
97+ return s .contents , nil
98+ }
99+
100+ func (s outputStub ) Run () error {
101+ return nil
81102}
82103
83104func TestPRView_NoActiveBranch (t * testing.T ) {
@@ -88,16 +109,20 @@ func TestPRView_NoActiveBranch(t *testing.T) {
88109 defer jsonFile .Close ()
89110 http .StubResponse (200 , jsonFile )
90111
91- teardown , callCount := mockOpenInBrowser ()
92- defer teardown ()
112+ var seenCmd * exec.Cmd
113+ restoreCmd := utils .SetPrepareCmd (func (cmd * exec.Cmd ) utils.Runnable {
114+ seenCmd = cmd
115+ return & outputStub {}
116+ })
117+ defer restoreCmd ()
93118
94119 output , err := test .RunCommand (RootCmd , "pr view" )
95120 if err == nil || err .Error () != "the 'master' branch has no open pull requests" {
96121 t .Errorf ("error running command `pr view`: %v" , err )
97122 }
98123
99- if * callCount > 0 {
100- t .Errorf ( "OpenInBrowser should NOT be called but was called %d time(s) " , * callCount )
124+ if seenCmd != nil {
125+ t .Fatalf ( "unexpected command: %v " , seenCmd . Args )
101126 }
102127
103128 // Now run again but provide a PR number
@@ -110,22 +135,11 @@ func TestPRView_NoActiveBranch(t *testing.T) {
110135 t .Errorf ("command output expected got an empty string" )
111136 }
112137
113- if * callCount != 1 {
114- t .Errorf ("OpenInBrowser should be called once but was called %d time(s)" , * callCount )
115- }
116- }
117-
118- func mockOpenInBrowser () (func (), * int ) {
119- callCount := 0
120- originalOpenInBrowser := utils .OpenInBrowser
121- teardown := func () {
122- utils .OpenInBrowser = originalOpenInBrowser
138+ if seenCmd == nil {
139+ t .Fatal ("expected a command to run" )
123140 }
124-
125- utils .OpenInBrowser = func (_ string ) error {
126- callCount ++
127- return nil
141+ url := seenCmd .Args [len (seenCmd .Args )- 1 ]
142+ if url != "https://github.com/OWNER/REPO/pull/23" {
143+ t .Errorf ("got: %q" , url )
128144 }
129-
130- return teardown , & callCount
131145}
0 commit comments