From 8f6a9f7833970ad6659f49c09c93f075a909d498 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Cort=C3=A9s?= Date: Fri, 8 Dec 2017 17:52:25 +0100 Subject: [PATCH] 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. --- cmd/prometheus/main_test.go | 2 +- config/config.go | 2 +- config/config_test.go | 5 ++--- util/testutil/testing.go | 4 ++-- 4 files changed, 6 insertions(+), 7 deletions(-) diff --git a/cmd/prometheus/main_test.go b/cmd/prometheus/main_test.go index e327b9014..055db7158 100644 --- a/cmd/prometheus/main_test.go +++ b/cmd/prometheus/main_test.go @@ -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) } } } diff --git a/config/config.go b/config/config.go index 0e9eadb69..ab4208008 100644 --- a/config/config.go +++ b/config/config.go @@ -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 diff --git a/config/config_test.go b/config/config_test.go index f99e85f35..557c28dde 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -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) { diff --git a/util/testutil/testing.go b/util/testutil/testing.go index cde0e4f91..5cd4a5bae 100644 --- a/util/testutil/testing.go +++ b/util/testutil/testing.go @@ -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() } }