From 6ffe82c7a1b3d45c0c72cdda857358f93d85675e Mon Sep 17 00:00:00 2001 From: Kimmo Lehto Date: Mon, 15 Jan 2024 11:07:10 +0200 Subject: [PATCH 1/3] Separate test package Signed-off-by: Kimmo Lehto --- collection_test.go | 21 +++++++++--------- constraint_test.go | 14 ++++++------ latest_test.go | 6 ++++-- version_test.go | 54 ++++++++++++++++++++++++---------------------- 4 files changed, 51 insertions(+), 44 deletions(-) diff --git a/collection_test.go b/collection_test.go index 8f68b10..faa8842 100644 --- a/collection_test.go +++ b/collection_test.go @@ -1,27 +1,28 @@ -package version +package version_test import ( "encoding/json" "sort" "testing" + "github.com/k0sproject/version" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestNewCollection(t *testing.T) { - c, err := NewCollection("1.23.3+k0s.1", "1.23.4+k0s.1") - require.NoError(t, err) + c, err := version.NewCollection("1.23.3+k0s.1", "1.23.4+k0s.1") + assert.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") + _, err = version.NewCollection("1.23.3+k0s.1", "1.23.b+k0s.1") assert.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", @@ -39,8 +40,8 @@ func TestSorting(t *testing.T) { } 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") + assert.NoError(t, err) t.Run("JSON", func(t *testing.T) { jsonData, err := json.Marshal(c) @@ -51,7 +52,7 @@ func TestCollectionMarshalling(t *testing.T) { 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()) @@ -61,7 +62,7 @@ func TestCollectionUnmarshalling(t *testing.T) { 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) err = json.Unmarshal([]byte(`["invalid_version"]`), &c) diff --git a/constraint_test.go b/constraint_test.go index f8d221f..acdb86e 100644 --- a/constraint_test.go +++ b/constraint_test.go @@ -1,9 +1,11 @@ -package version +package version_test import ( "fmt" "testing" + "github.com/k0sproject/version" + "github.com/stretchr/testify/assert" ) @@ -112,14 +114,14 @@ func TestConstraint(t *testing.T) { for _, tc := range testCases { t.Run(tc.constraint, func(t *testing.T) { - c, err := NewConstraint(tc.constraint) + c, err := version.NewConstraint(tc.constraint) assert.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))) + assert.Equal(t, expected, c.Check(version.MustParse(v))) }) } }) @@ -138,13 +140,13 @@ func TestInvalidConstraint(t *testing.T) { } for _, invalidConstraint := range invalidConstraints { - _, err := NewConstraint(invalidConstraint) + _, err := version.NewConstraint(invalidConstraint) assert.Error(t, err, "Expected error for invalid constraint: "+invalidConstraint) } } func TestCheckString(t *testing.T) { - c, err := NewConstraint(">= 1.0.0") + c, err := version.NewConstraint(">= 1.0.0") assert.NoError(t, err) assert.True(t, c.CheckString("1.0.0")) @@ -153,7 +155,7 @@ func TestCheckString(t *testing.T) { } func TestString(t *testing.T) { - c, err := NewConstraint(">= 1.0.0, < 2.0.0") + c, err := version.NewConstraint(">= 1.0.0, < 2.0.0") assert.NoError(t, err) assert.Equal(t, ">= 1.0.0, < 2.0.0", c.String()) diff --git a/latest_test.go b/latest_test.go index 96c924a..73e8632 100644 --- a/latest_test.go +++ b/latest_test.go @@ -1,14 +1,16 @@ -package version +package version_test import ( "regexp" "testing" + "github.com/k0sproject/version" + "github.com/stretchr/testify/assert" ) func TestLatestByPrerelease(t *testing.T) { - r, err := LatestByPrerelease(false) + r, err := version.LatestByPrerelease(false) assert.NoError(t, err) assert.Regexp(t, regexp.MustCompile(`^v\d+\.\d+\.\d+\+k0s\.\d+$`), r.String()) } diff --git a/version_test.go b/version_test.go index 3f6a7a4..9b678ca 100644 --- a/version_test.go +++ b/version_test.go @@ -1,25 +1,27 @@ -package version +package version_test import ( "encoding/json" "errors" "testing" + "github.com/k0sproject/version" + "github.com/stretchr/testify/assert" ) func TestNewVersion(t *testing.T) { - v, err := NewVersion("1.23.3+k0s.1") + v, err := version.NewVersion("1.23.3+k0s.1") assert.NoError(t, err) assert.Equal(t, "v1.23.3+k0s.1", v.String()) - _, err = NewVersion("v1.23.b+k0s.1") + _, err = version.NewVersion("v1.23.b+k0s.1") assert.Error(t, err) } func TestBasicComparison(t *testing.T) { - a, err := NewVersion("1.23.1+k0s.1") + a, err := version.NewVersion("1.23.1+k0s.1") assert.NoError(t, err) - b, err := NewVersion("1.23.2+k0s.1") + b, err := version.NewVersion("1.23.2+k0s.1") assert.NoError(t, err) assert.True(t, b.GreaterThan(a), "version %s should be greater than %s", b, a) assert.True(t, a.LessThan(b), "version %s should be less than %s", b, a) @@ -27,9 +29,9 @@ func TestBasicComparison(t *testing.T) { } func TestK0sComparison(t *testing.T) { - a, err := NewVersion("1.23.1+k0s.1") + a, err := version.NewVersion("1.23.1+k0s.1") assert.NoError(t, err) - b, err := NewVersion("1.23.1+k0s.2") + b, err := version.NewVersion("1.23.1+k0s.2") assert.NoError(t, err) assert.True(t, b.GreaterThan(a), "version %s should be greater than %s", b, a) assert.False(t, a.GreaterThan(a), "version %s should not be greater than %s", a, a) @@ -39,21 +41,21 @@ func TestK0sComparison(t *testing.T) { } func TestSatisfies(t *testing.T) { - v, err := NewVersion("1.23.1+k0s.1") + v, err := version.NewVersion("1.23.1+k0s.1") assert.NoError(t, err) - assert.True(t, v.Satisfies(MustConstraint(">=1.23.1"))) - assert.True(t, v.Satisfies(MustConstraint(">=1.23.1+k0s.0"))) - assert.True(t, v.Satisfies(MustConstraint(">=1.23.1+k0s.1"))) - assert.True(t, v.Satisfies(MustConstraint("=1.23.1+k0s.1"))) - assert.True(t, v.Satisfies(MustConstraint("<1.23.1+k0s.2"))) - assert.False(t, v.Satisfies(MustConstraint(">=1.23.1+k0s.2"))) - assert.False(t, v.Satisfies(MustConstraint(">=1.23.2"))) - assert.False(t, v.Satisfies(MustConstraint(">1.23.1+k0s.1"))) - assert.False(t, v.Satisfies(MustConstraint("<1.23.1+k0s.1"))) + assert.True(t, v.Satisfies(version.MustConstraint(">=1.23.1"))) + assert.True(t, v.Satisfies(version.MustConstraint(">=1.23.1+k0s.0"))) + assert.True(t, v.Satisfies(version.MustConstraint(">=1.23.1+k0s.1"))) + assert.True(t, v.Satisfies(version.MustConstraint("=1.23.1+k0s.1"))) + assert.True(t, v.Satisfies(version.MustConstraint("<1.23.1+k0s.2"))) + assert.False(t, v.Satisfies(version.MustConstraint(">=1.23.1+k0s.2"))) + assert.False(t, v.Satisfies(version.MustConstraint(">=1.23.2"))) + assert.False(t, v.Satisfies(version.MustConstraint(">1.23.1+k0s.1"))) + assert.False(t, v.Satisfies(version.MustConstraint("<1.23.1+k0s.1"))) } func TestURLs(t *testing.T) { - a, err := NewVersion("1.23.3+k0s.1") + a, err := version.NewVersion("1.23.3+k0s.1") assert.NoError(t, err) assert.Equal(t, "https://github.com/k0sproject/k0s/releases/tag/v1.23.3%2Bk0s.1", a.URL()) assert.Equal(t, "https://github.com/k0sproject/k0s/releases/download/v1.23.3%2Bk0s.1/k0s-v1.23.3+k0s.1-amd64.exe", a.DownloadURL("windows", "amd64")) @@ -62,7 +64,7 @@ func TestURLs(t *testing.T) { } func TestMarshalling(t *testing.T) { - v, err := NewVersion("v1.0.0+k0s.0") + v, err := version.NewVersion("v1.0.0+k0s.0") assert.NoError(t, err) t.Run("JSON", func(t *testing.T) { @@ -84,7 +86,7 @@ func TestMarshalling(t *testing.T) { }) t.Run("YAML", func(t *testing.T) { - var nilVersion *Version + var nilVersion *version.Version yamlData, err := nilVersion.MarshalYAML() assert.NoError(t, err) assert.Nil(t, yamlData) @@ -93,14 +95,14 @@ func TestMarshalling(t *testing.T) { func TestUnmarshalling(t *testing.T) { t.Run("JSON", func(t *testing.T) { - v := &Version{} + v := &version.Version{} err := json.Unmarshal([]byte(`"v1.0.0+k0s.1"`), v) assert.NoError(t, err) assert.Equal(t, "v1.0.0+k0s.1", v.String()) }) t.Run("YAML", func(t *testing.T) { - v := &Version{} + v := &version.Version{} err := v.UnmarshalYAML(func(i interface{}) error { *(i.(*string)) = "v1.0.0+k0s.1" return nil @@ -110,14 +112,14 @@ func TestUnmarshalling(t *testing.T) { }) t.Run("JSON with null", func(t *testing.T) { - v := &Version{} + v := &version.Version{} err := json.Unmarshal([]byte(`null`), v) assert.NoError(t, err) assert.True(t, v.IsZero()) }) t.Run("YAML with empty", func(t *testing.T) { - v := &Version{} + v := &version.Version{} err := v.UnmarshalYAML(func(i interface{}) error { *(i.(*string)) = "" return nil @@ -129,7 +131,7 @@ func TestUnmarshalling(t *testing.T) { func TestFailingUnmarshalling(t *testing.T) { t.Run("JSON", func(t *testing.T) { - var v Version + var v version.Version err := json.Unmarshal([]byte(`invalid_json`), &v) assert.Error(t, err) err = json.Unmarshal([]byte(`"invalid_version"`), &v) @@ -137,7 +139,7 @@ func TestFailingUnmarshalling(t *testing.T) { }) t.Run("YAML", func(t *testing.T) { - var v = &Version{} + var v = &version.Version{} err := v.UnmarshalYAML(func(i interface{}) error { return errors.New("forced error") }) From f04c4f973319386c268d21efdd2ad60114768b67 Mon Sep 17 00:00:00 2001 From: Kimmo Lehto Date: Mon, 15 Jan 2024 11:24:12 +0200 Subject: [PATCH 2/3] Implement simple test assertions locally Signed-off-by: Kimmo Lehto --- collection_test.go | 42 ++++++------- constraint_test.go | 20 +++---- latest_test.go | 6 +- version_test.go | 146 +++++++++++++++++++++++++++++---------------- 4 files changed, 123 insertions(+), 91 deletions(-) diff --git a/collection_test.go b/collection_test.go index faa8842..63ad169 100644 --- a/collection_test.go +++ b/collection_test.go @@ -6,19 +6,16 @@ import ( "testing" "github.com/k0sproject/version" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" ) func TestNewCollection(t *testing.T) { c, err := version.NewCollection("1.23.3+k0s.1", "1.23.4+k0s.1") - assert.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) + 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") - assert.Error(t, err) + Error(t, err) } func TestSorting(t *testing.T) { @@ -29,24 +26,23 @@ func TestSorting(t *testing.T) { "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 := version.NewCollection("v1.0.0+k0s.0", "v1.0.1+k0s.0") - assert.NoError(t, err) + 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)) }) } @@ -54,9 +50,9 @@ func TestCollectionUnmarshalling(t *testing.T) { t.Run("JSON", func(t *testing.T) { 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()) }) } @@ -64,8 +60,8 @@ func TestFailingCollectionUnmarshalling(t *testing.T) { t.Run("JSON", func(t *testing.T) { 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) }) } diff --git a/constraint_test.go b/constraint_test.go index acdb86e..01e591c 100644 --- a/constraint_test.go +++ b/constraint_test.go @@ -5,8 +5,6 @@ import ( "testing" "github.com/k0sproject/version" - - "github.com/stretchr/testify/assert" ) func TestConstraint(t *testing.T) { @@ -115,13 +113,13 @@ func TestConstraint(t *testing.T) { for _, tc := range testCases { t.Run(tc.constraint, func(t *testing.T) { c, err := version.NewConstraint(tc.constraint) - assert.NoError(t, err) + 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(version.MustParse(v))) + Equal(t, expected, c.Check(version.MustParse(v))) }) } }) @@ -141,22 +139,22 @@ func TestInvalidConstraint(t *testing.T) { for _, invalidConstraint := range invalidConstraints { _, err := version.NewConstraint(invalidConstraint) - assert.Error(t, err, "Expected error for invalid constraint: "+invalidConstraint) + Error(t, err) } } func TestCheckString(t *testing.T) { c, err := version.NewConstraint(">= 1.0.0") - assert.NoError(t, err) + 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 := version.NewConstraint(">= 1.0.0, < 2.0.0") - assert.NoError(t, err) + NoError(t, err) - assert.Equal(t, ">= 1.0.0, < 2.0.0", c.String()) + Equal(t, ">= 1.0.0, < 2.0.0", c.String()) } diff --git a/latest_test.go b/latest_test.go index 73e8632..998b20b 100644 --- a/latest_test.go +++ b/latest_test.go @@ -5,12 +5,10 @@ import ( "testing" "github.com/k0sproject/version" - - "github.com/stretchr/testify/assert" ) func TestLatestByPrerelease(t *testing.T) { r, err := version.LatestByPrerelease(false) - assert.NoError(t, err) - assert.Regexp(t, regexp.MustCompile(`^v\d+\.\d+\.\d+\+k0s\.\d+$`), r.String()) + NoError(t, err) + True(t, regexp.MustCompile(`^v\d+\.\d+\.\d+\+k0s\.\d+$`).MatchString(r.String())) } diff --git a/version_test.go b/version_test.go index 9b678ca..e74aac3 100644 --- a/version_test.go +++ b/version_test.go @@ -3,93 +3,132 @@ package version_test import ( "encoding/json" "errors" + "reflect" "testing" "github.com/k0sproject/version" - - "github.com/stretchr/testify/assert" ) +func NoError(t *testing.T, err error) { + if err != nil { + t.Fatalf("Received an unexpected error: %v", err) + } +} + +func Error(t *testing.T, err error) { + if err == nil { + t.Fatalf("Expected an error, got nil") + } +} + +func Equal(t *testing.T, expected, actual interface{}) { + if reflect.DeepEqual(expected, actual) { + return + } + t.Errorf("Expected %v, got %v", expected, actual) +} + +func True(t *testing.T, actual bool) { + if actual { + return + } + t.Errorf("Expected true, got false") +} + +func False(t *testing.T, actual bool) { + if !actual { + return + } + t.Errorf("Expected false, got true") +} + +func Nil(t *testing.T, actual interface{}) { + if actual == nil { + return + } + t.Errorf("Expected nil, got %v", actual) +} + func TestNewVersion(t *testing.T) { v, err := version.NewVersion("1.23.3+k0s.1") - assert.NoError(t, err) - assert.Equal(t, "v1.23.3+k0s.1", v.String()) + NoError(t, err) + Equal(t, "v1.23.3+k0s.1", v.String()) _, err = version.NewVersion("v1.23.b+k0s.1") - assert.Error(t, err) + Error(t, err) } func TestBasicComparison(t *testing.T) { a, err := version.NewVersion("1.23.1+k0s.1") - assert.NoError(t, err) + NoError(t, err) b, err := version.NewVersion("1.23.2+k0s.1") - assert.NoError(t, err) - assert.True(t, b.GreaterThan(a), "version %s should be greater than %s", b, a) - assert.True(t, a.LessThan(b), "version %s should be less than %s", b, a) - assert.False(t, b.Equal(a), "version %s should not be equal to %s", b, a) + NoError(t, err) + True(t, b.GreaterThan(a)) + True(t, a.LessThan(b)) + False(t, b.Equal(a)) } func TestK0sComparison(t *testing.T) { a, err := version.NewVersion("1.23.1+k0s.1") - assert.NoError(t, err) + NoError(t, err) b, err := version.NewVersion("1.23.1+k0s.2") - assert.NoError(t, err) - assert.True(t, b.GreaterThan(a), "version %s should be greater than %s", b, a) - assert.False(t, a.GreaterThan(a), "version %s should not be greater than %s", a, a) - assert.True(t, a.LessThan(b), "version %s should be less than %s", b, a) - assert.False(t, a.LessThan(a), "version %s should not be less than %s", a, a) - assert.False(t, b.Equal(a), "version %s should not be equal to %s", b, a) + NoError(t, err) + True(t, b.GreaterThan(a)) + False(t, a.GreaterThan(a)) + True(t, a.LessThan(b)) + False(t, a.LessThan(a)) + False(t, b.Equal(a)) } func TestSatisfies(t *testing.T) { v, err := version.NewVersion("1.23.1+k0s.1") - assert.NoError(t, err) - assert.True(t, v.Satisfies(version.MustConstraint(">=1.23.1"))) - assert.True(t, v.Satisfies(version.MustConstraint(">=1.23.1+k0s.0"))) - assert.True(t, v.Satisfies(version.MustConstraint(">=1.23.1+k0s.1"))) - assert.True(t, v.Satisfies(version.MustConstraint("=1.23.1+k0s.1"))) - assert.True(t, v.Satisfies(version.MustConstraint("<1.23.1+k0s.2"))) - assert.False(t, v.Satisfies(version.MustConstraint(">=1.23.1+k0s.2"))) - assert.False(t, v.Satisfies(version.MustConstraint(">=1.23.2"))) - assert.False(t, v.Satisfies(version.MustConstraint(">1.23.1+k0s.1"))) - assert.False(t, v.Satisfies(version.MustConstraint("<1.23.1+k0s.1"))) + NoError(t, err) + True(t, v.Satisfies(version.MustConstraint(">=1.23.1"))) + True(t, v.Satisfies(version.MustConstraint(">=1.23.1+k0s.0"))) + True(t, v.Satisfies(version.MustConstraint(">=1.23.1+k0s.1"))) + True(t, v.Satisfies(version.MustConstraint("=1.23.1+k0s.1"))) + True(t, v.Satisfies(version.MustConstraint("<1.23.1+k0s.2"))) + False(t, v.Satisfies(version.MustConstraint(">=1.23.1+k0s.2"))) + False(t, v.Satisfies(version.MustConstraint(">=1.23.2"))) + False(t, v.Satisfies(version.MustConstraint(">1.23.1+k0s.1"))) + False(t, v.Satisfies(version.MustConstraint("<1.23.1+k0s.1"))) } func TestURLs(t *testing.T) { a, err := version.NewVersion("1.23.3+k0s.1") - assert.NoError(t, err) - assert.Equal(t, "https://github.com/k0sproject/k0s/releases/tag/v1.23.3%2Bk0s.1", a.URL()) - assert.Equal(t, "https://github.com/k0sproject/k0s/releases/download/v1.23.3%2Bk0s.1/k0s-v1.23.3+k0s.1-amd64.exe", a.DownloadURL("windows", "amd64")) - assert.Equal(t, "https://github.com/k0sproject/k0s/releases/download/v1.23.3%2Bk0s.1/k0s-v1.23.3+k0s.1-arm64", a.DownloadURL("linux", "arm64")) - assert.Equal(t, "https://docs.k0sproject.io/v1.23.3+k0s.1/", a.DocsURL()) + NoError(t, err) + Equal(t, "https://github.com/k0sproject/k0s/releases/tag/v1.23.3%2Bk0s.1", a.URL()) + Equal(t, "https://github.com/k0sproject/k0s/releases/download/v1.23.3%2Bk0s.1/k0s-v1.23.3+k0s.1-amd64.exe", a.DownloadURL("windows", "amd64")) + Equal(t, "https://github.com/k0sproject/k0s/releases/download/v1.23.3%2Bk0s.1/k0s-v1.23.3+k0s.1-arm64", a.DownloadURL("linux", "arm64")) + Equal(t, "https://docs.k0sproject.io/v1.23.3+k0s.1/", a.DocsURL()) } func TestMarshalling(t *testing.T) { v, err := version.NewVersion("v1.0.0+k0s.0") - assert.NoError(t, err) + NoError(t, err) t.Run("JSON", func(t *testing.T) { jsonData, err := json.Marshal(v) - assert.NoError(t, err) - assert.Equal(t, `"v1.0.0+k0s.0"`, string(jsonData)) + NoError(t, err) + Equal(t, `"v1.0.0+k0s.0"`, string(jsonData)) }) t.Run("YAML", func(t *testing.T) { yamlData, err := v.MarshalYAML() - assert.NoError(t, err) - assert.Equal(t, "v1.0.0+k0s.0", yamlData) + NoError(t, err) + Equal(t, "v1.0.0+k0s.0", yamlData) }) t.Run("JSON with nil", func(t *testing.T) { jsonData, err := json.Marshal(nil) - assert.NoError(t, err) - assert.Equal(t, `null`, string(jsonData)) + NoError(t, err) + Equal(t, `null`, string(jsonData)) }) t.Run("YAML", func(t *testing.T) { var nilVersion *version.Version yamlData, err := nilVersion.MarshalYAML() - assert.NoError(t, err) - assert.Nil(t, yamlData) + NoError(t, err) + Nil(t, yamlData) }) } @@ -97,8 +136,8 @@ func TestUnmarshalling(t *testing.T) { t.Run("JSON", func(t *testing.T) { v := &version.Version{} err := json.Unmarshal([]byte(`"v1.0.0+k0s.1"`), v) - assert.NoError(t, err) - assert.Equal(t, "v1.0.0+k0s.1", v.String()) + NoError(t, err) + Equal(t, "v1.0.0+k0s.1", v.String()) }) t.Run("YAML", func(t *testing.T) { @@ -107,15 +146,15 @@ func TestUnmarshalling(t *testing.T) { *(i.(*string)) = "v1.0.0+k0s.1" return nil }) - assert.NoError(t, err) - assert.Equal(t, "v1.0.0+k0s.1", v.String()) + NoError(t, err) + Equal(t, "v1.0.0+k0s.1", v.String()) }) t.Run("JSON with null", func(t *testing.T) { v := &version.Version{} err := json.Unmarshal([]byte(`null`), v) - assert.NoError(t, err) - assert.True(t, v.IsZero()) + NoError(t, err) + True(t, v.IsZero()) }) t.Run("YAML with empty", func(t *testing.T) { @@ -124,8 +163,9 @@ func TestUnmarshalling(t *testing.T) { *(i.(*string)) = "" return nil }) - assert.NoError(t, err) - assert.Nil(t, nil, v) + t.Logf("what the shit") + NoError(t, err) + True(t, v.IsZero()) }) } @@ -133,9 +173,9 @@ func TestFailingUnmarshalling(t *testing.T) { t.Run("JSON", func(t *testing.T) { var v version.Version err := json.Unmarshal([]byte(`invalid_json`), &v) - assert.Error(t, err) + Error(t, err) err = json.Unmarshal([]byte(`"invalid_version"`), &v) - assert.Error(t, err) + Error(t, err) }) t.Run("YAML", func(t *testing.T) { @@ -143,11 +183,11 @@ func TestFailingUnmarshalling(t *testing.T) { err := v.UnmarshalYAML(func(i interface{}) error { return errors.New("forced error") }) - assert.Error(t, err) + Error(t, err) err = v.UnmarshalYAML(func(i interface{}) error { *(i.(*string)) = "invalid_version" return nil }) - assert.Error(t, err) + Error(t, err) }) } From d9f2af3c90da8e26e31717a378c61ff13f145264 Mon Sep 17 00:00:00 2001 From: Kimmo Lehto Date: Mon, 15 Jan 2024 11:24:27 +0200 Subject: [PATCH 3/3] Drop testify dependency Signed-off-by: Kimmo Lehto --- README.md | 2 +- go.mod | 8 -------- go.sum | 10 ---------- 3 files changed, 1 insertion(+), 19 deletions(-) delete mode 100644 go.sum diff --git a/README.md b/README.md index c4ca315..200127d 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/go.mod b/go.mod index 7c698d2..3841c52 100644 --- a/go.mod +++ b/go.mod @@ -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 -) diff --git a/go.sum b/go.sum deleted file mode 100644 index fa4b6e6..0000000 --- a/go.sum +++ /dev/null @@ -1,10 +0,0 @@ -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=