diff --git a/server/forge/gitea/gitea_test.go b/server/forge/gitea/gitea_test.go index 66d73da50..44a18319b 100644 --- a/server/forge/gitea/gitea_test.go +++ b/server/forge/gitea/gitea_test.go @@ -161,7 +161,7 @@ func Test_gitea(t *testing.T) { g.Assert(b).IsNotNil() g.Assert(err).IsNil() g.Assert(b.Event).Equal(model.EventPull) - g.Assert(utils.EqualStringSlice(b.ChangedFiles, []string{"README.md"})).IsTrue() + g.Assert(utils.EqualSliceValues(b.ChangedFiles, []string{"README.md"})).IsTrue() }) }) } diff --git a/server/forge/gitea/helper_test.go b/server/forge/gitea/helper_test.go index 0add63874..a554d660f 100644 --- a/server/forge/gitea/helper_test.go +++ b/server/forge/gitea/helper_test.go @@ -104,7 +104,7 @@ func Test_parse(t *testing.T) { g.Assert(pipeline.Message).Equal(hook.Commits[0].Message) g.Assert(pipeline.Avatar).Equal("http://1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87") g.Assert(pipeline.Author).Equal(hook.Sender.UserName) - g.Assert(utils.EqualStringSlice(pipeline.ChangedFiles, []string{"CHANGELOG.md", "app/controller/application.rb"})).IsTrue() + g.Assert(utils.EqualSliceValues(pipeline.ChangedFiles, []string{"CHANGELOG.md", "app/controller/application.rb"})).IsTrue() }) g.It("Should return a Repo struct from a push hook", func() { diff --git a/server/forge/gitea/parse_test.go b/server/forge/gitea/parse_test.go index 23f0c95aa..b1e468667 100644 --- a/server/forge/gitea/parse_test.go +++ b/server/forge/gitea/parse_test.go @@ -64,7 +64,7 @@ func Test_parser(t *testing.T) { g.Assert(r).IsNotNil() g.Assert(b).IsNotNil() g.Assert(b.Event).Equal(model.EventPush) - g.Assert(utils.EqualStringSlice(b.ChangedFiles, []string{"CHANGELOG.md", "app/controller/application.rb"})).IsTrue() + g.Assert(utils.EqualSliceValues(b.ChangedFiles, []string{"CHANGELOG.md", "app/controller/application.rb"})).IsTrue() }) }) g.Describe("given a push hook from an branch creation", func() { diff --git a/shared/utils/slices.go b/shared/utils/slices.go index 0546b5674..5aa6a832d 100644 --- a/shared/utils/slices.go +++ b/shared/utils/slices.go @@ -31,3 +31,29 @@ func MergeSlices[T any](slices ...[]T) []T { } return result } + +// EqualSliceValues compare two slices if they have equal values independent of how they are sorted +func EqualSliceValues[E comparable](s1, s2 []E) bool { + if len(s1) != len(s2) { + return false + } + + m1 := sliceToCountMap(s1) + m2 := sliceToCountMap(s2) + + for k, v := range m1 { + if m2[k] != v { + return false + } + } + + return true +} + +func sliceToCountMap[E comparable](list []E) map[E]int { + m := make(map[E]int) + for i := range list { + m[list[i]]++ + } + return m +} diff --git a/shared/utils/slices_test.go b/shared/utils/slices_test.go index 80e3feb57..291b34964 100644 --- a/shared/utils/slices_test.go +++ b/shared/utils/slices_test.go @@ -27,3 +27,34 @@ func TestMergeSlices(t *testing.T) { resultIS := MergeSlices([]int{}, []int{1, 2}, []int{4}, nil) assert.EqualValues(t, []int{1, 2, 4}, resultIS) } + +func TestEqualSliceValues(t *testing.T) { + tests := []struct { + in1 []string + in2 []string + out bool + }{{ + in1: []string{"", "ab", "12", "ab"}, + in2: []string{"12", "ab"}, + out: false, + }, { + in1: nil, + in2: nil, + out: true, + }, { + in1: []string{"AA", "AA", "2", " "}, + in2: []string{"2", "AA", " ", "AA"}, + out: true, + }, { + in1: []string{"AA", "AA", "2", " "}, + in2: []string{"2", "2", " ", "AA"}, + out: false, + }} + + for _, tc := range tests { + assert.EqualValues(t, tc.out, EqualSliceValues(tc.in1, tc.in2), "could not correctly process input: '%#v', %#v", tc.in1, tc.in2) + } + + assert.True(t, EqualSliceValues([]bool{true, false, false}, []bool{false, false, true})) + assert.False(t, EqualSliceValues([]bool{true, false, false}, []bool{true, false, true})) +} diff --git a/shared/utils/strings.go b/shared/utils/strings.go index 4534e6d60..6add8386e 100644 --- a/shared/utils/strings.go +++ b/shared/utils/strings.go @@ -34,29 +34,3 @@ func DedupStrings(src []string) []string { return dst } - -// EqualStringSlice compare two string slices if they have equal values independent of how they are sorted -func EqualStringSlice(l1, l2 []string) bool { - if len(l1) != len(l2) { - return false - } - - m1 := sliceToCountMap(l1) - m2 := sliceToCountMap(l2) - - for k, v := range m1 { - if m2[k] != v { - return false - } - } - - return true -} - -func sliceToCountMap(list []string) map[string]int { - m := make(map[string]int) - for i := range list { - m[list[i]]++ - } - return m -} diff --git a/shared/utils/strings_test.go b/shared/utils/strings_test.go index f59c6e9f2..393e56cd8 100644 --- a/shared/utils/strings_test.go +++ b/shared/utils/strings_test.go @@ -46,31 +46,3 @@ func TestDedupStrings(t *testing.T) { } } } - -func TestEqualStringSlice(t *testing.T) { - tests := []struct { - in1 []string - in2 []string - out bool - }{{ - in1: []string{"", "ab", "12", "ab"}, - in2: []string{"12", "ab"}, - out: false, - }, { - in1: nil, - in2: nil, - out: true, - }, { - in1: []string{"AA", "AA", "2", " "}, - in2: []string{"2", "AA", " ", "AA"}, - out: true, - }, { - in1: []string{"AA", "AA", "2", " "}, - in2: []string{"2", "2", " ", "AA"}, - out: false, - }} - - for _, tc := range tests { - assert.EqualValues(t, tc.out, EqualStringSlice(tc.in1, tc.in2), "could not correctly process input: '%#v', %#v", tc.in1, tc.in2) - } -}