From 16e68c01e4bf0df3da3a02a214f0076bed911d4d Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Thu, 1 Feb 2024 14:18:01 +0000 Subject: [PATCH] tests: remove err from message when testify prints it already For instance `require.NoError` will print the unexpected error; we don't need to include it in the message. Signed-off-by: Bryan Boreham --- cmd/prometheus/main_unix_test.go | 2 +- tsdb/db_test.go | 2 +- tsdb/fileutil/flock_test.go | 12 ++++++------ tsdb/querier_test.go | 2 +- tsdb/wlog/reader_test.go | 7 +++---- web/api/v1/api_test.go | 12 ++++++------ 6 files changed, 18 insertions(+), 19 deletions(-) diff --git a/cmd/prometheus/main_unix_test.go b/cmd/prometheus/main_unix_test.go index 59c5fe2bc..2011fb123 100644 --- a/cmd/prometheus/main_unix_test.go +++ b/cmd/prometheus/main_unix_test.go @@ -73,6 +73,6 @@ Loop: require.Error(t, err, "prometheus didn't shutdown gracefully after sending the Interrupt signal") // TODO - find a better way to detect when the process didn't exit as expected! if stoppedErr != nil { - require.EqualError(t, stoppedErr, "signal: interrupt", "prometheus exited with an unexpected error: %v", stoppedErr) + require.EqualError(t, stoppedErr, "signal: interrupt", "prometheus exit") } } diff --git a/tsdb/db_test.go b/tsdb/db_test.go index fb36bbb86..70b12d487 100644 --- a/tsdb/db_test.go +++ b/tsdb/db_test.go @@ -3480,7 +3480,7 @@ func testQuerierShouldNotPanicIfHeadChunkIsTruncatedWhileReadingQueriedChunks(t // when the iterator tries to fetch an head chunk which has been offloaded because // of the head compaction in the meanwhile. if firstErr != nil { - require.ErrorContains(t, firstErr, "cannot populate chunk", "unexpected error: %s", firstErr.Error()) + require.ErrorContains(t, firstErr, "cannot populate chunk") } } diff --git a/tsdb/fileutil/flock_test.go b/tsdb/fileutil/flock_test.go index 240e0954b..7aff789a2 100644 --- a/tsdb/fileutil/flock_test.go +++ b/tsdb/fileutil/flock_test.go @@ -33,12 +33,12 @@ func TestLocking(t *testing.T) { require.Error(t, err, "File %q unexpectedly exists.", fileName) lock, existed, err := Flock(fileName) - require.NoError(t, err, "Error locking file %q: %s", fileName, err) + require.NoError(t, err, "Error locking file %q", fileName) require.False(t, existed, "File %q reported as existing during locking.", fileName) // File must now exist. _, err = os.Stat(fileName) - require.NoError(t, err, "Could not stat file %q expected to exist: %s", fileName, err) + require.NoError(t, err, "Could not stat file %q expected to exist", fileName) // Try to lock again. lockedAgain, existed, err := Flock(fileName) @@ -47,17 +47,17 @@ func TestLocking(t *testing.T) { require.True(t, existed, "Existing file %q not recognized.", fileName) err = lock.Release() - require.NoError(t, err, "Error releasing lock for file %q: %s", fileName, err) + require.NoError(t, err, "Error releasing lock for file %q", fileName) // File must still exist. _, err = os.Stat(fileName) - require.NoError(t, err, "Could not stat file %q expected to exist: %s", fileName, err) + require.NoError(t, err, "Could not stat file %q expected to exist", fileName) // Lock existing file. lock, existed, err = Flock(fileName) - require.NoError(t, err, "Error locking file %q: %s", fileName, err) + require.NoError(t, err, "Error locking file %q", fileName) require.True(t, existed, "Existing file %q not recognized.", fileName) err = lock.Release() - require.NoError(t, err, "Error releasing lock for file %q: %s", fileName, err) + require.NoError(t, err, "Error releasing lock for file %q", fileName) } diff --git a/tsdb/querier_test.go b/tsdb/querier_test.go index 6e1a19ee4..93704809b 100644 --- a/tsdb/querier_test.go +++ b/tsdb/querier_test.go @@ -3084,7 +3084,7 @@ func TestClose(t *testing.T) { createBlock(t, dir, genSeries(1, 1, 10, 20)) db, err := Open(dir, nil, nil, DefaultOptions(), nil) - require.NoError(t, err, "Opening test storage failed: %s", err) + require.NoError(t, err, "Opening test storage failed: %s") defer func() { require.NoError(t, db.Close()) }() diff --git a/tsdb/wlog/reader_test.go b/tsdb/wlog/reader_test.go index 1546a9752..484eff366 100644 --- a/tsdb/wlog/reader_test.go +++ b/tsdb/wlog/reader_test.go @@ -186,10 +186,9 @@ func TestReader(t *testing.T) { require.Equal(t, c.exp[j], rec, "Bytes within record did not match expected Bytes") } if !c.fail { - require.NoError(t, r.Err(), "unexpected error: %s", r.Err()) - } - if c.fail { - require.Error(t, r.Err(), "expected error but got none") + require.NoError(t, r.Err()) + } else { + require.Error(t, r.Err()) } }) } diff --git a/web/api/v1/api_test.go b/web/api/v1/api_test.go index 97a4c6220..eceadb20a 100644 --- a/web/api/v1/api_test.go +++ b/web/api/v1/api_test.go @@ -295,7 +295,7 @@ func (m *rulesRetrieverMock) CreateRuleGroups() { } recordingExpr, err := parser.ParseExpr(`vector(1)`) - require.NoError(m.testing, err, "unable to parse alert expression: %s", err) + require.NoError(m.testing, err, "unable to parse alert expression") recordingRule := rules.NewRecordingRule("recording-rule-1", recordingExpr, labels.Labels{}) r = append(r, recordingRule) @@ -3238,10 +3238,10 @@ func TestRespondError(t *testing.T) { defer s.Close() resp, err := http.Get(s.URL) - require.NoError(t, err, "Error on test request: %s", err) + require.NoError(t, err, "Error on test request") body, err := io.ReadAll(resp.Body) defer resp.Body.Close() - require.NoError(t, err, "Error reading response body: %s", err) + require.NoError(t, err, "Error reading response body") want, have := http.StatusServiceUnavailable, resp.StatusCode require.Equal(t, want, have, "Return code %d expected in error response but got %d", want, have) h := resp.Header.Get("Content-Type") @@ -3249,7 +3249,7 @@ func TestRespondError(t *testing.T) { var res Response err = json.Unmarshal(body, &res) - require.NoError(t, err, "Error unmarshaling JSON body: %s", err) + require.NoError(t, err, "Error unmarshaling JSON body") exp := &Response{ Status: statusError, @@ -3379,7 +3379,7 @@ func TestParseTime(t *testing.T) { for _, test := range tests { ts, err := parseTime(test.input) if !test.fail { - require.NoError(t, err, "Unexpected error for %q: %s", test.input, err) + require.NoError(t, err, "Unexpected error for %q", test.input) require.NotNil(t, ts) require.True(t, ts.Equal(test.result), "Expected time %v for input %q but got %v", test.result, test.input, ts) continue @@ -3429,7 +3429,7 @@ func TestParseDuration(t *testing.T) { for _, test := range tests { d, err := parseDuration(test.input) if !test.fail { - require.NoError(t, err, "Unexpected error for %q: %s", test.input, err) + require.NoError(t, err, "Unexpected error for %q", test.input) require.Equal(t, test.result, d, "Expected duration %v for input %q but got %v", test.result, test.input, d) continue }