X Tutup
Skip to content

Commit 1717c8d

Browse files
committed
Migrate to new cmd stubber in git tests
1 parent bf4bc15 commit 1717c8d

File tree

2 files changed

+23
-56
lines changed

2 files changed

+23
-56
lines changed

git/git.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,21 @@ func CurrentBranch() (string, error) {
6767
return "", err
6868
}
6969

70+
stderr := bytes.Buffer{}
71+
refCmd.Stderr = &stderr
72+
7073
output, err := run.PrepareCmd(refCmd).Output()
7174
if err == nil {
7275
// Found the branch name
7376
return getBranchShortName(output), nil
7477
}
7578

76-
var cmdErr *run.CmdError
77-
if errors.As(err, &cmdErr) {
78-
if cmdErr.Stderr.Len() == 0 {
79-
// Detached head
80-
return "", ErrNotOnAnyBranch
81-
}
79+
if stderr.Len() == 0 {
80+
// Detached head
81+
return "", ErrNotOnAnyBranch
8282
}
8383

84-
// Unknown error
85-
return "", err
84+
return "", fmt.Errorf("%sgit: %s", stderr.String(), err)
8685
}
8786

8887
func listRemotes() ([]string, error) {

git/git_test.go

Lines changed: 16 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package git
22

33
import (
4-
"os/exec"
54
"reflect"
65
"testing"
76

87
"github.com/cli/cli/internal/run"
9-
"github.com/cli/cli/test"
108
)
119

1210
func Test_UncommittedChangeCount(t *testing.T) {
@@ -21,20 +19,17 @@ func Test_UncommittedChangeCount(t *testing.T) {
2119
{Label: "untracked file", Expected: 2, Output: " M poem.txt\n?? new.txt"},
2220
}
2321

24-
teardown := run.SetPrepareCmd(func(*exec.Cmd) run.Runnable {
25-
return &test.OutputStub{}
26-
})
27-
defer teardown()
28-
2922
for _, v := range cases {
30-
_ = run.SetPrepareCmd(func(*exec.Cmd) run.Runnable {
31-
return &test.OutputStub{Out: []byte(v.Output)}
23+
t.Run(v.Label, func(t *testing.T) {
24+
cs, restore := run.Stub()
25+
defer restore(t)
26+
cs.Register(`git status --porcelain`, 0, v.Output)
27+
28+
ucc, _ := UncommittedChangeCount()
29+
if ucc != v.Expected {
30+
t.Errorf("UncommittedChangeCount() = %d, expected %d", ucc, v.Expected)
31+
}
3232
})
33-
ucc, _ := UncommittedChangeCount()
34-
35-
if ucc != v.Expected {
36-
t.Errorf("got unexpected ucc value: %d for case %s", ucc, v.Label)
37-
}
3833
}
3934
}
4035

@@ -59,59 +54,32 @@ func Test_CurrentBranch(t *testing.T) {
5954
}
6055

6156
for _, v := range cases {
62-
cs, teardown := test.InitCmdStubber()
63-
cs.Stub(v.Stub)
57+
cs, teardown := run.Stub()
58+
cs.Register(`git symbolic-ref --quiet HEAD`, 0, v.Stub)
6459

6560
result, err := CurrentBranch()
6661
if err != nil {
6762
t.Errorf("got unexpected error: %w", err)
6863
}
69-
if len(cs.Calls) != 1 {
70-
t.Errorf("expected 1 git call, saw %d", len(cs.Calls))
71-
}
7264
if result != v.Expected {
7365
t.Errorf("unexpected branch name: %s instead of %s", result, v.Expected)
7466
}
75-
teardown()
67+
teardown(t)
7668
}
7769
}
7870

7971
func Test_CurrentBranch_detached_head(t *testing.T) {
80-
cs, teardown := test.InitCmdStubber()
81-
defer teardown()
82-
83-
cs.StubError("")
72+
cs, teardown := run.Stub()
73+
defer teardown(t)
74+
cs.Register(`git symbolic-ref --quiet HEAD`, 1, "")
8475

8576
_, err := CurrentBranch()
8677
if err == nil {
87-
t.Errorf("expected an error")
78+
t.Fatal("expected an error, got nil")
8879
}
8980
if err != ErrNotOnAnyBranch {
9081
t.Errorf("got unexpected error: %s instead of %s", err, ErrNotOnAnyBranch)
9182
}
92-
if len(cs.Calls) != 1 {
93-
t.Errorf("expected 1 git call, saw %d", len(cs.Calls))
94-
}
95-
}
96-
97-
func Test_CurrentBranch_unexpected_error(t *testing.T) {
98-
cs, teardown := test.InitCmdStubber()
99-
defer teardown()
100-
101-
cs.StubError("lol")
102-
103-
expectedError := "lol\nstub: lol"
104-
105-
_, err := CurrentBranch()
106-
if err == nil {
107-
t.Errorf("expected an error")
108-
}
109-
if err.Error() != expectedError {
110-
t.Errorf("got unexpected error: %s instead of %s", err.Error(), expectedError)
111-
}
112-
if len(cs.Calls) != 1 {
113-
t.Errorf("expected 1 git call, saw %d", len(cs.Calls))
114-
}
11583
}
11684

11785
func TestParseExtraCloneArgs(t *testing.T) {

0 commit comments

Comments
 (0)
X Tutup