11package git
22
33import (
4- "os/exec"
54 "reflect"
65 "testing"
76
87 "github.com/cli/cli/internal/run"
9- "github.com/cli/cli/test"
108)
119
1210func 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
7971func 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\n stub: 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
11785func TestParseExtraCloneArgs (t * testing.T ) {
0 commit comments