Skip to content

Commit

Permalink
lint (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc authored Dec 26, 2024
1 parent 61b346d commit 51fc79e
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 65 deletions.
15 changes: 12 additions & 3 deletions coalesce/coalesce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import (
)

func TestString(t *testing.T) {
tests := []struct{have []string; want string}{
tests := []struct {
have []string
want string
}{
{[]string{}, ""},
{[]string{"", "", ""}, ""},
{[]string{"", "", "c"}, "c"},
Expand All @@ -27,7 +30,10 @@ func TestString(t *testing.T) {
}

func TestInt(t *testing.T) {
tests := []struct{have []int; want int}{
tests := []struct {
have []int
want int
}{
{[]int{}, 0},
{[]int{0, 0, 0}, 0},
{[]int{0, 0, 3}, 3},
Expand All @@ -43,7 +49,10 @@ func TestInt(t *testing.T) {
}

func TestNotNil(t *testing.T) {
tests := []struct{have []*int; want *int}{
tests := []struct {
have []*int
want *int
}{
{[]*int{}, nil},
{[]*int{nil, nil, nil}, nil},
{[]*int{nil, nil, ptr.Of(3)}, ptr.Of(3)},
Expand Down
3 changes: 1 addition & 2 deletions cstest/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import (
"strings"
"testing"

logtest "github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

logtest "github.com/sirupsen/logrus/hooks/test"
)

// The following functions are used to test for errors and log messages.
Expand Down
8 changes: 4 additions & 4 deletions cstime/cstime.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ func ParseDuration(s string) (time.Duration, error) {
if len(parts) != 2 {
return 0, fmt.Errorf("invalid duration format '%s'", s)
}

days, err := strconv.Atoi(parts[0])
if err != nil {
return 0, err
}

daysDuration = time.Hour * time.Duration(24 * days)
daysDuration = time.Hour * time.Duration(24*days)

s = parts[1]

Expand All @@ -36,8 +36,8 @@ func ParseDuration(s string) (time.Duration, error) {
if err != nil {
return 0, err
}
if (daysDuration < 0) {

if daysDuration < 0 {
return daysDuration - d, nil
}

Expand Down
80 changes: 40 additions & 40 deletions cstime/cstime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,64 +9,64 @@ import (

func TestParseDuration(t *testing.T) {
tests := []struct {
name string
input string
want time.Duration
wantErr bool
name string
input string
want time.Duration
wantErr bool
}{
{
name: "only days",
input: "2d",
want: 48 * time.Hour,
wantErr: false,
name: "only days",
input: "2d",
want: 48 * time.Hour,
wantErr: false,
},
{
name: "only days, negative",
input: "-2d",
want: -48 * time.Hour,
wantErr: false,
name: "only days, negative",
input: "-2d",
want: -48 * time.Hour,
wantErr: false,
},
{
name: "days and hours",
input: "1d2h",
want: 26 * time.Hour,
wantErr: false,
name: "days and hours",
input: "1d2h",
want: 26 * time.Hour,
wantErr: false,
},
{
name: "days and hours, negative",
input: "-1d2h",
want: -26 * time.Hour,
wantErr: false,
name: "days and hours, negative",
input: "-1d2h",
want: -26 * time.Hour,
wantErr: false,
},
{
name: "hours minutes seconds",
input: "2h45m30s",
want: 2*time.Hour + 45*time.Minute + 30*time.Second,
wantErr: false,
name: "hours minutes seconds",
input: "2h45m30s",
want: 2*time.Hour + 45*time.Minute + 30*time.Second,
wantErr: false,
},
{
name: "invalid format",
input: "2x",
want: 0,
wantErr: true,
name: "invalid format",
input: "2x",
want: 0,
wantErr: true,
},
{
name: "invalid days format",
input: "2x3h",
want: 0,
wantErr: true,
name: "invalid days format",
input: "2x3h",
want: 0,
wantErr: true,
},
{
name: "invalid remainder format",
input: "2d3x",
want: 0,
wantErr: true,
name: "invalid remainder format",
input: "2d3x",
want: 0,
wantErr: true,
},
{
name: "empty input",
input: "",
want: 0,
wantErr: true,
name: "empty input",
input: "",
want: 0,
wantErr: true,
},
}

Expand Down
4 changes: 2 additions & 2 deletions downloader/download_compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"path/filepath"
"testing"

"github.com/crowdsecurity/go-cs-lib/downloader"

"github.com/stretchr/testify/require"

"github.com/crowdsecurity/go-cs-lib/downloader"
)

// Download to a temporary location, and compare the downloaded
Expand Down
4 changes: 2 additions & 2 deletions downloader/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"path/filepath"
"testing"

"github.com/crowdsecurity/go-cs-lib/downloader"

"github.com/stretchr/testify/require"

"github.com/crowdsecurity/go-cs-lib/downloader"
)

// simplest case: just download a file every time
Expand Down
1 change: 1 addition & 0 deletions maptools/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func SortedKeysNoCase[V any](m map[string]V) []string {
// will have uppercase first
return keys[i] < keys[j]
}

return li < lj
})

Expand Down
2 changes: 1 addition & 1 deletion slicetools/backward.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package slicetools
// Backward iterates over a slice in reverse order.
func Backward[E any](s []E) func(func(int, E) bool) {
return func(yield func(int, E) bool) {
for i := len(s)-1; i >= 0; i-- {
for i := len(s) - 1; i >= 0; i-- {
if !yield(i, s[i]) {
return
}
Expand Down
1 change: 0 additions & 1 deletion slicetools/backward_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/stretchr/testify/assert"
)


func TestBackwardInts(t *testing.T) {
ints := []int{10, 20, 30, 40, 50}
expected := []int{50, 40, 30, 20, 10}
Expand Down
16 changes: 8 additions & 8 deletions slicetools/chunk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ func TestChunks(t *testing.T) {
chunkSize int
expected [][]int
}{
{"empty slice, chunk size 2", []int{}, 2, [][]int{}},
{"1 element, chunk size 2", []int{1}, 2, [][]int{{1}}},
{"empty slice, chunk size 0", []int{}, 0, [][]int{}},
{"5 elements, chunk size 2", []int{1, 2, 3, 4, 5}, 2, [][]int{{1, 2}, {3, 4}, {5}}},
{"5 elements, chunk size 3", []int{1, 2, 3, 4, 5}, 3, [][]int{{1, 2, 3}, {4, 5}}},
{"5 elements, chunk size 4", []int{1, 2, 3, 4, 5}, 5, [][]int{{1, 2, 3, 4, 5}}},
{"5 elements, chunk size 6", []int{1, 2, 3, 4, 5}, 6, [][]int{{1, 2, 3, 4, 5}}},
{"empty slice, chunk size 2", []int{}, 2, [][]int{}},
{"1 element, chunk size 2", []int{1}, 2, [][]int{{1}}},
{"empty slice, chunk size 0", []int{}, 0, [][]int{}},
{"5 elements, chunk size 2", []int{1, 2, 3, 4, 5}, 2, [][]int{{1, 2}, {3, 4}, {5}}},
{"5 elements, chunk size 3", []int{1, 2, 3, 4, 5}, 3, [][]int{{1, 2, 3}, {4, 5}}},
{"5 elements, chunk size 4", []int{1, 2, 3, 4, 5}, 5, [][]int{{1, 2, 3, 4, 5}}},
{"5 elements, chunk size 6", []int{1, 2, 3, 4, 5}, 6, [][]int{{1, 2, 3, 4, 5}}},
{"chunk size 0 = don't chunk", []int{1, 2, 3, 4, 5}, 0, [][]int{{1, 2, 3, 4, 5}}},
{"look ma, no sorting", []int{1, 2, 4, 1, 5}, 2, [][]int{{1, 2}, {4, 1}, {5}}},
{"look ma, no sorting", []int{1, 2, 4, 1, 5}, 2, [][]int{{1, 2}, {4, 1}, {5}}},
}

for _, tc := range testCases {
Expand Down
2 changes: 1 addition & 1 deletion slicetools/concat.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package slicetools

// Concat concatenates multiple slices and returns the result.
func Concat[T any](slices...[]T) []T {
func Concat[T any](slices ...[]T) []T {
tot := 0
for _, s := range slices {
tot += len(s)
Expand Down
3 changes: 2 additions & 1 deletion yamlpatch/patcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
"path/filepath"
"testing"

"github.com/crowdsecurity/go-cs-lib/yamlpatch"
"github.com/stretchr/testify/require"

"github.com/crowdsecurity/go-cs-lib/yamlpatch"
)

// similar to the one in cstest, but with test number too. We cannot import
Expand Down

0 comments on commit 51fc79e

Please sign in to comment.