Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop testify dependency #15

Merged
merged 3 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A go-language package for parsing, comparing, sorting and constraint-checking [k

K0s versioning follows [semver](https://semver.org/) v2.0 with the exception that there is a special metadata field for the k0s build version like `v1.23.4+k0s.1` which affects precedence while sorting or comparing version numbers.

The library should work fine for performing the same operations on non-k0s version numbers as long as there are maximum of 3 numeric segments (1.2.3), but this is not a priority.
The library should work fine for performing the same operations on non-k0s version numbers as long as there are maximum of 3 numeric segments (1.2.3), but this is not a priority. There are no dependencies.

## Usage

Expand Down
57 changes: 27 additions & 30 deletions collection_test.go
Original file line number Diff line number Diff line change
@@ -1,70 +1,67 @@
package version
package version_test

import (
"encoding/json"
"sort"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/k0sproject/version"
)

func TestNewCollection(t *testing.T) {
c, err := NewCollection("1.23.3+k0s.1", "1.23.4+k0s.1")
require.NoError(t, err)
assert.Equal(t, "v1.23.3+k0s.1", c[0].String())
assert.Equal(t, "v1.23.4+k0s.1", c[1].String())
assert.Len(t, c, 2)
_, err = NewCollection("1.23.3+k0s.1", "1.23.b+k0s.1")
assert.Error(t, err)
c, err := version.NewCollection("1.23.3+k0s.1", "1.23.4+k0s.1")
NoError(t, err)
Equal(t, "v1.23.3+k0s.1", c[0].String())
Equal(t, "v1.23.4+k0s.1", c[1].String())
Equal(t, len(c), 2)
_, err = version.NewCollection("1.23.3+k0s.1", "1.23.b+k0s.1")
Error(t, err)
}

func TestSorting(t *testing.T) {
c, err := NewCollection(
"1.22.3+k0s.0",
c, err := version.NewCollection(
"1.21.2+k0s.0",
"1.21.2-beta.1+k0s.0",
"1.21.1+k0s.1",
"0.13.1",
"v1.21.1+k0s.2",
)
require.NoError(t, err)
NoError(t, err)
sort.Sort(c)
assert.Equal(t, "v0.13.1", c[0].String())
assert.Equal(t, "v1.21.1+k0s.1", c[1].String())
assert.Equal(t, "v1.21.1+k0s.2", c[2].String())
assert.Equal(t, "v1.21.2-beta.1+k0s.0", c[3].String())
assert.Equal(t, "v1.21.2+k0s.0", c[4].String())
assert.Equal(t, "v1.22.3+k0s.0", c[5].String())
Equal(t, "v0.13.1", c[0].String())
Equal(t, "v1.21.1+k0s.1", c[1].String())
Equal(t, "v1.21.1+k0s.2", c[2].String())
Equal(t, "v1.21.2-beta.1+k0s.0", c[3].String())
Equal(t, "v1.21.2+k0s.0", c[4].String())
}

func TestCollectionMarshalling(t *testing.T) {
c, err := NewCollection("v1.0.0+k0s.0", "v1.0.1+k0s.0")
require.NoError(t, err)
c, err := version.NewCollection("v1.0.0+k0s.0", "v1.0.1+k0s.0")
NoError(t, err)

t.Run("JSON", func(t *testing.T) {
jsonData, err := json.Marshal(c)
require.NoError(t, err)
assert.Equal(t, `["v1.0.0+k0s.0","v1.0.1+k0s.0"]`, string(jsonData))
NoError(t, err)
Equal(t, `["v1.0.0+k0s.0","v1.0.1+k0s.0"]`, string(jsonData))
})
}

func TestCollectionUnmarshalling(t *testing.T) {
t.Run("JSON", func(t *testing.T) {
var c Collection
var c version.Collection
err := json.Unmarshal([]byte(`["v1.0.0+k0s.1","v1.0.1+k0s.1"]`), &c)
require.NoError(t, err)
assert.Equal(t, "v1.0.0+k0s.1", c[0].String())
assert.Equal(t, "v1.0.1+k0s.1", c[1].String())
NoError(t, err)
Equal(t, "v1.0.0+k0s.1", c[0].String())
Equal(t, "v1.0.1+k0s.1", c[1].String())
})
}

func TestFailingCollectionUnmarshalling(t *testing.T) {
t.Run("JSON", func(t *testing.T) {
var c Collection
var c version.Collection
err := json.Unmarshal([]byte(`invalid_json`), &c)
assert.Error(t, err)
Error(t, err)
err = json.Unmarshal([]byte(`["invalid_version"]`), &c)
assert.Error(t, err)
Error(t, err)
})
}
30 changes: 15 additions & 15 deletions constraint_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package version
package version_test

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"github.com/k0sproject/version"
)

func TestConstraint(t *testing.T) {
Expand Down Expand Up @@ -112,14 +112,14 @@ func TestConstraint(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.constraint, func(t *testing.T) {
c, err := NewConstraint(tc.constraint)
assert.NoError(t, err)
c, err := version.NewConstraint(tc.constraint)
NoError(t, err)

for expected, versions := range tc.truthTable {
t.Run(fmt.Sprintf("%t", expected), func(t *testing.T) {
for _, v := range versions {
t.Run(v, func(t *testing.T) {
assert.Equal(t, expected, c.Check(MustParse(v)))
Equal(t, expected, c.Check(version.MustParse(v)))
})
}
})
Expand All @@ -138,23 +138,23 @@ func TestInvalidConstraint(t *testing.T) {
}

for _, invalidConstraint := range invalidConstraints {
_, err := NewConstraint(invalidConstraint)
assert.Error(t, err, "Expected error for invalid constraint: "+invalidConstraint)
_, err := version.NewConstraint(invalidConstraint)
Error(t, err)
}
}

func TestCheckString(t *testing.T) {
c, err := NewConstraint(">= 1.0.0")
assert.NoError(t, err)
c, err := version.NewConstraint(">= 1.0.0")
NoError(t, err)

assert.True(t, c.CheckString("1.0.0"))
assert.False(t, c.CheckString("0.9.9"))
assert.False(t, c.CheckString("x"))
True(t, c.CheckString("1.0.0"))
False(t, c.CheckString("0.9.9"))
False(t, c.CheckString("x"))
}

func TestString(t *testing.T) {
c, err := NewConstraint(">= 1.0.0, < 2.0.0")
assert.NoError(t, err)
c, err := version.NewConstraint(">= 1.0.0, < 2.0.0")
NoError(t, err)

assert.Equal(t, ">= 1.0.0, < 2.0.0", c.String())
Equal(t, ">= 1.0.0, < 2.0.0", c.String())
}
8 changes: 0 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
module github.com/k0sproject/version

go 1.17

require github.com/stretchr/testify v1.8.4

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 0 additions & 10 deletions go.sum

This file was deleted.

10 changes: 5 additions & 5 deletions latest_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package version
package version_test

import (
"regexp"
"testing"

"github.com/stretchr/testify/assert"
"github.com/k0sproject/version"
)

func TestLatestByPrerelease(t *testing.T) {
r, err := LatestByPrerelease(false)
assert.NoError(t, err)
assert.Regexp(t, regexp.MustCompile(`^v\d+\.\d+\.\d+\+k0s\.\d+$`), r.String())
r, err := version.LatestByPrerelease(false)
NoError(t, err)
True(t, regexp.MustCompile(`^v\d+\.\d+\.\d+\+k0s\.\d+$`).MatchString(r.String()))
}
Loading
Loading