From 0a908c429475a5cb8826e1e38538142472bdfe63 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Thu, 5 Dec 2024 23:09:35 +0100 Subject: [PATCH] lint: apply latest testifylint rules Signed-off-by: Matthieu MOREL --- .golangci.yml | 13 ++----------- api/v2/api_test.go | 5 ++--- config/config_test.go | 4 ++-- notify/discord/discord_test.go | 8 +++----- notify/email/email_test.go | 9 +++------ notify/jira/jira_test.go | 6 ++---- notify/msteams/msteams_test.go | 6 ++---- notify/msteamsv2/msteamsv2_test.go | 6 ++---- notify/notify_test.go | 2 +- notify/opsgenie/opsgenie_test.go | 6 +++--- notify/pagerduty/pagerduty_test.go | 8 +++----- notify/pushover/pushover_test.go | 3 +-- notify/rocketchat/rocketchat_test.go | 3 +-- notify/slack/slack_test.go | 7 +++---- notify/sns/sns_test.go | 6 ++---- notify/telegram/telegram_test.go | 3 +-- notify/test/test.go | 3 +-- notify/victorops/victorops_test.go | 5 ++--- notify/webex/webex_test.go | 6 ++---- notify/webhook/webhook_test.go | 2 +- provider/mem/mem_test.go | 3 +-- test/with_api_v2/acceptance/utf8_test.go | 7 ++----- 22 files changed, 42 insertions(+), 79 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 59b4d39278..4bd58727b3 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -84,14 +84,5 @@ linters-settings: disable: - float-compare - go-require - enable: - - bool-compare - - compares - - empty - - error-is-as - - error-nil - - expected-actual - - len - - require-error - - suite-dont-use-pkg - - suite-extra-assert-call + - useless-assert + enable-all: true diff --git a/api/v2/api_test.go b/api/v2/api_test.go index 4baa4bdef4..21e83b46e5 100644 --- a/api/v2/api_test.go +++ b/api/v2/api_test.go @@ -16,7 +16,6 @@ package v2 import ( "bytes" "encoding/json" - "fmt" "io" "net/http" "net/http/httptest" @@ -206,7 +205,7 @@ func TestDeleteSilenceHandler(t *testing.T) { responder.WriteResponse(w, p) body, _ := io.ReadAll(w.Result().Body) - require.Equal(t, tc.expectedCode, w.Code, fmt.Sprintf("test case: %d, response: %s", i, string(body))) + require.Equalf(t, tc.expectedCode, w.Code, "test case: %d, response: %s", i, string(body)) } } @@ -280,7 +279,7 @@ func TestPostSilencesHandler(t *testing.T) { w := httptest.NewRecorder() postSilences(t, w, api.postSilencesHandler, sil) body, _ := io.ReadAll(w.Result().Body) - require.Equal(t, tc.expectedCode, w.Code, fmt.Sprintf("test case: %d, response: %s", i, string(body))) + require.Equalf(t, tc.expectedCode, w.Code, "test case: %d, response: %s", i, string(body)) }) } }) diff --git a/config/config_test.go b/config/config_test.go index 15edfeed2d..e5cdbd5f8e 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -547,7 +547,7 @@ func TestJSONMarshalSecret(t *testing.T) { // u003c -> "<" // u003e -> ">" - require.Equal(t, "{\"S\":\"\\u003csecret\\u003e\"}", string(c), "Secret not properly elided.") + require.JSONEq(t, "{\"S\":\"\\u003csecret\\u003e\"}", string(c), "Secret not properly elided.") } func TestMarshalSecretURL(t *testing.T) { @@ -1383,7 +1383,7 @@ func TestNilRegexp(t *testing.T) { _, err = LoadFile(tc.file) require.Error(t, err) - require.Contains(t, err.Error(), tc.errMsg) + require.ErrorContains(t, err, tc.errMsg) }) } } diff --git a/notify/discord/discord_test.go b/notify/discord/discord_test.go index 23425c5c0e..ee822cfe04 100644 --- a/notify/discord/discord_test.go +++ b/notify/discord/discord_test.go @@ -16,7 +16,6 @@ package discord import ( "context" "encoding/json" - "fmt" "io" "net/http" "net/http/httptest" @@ -52,7 +51,7 @@ func TestDiscordRetry(t *testing.T) { for statusCode, expected := range test.RetryTests(test.DefaultRetryCodes()) { actual, _ := notifier.retrier.Check(statusCode, nil) - require.Equal(t, expected, actual, fmt.Sprintf("retry - error on status %d", statusCode)) + require.Equalf(t, expected, actual, "retry - error on status %d", statusCode) } } @@ -122,8 +121,7 @@ func TestDiscordTemplating(t *testing.T) { if tc.errMsg == "" { require.NoError(t, err) } else { - require.Error(t, err) - require.Contains(t, err.Error(), tc.errMsg) + require.ErrorContains(t, err, tc.errMsg) } require.Equal(t, tc.retry, ok) }) @@ -229,5 +227,5 @@ func TestDiscord_Notify(t *testing.T) { require.NoError(t, err) require.False(t, ok) - require.Equal(t, "{\"content\":\"Test Content\",\"embeds\":[{\"title\":\"Test Title\",\"description\":\"Test Message\",\"color\":10038562}],\"username\":\"Test Username\",\"avatar_url\":\"http://example.com/avatar.png\"}\n", resp) + require.JSONEq(t, "{\"content\":\"Test Content\",\"embeds\":[{\"title\":\"Test Title\",\"description\":\"Test Message\",\"color\":10038562}],\"username\":\"Test Username\",\"avatar_url\":\"http://example.com/avatar.png\"}\n", resp) } diff --git a/notify/email/email_test.go b/notify/email/email_test.go index 0eec7159ec..74833dd645 100644 --- a/notify/email/email_test.go +++ b/notify/email/email_test.go @@ -319,8 +319,7 @@ func TestEmailNotifyWithErrors(t *testing.T) { } _, retry, err := notifyEmail(emailCfg, c.Server) - require.Error(t, err) - require.Contains(t, err.Error(), tc.errMsg) + require.ErrorContains(t, err, tc.errMsg) require.False(t, retry) e, err := c.Server.getLastEmail() @@ -359,8 +358,7 @@ func TestEmailNotifyWithDoneContext(t *testing.T) { }, c.Server, ) - require.Error(t, err) - require.Contains(t, err.Error(), "establish connection to server") + require.ErrorContains(t, err, "establish connection to server") } // TestEmailNotifyWithoutAuthentication sends an email to an instance of @@ -589,8 +587,7 @@ func TestEmailNotifyWithAuthentication(t *testing.T) { e, retry, err := notifyEmail(emailCfg, c.Server) if len(tc.errMsg) > 0 { - require.Error(t, err) - require.Contains(t, err.Error(), tc.errMsg) + require.ErrorContains(t, err, tc.errMsg) require.Equal(t, tc.retry, retry) return } diff --git a/notify/jira/jira_test.go b/notify/jira/jira_test.go index fd2583a107..f5ffd057f6 100644 --- a/notify/jira/jira_test.go +++ b/notify/jira/jira_test.go @@ -16,7 +16,6 @@ package jira import ( "context" "encoding/json" - "fmt" "io" "net/http" "net/http/httptest" @@ -58,7 +57,7 @@ func TestJiraRetry(t *testing.T) { for statusCode, expected := range test.RetryTests(retryCodes) { actual, _ := notifier.retrier.Check(statusCode, nil) - require.Equal(t, expected, actual, fmt.Sprintf("retry - error on status %d", statusCode)) + require.Equalf(t, expected, actual, "retry - error on status %d", statusCode) } } @@ -145,8 +144,7 @@ func TestJiraTemplating(t *testing.T) { if tc.errMsg == "" { require.NoError(t, err) } else { - require.Error(t, err) - require.Contains(t, err.Error(), tc.errMsg) + require.ErrorContains(t, err, tc.errMsg) } require.Equal(t, tc.retry, ok) }) diff --git a/notify/msteams/msteams_test.go b/notify/msteams/msteams_test.go index 86d3bae48a..6610011fa2 100644 --- a/notify/msteams/msteams_test.go +++ b/notify/msteams/msteams_test.go @@ -16,7 +16,6 @@ package msteams import ( "context" "encoding/json" - "fmt" "io" "net/http" "net/http/httptest" @@ -52,7 +51,7 @@ func TestMSTeamsRetry(t *testing.T) { for statusCode, expected := range test.RetryTests(test.DefaultRetryCodes()) { actual, _ := notifier.retrier.Check(statusCode, nil) - require.Equal(t, expected, actual, fmt.Sprintf("retry - error on status %d", statusCode)) + require.Equalf(t, expected, actual, "retry - error on status %d", statusCode) } } @@ -132,8 +131,7 @@ func TestMSTeamsTemplating(t *testing.T) { if tc.errMsg == "" { require.NoError(t, err) } else { - require.Error(t, err) - require.Contains(t, err.Error(), tc.errMsg) + require.ErrorContains(t, err, tc.errMsg) } require.Equal(t, tc.retry, ok) }) diff --git a/notify/msteamsv2/msteamsv2_test.go b/notify/msteamsv2/msteamsv2_test.go index 847920915b..963679e7f8 100644 --- a/notify/msteamsv2/msteamsv2_test.go +++ b/notify/msteamsv2/msteamsv2_test.go @@ -16,7 +16,6 @@ package msteamsv2 import ( "context" "encoding/json" - "fmt" "io" "net/http" "net/http/httptest" @@ -52,7 +51,7 @@ func TestMSTeamsV2Retry(t *testing.T) { for statusCode, expected := range test.RetryTests(test.DefaultRetryCodes()) { actual, _ := notifier.retrier.Check(statusCode, nil) - require.Equal(t, expected, actual, fmt.Sprintf("retry - error on status %d", statusCode)) + require.Equalf(t, expected, actual, "retry - error on status %d", statusCode) } } @@ -176,8 +175,7 @@ func TestMSTeamsV2Templating(t *testing.T) { if tc.errMsg == "" { require.NoError(t, err) } else { - require.Error(t, err) - require.Contains(t, err.Error(), tc.errMsg) + require.ErrorContains(t, err, tc.errMsg) } require.Equal(t, tc.retry, ok) }) diff --git a/notify/notify_test.go b/notify/notify_test.go index 9d2fad5a17..7aa20cbaf0 100644 --- a/notify/notify_test.go +++ b/notify/notify_test.go @@ -268,7 +268,7 @@ func TestDedupStage(t *testing.T) { }, } ctx, _, err = s.Exec(ctx, promslog.NewNopLogger(), alerts...) - require.Contains(t, err.Error(), "result size") + require.ErrorContains(t, err, "result size") // Must return no error and no alerts no need to update. i = 0 diff --git a/notify/opsgenie/opsgenie_test.go b/notify/opsgenie/opsgenie_test.go index 9db635d1d9..28d6a34d2a 100644 --- a/notify/opsgenie/opsgenie_test.go +++ b/notify/opsgenie/opsgenie_test.go @@ -47,7 +47,7 @@ func TestOpsGenieRetry(t *testing.T) { retryCodes := append(test.DefaultRetryCodes(), http.StatusTooManyRequests) for statusCode, expected := range test.RetryTests(retryCodes) { actual, _ := notifier.retrier.Check(statusCode, nil) - require.Equal(t, expected, actual, fmt.Sprintf("error on status %d", statusCode)) + require.Equalf(t, expected, actual, "error on status %d", statusCode) } } @@ -314,10 +314,10 @@ func TestOpsGenieWithUpdate(t *testing.T) { require.NotEmpty(t, body0) require.Equal(t, requests[1].URL.String(), fmt.Sprintf("https://test-opsgenie-url/v2/alerts/%s/message?identifierType=alias", alias)) - require.Equal(t, `{"message":"new message"} + require.JSONEq(t, `{"message":"new message"} `, body1) require.Equal(t, requests[2].URL.String(), fmt.Sprintf("https://test-opsgenie-url/v2/alerts/%s/description?identifierType=alias", alias)) - require.Equal(t, `{"description":"new description"} + require.JSONEq(t, `{"description":"new description"} `, body2) } diff --git a/notify/pagerduty/pagerduty_test.go b/notify/pagerduty/pagerduty_test.go index 60e2e49259..d756743bf8 100644 --- a/notify/pagerduty/pagerduty_test.go +++ b/notify/pagerduty/pagerduty_test.go @@ -17,7 +17,6 @@ import ( "bytes" "context" "encoding/json" - "fmt" "io" "net/http" "net/http/httptest" @@ -52,7 +51,7 @@ func TestPagerDutyRetryV1(t *testing.T) { retryCodes := append(test.DefaultRetryCodes(), http.StatusForbidden) for statusCode, expected := range test.RetryTests(retryCodes) { actual, _ := notifier.retrier.Check(statusCode, nil) - require.Equal(t, expected, actual, fmt.Sprintf("retryv1 - error on status %d", statusCode)) + require.Equalf(t, expected, actual, "retryv1 - error on status %d", statusCode) } } @@ -70,7 +69,7 @@ func TestPagerDutyRetryV2(t *testing.T) { retryCodes := append(test.DefaultRetryCodes(), http.StatusTooManyRequests) for statusCode, expected := range test.RetryTests(retryCodes) { actual, _ := notifier.retrier.Check(statusCode, nil) - require.Equal(t, expected, actual, fmt.Sprintf("retryv2 - error on status %d", statusCode)) + require.Equalf(t, expected, actual, "retryv2 - error on status %d", statusCode) } } @@ -274,8 +273,7 @@ func TestPagerDutyTemplating(t *testing.T) { if tc.errMsg == "" { require.NoError(t, err) } else { - require.Error(t, err) - require.Contains(t, err.Error(), tc.errMsg) + require.ErrorContains(t, err, tc.errMsg) } require.Equal(t, tc.retry, ok) }) diff --git a/notify/pushover/pushover_test.go b/notify/pushover/pushover_test.go index e5e99a2f2a..1fef42c80c 100644 --- a/notify/pushover/pushover_test.go +++ b/notify/pushover/pushover_test.go @@ -14,7 +14,6 @@ package pushover import ( - "fmt" "os" "testing" @@ -37,7 +36,7 @@ func TestPushoverRetry(t *testing.T) { require.NoError(t, err) for statusCode, expected := range test.RetryTests(test.DefaultRetryCodes()) { actual, _ := notifier.retrier.Check(statusCode, nil) - require.Equal(t, expected, actual, fmt.Sprintf("error on status %d", statusCode)) + require.Equalf(t, expected, actual, "error on status %d", statusCode) } } diff --git a/notify/rocketchat/rocketchat_test.go b/notify/rocketchat/rocketchat_test.go index 765e1ae7f7..198c1ef86e 100644 --- a/notify/rocketchat/rocketchat_test.go +++ b/notify/rocketchat/rocketchat_test.go @@ -14,7 +14,6 @@ package rocketchat import ( - "fmt" "net/url" "os" "testing" @@ -42,7 +41,7 @@ func TestRocketchatRetry(t *testing.T) { for statusCode, expected := range test.RetryTests(test.DefaultRetryCodes()) { actual, _ := notifier.retrier.Check(statusCode, nil) - require.Equal(t, expected, actual, fmt.Sprintf("error on status %d", statusCode)) + require.Equalf(t, expected, actual, "error on status %d", statusCode) } } diff --git a/notify/slack/slack_test.go b/notify/slack/slack_test.go index 720a5464e1..fc161007d1 100644 --- a/notify/slack/slack_test.go +++ b/notify/slack/slack_test.go @@ -15,7 +15,6 @@ package slack import ( "context" - "fmt" "io" "net/http" "net/http/httptest" @@ -48,7 +47,7 @@ func TestSlackRetry(t *testing.T) { for statusCode, expected := range test.RetryTests(test.DefaultRetryCodes()) { actual, _ := notifier.retrier.Check(statusCode, nil) - require.Equal(t, expected, actual, fmt.Sprintf("error on status %d", statusCode)) + require.Equalf(t, expected, actual, "error on status %d", statusCode) } } @@ -229,8 +228,8 @@ func TestNotifier_Notify_WithReason(t *testing.T) { var reasonError *notify.ErrorWithReason require.ErrorAs(t, err, &reasonError) require.Equal(t, tt.expectedReason, reasonError.Reason) - require.Contains(t, err.Error(), tt.expectedErr) - require.Contains(t, err.Error(), "channelname") + require.ErrorContains(t, err, tt.expectedErr) + require.ErrorContains(t, err, "channelname") } }) } diff --git a/notify/sns/sns_test.go b/notify/sns/sns_test.go index 7e00616e92..f07b5159e7 100644 --- a/notify/sns/sns_test.go +++ b/notify/sns/sns_test.go @@ -16,7 +16,6 @@ package sns import ( "context" "net/url" - "strings" "testing" commoncfg "github.com/prometheus/common/config" @@ -134,9 +133,8 @@ func TestNotifyWithInvalidTemplate(t *testing.T) { require.NoError(t, err) var alerts []*types.Alert _, err = notifier.Notify(context.Background(), alerts...) - require.Error(t, err) - require.True(t, strings.Contains(err.Error(), "template \"unknown_template\" not defined")) - require.True(t, strings.Contains(err.Error(), tc.errMsg)) + require.ErrorContains(t, err, "template \"unknown_template\" not defined") + require.ErrorContains(t, err, tc.errMsg) }) } } diff --git a/notify/telegram/telegram_test.go b/notify/telegram/telegram_test.go index 6966109f30..1aeb47c0f9 100644 --- a/notify/telegram/telegram_test.go +++ b/notify/telegram/telegram_test.go @@ -16,7 +16,6 @@ package telegram import ( "context" "encoding/json" - "fmt" "io" "net/http" "net/http/httptest" @@ -82,7 +81,7 @@ func TestTelegramRetry(t *testing.T) { for statusCode, expected := range test.RetryTests(test.DefaultRetryCodes()) { actual, _ := notifier.retrier.Check(statusCode, nil) - require.Equal(t, expected, actual, fmt.Sprintf("error on status %d", statusCode)) + require.Equalf(t, expected, actual, "error on status %d", statusCode) } } diff --git a/notify/test/test.go b/notify/test/test.go index 75729b2a4f..570dd94b3c 100644 --- a/notify/test/test.go +++ b/notify/test/test.go @@ -154,8 +154,7 @@ func AssertNotifyLeaksNoSecret(ctx context.Context, t *testing.T, n notify.Notif }, }...) - require.Error(t, err) - require.Contains(t, err.Error(), context.Canceled.Error()) + require.ErrorContains(t, err, context.Canceled.Error()) for _, s := range secret { require.NotContains(t, err.Error(), s) } diff --git a/notify/victorops/victorops_test.go b/notify/victorops/victorops_test.go index d71de96749..5b42a31100 100644 --- a/notify/victorops/victorops_test.go +++ b/notify/victorops/victorops_test.go @@ -16,7 +16,6 @@ package victorops import ( "context" "encoding/json" - "fmt" "net/http" "net/http/httptest" "net/url" @@ -97,7 +96,7 @@ func TestVictorOpsRetry(t *testing.T) { require.NoError(t, err) for statusCode, expected := range test.RetryTests(test.DefaultRetryCodes()) { actual, _ := notifier.retrier.Check(statusCode, nil) - require.Equal(t, expected, actual, fmt.Sprintf("error on status %d", statusCode)) + require.Equalf(t, expected, actual, "error on status %d", statusCode) } } @@ -226,7 +225,7 @@ func TestVictorOpsTemplating(t *testing.T) { if tc.errMsg == "" { require.NoError(t, err) } else { - require.Contains(t, err.Error(), tc.errMsg) + require.ErrorContains(t, err, tc.errMsg) } }) } diff --git a/notify/webex/webex_test.go b/notify/webex/webex_test.go index 1e09be42fd..e1608a52cc 100644 --- a/notify/webex/webex_test.go +++ b/notify/webex/webex_test.go @@ -15,7 +15,6 @@ package webex import ( "context" - "fmt" "io" "net/http" "net/http/httptest" @@ -50,7 +49,7 @@ func TestWebexRetry(t *testing.T) { for statusCode, expected := range test.RetryTests(test.DefaultRetryCodes()) { actual, _ := notifier.retrier.Check(statusCode, nil) - require.Equal(t, expected, actual, fmt.Sprintf("error on status %d", statusCode)) + require.Equalf(t, expected, actual, "error on status %d", statusCode) } } @@ -159,8 +158,7 @@ func TestWebexTemplating(t *testing.T) { require.Equal(t, tt.expHeader, header.Get("Authorization")) require.JSONEq(t, tt.expJSON, string(out)) } else { - require.Error(t, err) - require.Contains(t, err.Error(), tt.errMsg) + require.ErrorContains(t, err, tt.errMsg) } require.Equal(t, tt.retry, ok) diff --git a/notify/webhook/webhook_test.go b/notify/webhook/webhook_test.go index 777d1c699d..57dff863c0 100644 --- a/notify/webhook/webhook_test.go +++ b/notify/webhook/webhook_test.go @@ -51,7 +51,7 @@ func TestWebhookRetry(t *testing.T) { t.Run("test retry status code", func(t *testing.T) { for statusCode, expected := range test.RetryTests(test.DefaultRetryCodes()) { actual, _ := notifier.retrier.Check(statusCode, nil) - require.Equal(t, expected, actual, fmt.Sprintf("error on status %d", statusCode)) + require.Equalf(t, expected, actual, "error on status %d", statusCode) } }) diff --git a/provider/mem/mem_test.go b/provider/mem/mem_test.go index 880b2bdcf4..99dbd78767 100644 --- a/provider/mem/mem_test.go +++ b/provider/mem/mem_test.go @@ -366,8 +366,7 @@ func TestAlertsGC(t *testing.T) { for i, a := range insert { _, err := alerts.Get(a.Fingerprint()) - require.Error(t, err) - require.Equal(t, store.ErrNotFound, err, fmt.Sprintf("alert %d didn't get GC'd: %v", i, err)) + require.ErrorIs(t, err, store.ErrNotFound, "alert %d didn't get GC'd: %v", i, err) s := marker.Status(a.Fingerprint()) if s.State != types.AlertStateUnprocessed { diff --git a/test/with_api_v2/acceptance/utf8_test.go b/test/with_api_v2/acceptance/utf8_test.go index c0c1dce256..c2a9fbe55c 100644 --- a/test/with_api_v2/acceptance/utf8_test.go +++ b/test/with_api_v2/acceptance/utf8_test.go @@ -15,7 +15,6 @@ package test import ( "fmt" - "strings" "testing" "time" @@ -150,8 +149,7 @@ receivers: alertParams.Alerts = models.PostableAlerts{pa} _, err := am.Client().Alert.PostAlerts(alertParams) - require.Error(t, err) - require.True(t, strings.Contains(err.Error(), "invalid label set")) + require.ErrorContains(t, err, "invalid label set") } func TestAddUTF8Silences(t *testing.T) { @@ -266,8 +264,7 @@ receivers: silenceParams.Silence = &ps _, err := am.Client().Silence.PostSilences(silenceParams) - require.Error(t, err) - require.True(t, strings.Contains(err.Error(), "invalid silence: invalid label matcher")) + require.ErrorContains(t, err, "invalid silence: invalid label matcher") } func TestSendAlertsToUTF8Route(t *testing.T) {