X Tutup
Skip to content

Commit f6fcdf1

Browse files
committed
Use SetPrepareCmd hook to spy on OpenInBrowser
We are now able to assert that the browse command was called with the correct URL
1 parent d881a2e commit f6fcdf1

File tree

3 files changed

+45
-30
lines changed

3 files changed

+45
-30
lines changed

command/pr_test.go

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package command
22

33
import (
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

83104
func 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
}

test/fixtures/prView.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"node": {
77
"number": 10,
88
"title": "Blueberries are a good fruit",
9-
"url": "https://github.com/github/gh-cli/pull/10",
9+
"url": "https://github.com/OWNER/REPO/pull/10",
1010
"headRefName": "[blueberries]"
1111
}
1212
}
@@ -19,7 +19,7 @@
1919
"node": {
2020
"number": 8,
2121
"title": "Strawberries are not actually berries",
22-
"url": "https://github.com/github/gh-cli/pull/8",
22+
"url": "https://github.com/OWNER/REPO/pull/8",
2323
"headRefName": "[strawberries]"
2424
}
2525
}
@@ -32,15 +32,15 @@
3232
"node": {
3333
"number": 9,
3434
"title": "Apples are tasty",
35-
"url": "https://github.com/github/gh-cli/pull/9",
35+
"url": "https://github.com/OWNER/REPO/pull/9",
3636
"headRefName": "[apples]"
3737
}
3838
},
3939
{
4040
"node": {
4141
"number": 11,
4242
"title": "Figs are my favorite",
43-
"url": "https://github.com/github/gh-cli/pull/1",
43+
"url": "https://github.com/OWNER/REPO/pull/1",
4444
"headRefName": "[figs]"
4545
}
4646
}

utils/utils.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func ConcatPaths(paths ...string) string {
2727
return strings.Join(paths, "/")
2828
}
2929

30-
var OpenInBrowser = func(url string) error {
30+
func OpenInBrowser(url string) error {
3131
browser := os.Getenv("BROWSER")
3232
if browser == "" {
3333
browser = searchBrowserLauncher(runtime.GOOS)
@@ -45,7 +45,8 @@ var OpenInBrowser = func(url string) error {
4545
}
4646

4747
endingArgs := append(browserArgs[1:], url)
48-
return exec.Command(browserArgs[0], endingArgs...).Run()
48+
browseCmd := exec.Command(browserArgs[0], endingArgs...)
49+
return PrepareCmd(browseCmd).Run()
4950
}
5051

5152
func searchBrowserLauncher(goos string) (browser string) {

0 commit comments

Comments
 (0)
X Tutup