-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit_test.go
96 lines (79 loc) · 2.08 KB
/
git_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package git
import (
"os/exec"
"testing"
"github.com/cli/cli/internal/run"
"github.com/cli/cli/test"
)
func Test_UncommittedChangeCount(t *testing.T) {
type c struct {
Label string
Expected int
Output string
}
cases := []c{
c{Label: "no changes", Expected: 0, Output: ""},
c{Label: "one change", Expected: 1, Output: " M poem.txt"},
c{Label: "untracked file", Expected: 2, Output: " M poem.txt\n?? new.txt"},
}
teardown := run.SetPrepareCmd(func(*exec.Cmd) run.Runnable {
return &test.OutputStub{}
})
defer teardown()
for _, v := range cases {
_ = run.SetPrepareCmd(func(*exec.Cmd) run.Runnable {
return &test.OutputStub{Out: []byte(v.Output)}
})
ucc, _ := UncommittedChangeCount()
if ucc != v.Expected {
t.Errorf("got unexpected ucc value: %d for case %s", ucc, v.Label)
}
}
}
func Test_CurrentBranch(t *testing.T) {
cs, teardown := test.InitCmdStubber()
defer teardown()
expected := "branch-name"
cs.Stub(expected)
result, err := CurrentBranch()
if err != nil {
t.Errorf("got unexpected error: %w", err)
}
if len(cs.Calls) != 1 {
t.Errorf("expected 1 git call, saw %d", len(cs.Calls))
}
if result != expected {
t.Errorf("unexpected branch name: %s instead of %s", result, expected)
}
}
func Test_CurrentBranch_detached_head(t *testing.T) {
cs, teardown := test.InitCmdStubber()
defer teardown()
cs.StubError("")
_, err := CurrentBranch()
if err == nil {
t.Errorf("expected an error")
}
if err != ErrNotOnAnyBranch {
t.Errorf("got unexpected error: %s instead of %s", err, ErrNotOnAnyBranch)
}
if len(cs.Calls) != 1 {
t.Errorf("expected 1 git call, saw %d", len(cs.Calls))
}
}
func Test_CurrentBranch_unexpected_error(t *testing.T) {
cs, teardown := test.InitCmdStubber()
defer teardown()
cs.StubError("lol")
expectedError := "lol\nstub: lol"
_, err := CurrentBranch()
if err == nil {
t.Errorf("expected an error")
}
if err.Error() != expectedError {
t.Errorf("got unexpected error: %s instead of %s", err.Error(), expectedError)
}
if len(cs.Calls) != 1 {
t.Errorf("expected 1 git call, saw %d", len(cs.Calls))
}
}