diff --git a/git/git.go b/git/git.go index bbdc6e301..16570a0fa 100644 --- a/git/git.go +++ b/git/git.go @@ -21,7 +21,6 @@ func VerifyRef(ref string) bool { // CurrentBranch reads the checked-out branch for the git repository func CurrentBranch() (string, error) { - refCmd := GitCommand("symbolic-ref", "--quiet", "--short", "HEAD") output, err := run.PrepareCmd(refCmd).Output() diff --git a/git/git_test.go b/git/git_test.go index 50ce4c71c..b73104c95 100644 --- a/git/git_test.go +++ b/git/git_test.go @@ -37,27 +37,6 @@ func Test_UncommittedChangeCount(t *testing.T) { } } -func Test_CurrentBranch_no_commits(t *testing.T) { - cs, teardown := test.InitCmdStubber() - defer teardown() - - expected := "branch-name" - - cs.StubError("") - cs.Stub(expected) - - result, err := CurrentBranch() - if err != nil { - t.Errorf("got unexpected error: %w", err) - } - if len(cs.Calls) != 2 { - t.Errorf("expected 2 git calls, saw %d", len(cs.Calls)) - } - if result != expected { - t.Errorf("unexpected branch name: %s instead of %s", result, expected) - } -} - func Test_CurrentBranch(t *testing.T) { cs, teardown := test.InitCmdStubber() defer teardown() @@ -82,7 +61,7 @@ func Test_CurrentBranch_detached_head(t *testing.T) { cs, teardown := test.InitCmdStubber() defer teardown() - cs.Stub("HEAD") + cs.StubError("") _, err := CurrentBranch() if err == nil { @@ -96,3 +75,23 @@ func Test_CurrentBranch_detached_head(t *testing.T) { 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)) + } +} diff --git a/test/helpers.go b/test/helpers.go index cffdc5272..390d779c4 100644 --- a/test/helpers.go +++ b/test/helpers.go @@ -1,6 +1,7 @@ package test import ( + "bytes" "errors" "fmt" "os/exec" @@ -13,7 +14,7 @@ import ( // OutputStub implements a simple utils.Runnable type OutputStub struct { Out []byte - Error error + Error *run.CmdError } func (s OutputStub) Output() ([]byte, error) { @@ -47,9 +48,11 @@ func (cs *CmdStubber) Stub(desiredOutput string) { cs.Stubs = append(cs.Stubs, &OutputStub{[]byte(desiredOutput), nil}) } -func (cs *CmdStubber) StubError(msg string) { - // TODO consider handling CmdErr instead of a raw error - cs.Stubs = append(cs.Stubs, &OutputStub{[]byte{}, errors.New(msg)}) +func (cs *CmdStubber) StubError(errText string) { + stderrBuff := bytes.NewBufferString(errText) + args := []string{"stub"} // TODO make more real? + err := errors.New(errText) + cs.Stubs = append(cs.Stubs, &OutputStub{[]byte{}, &run.CmdError{stderrBuff, args, err}}) } func createStubbedPrepareCmd(cs *CmdStubber) func(*exec.Cmd) run.Runnable {