@@ -3,45 +3,14 @@ package command
33import (
44 "bytes"
55 "encoding/json"
6- "fmt"
76 "io/ioutil"
8- "os"
9- "os/exec"
107 "strings"
118 "testing"
129
1310 "github.com/cli/cli/context"
14- "github.com/cli/cli/git"
15- "github.com/cli/cli/test"
1611 "github.com/cli/cli/utils"
1712)
1813
19- func TestPrCreateHelperProcess (* testing.T ) {
20- if test .SkipTestHelperProcess () {
21- return
22- }
23-
24- args := test .GetTestHelperProcessArgs ()
25- switch args [1 ] {
26- case "log" :
27- fmt .Println ("123,cool,\" \" " )
28- case "status" :
29- switch args [0 ] {
30- case "clean" :
31- case "dirty" :
32- fmt .Println (" M git/git.go" )
33- default :
34- fmt .Fprintf (os .Stderr , "unknown scenario: %q" , args [0 ])
35- os .Exit (1 )
36- }
37- case "push" :
38- default :
39- fmt .Fprintf (os .Stderr , "unknown command: %q" , args [1 ])
40- os .Exit (1 )
41- }
42- os .Exit (0 )
43- }
44-
4514func TestPRCreate (t * testing.T ) {
4615 initBlankContext ("OWNER/REPO" , "feature" )
4716 http := initFakeHTTP ()
@@ -52,11 +21,13 @@ func TestPRCreate(t *testing.T) {
5221 } } } }
5322 ` ))
5423
55- origGitCommand := git .GitCommand
56- git .GitCommand = test .StubExecCommand ("TestPrCreateHelperProcess" , "clean" )
57- defer func () {
58- git .GitCommand = origGitCommand
59- }()
24+ cs := CmdStubber {}
25+ teardown := utils .SetPrepareCmd (createStubbedPrepareCmd (& cs ))
26+ defer teardown ()
27+
28+ cs .Stub ("" ) // git status
29+ cs .Stub ("1234567890,commit 0\n 2345678901,commit 1" ) // git log
30+ cs .Stub ("" ) // git push
6031
6132 output , err := RunCommand (prCreateCmd , `pr create -t "my title" -b "my body"` )
6233 eq (t , err , nil )
@@ -89,22 +60,25 @@ func TestPRCreate_web(t *testing.T) {
8960 http := initFakeHTTP ()
9061 http .StubRepoResponse ("OWNER" , "REPO" )
9162
92- ranCommands := [][]string {}
93- restoreCmd := utils .SetPrepareCmd (func (cmd * exec.Cmd ) utils.Runnable {
94- ranCommands = append (ranCommands , cmd .Args )
95- return & outputStub {}
96- })
97- defer restoreCmd ()
63+ cs := CmdStubber {}
64+ teardown := utils .SetPrepareCmd (createStubbedPrepareCmd (& cs ))
65+ defer teardown ()
66+
67+ cs .Stub ("" ) // git status
68+ cs .Stub ("1234567890,commit 0\n 2345678901,commit 1" ) // git log
69+ cs .Stub ("" ) // git push
70+ cs .Stub ("" ) // browser
9871
9972 output , err := RunCommand (prCreateCmd , `pr create --web` )
10073 eq (t , err , nil )
10174
10275 eq (t , output .String (), "" )
10376 eq (t , output .Stderr (), "Opening github.com/OWNER/REPO/compare/master...feature in your browser.\n " )
10477
105- eq (t , len (ranCommands ), 4 )
106- eq (t , strings .Join (ranCommands [1 ], " " ), "git push --set-upstream origin HEAD:feature" )
107- eq (t , ranCommands [3 ][len (ranCommands [3 ])- 1 ], "https://github.com/OWNER/REPO/compare/master...feature?expand=1" )
78+ eq (t , len (cs .Calls ), 4 )
79+ eq (t , strings .Join (cs .Calls [2 ].Args , " " ), "git push --set-upstream origin HEAD:feature" )
80+ browserCall := cs .Calls [3 ].Args
81+ eq (t , browserCall [len (browserCall )- 1 ], "https://github.com/OWNER/REPO/compare/master...feature?expand=1" )
10882}
10983
11084func TestPRCreate_ReportsUncommittedChanges (t * testing.T ) {
@@ -118,11 +92,13 @@ func TestPRCreate_ReportsUncommittedChanges(t *testing.T) {
11892 } } } }
11993 ` ))
12094
121- origGitCommand := git .GitCommand
122- git .GitCommand = test .StubExecCommand ("TestPrCreateHelperProcess" , "dirty" )
123- defer func () {
124- git .GitCommand = origGitCommand
125- }()
95+ cs := CmdStubber {}
96+ teardown := utils .SetPrepareCmd (createStubbedPrepareCmd (& cs ))
97+ defer teardown ()
98+
99+ cs .Stub (" M git/git.go" ) // git status
100+ cs .Stub ("1234567890,commit 0\n 2345678901,commit 1" ) // git log
101+ cs .Stub ("" ) // git push
126102
127103 output , err := RunCommand (prCreateCmd , `pr create -t "my title" -b "my body"` )
128104 eq (t , err , nil )
@@ -183,11 +159,13 @@ func TestPRCreate_cross_repo_same_branch(t *testing.T) {
183159 } } } }
184160 ` ))
185161
186- origGitCommand := git .GitCommand
187- git .GitCommand = test .StubExecCommand ("TestPrCreateHelperProcess" , "clean" )
188- defer func () {
189- git .GitCommand = origGitCommand
190- }()
162+ cs := CmdStubber {}
163+ teardown := utils .SetPrepareCmd (createStubbedPrepareCmd (& cs ))
164+ defer teardown ()
165+
166+ cs .Stub ("" ) // git status
167+ cs .Stub ("1234567890,commit 0\n 2345678901,commit 1" ) // git log
168+ cs .Stub ("" ) // git push
191169
192170 output , err := RunCommand (prCreateCmd , `pr create -t "cross repo" -b "same branch"` )
193171 eq (t , err , nil )
0 commit comments