Skip to content

Commit

Permalink
Added CompatibleWith method to RelaxedVersion too
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaglie committed May 28, 2022
1 parent 2a4c8da commit 4b8c309
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ This library allows the use of an even more relaxed semver specification using t
- If the parsed string is a valid semver (following the rules above), then the `RelaxedVersion` will behave exactly as a normal `Version` object
- if the parsed string is **not** a valid semver, then the string is kept as-is inside the `RelaxedVersion` object as a custom version string
- when comparing two `RelaxedVersion` the rule is simple: if both are valid semver, the semver rules applies; if both are custom version string they are compared as alphanumeric strings; if one is valid semver and the other is a custom version string the valid semver is always greater
- two `RelaxedVersion` are compatible (by the `CompatibleWith` operation) only if
- they are equal
- they are both valid semver and they are compatible as per semver specification

The `RelaxedVersion` object is basically made to allow systems that do not use semver to soft transition to semantic versioning, because it allows an intermediate period where the invalid version is still tolerated.

Expand Down
8 changes: 8 additions & 0 deletions relaxed_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,11 @@ func (v *RelaxedVersion) GreaterThan(u *RelaxedVersion) bool {
func (v *RelaxedVersion) GreaterThanOrEqual(u *RelaxedVersion) bool {
return v.CompareTo(u) >= 0
}

// CompatibleWith returns true if the RelaxedVersion is compatible with the RelaxedVersion passed as paramater
func (v *RelaxedVersion) CompatibleWith(u *RelaxedVersion) bool {
if v.version != nil && u.version != nil {
return v.version.CompatibleWith(u.version)
}
return v.Equal(u)
}
33 changes: 33 additions & 0 deletions relaxed_version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,39 @@ func TestRelaxedVersionComparator(t *testing.T) {
)
}

func TestRelaxedCompatibleWith(t *testing.T) {
inv := ParseRelaxed("invalid-semver")
inv2 := ParseRelaxed("invalid-semver-2")
v145 := ParseRelaxed("1.4.5")
v152 := ParseRelaxed("1.5.2")
v213 := ParseRelaxed("2.1.3")
require.True(t, inv.CompatibleWith(inv))
require.False(t, inv.CompatibleWith(inv2))
require.False(t, inv.CompatibleWith(v145))
require.False(t, inv.CompatibleWith(v152))
require.False(t, inv.CompatibleWith(v213))
require.False(t, inv2.CompatibleWith(inv))
require.True(t, inv2.CompatibleWith(inv2))
require.False(t, inv2.CompatibleWith(v145))
require.False(t, inv2.CompatibleWith(v152))
require.False(t, inv2.CompatibleWith(v213))
require.False(t, v145.CompatibleWith(inv))
require.False(t, v145.CompatibleWith(inv2))
require.True(t, v145.CompatibleWith(v145))
require.True(t, v145.CompatibleWith(v152))
require.False(t, v145.CompatibleWith(v213))
require.False(t, v152.CompatibleWith(inv))
require.False(t, v152.CompatibleWith(inv2))
require.False(t, v152.CompatibleWith(v145))
require.True(t, v152.CompatibleWith(v152))
require.False(t, v152.CompatibleWith(v213))
require.False(t, v213.CompatibleWith(inv))
require.False(t, v213.CompatibleWith(inv2))
require.False(t, v213.CompatibleWith(v145))
require.False(t, v213.CompatibleWith(v152))
require.True(t, v213.CompatibleWith(v213))
}

func TestNilRelaxedVersionString(t *testing.T) {
var nilVersion *RelaxedVersion
require.Equal(t, "", nilVersion.String())
Expand Down

0 comments on commit 4b8c309

Please sign in to comment.