X Tutup
Skip to content

Commit ca99096

Browse files
committed
use CmdError in StubError and fix git_test
1 parent 0af2324 commit ca99096

File tree

3 files changed

+23
-22
lines changed

3 files changed

+23
-22
lines changed

git/git.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ func VerifyRef(ref string) bool {
2121

2222
// CurrentBranch reads the checked-out branch for the git repository
2323
func CurrentBranch() (string, error) {
24-
2524
refCmd := GitCommand("symbolic-ref", "--quiet", "--short", "HEAD")
2625

2726
output, err := run.PrepareCmd(refCmd).Output()

git/git_test.go

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,58 +37,57 @@ func Test_UncommittedChangeCount(t *testing.T) {
3737
}
3838
}
3939

40-
func Test_CurrentBranch_no_commits(t *testing.T) {
40+
func Test_CurrentBranch(t *testing.T) {
4141
cs, teardown := test.InitCmdStubber()
4242
defer teardown()
4343

4444
expected := "branch-name"
4545

46-
cs.StubError("")
4746
cs.Stub(expected)
4847

4948
result, err := CurrentBranch()
5049
if err != nil {
5150
t.Errorf("got unexpected error: %w", err)
5251
}
53-
if len(cs.Calls) != 2 {
54-
t.Errorf("expected 2 git calls, saw %d", len(cs.Calls))
52+
if len(cs.Calls) != 1 {
53+
t.Errorf("expected 1 git call, saw %d", len(cs.Calls))
5554
}
5655
if result != expected {
5756
t.Errorf("unexpected branch name: %s instead of %s", result, expected)
5857
}
5958
}
6059

61-
func Test_CurrentBranch(t *testing.T) {
60+
func Test_CurrentBranch_detached_head(t *testing.T) {
6261
cs, teardown := test.InitCmdStubber()
6362
defer teardown()
6463

65-
expected := "branch-name"
66-
67-
cs.Stub(expected)
64+
cs.StubError("")
6865

69-
result, err := CurrentBranch()
70-
if err != nil {
71-
t.Errorf("got unexpected error: %w", err)
66+
_, err := CurrentBranch()
67+
if err == nil {
68+
t.Errorf("expected an error")
69+
}
70+
expectedError := "git: not on any branch"
71+
if err.Error() != expectedError {
72+
t.Errorf("got unexpected error: %s instead of %s", err.Error(), expectedError)
7273
}
7374
if len(cs.Calls) != 1 {
7475
t.Errorf("expected 1 git call, saw %d", len(cs.Calls))
7576
}
76-
if result != expected {
77-
t.Errorf("unexpected branch name: %s instead of %s", result, expected)
78-
}
7977
}
8078

81-
func Test_CurrentBranch_detached_head(t *testing.T) {
79+
func Test_CurrentBranch_unexpected_error(t *testing.T) {
8280
cs, teardown := test.InitCmdStubber()
8381
defer teardown()
8482

85-
cs.Stub("HEAD")
83+
cs.StubError("lol")
84+
85+
expectedError := "lol\nstub: lol"
8686

8787
_, err := CurrentBranch()
8888
if err == nil {
8989
t.Errorf("expected an error")
9090
}
91-
expectedError := "git: not on any branch"
9291
if err.Error() != expectedError {
9392
t.Errorf("got unexpected error: %s instead of %s", err.Error(), expectedError)
9493
}

test/helpers.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package test
22

33
import (
4+
"bytes"
45
"errors"
56
"fmt"
67
"os/exec"
@@ -13,7 +14,7 @@ import (
1314
// OutputStub implements a simple utils.Runnable
1415
type OutputStub struct {
1516
Out []byte
16-
Error error
17+
Error *run.CmdError
1718
}
1819

1920
func (s OutputStub) Output() ([]byte, error) {
@@ -47,9 +48,11 @@ func (cs *CmdStubber) Stub(desiredOutput string) {
4748
cs.Stubs = append(cs.Stubs, &OutputStub{[]byte(desiredOutput), nil})
4849
}
4950

50-
func (cs *CmdStubber) StubError(msg string) {
51-
// TODO consider handling CmdErr instead of a raw error
52-
cs.Stubs = append(cs.Stubs, &OutputStub{[]byte{}, errors.New(msg)})
51+
func (cs *CmdStubber) StubError(errText string) {
52+
stderrBuff := bytes.NewBufferString(errText)
53+
args := []string{"stub"} // TODO make more real?
54+
err := errors.New(errText)
55+
cs.Stubs = append(cs.Stubs, &OutputStub{[]byte{}, &run.CmdError{stderrBuff, args, err}})
5356
}
5457

5558
func createStubbedPrepareCmd(cs *CmdStubber) func(*exec.Cmd) run.Runnable {

0 commit comments

Comments
 (0)
X Tutup