Skip to content

Commit

Permalink
lint: apply latest testifylint rules
Browse files Browse the repository at this point in the history
Signed-off-by: Matthieu MOREL <[email protected]>
  • Loading branch information
mmorel-35 committed Dec 6, 2024
1 parent 0f65e8f commit 01e3753
Show file tree
Hide file tree
Showing 22 changed files with 42 additions and 80 deletions.
13 changes: 2 additions & 11 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 2 additions & 3 deletions api/v2/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package v2
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -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))
}
}

Expand Down Expand Up @@ -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))
})
}
})
Expand Down
5 changes: 2 additions & 3 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -1382,8 +1382,7 @@ func TestNilRegexp(t *testing.T) {
require.NoError(t, err)

_, err = LoadFile(tc.file)
require.Error(t, err)
require.Contains(t, err.Error(), tc.errMsg)
require.ErrorContains(t, err, tc.errMsg)
})
}
}
8 changes: 3 additions & 5 deletions notify/discord/discord_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package discord
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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)
})
Expand Down Expand Up @@ -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)
}
9 changes: 3 additions & 6 deletions notify/email/email_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 2 additions & 4 deletions notify/jira/jira_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package jira
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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)
})
Expand Down
6 changes: 2 additions & 4 deletions notify/msteams/msteams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package msteams
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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)
})
Expand Down
6 changes: 2 additions & 4 deletions notify/msteamsv2/msteamsv2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package msteamsv2
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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)
})
Expand Down
2 changes: 1 addition & 1 deletion notify/notify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions notify/opsgenie/opsgenie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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)
}

Expand Down
8 changes: 3 additions & 5 deletions notify/pagerduty/pagerduty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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)
})
Expand Down
3 changes: 1 addition & 2 deletions notify/pushover/pushover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package pushover

import (
"fmt"
"os"
"testing"

Expand All @@ -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)
}
}

Expand Down
3 changes: 1 addition & 2 deletions notify/rocketchat/rocketchat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package rocketchat

import (
"fmt"
"net/url"
"os"
"testing"
Expand Down Expand Up @@ -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)
}
}

Expand Down
7 changes: 3 additions & 4 deletions notify/slack/slack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ package slack

import (
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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")
}
})
}
Expand Down
6 changes: 2 additions & 4 deletions notify/sns/sns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package sns
import (
"context"
"net/url"
"strings"
"testing"

commoncfg "github.com/prometheus/common/config"
Expand Down Expand Up @@ -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)
})
}
}
Expand Down
3 changes: 1 addition & 2 deletions notify/telegram/telegram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package telegram
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -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)
}
}

Expand Down
3 changes: 1 addition & 2 deletions notify/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Loading

0 comments on commit 01e3753

Please sign in to comment.