From 8c1b9e45cf59ef269e4960fb4c6d803943f27e3f Mon Sep 17 00:00:00 2001 From: Julien Levesy Date: Thu, 12 Oct 2017 13:14:58 +0200 Subject: [PATCH 1/2] add a NotOk helper method in the testing package --- util/testutil/testing.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/util/testutil/testing.go b/util/testutil/testing.go index 270ad6c42c..cde0e4f919 100644 --- a/util/testutil/testing.go +++ b/util/testutil/testing.go @@ -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) { From d7b4fa8d784088d2f5972fef7a9d25cb8e1af847 Mon Sep 17 00:00:00 2001 From: Julien Levesy Date: Thu, 12 Oct 2017 13:16:54 +0200 Subject: [PATCH 2/2] use testutil assertions in the cmd/prometheus package --- cmd/prometheus/main_test.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/cmd/prometheus/main_test.go b/cmd/prometheus/main_test.go index 8921827c72..e327b90141 100644 --- a/cmd/prometheus/main_test.go +++ b/cmd/prometheus/main_test.go @@ -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) } } }