Skip to content

Commit

Permalink
fix: not modify the original version (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikpivkin authored May 30, 2024
1 parent 18dee88 commit 9f59392
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
18 changes: 18 additions & 0 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ func (v Version) Original() string {
// It works like Gem::Version.bump()
// https://docs.ruby-lang.org/en/2.6.0/Gem/Version.html#method-i-bump
func (v Version) PessimisticBump() Version {
v = v.copy()

size := len(v.segments)
if size == 1 {
v.segments[0] += 1
Expand All @@ -161,6 +163,8 @@ func (v Version) PessimisticBump() Version {
// TildeBump returns the maximum version of "~"
// https://docs.npmjs.com/cli/v6/using-npm/semver#tilde-ranges-123-12-1
func (v Version) TildeBump() Version {
v = v.copy()

if len(v.segments) == 2 {
v.segments[1] += 1
return v
Expand All @@ -172,6 +176,8 @@ func (v Version) TildeBump() Version {
// CaretBump returns the maximum version of "^"
// https://docs.npmjs.com/cli/v6/using-npm/semver#caret-ranges-123-025-004
func (v Version) CaretBump() Version {
v = v.copy()

found := -1
for i, s := range v.segments {
if s != 0 {
Expand All @@ -197,3 +203,15 @@ func (v Version) CaretBump() Version {

return v
}

func (v Version) copy() Version {
segments := make([]part.Uint64, len(v.segments))
copy(segments, v.segments)

return Version{
segments: segments,
preRelease: v.preRelease,
buildMetadata: v.buildMetadata,
original: v.original,
}
}
54 changes: 54 additions & 0 deletions pkg/version/version_collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,59 @@ func TestCollection(t *testing.T) {
assert.Equal(t, tt.want, got)
})
}
}

func TestCollection_Constraints(t *testing.T) {
tests := []struct {
name string
versions []string
constraints string
want string
}{
{
name: "pessimistic constraints",
versions: []string{"3.0.0", "3.1.0", "3.1.1", "3.1.2", "3.1.3", "3.2.0", "4.0.0"},
constraints: "~> 3.0",
want: "3.2.0",
},
{
name: "caret constraints",
versions: []string{"3.0.0", "3.1.0", "3.1.1", "3.1.2", "3.1.3", "3.2.0", "4.0.0"},
constraints: "^3.1",
want: "3.2.0",
},
{
name: "tilde constraints",
versions: []string{"3.0.0", "3.1.0", "3.1.1", "3.1.2", "3.1.3", "3.2.0", "4.0.0"},
constraints: "~ 3.1",
want: "3.1.3",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
constraints, err := NewConstraints(tt.constraints)
require.NoError(t, err)

expected, err := Parse(tt.want)
require.NoError(t, err)

versions := make([]Version, len(tt.versions))
for i, raw := range tt.versions {
v, err := Parse(raw)
require.NoError(t, err)
versions[i] = v
}

sort.Sort(sort.Reverse(Collection(versions)))

for _, ver := range versions {
if constraints.Check(ver) {
assert.Equal(t, expected, ver)
return
}
}

t.Fatalf("failed to satisfy %s", tt.constraints)
})
}
}

0 comments on commit 9f59392

Please sign in to comment.