use CmdError in StubError and fix git_test

This commit is contained in:
vilmibm 2020-03-26 15:32:31 -05:00
parent 0af232444c
commit ca99096ca8
3 changed files with 28 additions and 27 deletions

View file

@ -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()

View file

@ -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))
}
}

View file

@ -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 {