Merge pull request #3285 from jlevesy/use-testutils-in-cmd-subpackage

Use testutil assertion helpers in cmd package
This commit is contained in:
Tobias Schmidt 2017-10-13 00:12:39 +02:00 committed by GitHub
commit 3589f2f1d4
2 changed files with 20 additions and 8 deletions

View file

@ -13,7 +13,11 @@
package main
import "testing"
import (
"testing"
"github.com/prometheus/prometheus/util/testutil"
)
func TestComputeExternalURL(t *testing.T) {
tests := []struct {
@ -54,13 +58,12 @@ func TestComputeExternalURL(t *testing.T) {
},
}
for i, test := range tests {
r, err := computeExternalURL(test.input, "0.0.0.0:9090")
if test.valid && err != nil {
t.Errorf("%d. expected input to be valid, got %s", i, err)
} else if !test.valid && err == nil {
t.Logf("%+v", test, r)
t.Errorf("%d. expected input to be invalid", i)
for _, test := range tests {
_, err := computeExternalURL(test.input, "0.0.0.0:9090")
if test.valid {
testutil.Ok(t, err)
} else {
testutil.NotOk(t, err)
}
}
}

View file

@ -48,6 +48,15 @@ func Ok(tb testing.TB, err error) {
}
}
// NotOk fails the test if an err is nil.
func NotOk(tb testing.TB, err error) {
if err == nil {
_, file, line, _ := runtime.Caller(1)
fmt.Printf("\033[31m%s:%d: expected error, got nothing \033[39m\n\n", filepath.Base(file), line)
tb.FailNow()
}
}
// Equals fails the test if exp is not equal to act.
func Equals(tb testing.TB, exp, act interface{}) {
if !reflect.DeepEqual(exp, act) {