Skip to content

Commit

Permalink
ensure deterministic sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
choffmeister committed Nov 19, 2021
1 parent b3e973a commit d816292
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions internal/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ func (l versionParsedList) Less(i, j int) bool {
if cmp < 0 {
return false
}
cmp2 := strings.Compare(a.Version, b.Version)
if cmp2 > 0 {
return true
}
if cmp2 < 0 {
return false
}
}
return false
}
Expand Down
14 changes: 14 additions & 0 deletions internal/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,20 @@ func TestPolicyFilterAndSort(t *testing.T) {
assert.Error(t, err)
_, err = p2.FilterAndSort("1.0-ubuntu", strings.Split("17.10 v18.04-ubuntu v18.10-ubuntu v19.04-ubuntu v19.10-ubuntu v20.04-ubuntu v20.10-ubuntu v21.04-ubuntu v21.10-ubuntu v22.04-ubuntu", " "), "v", "-ubuntu")
assert.Error(t, err)

p3 := Policy{
Pattern: regexp.MustCompile(`^(?P<number>\d+)(?P<rest>.*)?$`),
Extracts: []Extract{
{
Value: "<number>",
Strategy: NumericExtractStrategy{},
},
},
}
actual, err = p3.FilterAndSort("1", strings.Split("1-ab 1 2-ab 2-b 2-a 2-ab 2 1-a 1-b 1-ab", " "), "", "")
if assert.NoError(t, err) {
assert.Equal(t, strings.Split("2-b 2-ab 2-ab 2-a 2 1-b 1-ab 1-ab 1-a 1", " "), *actual)
}
}

func TestPolicyFindNext(t *testing.T) {
Expand Down

0 comments on commit d816292

Please sign in to comment.