X Tutup
Skip to content

Commit 696cbfc

Browse files
committed
use Stub instead of SetPrepareCmd in 'repo create' tests
1 parent a04e0ec commit 696cbfc

File tree

1 file changed

+30
-90
lines changed

1 file changed

+30
-90
lines changed

pkg/cmd/repo/create/create_test.go

Lines changed: 30 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@ package create
33
import (
44
"bytes"
55
"encoding/json"
6-
"errors"
76
"io/ioutil"
87
"net/http"
9-
"os/exec"
10-
"strings"
118
"testing"
129

1310
"github.com/cli/cli/internal/config"
@@ -84,13 +81,11 @@ func TestRepoCreate(t *testing.T) {
8481

8582
httpClient := &http.Client{Transport: reg}
8683

87-
var seenCmd *exec.Cmd
88-
//nolint:staticcheck // SA1019 TODO: rewrite to use run.Stub
89-
restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable {
90-
seenCmd = cmd
91-
return &test.OutputStub{}
92-
})
93-
defer restoreCmd()
84+
cs, cmdTeardown := run.Stub()
85+
defer cmdTeardown(t)
86+
87+
cs.Register(`git remote add -f origin https://github\.com/OWNER/REPO\.git`, 0, "")
88+
cs.Register(`git rev-parse --show-toplevel`, 0, "")
9489

9590
as, surveyTearDown := prompt.InitAskStubber()
9691
defer surveyTearDown()
@@ -116,11 +111,6 @@ func TestRepoCreate(t *testing.T) {
116111
assert.Equal(t, "", output.String())
117112
assert.Equal(t, "✓ Created repository OWNER/REPO on GitHub\n✓ Added remote https://github.com/OWNER/REPO.git\n", output.Stderr())
118113

119-
if seenCmd == nil {
120-
t.Fatal("expected a command to run")
121-
}
122-
assert.Equal(t, "git remote add -f origin https://github.com/OWNER/REPO.git", strings.Join(seenCmd.Args, " "))
123-
124114
var reqBody struct {
125115
Query string
126116
Variables struct {
@@ -163,25 +153,11 @@ func TestRepoCreate_outsideGitWorkDir(t *testing.T) {
163153

164154
httpClient := &http.Client{Transport: reg}
165155

166-
var seenCmds []*exec.Cmd
167-
cmdOutputs := []test.OutputStub{
168-
{
169-
Error: errors.New("Not a git repository"),
170-
},
171-
{},
172-
{},
173-
}
174-
//nolint:staticcheck // SA1019 TODO: rewrite to use run.Stub
175-
restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable {
176-
if len(cmdOutputs) == 0 {
177-
t.Fatal("Too many calls to git command")
178-
}
179-
out := cmdOutputs[0]
180-
cmdOutputs = cmdOutputs[1:]
181-
seenCmds = append(seenCmds, cmd)
182-
return &out
183-
})
184-
defer restoreCmd()
156+
cs, cmdTeardown := run.Stub()
157+
defer cmdTeardown(t)
158+
159+
cs.Register(`git remote add -f origin https://github\.com/OWNER/REPO\.git`, 0, "")
160+
cs.Register(`git rev-parse --show-toplevel`, 0, "")
185161

186162
output, err := runCommand(httpClient, "REPO --private --confirm", false)
187163
if err != nil {
@@ -191,14 +167,6 @@ func TestRepoCreate_outsideGitWorkDir(t *testing.T) {
191167
assert.Equal(t, "https://github.com/OWNER/REPO\n", output.String())
192168
assert.Equal(t, "", output.Stderr())
193169

194-
if len(seenCmds) != 3 {
195-
t.Fatal("expected three commands to run")
196-
}
197-
198-
assert.Equal(t, "git rev-parse --show-toplevel", strings.Join(seenCmds[0].Args, " "))
199-
assert.Equal(t, "git init REPO", strings.Join(seenCmds[1].Args, " "))
200-
assert.Equal(t, "git -C REPO remote add origin https://github.com/OWNER/REPO.git", strings.Join(seenCmds[2].Args, " "))
201-
202170
var reqBody struct {
203171
Query string
204172
Variables struct {
@@ -245,13 +213,11 @@ func TestRepoCreate_org(t *testing.T) {
245213
} } }`))
246214
httpClient := &http.Client{Transport: reg}
247215

248-
var seenCmd *exec.Cmd
249-
//nolint:staticcheck // SA1019 TODO: rewrite to use run.Stub
250-
restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable {
251-
seenCmd = cmd
252-
return &test.OutputStub{}
253-
})
254-
defer restoreCmd()
216+
cs, cmdTeardown := run.Stub()
217+
defer cmdTeardown(t)
218+
219+
cs.Register(`git remote add -f origin https://github\.com/ORG/REPO\.git`, 0, "")
220+
cs.Register(`git rev-parse --show-toplevel`, 0, "")
255221

256222
as, surveyTearDown := prompt.InitAskStubber()
257223
defer surveyTearDown()
@@ -277,11 +243,6 @@ func TestRepoCreate_org(t *testing.T) {
277243
assert.Equal(t, "", output.String())
278244
assert.Equal(t, "✓ Created repository ORG/REPO on GitHub\n✓ Added remote https://github.com/ORG/REPO.git\n", output.Stderr())
279245

280-
if seenCmd == nil {
281-
t.Fatal("expected a command to run")
282-
}
283-
assert.Equal(t, "git remote add -f origin https://github.com/ORG/REPO.git", strings.Join(seenCmd.Args, " "))
284-
285246
var reqBody struct {
286247
Query string
287248
Variables struct {
@@ -328,13 +289,11 @@ func TestRepoCreate_orgWithTeam(t *testing.T) {
328289
} } }`))
329290
httpClient := &http.Client{Transport: reg}
330291

331-
var seenCmd *exec.Cmd
332-
//nolint:staticcheck // SA1019 TODO: rewrite to use run.Stub
333-
restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable {
334-
seenCmd = cmd
335-
return &test.OutputStub{}
336-
})
337-
defer restoreCmd()
292+
cs, cmdTeardown := run.Stub()
293+
defer cmdTeardown(t)
294+
295+
cs.Register(`git remote add -f origin https://github\.com/ORG/REPO\.git`, 0, "")
296+
cs.Register(`git rev-parse --show-toplevel`, 0, "")
338297

339298
as, surveyTearDown := prompt.InitAskStubber()
340299
defer surveyTearDown()
@@ -360,11 +319,6 @@ func TestRepoCreate_orgWithTeam(t *testing.T) {
360319
assert.Equal(t, "", output.String())
361320
assert.Equal(t, "✓ Created repository ORG/REPO on GitHub\n✓ Added remote https://github.com/ORG/REPO.git\n", output.Stderr())
362321

363-
if seenCmd == nil {
364-
t.Fatal("expected a command to run")
365-
}
366-
assert.Equal(t, "git remote add -f origin https://github.com/ORG/REPO.git", strings.Join(seenCmd.Args, " "))
367-
368322
var reqBody struct {
369323
Query string
370324
Variables struct {
@@ -412,13 +366,11 @@ func TestRepoCreate_template(t *testing.T) {
412366

413367
httpClient := &http.Client{Transport: reg}
414368

415-
var seenCmd *exec.Cmd
416-
//nolint:staticcheck // SA1019 TODO: rewrite to use run.Stub
417-
restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable {
418-
seenCmd = cmd
419-
return &test.OutputStub{}
420-
})
421-
defer restoreCmd()
369+
cs, cmdTeardown := run.Stub()
370+
defer cmdTeardown(t)
371+
372+
cs.Register(`git remote add -f origin https://github\.com/OWNER/REPO\.git`, 0, "")
373+
cs.Register(`git rev-parse --show-toplevel`, 0, "")
422374

423375
as, surveyTearDown := prompt.InitAskStubber()
424376
defer surveyTearDown()
@@ -444,11 +396,6 @@ func TestRepoCreate_template(t *testing.T) {
444396
assert.Equal(t, "", output.String())
445397
assert.Equal(t, "✓ Created repository OWNER/REPO on GitHub\n✓ Added remote https://github.com/OWNER/REPO.git\n", output.Stderr())
446398

447-
if seenCmd == nil {
448-
t.Fatal("expected a command to run")
449-
}
450-
assert.Equal(t, "git remote add -f origin https://github.com/OWNER/REPO.git", strings.Join(seenCmd.Args, " "))
451-
452399
var reqBody struct {
453400
Query string
454401
Variables struct {
@@ -493,13 +440,11 @@ func TestRepoCreate_withoutNameArg(t *testing.T) {
493440
} } }`))
494441
httpClient := &http.Client{Transport: reg}
495442

496-
var seenCmd *exec.Cmd
497-
//nolint:staticcheck // SA1019 TODO: rewrite to use run.Stub
498-
restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable {
499-
seenCmd = cmd
500-
return &test.OutputStub{}
501-
})
502-
defer restoreCmd()
443+
cs, cmdTeardown := run.Stub()
444+
defer cmdTeardown(t)
445+
446+
cs.Register(`git remote add -f origin https://github\.com/OWNER/REPO\.git`, 0, "")
447+
cs.Register(`git rev-parse --show-toplevel`, 0, "")
503448

504449
as, surveyTearDown := prompt.InitAskStubber()
505450
defer surveyTearDown()
@@ -533,11 +478,6 @@ func TestRepoCreate_withoutNameArg(t *testing.T) {
533478
assert.Equal(t, "", output.String())
534479
assert.Equal(t, "✓ Created repository OWNER/REPO on GitHub\n✓ Added remote https://github.com/OWNER/REPO.git\n", output.Stderr())
535480

536-
if seenCmd == nil {
537-
t.Fatal("expected a command to run")
538-
}
539-
assert.Equal(t, "git remote add -f origin https://github.com/OWNER/REPO.git", strings.Join(seenCmd.Args, " "))
540-
541481
var reqBody struct {
542482
Query string
543483
Variables struct {

0 commit comments

Comments
 (0)
X Tutup