From d25028204eca4807abcf76971fcd048485614649 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20D=C3=B6tsch?= Date: Thu, 16 May 2024 08:04:05 +0200 Subject: [PATCH] require go 1.21 + updated linters --- bot/bot_init_test.go | 3 +- bot/config/config_test.go | 13 +++--- bot/interaction_test.go | 9 ++-- bot/stats/stats_test.go | 13 +++--- bot/storage/chain_test.go | 4 +- bot/storage/file_test.go | 4 +- bot/storage/storage_test.go | 51 +++++++++++----------- bot/util/duration_test.go | 7 +-- bot/util/template_test.go | 3 +- client/bitbucket_test.go | 7 +-- client/http_test.go | 3 +- client/vcs/bitbucket_test.go | 3 +- client/vcs/git_test.go | 5 ++- client/vcs/null_test.go | 3 +- client/vcs/vcs_test.go | 11 ++--- cmd/cli/integration_test.go | 3 +- command/add_button_test.go | 2 +- command/jenkins/client/jenkins_test.go | 19 ++++---- command/jenkins/client/job_watcher_test.go | 9 ++-- command/jenkins/client/start_build_test.go | 5 ++- command/jenkins/nodes_test.go | 2 +- command/jenkins/retry_build_test.go | 6 +-- command/jira/client_test.go | 8 ++-- command/jira/comment_test.go | 5 ++- command/jira/search_test.go | 11 ++--- command/jira/watch_test.go | 5 ++- command/openai/openai_test.go | 5 ++- command/pullrequest/bitbucket_test.go | 15 ++++--- command/pullrequest/github_test.go | 9 ++-- command/pullrequest/gitlab_test.go | 11 ++--- command/queue/queue_test.go | 9 ++-- mocks/testing.go | 9 ++-- 32 files changed, 150 insertions(+), 122 deletions(-) diff --git a/bot/bot_init_test.go b/bot/bot_init_test.go index 1ff7b531..06df17ad 100644 --- a/bot/bot_init_test.go +++ b/bot/bot_init_test.go @@ -9,6 +9,7 @@ import ( "github.com/innogames/slack-bot/v2/client" "github.com/slack-go/slack" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestBot(t *testing.T) { @@ -20,7 +21,7 @@ func TestBot(t *testing.T) { } slackClient, err := client.GetSlackClient(cfg.Slack) - assert.Nil(t, err) + require.NoError(t, err) commands := &Commands{} commands.AddCommand(testCommand2{}) diff --git a/bot/config/config_test.go b/bot/config/config_test.go index 32b9d99f..beaad5fa 100644 --- a/bot/config/config_test.go +++ b/bot/config/config_test.go @@ -7,11 +7,12 @@ import ( "github.com/brainexe/viper" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestLoadExampleConfig(t *testing.T) { cfg, err := Load("../../config.example.yaml") - assert.Nil(t, err) + require.NoError(t, err) assert.NotNil(t, cfg.Slack) assert.False(t, cfg.Jenkins.IsEnabled()) @@ -30,13 +31,13 @@ func TestLoadDirectory(t *testing.T) { cfg, err := Load("../../") // load root pass == okay - assert.Nil(t, err) + require.NoError(t, err) assert.NotNil(t, cfg.Slack) // invalid directory cfg, err = Load("/sdsdsdds") cfg.viper = nil - assert.NotNil(t, err) + require.Error(t, err) assert.Equal(t, DefaultConfig, cfg) } @@ -45,7 +46,7 @@ func TestLoadFile(t *testing.T) { configPath := path.Join("..", "..", "readme.sdsdsd") cfg, err := Load(configPath) cfg.viper = nil - assert.NotNil(t, err) + require.Error(t, err) assert.Equal(t, DefaultConfig, cfg) // parse invalid file @@ -58,7 +59,7 @@ func TestLoadFile(t *testing.T) { // load example file == okay configPath = path.Join("..", "..", "config.example.yaml") cfg, err = Load(configPath) - assert.Nil(t, err) + require.NoError(t, err) assert.NotNil(t, cfg.Slack) assert.Equal(t, "info", cfg.Logger.Level) @@ -76,7 +77,7 @@ func TestEnvironment(t *testing.T) { // load example file == okay cfg, err := Load("../../config.example.yaml") - assert.Nil(t, err) + require.NoError(t, err) assert.Equal(t, "Europe/Berlin", cfg.Timezone) assert.Equal(t, "mySlackToken", cfg.Slack.Token) assert.Equal(t, "mySlackSocketToken", cfg.Slack.SocketToken) diff --git a/bot/interaction_test.go b/bot/interaction_test.go index 5dc36064..ee8aff83 100644 --- a/bot/interaction_test.go +++ b/bot/interaction_test.go @@ -14,6 +14,7 @@ import ( "github.com/slack-go/slack/slackevents" "github.com/slack-go/slack/socketmode" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) // dummy command which set "called" flag when a command was called with the text "dummy" @@ -67,7 +68,7 @@ func TestInteraction(t *testing.T) { time.Sleep(time.Millisecond * 20) commandsProcessed, err := stats.Get(stats.TotalCommands) - assert.Nil(t, err) + require.NoError(t, err) assert.Equal(t, uint(0), commandsProcessed) }) @@ -94,7 +95,7 @@ func TestInteraction(t *testing.T) { time.Sleep(time.Millisecond * 20) commandsProcessed, err := stats.Get(stats.TotalCommands) - assert.Nil(t, err) + require.NoError(t, err) assert.Equal(t, uint(0), commandsProcessed) }) @@ -124,7 +125,7 @@ func TestInteraction(t *testing.T) { time.Sleep(time.Millisecond * 20) commandsProcessed, err := stats.Get(stats.TotalCommands) - assert.Nil(t, err) + require.NoError(t, err) assert.Equal(t, uint(1), commandsProcessed) }) @@ -212,6 +213,6 @@ func TestReplaceClickedButton(t *testing.T) { expected := `{"replace_original":false,"delete_original":false,"metadata":{"event_type":"","event_payload":null},"blocks":[{"type":"actions","elements":[{"type":"button","text":{"type":"plain_text","text":"my text (worked)","emoji":true},"action_id":"reply","style":"danger"}]}]}` - assert.Nil(t, err) + require.NoError(t, err) assert.Equal(t, expected, string(jsonString)) } diff --git a/bot/stats/stats_test.go b/bot/stats/stats_test.go index 14e263cf..956ef219 100644 --- a/bot/stats/stats_test.go +++ b/bot/stats/stats_test.go @@ -4,37 +4,38 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestStats(t *testing.T) { value, err := Get("test") - assert.NotNil(t, err) + require.Error(t, err) assert.Equal(t, value, uint(0)) Increase("test", 2) value, err = Get("test") - assert.Nil(t, err) + require.NoError(t, err) assert.Equal(t, value, uint(2)) IncreaseOne("test") value, err = Get("test") - assert.Nil(t, err) + require.NoError(t, err) assert.Equal(t, value, uint(3)) Set("test", 42) value, err = Get("test") - assert.Nil(t, err) + require.NoError(t, err) assert.Equal(t, value, uint(42)) Increase("test", int64(1)) Increase("test", int8(1)) Increase("test", 1) value, err = Get("test") - assert.Nil(t, err) + require.NoError(t, err) assert.Equal(t, value, uint(45)) IncreaseOne("test") value, err = Get("test") - assert.Nil(t, err) + require.NoError(t, err) assert.Equal(t, value, uint(46)) } diff --git a/bot/storage/chain_test.go b/bot/storage/chain_test.go index e3b70640..198fa3a2 100644 --- a/bot/storage/chain_test.go +++ b/bot/storage/chain_test.go @@ -4,7 +4,7 @@ import ( "os" "testing" - "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestChainStorage(t *testing.T) { @@ -12,7 +12,7 @@ func TestChainStorage(t *testing.T) { defer os.RemoveAll(dir) fileStorage, err := newFileStorage(dir) - assert.Nil(t, err) + require.NoError(t, err) storage := NewChainStorage(fileStorage, newMemoryStorage()) diff --git a/bot/storage/file_test.go b/bot/storage/file_test.go index bfc1dbec..a340d38e 100644 --- a/bot/storage/file_test.go +++ b/bot/storage/file_test.go @@ -4,7 +4,7 @@ import ( "os" "testing" - "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestFileStorage(t *testing.T) { @@ -12,7 +12,7 @@ func TestFileStorage(t *testing.T) { defer os.RemoveAll(dir) storage, err := newFileStorage(dir) - assert.Nil(t, err) + require.NoError(t, err) t.Run("test file", func(t *testing.T) { testStorage(t, storage) diff --git a/bot/storage/storage_test.go b/bot/storage/storage_test.go index c9040f76..48c976ca 100644 --- a/bot/storage/storage_test.go +++ b/bot/storage/storage_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func testStorage(t *testing.T, storage Storage) { @@ -17,37 +18,37 @@ func testStorage(t *testing.T, storage Storage) { const collection = "collection" err = storage.Write(collection, "test-string", "1") - assert.Nil(t, err) + require.NoError(t, err) err = storage.Write(collection, "test-int", 2) - assert.Nil(t, err) + require.NoError(t, err) err = storage.Write(collection, "test-map", map[int]float32{12: 5.5}) - assert.Nil(t, err) + require.NoError(t, err) // read invalid data err = storage.Read("collection1", "test", &stringValue) - assert.Error(t, err) + require.Error(t, err) err = storage.Read(collection, "test1", &stringValue) - assert.Error(t, err) + require.Error(t, err) assert.Equal(t, "", stringValue) // get keys all keys, err := storage.GetKeys(collection) - assert.Nil(t, err) + require.NoError(t, err) assert.Len(t, keys, 3) keys, err = storage.GetKeys("invalid-collection") - assert.Nil(t, err) + require.NoError(t, err) assert.Len(t, keys, 0) // read valid data err = storage.Read(collection, "test-string", &stringValue) - assert.Nil(t, err) + require.NoError(t, err) assert.Equal(t, "1", stringValue) err = storage.Read(collection, "test-int", &intValue) - assert.Nil(t, err) + require.NoError(t, err) assert.Equal(t, 2, intValue) err = storage.Read(collection, "test-map", &mapValue) - assert.Nil(t, err) + require.NoError(t, err) assert.Equal(t, map[int]float32{12: 5.5}, mapValue) // expected unmarshall error when accessing wrong data @@ -57,9 +58,9 @@ func testStorage(t *testing.T, storage Storage) { // Atomic Atomic(func() { err = storage.Write(collection, "test-int", 1) - assert.Nil(t, err) + require.NoError(t, err) err = storage.Write(collection, "test-int", 2) - assert.Nil(t, err) + require.NoError(t, err) }) var expectedInt int err = storage.Read(collection, "test-int", &expectedInt) @@ -67,29 +68,29 @@ func testStorage(t *testing.T, storage Storage) { // delete err = storage.Delete(collection, "test-string") - assert.Nil(t, err) + require.NoError(t, err) err = storage.Delete(collection, "test-int") - assert.Nil(t, err) + require.NoError(t, err) err = storage.Delete(collection, "test-map") - assert.Nil(t, err) + require.NoError(t, err) err = storage.Read("collection1", "test-map", &stringValue) - assert.Error(t, err) + require.Error(t, err) // Atomic Atomic(func() { err = storage.Write(collection, "test-int2", 1) - assert.Nil(t, err) + require.NoError(t, err) err = storage.Write(collection, "test-int2", 2) - assert.Nil(t, err) + require.NoError(t, err) }) err = storage.Read(collection, "test-int2", &expectedInt) assert.Equal(t, 2, expectedInt) - assert.Nil(t, err) + require.NoError(t, err) err = storage.Delete(collection, "test-int2") - assert.Nil(t, err) + require.NoError(t, err) keys, err = storage.GetKeys(collection) - assert.Nil(t, err) + require.NoError(t, err) assert.Len(t, keys, 0) keys, err = GetKeys("../") @@ -101,7 +102,7 @@ func TestStorage(t *testing.T) { t.Run("validate keys", func(t *testing.T) { var err error err = validateKey("valid", "also-val-id") - assert.Nil(t, err) + require.NoError(t, err) err = validateKey("valid", "not#valid") assert.EqualError(t, err, "invalid Storage key: not#valid") @@ -120,17 +121,17 @@ func TestStorage(t *testing.T) { err := InitStorage(".") storage = getStorage() - assert.NoError(t, err) + require.NoError(t, err) assert.IsType(t, &chainStorage{}, storage) err = InitStorage("") storage = getStorage() - assert.NoError(t, err) + require.NoError(t, err) assert.IsType(t, &memoryStorage{}, storage) fileStorage, _ := newFileStorage(".") SetStorage(fileStorage) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, fileStorage, getStorage()) }) diff --git a/bot/util/duration_test.go b/bot/util/duration_test.go index 8c9b4f46..c7860b47 100644 --- a/bot/util/duration_test.go +++ b/bot/util/duration_test.go @@ -6,6 +6,7 @@ import ( "time" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) var parserTestCases = []struct { @@ -30,12 +31,12 @@ func TestParseDuration(t *testing.T) { t.Run("ParseDuration", func(t *testing.T) { for _, testCase := range parserTestCases { native, err := time.ParseDuration(testCase.normal) - assert.Nil(t, err) + require.NoError(t, err) actualFull, err := ParseDuration(testCase.long) - assert.Nil(t, err) + require.NoError(t, err) actualShort, err := ParseDuration(testCase.normal) - assert.Nil(t, err) + require.NoError(t, err) assert.Equal(t, native.String(), actualFull.String()) assert.Equal(t, native.String(), actualShort.String()) diff --git a/bot/util/template_test.go b/bot/util/template_test.go index 841872c4..6ccbcbcb 100644 --- a/bot/util/template_test.go +++ b/bot/util/template_test.go @@ -5,6 +5,7 @@ import ( "text/template" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestFunctions(t *testing.T) { @@ -29,7 +30,7 @@ func TestTemplate(t *testing.T) { text := "{{ test }} {{ $users := makeSlice \"2222\"}}{{ $users }} {{ .foo }}" temp, err := CompileTemplate(text) - assert.Nil(t, err) + require.NoError(t, err) finalText, _ := EvalTemplate(temp, map[string]string{"foo": "bar"}) assert.Equal(t, finalText, "foo [2222] bar") diff --git a/client/bitbucket_test.go b/client/bitbucket_test.go index c4ad37a0..be1033bf 100644 --- a/client/bitbucket_test.go +++ b/client/bitbucket_test.go @@ -5,6 +5,7 @@ import ( "github.com/innogames/slack-bot/v2/bot/config" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestBitbucket(t *testing.T) { @@ -23,7 +24,7 @@ func TestBitbucket(t *testing.T) { } client, err := GetBitbucketClient(cfg) - assert.Nil(t, err) + require.NoError(t, err) assert.NotNil(t, client) }) @@ -36,7 +37,7 @@ func TestBitbucket(t *testing.T) { client, err := GetBitbucketClient(cfg) - assert.Nil(t, err) + require.NoError(t, err) assert.NotNil(t, client) }) @@ -48,7 +49,7 @@ func TestBitbucket(t *testing.T) { client, err := GetBitbucketClient(cfg) - assert.Nil(t, err) + require.NoError(t, err) assert.NotNil(t, client) }) } diff --git a/client/http_test.go b/client/http_test.go index e80b21f0..1578283d 100644 --- a/client/http_test.go +++ b/client/http_test.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestHttp(t *testing.T) { @@ -19,7 +20,7 @@ func TestHttp(t *testing.T) { client := GetHTTPClient() resp, err := client.Get(server.URL) - assert.Nil(t, err) + require.NoError(t, err) defer resp.Body.Close() bodyBytes, _ := io.ReadAll(resp.Body) diff --git a/client/vcs/bitbucket_test.go b/client/vcs/bitbucket_test.go index 67d9b8f2..8179b5fa 100644 --- a/client/vcs/bitbucket_test.go +++ b/client/vcs/bitbucket_test.go @@ -9,6 +9,7 @@ import ( "github.com/innogames/slack-bot/v2/bot/config" "github.com/innogames/slack-bot/v2/client" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestBitbucketLoader(t *testing.T) { @@ -29,7 +30,7 @@ func TestBitbucketLoader(t *testing.T) { } branches, err := fetcher.LoadBranches() - assert.Nil(t, err) + require.NoError(t, err) assert.Equal(t, []string{"master", "release"}, branches) }) diff --git a/client/vcs/git_test.go b/client/vcs/git_test.go index ea20956e..8a38e394 100644 --- a/client/vcs/git_test.go +++ b/client/vcs/git_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestGitLoader(t *testing.T) { @@ -14,7 +15,7 @@ func TestGitLoader(t *testing.T) { branches, err := fetcher.LoadBranches() assert.GreaterOrEqual(t, len(branches), 1) - assert.Nil(t, err) + require.NoError(t, err) }) t.Run("Load branches with invalid repoURL", func(t *testing.T) { @@ -25,6 +26,6 @@ func TestGitLoader(t *testing.T) { branches, err := fetcher.LoadBranches() assert.Empty(t, branches) - assert.NotNil(t, err) + require.Error(t, err) }) } diff --git a/client/vcs/null_test.go b/client/vcs/null_test.go index 2aa00f79..506ee9d1 100644 --- a/client/vcs/null_test.go +++ b/client/vcs/null_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestNullLoader(t *testing.T) { @@ -11,6 +12,6 @@ func TestNullLoader(t *testing.T) { t.Run("Load branches with null loader", func(t *testing.T) { branches, err := fetcher.LoadBranches() assert.Len(t, branches, 0) - assert.Nil(t, err) + require.NoError(t, err) }) } diff --git a/client/vcs/vcs_test.go b/client/vcs/vcs_test.go index eedb3575..e5b5e6cf 100644 --- a/client/vcs/vcs_test.go +++ b/client/vcs/vcs_test.go @@ -7,6 +7,7 @@ import ( "github.com/innogames/slack-bot/v2/bot/config" "github.com/innogames/slack-bot/v2/bot/util" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestInitBranchWatcher(t *testing.T) { @@ -72,14 +73,14 @@ func TestGetMatchingBranches(t *testing.T) { t.Run("Empty", func(t *testing.T) { actual, err := GetMatchingBranch("") - assert.NotNil(t, err) + require.Error(t, err) assert.Equal(t, "", actual) }) t.Run("Not found", func(t *testing.T) { actual, err := GetMatchingBranch("this-might-be-a-branch") assert.Equal(t, "this-might-be-a-branch", actual) - assert.Nil(t, err) + require.NoError(t, err) }) t.Run("Not unique", func(t *testing.T) { @@ -91,14 +92,14 @@ func TestGetMatchingBranches(t *testing.T) { t.Run("Test unique branches", func(t *testing.T) { actual, err := GetMatchingBranch("master") assert.Equal(t, "master", actual) - assert.Nil(t, err) + require.NoError(t, err) actual, err = GetMatchingBranch("PROJ-1235") assert.Equal(t, "bugfix/PROJ-1235-fixed", actual) - assert.Nil(t, err) + require.NoError(t, err) actual, err = GetMatchingBranch("feature/PROJ-1234-do-something") assert.Equal(t, "feature/PROJ-1234-do-something", actual) - assert.Nil(t, err) + require.NoError(t, err) }) } diff --git a/cmd/cli/integration_test.go b/cmd/cli/integration_test.go index 04bba0cf..8ae35f5c 100644 --- a/cmd/cli/integration_test.go +++ b/cmd/cli/integration_test.go @@ -15,6 +15,7 @@ import ( "github.com/innogames/slack-bot/v2/bot/util" "github.com/innogames/slack-bot/v2/mocks" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestAll(t *testing.T) { @@ -57,7 +58,7 @@ func TestAll(t *testing.T) { testURL := tester.FakeServerURL + "command?command=reply%20X" r, err := http.Get(testURL) //nolint:gosec - assert.Nil(t, err) + require.NoError(t, err) resp, _ := io.ReadAll(r.Body) defer r.Body.Close() diff --git a/command/add_button_test.go b/command/add_button_test.go index b0525252..6128b9f9 100644 --- a/command/add_button_test.go +++ b/command/add_button_test.go @@ -31,6 +31,6 @@ func TestAddButton(t *testing.T) { t.Run("Test help", func(t *testing.T) { help := command.GetHelp() - assert.Equal(t, 1, len(help)) + assert.Len(t, help, 1) }) } diff --git a/command/jenkins/client/jenkins_test.go b/command/jenkins/client/jenkins_test.go index ce3035d2..2b5532c2 100644 --- a/command/jenkins/client/jenkins_test.go +++ b/command/jenkins/client/jenkins_test.go @@ -9,12 +9,13 @@ import ( "github.com/innogames/slack-bot/v2/client" "github.com/innogames/slack-bot/v2/mocks" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestGetClient(t *testing.T) { cfg := config.Jenkins{} jenkinsClient, err := GetClient(cfg) - assert.Nil(t, err) + require.NoError(t, err) assert.Nil(t, jenkinsClient) } @@ -23,12 +24,12 @@ func TestJenkinsNoParameters(t *testing.T) { params := &Parameters{} err := ParseParameters(jobConfig, "", *params) - assert.Equal(t, nil, err) + require.NoError(t, err) assert.Equal(t, &Parameters{}, params) params = &Parameters{} err = ParseParameters(jobConfig, "test", *params) - assert.Equal(t, nil, err) + require.NoError(t, err) assert.Equal(t, &Parameters{}, params) } @@ -59,7 +60,7 @@ func TestJenkinsParameters(t *testing.T) { params = &Parameters{} err = ParseParameters(jobConfig, `testname testvalue "" ""`, *params) - assert.Equal(t, nil, err) + require.NoError(t, err) assert.Equal(t, &Parameters{ "NAME": "testname", "VALUE": "testvalue", @@ -69,7 +70,7 @@ func TestJenkinsParameters(t *testing.T) { params = &Parameters{} err = ParseParameters(jobConfig, "testname \"test value\" uPper lOwer", *params) - assert.Equal(t, nil, err) + require.NoError(t, err) assert.Equal(t, &Parameters{ "NAME": "testname", "VALUE": "test value", @@ -94,7 +95,7 @@ func TestJenkinsDefaultParameters(t *testing.T) { params = &Parameters{} err = ParseParameters(jobConfig, "testname TRUE", *params) - assert.Equal(t, nil, err) + require.NoError(t, err) assert.Equal(t, &Parameters{ "NAME": "testname", "FLAG": "true", @@ -103,7 +104,7 @@ func TestJenkinsDefaultParameters(t *testing.T) { params = &Parameters{} err = ParseParameters(jobConfig, "testname false testvalue", *params) - assert.Equal(t, nil, err) + require.NoError(t, err) assert.Equal(t, &Parameters{ "NAME": "testname", "FLAG": "false", @@ -184,7 +185,7 @@ func TestJenkinsMixedParameters(t *testing.T) { "SUBTYPE": "mySubtype", } err = ParseParameters(jobConfig, "testname", *params) - assert.Equal(t, nil, err) + require.NoError(t, err) assert.Equal(t, &Parameters{ "NAME": "testname", "SUBTYPE": "mySubtype", @@ -193,7 +194,7 @@ func TestJenkinsMixedParameters(t *testing.T) { params = &Parameters{} err = ParseParameters(jobConfig, "testname testsubtype testvalue", *params) - assert.Equal(t, nil, err) + require.NoError(t, err) assert.Equal(t, &Parameters{ "NAME": "testname", "SUBTYPE": "testsubtype", diff --git a/command/jenkins/client/job_watcher_test.go b/command/jenkins/client/job_watcher_test.go index 029e35f1..6147e1a3 100644 --- a/command/jenkins/client/job_watcher_test.go +++ b/command/jenkins/client/job_watcher_test.go @@ -11,6 +11,7 @@ import ( "github.com/bndr/gojenkins" "github.com/innogames/slack-bot/v2/bot/config" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestWatchJob(t *testing.T) { @@ -24,7 +25,7 @@ func TestWatchJob(t *testing.T) { t.Run("Watch not existing Job", func(t *testing.T) { ctx := context.Background() client, err := GetClient(cfg) - assert.Nil(t, err) + require.NoError(t, err) stop := make(chan bool, 1) builds, err := WatchJob(ctx, client, "notExistingJob", stop) @@ -36,12 +37,12 @@ func TestWatchJob(t *testing.T) { t.Run("Watch Job", func(t *testing.T) { ctx := context.Background() client, err := GetClient(cfg) - assert.Nil(t, err) + require.NoError(t, err) stop := make(chan bool, 1) builds, err := WatchJob(ctx, client, "testJob", stop) - assert.Nil(t, err) + require.NoError(t, err) assert.Empty(t, builds) stop <- true @@ -51,7 +52,7 @@ func TestWatchJob(t *testing.T) { t.Run("Watch Job with invalid build", func(t *testing.T) { ctx := context.Background() client, err := GetClient(cfg) - assert.Nil(t, err) + require.NoError(t, err) stop := make(chan bool, 1) builds, err := WatchJob(ctx, client, "testJob2", stop) diff --git a/command/jenkins/client/start_build_test.go b/command/jenkins/client/start_build_test.go index 71cecdfa..fd589f6a 100644 --- a/command/jenkins/client/start_build_test.go +++ b/command/jenkins/client/start_build_test.go @@ -15,6 +15,7 @@ import ( "github.com/innogames/slack-bot/v2/mocks" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" ) func TestStartBuild(t *testing.T) { @@ -46,7 +47,7 @@ func TestStartBuild(t *testing.T) { jobCfg := config.JobConfig{} client, err := GetClient(cfg) - assert.Nil(t, err) + require.NoError(t, err) jobName := "testJob" params := Parameters{ @@ -59,7 +60,7 @@ func TestStartBuild(t *testing.T) { err = TriggerJenkinsJob(jobCfg, jobName, params, slackClient, client, message) time.Sleep(time.Millisecond * 100) - assert.Nil(t, err) + require.NoError(t, err) }) t.Run("format finish build", func(t *testing.T) { diff --git a/command/jenkins/nodes_test.go b/command/jenkins/nodes_test.go index b8fb9e0d..546eff66 100644 --- a/command/jenkins/nodes_test.go +++ b/command/jenkins/nodes_test.go @@ -85,7 +85,7 @@ In total there are 0 build(s) running right now`) t.Run("Test help", func(t *testing.T) { help := command.GetHelp() - assert.Equal(t, 1, len(help)) + assert.Len(t, help, 1) }) } diff --git a/command/jenkins/retry_build_test.go b/command/jenkins/retry_build_test.go index ceb78152..5f1f52c2 100644 --- a/command/jenkins/retry_build_test.go +++ b/command/jenkins/retry_build_test.go @@ -2,7 +2,7 @@ package jenkins import ( "context" - "fmt" + "errors" "testing" "github.com/innogames/slack-bot/v2/bot" @@ -56,13 +56,13 @@ func TestJenkinsRetry(t *testing.T) { mocks.AssertSlackMessage(slackClient, message, "Job *TestJob* does not exist") ctx := context.TODO() - jenkinsClient.On("GetJob", ctx, "TestJob").Return(nil, fmt.Errorf("")) + jenkinsClient.On("GetJob", ctx, "TestJob").Return(nil, errors.New("")) actual := command.Run(message) assert.True(t, actual) }) t.Run("Test help", func(t *testing.T) { help := command.GetHelp() - assert.Equal(t, 1, len(help)) + assert.Len(t, help, 1) }) } diff --git a/command/jira/client_test.go b/command/jira/client_test.go index 87e3ce20..c915bd2c 100644 --- a/command/jira/client_test.go +++ b/command/jira/client_test.go @@ -3,6 +3,8 @@ package jira import ( "testing" + "github.com/stretchr/testify/require" + "github.com/innogames/slack-bot/v2/bot/config" "github.com/stretchr/testify/assert" ) @@ -14,7 +16,7 @@ func TestJiraClient(t *testing.T) { } client, err := getClient(cfg) - assert.Nil(t, err) + require.NoError(t, err) assert.Equal(t, "jira.example.com", client.GetBaseURL().Host) }) @@ -26,7 +28,7 @@ func TestJiraClient(t *testing.T) { } client, err := getClient(cfg) - assert.Nil(t, err) + require.NoError(t, err) assert.False(t, client.Authentication.Authenticated()) }) @@ -38,7 +40,7 @@ func TestJiraClient(t *testing.T) { } client, err := getClient(cfg) - assert.Nil(t, err) + require.NoError(t, err) assert.False(t, client.Authentication.Authenticated()) client.Authentication.GetCurrentUser() }) diff --git a/command/jira/comment_test.go b/command/jira/comment_test.go index e3cdb3f4..45bf6987 100644 --- a/command/jira/comment_test.go +++ b/command/jira/comment_test.go @@ -10,6 +10,7 @@ import ( "github.com/innogames/slack-bot/v2/bot/msg" "github.com/innogames/slack-bot/v2/mocks" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestCommentJira(t *testing.T) { @@ -31,7 +32,7 @@ func TestCommentJira(t *testing.T) { Project: "TEST", } jiraClient, err := getClient(cfg) - assert.Nil(t, err) + require.NoError(t, err) command := bot.Commands{} command.AddCommand(newCommentCommand(jiraClient, slackClient, cfg)) @@ -72,6 +73,6 @@ func TestCommentJira(t *testing.T) { t.Run("Test help", func(t *testing.T) { help := command.GetHelp() - assert.Equal(t, 1, len(help)) + assert.Len(t, help, 1) }) } diff --git a/command/jira/search_test.go b/command/jira/search_test.go index c82ec548..1e7963b1 100644 --- a/command/jira/search_test.go +++ b/command/jira/search_test.go @@ -11,6 +11,7 @@ import ( "github.com/innogames/slack-bot/v2/mocks" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" ) func TestJiraSearch(t *testing.T) { @@ -21,7 +22,7 @@ func TestJiraSearch(t *testing.T) { Project: "ZOOKEEPER", } jiraClient, err := getClient(cfg) - assert.Nil(t, err) + require.NoError(t, err) command := bot.Commands{} command.AddCommand(newJiraCommand(jiraClient, slackClient, cfg)) @@ -122,20 +123,20 @@ func TestJiraSearch(t *testing.T) { t.Run("Render template with jiraTicket()", func(t *testing.T) { tpl, err := util.CompileTemplate(`{{$ticket := jiraTicket "ZOOKEEPER-3455"}}ID: {{$ticket.ID}} Key: {{$ticket.Key}}`) - assert.Nil(t, err) + require.NoError(t, err) res, err := util.EvalTemplate(tpl, util.Parameters{}) - assert.Nil(t, err) + require.NoError(t, err) assert.Equal(t, "ID: 13242741 Key: ZOOKEEPER-3455", res) }) t.Run("Render template with jiraTicketUrl()", func(t *testing.T) { tpl, err := util.CompileTemplate(`{{ jiraTicketUrl "ZOOKEEPER-3455"}}`) - assert.Nil(t, err) + require.NoError(t, err) res, err := util.EvalTemplate(tpl, util.Parameters{}) - assert.Nil(t, err) + require.NoError(t, err) assert.Equal(t, "https://issues.apache.org/jira/browse/ZOOKEEPER-3455", res) }) diff --git a/command/jira/watch_test.go b/command/jira/watch_test.go index b149ddd2..0c107778 100644 --- a/command/jira/watch_test.go +++ b/command/jira/watch_test.go @@ -8,6 +8,7 @@ import ( "github.com/innogames/slack-bot/v2/bot/msg" "github.com/innogames/slack-bot/v2/mocks" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestWatchJira(t *testing.T) { @@ -18,7 +19,7 @@ func TestWatchJira(t *testing.T) { Host: "https://issues.apache.org/jira/", } jiraClient, err := getClient(cfg) - assert.Nil(t, err) + require.NoError(t, err) command := bot.Commands{} command.AddCommand(newWatchCommand(jiraClient, slackClient, cfg)) @@ -47,6 +48,6 @@ func TestWatchJira(t *testing.T) { t.Run("Test help", func(t *testing.T) { help := command.GetHelp() - assert.Equal(t, 1, len(help)) + assert.Len(t, help, 1) }) } diff --git a/command/openai/openai_test.go b/command/openai/openai_test.go index 5baf3a86..9fa7e42a 100644 --- a/command/openai/openai_test.go +++ b/command/openai/openai_test.go @@ -19,6 +19,7 @@ import ( "github.com/slack-go/slack" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" ) type testRequest struct { @@ -278,10 +279,10 @@ data: [DONE]`, util.RegisterFunctions(command.GetTemplateFunction()) tpl, err := util.CompileTemplate(`{{ openai "whats 1+1?"}}`) - assert.Nil(t, err) + require.NoError(t, err) res, err := util.EvalTemplate(tpl, util.Parameters{}) - assert.Nil(t, err) + require.NoError(t, err) assert.Equal(t, "The answer is 2", res) }) diff --git a/command/pullrequest/bitbucket_test.go b/command/pullrequest/bitbucket_test.go index 1eeffca0..06ea1757 100644 --- a/command/pullrequest/bitbucket_test.go +++ b/command/pullrequest/bitbucket_test.go @@ -15,6 +15,7 @@ import ( "github.com/innogames/slack-bot/v2/mocks" "github.com/slack-go/slack" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestBitbucketNotActive(t *testing.T) { @@ -74,35 +75,35 @@ func TestBitbucketFakeServer(t *testing.T) { t.Run("Test help when bitbucket is disabled", func(t *testing.T) { help := command.GetHelp() - assert.Equal(t, 1, len(help)) + assert.Len(t, help, 1) }) t.Run("Render template", func(t *testing.T) { tpl, err := util.CompileTemplate(`{{$pr := bitbucketPullRequest "myProject" "myRepo" "1337"}}PR: {{$pr.Name}} - {{$pr.BuildStatus}}`) - assert.Nil(t, err) + require.NoError(t, err) res, err := util.EvalTemplate(tpl, util.Parameters{}) - assert.Nil(t, err) + require.NoError(t, err) assert.Equal(t, fmt.Sprintf("PR: test - %d", buildStatusFailed), res) }) t.Run("Render template with not existing PR", func(t *testing.T) { tpl, err := util.CompileTemplate(`{{$pr := bitbucketPullRequest "myProject" "myRepo" "1338"}}{{$pr.Status}} - {{$pr.BuildStatus}}`) - assert.Nil(t, err) + require.NoError(t, err) res, err := util.EvalTemplate(tpl, util.Parameters{}) - assert.Nil(t, err) + require.NoError(t, err) assert.Equal(t, fmt.Sprintf("%d - %d", closedPr.Status, buildStatusUnknown), res) }) t.Run("Render template with not open PR", func(t *testing.T) { tpl, err := util.CompileTemplate(`{{$pr := bitbucketPullRequest "myProject" "myRepo" "1339"}}{{$pr.Name}}: {{$pr.Status}} - {{$pr.BuildStatus}}`) - assert.Nil(t, err) + require.NoError(t, err) res, err := util.EvalTemplate(tpl, util.Parameters{}) - assert.Nil(t, err) + require.NoError(t, err) assert.Equal(t, fmt.Sprintf("test: %d - %d", prStatusOpen, buildStatusSuccess), res) }) diff --git a/command/pullrequest/github_test.go b/command/pullrequest/github_test.go index 27b9a6ba..a2c603e8 100644 --- a/command/pullrequest/github_test.go +++ b/command/pullrequest/github_test.go @@ -15,6 +15,7 @@ import ( "github.com/innogames/slack-bot/v2/mocks" "github.com/slack-go/slack" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestGithub(t *testing.T) { @@ -31,7 +32,7 @@ func TestGithub(t *testing.T) { t.Run("help", func(t *testing.T) { help := commands.GetHelp() - assert.Equal(t, 1, len(help)) + assert.Len(t, help, 1) }) t.Run("invalid PR", func(t *testing.T) { @@ -77,16 +78,16 @@ func TestGithub(t *testing.T) { Approvers: []string{}, } - assert.Nil(t, err) + require.NoError(t, err) assert.Equal(t, expected, pr) }) t.Run("Render template ", func(t *testing.T) { tpl, err := util.CompileTemplate(`{{$pr := githubPullRequest "innogames" "slack-bot" "1"}}PR: "{{$pr.Name}}"`) - assert.Nil(t, err) + require.NoError(t, err) res, err := util.EvalTemplate(tpl, util.Parameters{}) - assert.Nil(t, err) + require.NoError(t, err) assert.Equal(t, `PR: "Add weather command"`, res) }) diff --git a/command/pullrequest/gitlab_test.go b/command/pullrequest/gitlab_test.go index de5a932a..a9f59fee 100644 --- a/command/pullrequest/gitlab_test.go +++ b/command/pullrequest/gitlab_test.go @@ -10,6 +10,7 @@ import ( "github.com/innogames/slack-bot/v2/bot/util" "github.com/innogames/slack-bot/v2/mocks" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "github.com/xanzy/go-gitlab" ) @@ -33,7 +34,7 @@ func TestGitlab(t *testing.T) { t.Run("help", func(t *testing.T) { help := commands.GetHelp() - assert.Equal(t, 1, len(help)) + assert.Len(t, help, 1) }) t.Run("get status", func(t *testing.T) { @@ -102,19 +103,19 @@ func TestGitlab(t *testing.T) { t.Run("Render template with gitlabPullRequest()", func(t *testing.T) { tpl, err := util.CompileTemplate(`{{ $pr := gitlabPullRequest "12" "13" }}{{$pr.Name}} - {{$pr.Approvers}}`) - assert.Nil(t, err) + require.NoError(t, err) res, err := util.EvalTemplate(tpl, util.Parameters{}) - assert.Nil(t, err) + require.NoError(t, err) assert.Equal(t, "Update XYZ - [foobar]", res) }) t.Run("Render template with gitlabPullRequest() with not existing PR", func(t *testing.T) { tpl, err := util.CompileTemplate(`{{ gitlabPullRequest "12" "14" }}`) - assert.Nil(t, err) + require.NoError(t, err) _, err = util.EvalTemplate(tpl, util.Parameters{}) - assert.NotNil(t, err) + require.Error(t, err) }) } diff --git a/command/queue/queue_test.go b/command/queue/queue_test.go index 17f183c9..be770fcc 100644 --- a/command/queue/queue_test.go +++ b/command/queue/queue_test.go @@ -12,6 +12,7 @@ import ( "github.com/innogames/slack-bot/v2/mocks" "github.com/slack-go/slack" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestQueue(t *testing.T) { @@ -66,10 +67,10 @@ func TestQueue(t *testing.T) { t.Run("Render template with not open PR", func(t *testing.T) { tpl, err := util.CompileTemplate(`{{$count1 := countBackgroundJobs}}{{$count2 := countBackgroundJobsInChannel "C1234"}}{{$count1}} - {{$count2}}`) - assert.Nil(t, err) + require.NoError(t, err) res, err := util.EvalTemplate(tpl, util.Parameters{}) - assert.Nil(t, err) + require.NoError(t, err) assert.Equal(t, "0 - 0", res) }) @@ -125,10 +126,10 @@ func TestQueue(t *testing.T) { t.Run("Render template with not open PR", func(t *testing.T) { tpl, err := util.CompileTemplate(`{{$count1 := countBackgroundJobs}}{{$count2 := countBackgroundJobsInChannel "C1234"}}{{$count3 := countBackgroundJobsInChannel "C4321"}}{{$count1}} - {{$count2}} - {{$count3}}`) - assert.Nil(t, err) + require.NoError(t, err) res, err := util.EvalTemplate(tpl, util.Parameters{}) - assert.Nil(t, err) + require.NoError(t, err) assert.Equal(t, "1 - 1 - 0", res) }) diff --git a/mocks/testing.go b/mocks/testing.go index 7ffb6659..63119ea6 100644 --- a/mocks/testing.go +++ b/mocks/testing.go @@ -18,6 +18,7 @@ import ( "github.com/slack-go/slack" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" ) // testLock is only used in integration test to avoid running some tests in parallel as we use one shared Message list @@ -70,7 +71,7 @@ func AssertQueuedMessage(t *testing.T, expected msg.Message) { t.Helper() actual := <-client.InternalMessages - assert.Equal(t, actual, expected) + assert.Equal(t, expected, actual) } // WaitForQueuedMessages waits until all "count" messages are queued and returns them in the returned function. @@ -139,7 +140,7 @@ func AssertSlackBlocks(t *testing.T, slackClient *SlackClient, message msg.Ref, slackClient.On("SendBlockMessage", message, mock.MatchedBy(func(givenBlocks []slack.Block) bool { givenJSON, err := json.Marshal(givenBlocks) - assert.Nil(t, err) + require.NoError(t, err) if expectedJSON != string(givenJSON) { fmt.Println(expectedJSON) @@ -157,9 +158,9 @@ func AssertContainsSlackBlocks(t *testing.T, slackClient *SlackClient, message m slackClient.On("SendBlockMessage", message, mock.MatchedBy(func(givenBlocks []slack.Block) bool { givenJSON, err := json.Marshal(givenBlocks) - assert.Nil(t, err) + require.NoError(t, err) expectedJSONBlock, err := json.Marshal(block) - assert.Nil(t, err) + require.NoError(t, err) if bytes.Contains(givenJSON, expectedJSONBlock) { // all good!