-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Separate test package Signed-off-by: Kimmo Lehto <[email protected]> * Implement simple test assertions locally Signed-off-by: Kimmo Lehto <[email protected]> * Drop testify dependency Signed-off-by: Kimmo Lehto <[email protected]> --------- Signed-off-by: Kimmo Lehto <[email protected]>
- Loading branch information
Showing
7 changed files
with
159 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())) | ||
} |
Oops, something went wrong.