@@ -40,22 +40,75 @@ func TestPRCheckout_sameRepo(t *testing.T) {
4040
4141 ranCommands := [][]string {}
4242 restoreCmd := utils .SetPrepareCmd (func (cmd * exec.Cmd ) utils.Runnable {
43- ranCommands = append (ranCommands , cmd .Args )
44- return & outputStub {}
43+ switch strings .Join (cmd .Args , " " ) {
44+ case "git show-ref --verify --quiet refs/heads/feature" :
45+ return & errorStub {"exit status: 1" }
46+ default :
47+ ranCommands = append (ranCommands , cmd .Args )
48+ return & outputStub {}
49+ }
50+ })
51+ defer restoreCmd ()
52+
53+ RootCmd .SetArgs ([]string {"pr" , "checkout" , "123" })
54+ _ , err := prCheckoutCmd .ExecuteC ()
55+ eq (t , err , nil )
56+
57+ eq (t , len (ranCommands ), 4 )
58+ eq (t , strings .Join (ranCommands [0 ], " " ), "git fetch origin +refs/heads/feature:refs/remotes/origin/feature" )
59+ eq (t , strings .Join (ranCommands [1 ], " " ), "git checkout -b feature --no-track origin/feature" )
60+ eq (t , strings .Join (ranCommands [2 ], " " ), "git config branch.feature.remote origin" )
61+ eq (t , strings .Join (ranCommands [3 ], " " ), "git config branch.feature.merge refs/heads/feature" )
62+ }
63+
64+ func TestPRCheckout_existingBranch (t * testing.T ) {
65+ ctx := context .NewBlank ()
66+ ctx .SetBranch ("master" )
67+ ctx .SetRemotes (map [string ]string {
68+ "origin" : "OWNER/REPO" ,
69+ })
70+ initContext = func () context.Context {
71+ return ctx
72+ }
73+ http := initFakeHTTP ()
74+
75+ http .StubResponse (200 , bytes .NewBufferString (`
76+ { "data": { "repository": { "pullRequest": {
77+ "headRefName": "feature",
78+ "headRepositoryOwner": {
79+ "login": "hubot"
80+ },
81+ "headRepository": {
82+ "name": "REPO",
83+ "defaultBranchRef": {
84+ "name": "master"
85+ }
86+ },
87+ "isCrossRepository": false,
88+ "maintainerCanModify": false
89+ } } } }
90+ ` ))
91+
92+ ranCommands := [][]string {}
93+ restoreCmd := utils .SetPrepareCmd (func (cmd * exec.Cmd ) utils.Runnable {
94+ switch strings .Join (cmd .Args , " " ) {
95+ case "git show-ref --verify --quiet refs/heads/feature" :
96+ return & outputStub {}
97+ default :
98+ ranCommands = append (ranCommands , cmd .Args )
99+ return & outputStub {}
100+ }
45101 })
46102 defer restoreCmd ()
47103
48104 RootCmd .SetArgs ([]string {"pr" , "checkout" , "123" })
49105 _ , err := prCheckoutCmd .ExecuteC ()
50106 eq (t , err , nil )
51107
52- eq (t , len (ranCommands ), 6 )
53- eq (t , strings .Join (ranCommands [0 ], " " ), "git rev-parse -q --git-path refs/heads/feature" )
54- eq (t , strings .Join (ranCommands [1 ], " " ), "git rev-parse -q --git-dir" )
55- eq (t , strings .Join (ranCommands [2 ], " " ), "git fetch origin +refs/heads/feature:refs/remotes/origin/feature" )
56- eq (t , strings .Join (ranCommands [3 ], " " ), "git checkout -b feature --no-track origin/feature" )
57- eq (t , strings .Join (ranCommands [4 ], " " ), "git config branch.feature.remote origin" )
58- eq (t , strings .Join (ranCommands [5 ], " " ), "git config branch.feature.merge refs/heads/feature" )
108+ eq (t , len (ranCommands ), 3 )
109+ eq (t , strings .Join (ranCommands [0 ], " " ), "git fetch origin +refs/heads/feature:refs/remotes/origin/feature" )
110+ eq (t , strings .Join (ranCommands [1 ], " " ), "git checkout feature" )
111+ eq (t , strings .Join (ranCommands [2 ], " " ), "git merge --ff-only refs/remotes/origin/feature" )
59112}
60113
61114func TestPRCheckout_differentRepo (t * testing.T ) {
0 commit comments