Skip to content

Commit

Permalink
Fix tests when git config uses gpg signing.
Browse files Browse the repository at this point in the history
- some of the tests run git so if you're using gpg signing you need
  to disable that.
- refactor test helpers to use tb.Helper() and make the error output red.
  This is useful because if another helper uses one of these helpers,
  when it calls .Helper() its output will also be up the stack.
  • Loading branch information
lkysow committed Oct 30, 2018
1 parent 5a8230d commit 4fcece0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
3 changes: 2 additions & 1 deletion server/events/pending_plan_finder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func TestPendingPlanFinder_FindPlanCheckedIn(t *testing.T) {
runCmd(t, repoDir, "git", "add", ".")
runCmd(t, repoDir, "git", "config", "--local", "user.email", "[email protected]")
runCmd(t, repoDir, "git", "config", "--local", "user.name", "atlantisbot")
runCmd(t, repoDir, "git", "commit", "-m", "initial commit")
runCmd(t, repoDir, "git", "commit", "--no-gpg-sign", "-m", "initial commit")

pf := &events.PendingPlanFinder{}
actPlans, err := pf.Find(tmpDir)
Expand All @@ -193,6 +193,7 @@ func TestPendingPlanFinder_FindPlanCheckedIn(t *testing.T) {
}

func runCmd(t *testing.T, dir string, name string, args ...string) string {
t.Helper()
cpCmd := exec.Command(name, args...)
cpCmd.Dir = dir
cpOut, err := cpCmd.CombinedOutput()
Expand Down
36 changes: 21 additions & 15 deletions testing/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
package testing

import (
"fmt"
"path/filepath"
"runtime"
"strings"
"testing"

Expand All @@ -27,19 +24,19 @@ import (
// Assert fails the test if the condition is false.
// Taken from https://github.com/benbjohnson/testing.
func Assert(tb testing.TB, condition bool, msg string, v ...interface{}) {
tb.Helper()
if !condition {
_, file, line, _ := runtime.Caller(1)
fmt.Printf("\033[31m%s:%d: "+msg+"\033[39m\n\n", append([]interface{}{filepath.Base(file), line}, v...)...)
errLog(tb, msg, v...)
tb.FailNow()
}
}

// Ok fails the test if an err is not nil.
// Taken from https://github.com/benbjohnson/testing.
func Ok(tb testing.TB, err error) {
tb.Helper()
if err != nil {
_, file, line, _ := runtime.Caller(1)
fmt.Printf("\033[31m%s:%d: unexpected error: %s\033[39m\n\n", filepath.Base(file), line, err.Error())
errLog(tb, "unexpected error: %s", err.Error())
tb.FailNow()
}
}
Expand All @@ -49,19 +46,21 @@ func Ok(tb testing.TB, err error) {
func Equals(tb testing.TB, exp, act interface{}) {
tb.Helper()
if diff := deep.Equal(exp, act); diff != nil {
_, file, line, _ := runtime.Caller(1)
tb.Fatalf("\033[31m%s:%d: %s\n\nexp: %s******\ngot: %s\033[39m\n", filepath.Base(file), line, diff, spew.Sdump(exp), spew.Sdump(act))
errLog(tb, "%s\n\nexp: %s******\ngot: %s", diff, spew.Sdump(exp), spew.Sdump(act))
tb.FailNow()
}
}

// ErrEquals fails the test if act is nil or act.Error() != exp
func ErrEquals(tb testing.TB, exp string, act error) {
tb.Helper()
if act == nil {
tb.Fatalf("exp err %q but err was nil\n", exp)
errLog(tb, "exp err %q but err was nil\n", exp)
tb.FailNow()
}
if act.Error() != exp {
tb.Fatalf("exp err: %q but got: %q\n", exp, act.Error())
errLog(tb, "exp err: %q but got: %q\n", exp, act.Error())
tb.FailNow()
}
}

Expand All @@ -70,21 +69,28 @@ func ErrEquals(tb testing.TB, exp string, act error) {
func ErrContains(tb testing.TB, substr string, act error) {
tb.Helper()
if act == nil {
tb.Fatalf("exp err to contain %q but err was nil", substr)
errLog(tb, "exp err to contain %q but err was nil", substr)
tb.FailNow()
}
if !strings.Contains(act.Error(), substr) {
tb.Fatalf("exp err %q to contain %q", act.Error(), substr)
errLog(tb, "exp err %q to contain %q", act.Error(), substr)
tb.FailNow()
}
}

// Contains fails the test if the slice doesn't contain the expected element
func Contains(tb testing.TB, exp interface{}, slice []string) {
tb.Helper()
for _, v := range slice {
if v == exp {
return
}
}
_, file, line, _ := runtime.Caller(1)
fmt.Printf("\033[31m%s:%d:\n\n\texp: %#v\n\n\twas not in: %#v\033[39m\n\n", filepath.Base(file), line, exp, slice)
errLog(tb, "exp: %#v\n\n\twas not in: %#v", exp, slice)
tb.FailNow()
}

func errLog(tb testing.TB, fmt string, args ...interface{}) {
tb.Helper()
tb.Logf("\033[31m"+fmt+"\033[39m", args...)
}

0 comments on commit 4fcece0

Please sign in to comment.