Skip to content

Commit

Permalink
require go 1.21 + updated linters
Browse files Browse the repository at this point in the history
  • Loading branch information
brainexe committed May 16, 2024
1 parent f3b8ee3 commit d250282
Show file tree
Hide file tree
Showing 32 changed files with 150 additions and 122 deletions.
3 changes: 2 additions & 1 deletion bot/bot_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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{})
Expand Down
13 changes: 7 additions & 6 deletions bot/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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)
}

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

Expand All @@ -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)
Expand Down
9 changes: 5 additions & 4 deletions bot/interaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
})

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

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

Expand Down Expand Up @@ -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))
}
13 changes: 7 additions & 6 deletions bot/stats/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
4 changes: 2 additions & 2 deletions bot/storage/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import (
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestChainStorage(t *testing.T) {
dir := "./test_chain_storage"
defer os.RemoveAll(dir)

fileStorage, err := newFileStorage(dir)
assert.Nil(t, err)
require.NoError(t, err)

storage := NewChainStorage(fileStorage, newMemoryStorage())

Expand Down
4 changes: 2 additions & 2 deletions bot/storage/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import (
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestFileStorage(t *testing.T) {
dir := "./test_storage"
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)
Expand Down
51 changes: 26 additions & 25 deletions bot/storage/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func testStorage(t *testing.T, storage Storage) {
Expand All @@ -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
Expand All @@ -57,39 +58,39 @@ 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)
assert.Equal(t, 2, expectedInt)

// 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("../")
Expand All @@ -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")
Expand All @@ -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())
})

Expand Down
7 changes: 4 additions & 3 deletions bot/util/duration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var parserTestCases = []struct {
Expand All @@ -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())
Expand Down
3 changes: 2 additions & 1 deletion bot/util/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"text/template"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestFunctions(t *testing.T) {
Expand All @@ -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")
Expand Down
Loading

0 comments on commit d250282

Please sign in to comment.