config: simplify tests by using testutil.NotOk (#3289)

Also include filename in all LoadFile errors

Also add mesage to testuitl.NotOk so we can identify failing tests when
using table driven tests.
This commit is contained in:
Alberto Cortés 2017-12-08 17:52:25 +01:00 committed by Brian Brazil
parent e0d917e2f5
commit 8f6a9f7833
4 changed files with 6 additions and 7 deletions

View File

@ -63,7 +63,7 @@ func TestComputeExternalURL(t *testing.T) {
if test.valid {
testutil.Ok(t, err)
} else {
testutil.NotOk(t, err)
testutil.NotOk(t, err, "input=%q: ", test.input)
}
}
}

View File

@ -59,7 +59,7 @@ func LoadFile(filename string) (*Config, error) {
}
cfg, err := Load(string(content))
if err != nil {
return nil, err
return nil, fmt.Errorf("parsing YAML file %s: %v", filename, err)
}
resolveFilepaths(filepath.Dir(filename), cfg)
return cfg, nil

View File

@ -673,8 +673,7 @@ var expectedErrors = []struct {
func TestBadConfigs(t *testing.T) {
for _, ee := range expectedErrors {
_, err := LoadFile("testdata/" + ee.filename)
testutil.Assert(t, err != nil,
"Expected error parsing %s but got none", ee.filename)
testutil.NotOk(t, err, "%s: ", ee.filename)
testutil.Assert(t, strings.Contains(err.Error(), ee.errMsg),
"Expected error for %s to contain %q but got: %s", ee.filename, ee.errMsg, err)
}
@ -685,7 +684,7 @@ func TestBadStaticConfigs(t *testing.T) {
testutil.Ok(t, err)
var tg TargetGroup
err = json.Unmarshal(content, &tg)
testutil.Assert(t, err != nil, "Expected unmarshal error but got none.")
testutil.NotOk(t, err, "", nil)
}
func TestEmptyConfig(t *testing.T) {

View File

@ -49,10 +49,10 @@ func Ok(tb testing.TB, err error) {
}
// NotOk fails the test if an err is nil.
func NotOk(tb testing.TB, err error) {
func NotOk(tb testing.TB, err error, msg string, v ...interface{}) {
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)
fmt.Printf("\033[31m%s:%d: "+msg+"expected error, got nothing\033[39m\n\n", append([]interface{}{filepath.Base(file), line}, v...)...)
tb.FailNow()
}
}