From c62204dca6067a96c5c115f40ddd895651b57969 Mon Sep 17 00:00:00 2001 From: Nikita Pivkin Date: Thu, 30 May 2024 13:26:15 +0700 Subject: [PATCH] fix: not modify the original version --- pkg/version/version.go | 18 +++++++++ pkg/version/version_collection_test.go | 54 ++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/pkg/version/version.go b/pkg/version/version.go index 62c7836..5ffb998 100644 --- a/pkg/version/version.go +++ b/pkg/version/version.go @@ -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 @@ -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 @@ -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 { @@ -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, + } +} diff --git a/pkg/version/version_collection_test.go b/pkg/version/version_collection_test.go index d4983b0..6b372e3 100644 --- a/pkg/version/version_collection_test.go +++ b/pkg/version/version_collection_test.go @@ -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) + }) + } }