Skip to content

Commit

Permalink
Prefer matching versions
Browse files Browse the repository at this point in the history
  • Loading branch information
choffmeister committed Mar 6, 2024
1 parent 2ff904d commit 368340c
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 4 deletions.
5 changes: 4 additions & 1 deletion internal/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,11 @@ func (p Policy) FindNext(currentVersion string, availableVersions []string, pref
}
if len(allFilteredSortedVersions) > 0 {
nextVersion := (allFilteredSortedVersions)[0]
if !SliceExists(allFilteredSortedVersions, func(v string) bool { return v == currentVersion }) {
return &nextVersion, nil
}
if p.Compare(currentVersion, nextVersion, prefix, suffix) < 0 {
return &allFilteredSortedVersions[0], nil
return &nextVersion, nil
}
}
return &currentVersion, nil
Expand Down
69 changes: 66 additions & 3 deletions internal/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,13 @@ func TestPolicyFilterAndSort(t *testing.T) {
},
}

actual, err = p5.FilterAndSort("1.0.0", strings.Split("2.0.0+a 2.0.0+b 2.0.0+c", " "), "", "", map[string]interface{}{
actual, err = p5.FilterAndSort("1.0.0", strings.Split("1.0.0 2.0.0+a 2.0.0+b 2.0.0+c", " "), "", "", map[string]interface{}{
"key.build": "b",
})
if assert.NoError(t, err) {
assert.Equal(t, strings.Split("2.0.0+b", " "), actual)
}
actual, err = p5.FilterAndSort("1.0.0", strings.Split("2.0.0+a 2.0.0+b 2.0.0+c", " "), "", "", map[string]interface{}{
actual, err = p5.FilterAndSort("1.0.0", strings.Split("1.0.0 2.0.0+a 2.0.0+b 2.0.0+c", " "), "", "", map[string]interface{}{
"key.build": []interface{}{"a", "c"},
})
if assert.NoError(t, err) {
Expand Down Expand Up @@ -334,13 +334,76 @@ func TestPolicyFindNext(t *testing.T) {
Pattern: regexp.MustCompile(`^(?P<all>.*)$`),
Extracts: []Extract{
{
Key: "version",
Value: "<all>",
Strategy: SemverExtractStrategy{},
},
},
}
actual, err = p4.FindNext("0.10.0", []string{"0.10.0", "0.10.1", "0.10.2", "0.10.3", "0.10.4-pre", "0.11.0-pre"}, "", "", nil)
if assert.NoError(t, err) {
assert.Equal(t, "0.10.3", *actual)
}
actual, err = p4.FindNext("0.10.4-pre", []string{"0.10.0", "0.10.1", "0.10.2", "0.10.3", "0.10.4-pre", "0.11.0-pre"}, "", "", nil)
if assert.NoError(t, err) {
assert.Equal(t, "0.10.3", *actual)
}
actual, err = p4.FindNext("0.11.0-pre", []string{"0.10.0", "0.10.1", "0.10.2", "0.10.3", "0.10.4-pre", "0.11.0-pre"}, "", "", nil)
if assert.NoError(t, err) {
assert.Equal(t, "0.10.3", *actual)
}
actual, err = p4.FindNext("0.12.0-pre", []string{"0.10.0", "0.10.1", "0.10.2", "0.10.3", "0.10.4-pre", "0.11.0-pre"}, "", "", nil)
if assert.NoError(t, err) {
assert.Equal(t, "0.10.3", *actual)
}
actual, err = p4.FindNext("0.12.0", []string{"0.10.0", "0.10.1", "0.10.2", "0.10.3", "0.10.4-pre", "0.11.0-pre"}, "", "", nil)
if assert.NoError(t, err) {
assert.Equal(t, "0.12.0", *actual)
}
actual, err = p4.FindNext("0.12.0", []string{"0.10.0", "0.10.1", "0.10.2", "0.10.3", "0.10.4-pre", "0.11.0-pre"}, "", "", map[string]interface{}{
"version.major": "0",
"version.minor": "10",
})
if assert.NoError(t, err) {
assert.Equal(t, "0.10.3", *actual)
}

actual, err = p4.FindNext("0.10.4-pre", []string{"0.10.0", "0.10.1", "0.10.2", "0.10.3", "0.11.0-pre"}, "", "", nil)
p5 := Policy{
Pattern: regexp.MustCompile(`^(?P<all>.*)$`),
Extracts: []Extract{
{
Key: "version",
Value: "<all>",
Strategy: SemverExtractStrategy{
AllowPrereleases: true,
},
},
},
}
actual, err = p5.FindNext("0.10.0", []string{"0.10.0", "0.10.1", "0.10.2", "0.10.3", "0.10.4-pre", "0.11.0-pre"}, "", "", nil)
if assert.NoError(t, err) {
assert.Equal(t, "0.11.0-pre", *actual)
}
actual, err = p5.FindNext("0.10.4-pre", []string{"0.10.0", "0.10.1", "0.10.2", "0.10.3", "0.10.4-pre", "0.11.0-pre"}, "", "", nil)
if assert.NoError(t, err) {
assert.Equal(t, "0.11.0-pre", *actual)
}
actual, err = p5.FindNext("0.11.0-pre", []string{"0.10.0", "0.10.1", "0.10.2", "0.10.3", "0.10.4-pre", "0.11.0-pre"}, "", "", nil)
if assert.NoError(t, err) {
assert.Equal(t, "0.11.0-pre", *actual)
}
actual, err = p5.FindNext("0.12.0-pre", []string{"0.10.0", "0.10.1", "0.10.2", "0.10.3", "0.10.4-pre", "0.11.0-pre"}, "", "", nil)
if assert.NoError(t, err) {
assert.Equal(t, "0.12.0-pre", *actual)
}
actual, err = p5.FindNext("0.12.0", []string{"0.10.0", "0.10.1", "0.10.2", "0.10.3", "0.10.4-pre", "0.11.0-pre"}, "", "", nil)
if assert.NoError(t, err) {
assert.Equal(t, "0.12.0", *actual)
}
actual, err = p5.FindNext("0.12.0", []string{"0.10.0", "0.10.1", "0.10.2", "0.10.3", "0.10.4-pre", "0.11.0-pre"}, "", "", map[string]interface{}{
"version.major": "0",
"version.minor": "10",
})
if assert.NoError(t, err) {
assert.Equal(t, "0.10.4-pre", *actual)
}
Expand Down
9 changes: 9 additions & 0 deletions internal/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ func trimRightMultilineString(str string, cutset string) string {
return strings.Join(lines, "\n")
}

func SliceExists[E any](slice []E, existFn func(e E) bool) bool {
for _, e := range slice {
if existFn(e) {
return true
}
}
return false
}

func SliceMap[E any, E2 any](slice []E, mapFn func(e E) E2) []E2 {
result := []E2{}
for _, e := range slice {
Expand Down

0 comments on commit 368340c

Please sign in to comment.