Skip to content

Commit

Permalink
Merge pull request #1656 from grafana/fix/sort-plugins
Browse files Browse the repository at this point in the history
fix: sort plugin lists
  • Loading branch information
theSuess authored Sep 2, 2024
2 parents 02929fb + 877ca4f commit ed3b87c
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
4 changes: 3 additions & 1 deletion api/v1beta1/plugin_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/sha256"
"fmt"
"io"
"sort"
"strings"

"github.com/blang/semver"
Expand All @@ -30,10 +31,11 @@ func (l PluginList) Hash() string {
}

func (l PluginList) String() string {
plugins := make([]string, 0, len(l))
plugins := make(sort.StringSlice, 0, len(l))
for _, plugin := range l {
plugins = append(plugins, fmt.Sprintf("%s %s", plugin.Name, plugin.Version))
}
sort.Sort(plugins)
return strings.Join(plugins, ",")
}

Expand Down
65 changes: 65 additions & 0 deletions api/v1beta1/plugin_list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package v1beta1

import (
"strings"
"testing"
"testing/quick"

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

func TestPluginString(t *testing.T) {
err := quick.Check(func(a string, b string, c string) bool {
if strings.Contains(a, ",") || strings.Contains(b, ",") || strings.Contains(c, ",") {
return true // skip plugins with ,
}
pl := PluginList{
{
Name: a,
Version: "7.2",
},
{
Name: b,
Version: "2.2",
},
{
Name: c,
Version: "6.7",
},
}
out := pl.String()
split := strings.Split(out, ",")
if len(split) != 3 {
return false
}
if split[0] > split[1] {
return false
}
if split[1] > split[2] {
return false
}
return true
}, nil)
if err != nil {
t.Errorf("plugin list was not sorted: %s", err.Error())
}
}

func TestPluginSanitize(t *testing.T) {
pl := PluginList{
{
Name: "plugin-a",
Version: "1.0.0",
},
{
Name: "plugin-b",
Version: "2.0.0",
},
{
Name: "plugin-a",
Version: "3.0.0",
},
}
sanitized := pl.Sanitize()
assert.Len(t, sanitized, 2)
}

0 comments on commit ed3b87c

Please sign in to comment.