From 538bb6bd9dd8cf328d9f7236d3fc416cd8a1d845 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 4 Dec 2026 17:31:31 +0100 Subject: [PATCH 1/2] client/version: expand test coverage Extend the semver test vectors to cover large numeric identifiers, numeric and ASCII ordering, equivalent versions, and malformed identifiers. Add tests for MustNewName, MustNewVersion, Compare, and the remaining version validation path. This brings the version package to 100% statement coverage in preparation for removing the golang.org/x/mod test dependency. Signed-off-by: Sebastiaan van Stijn --- client/version/name_test.go | 9 ++++++ client/version/semver_test.go | 36 +++++++++++++++++++++++ client/version/version_test.go | 53 ++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+) diff --git a/client/version/name_test.go b/client/version/name_test.go index f9efa906..ed7c2bc0 100644 --- a/client/version/name_test.go +++ b/client/version/name_test.go @@ -32,3 +32,12 @@ func Test_Name(t *testing.T) { assert.ErrorIs(t, err, ErrEmptyName) }) } + +func TestMustNewName(t *testing.T) { + t.Parallel() + + assert.Equal(t, "foo", MustNewName("foo").String()) + assert.PanicsWithError(t, "parsing name: empty name", func() { + MustNewName("") + }) +} diff --git a/client/version/semver_test.go b/client/version/semver_test.go index fb1777a8..8c955787 100644 --- a/client/version/semver_test.go +++ b/client/version/semver_test.go @@ -77,6 +77,42 @@ var semverVectors = []string{ "v1.0.0 ", "v-1", "v1.-1.0", + + // Abbreviated versions and equivalent versions. + "v0.0", + "v1.2.3+other", + "v1.2.3-rc.1+other", + + // Numeric rather than lexical ordering. + "v1.2.9", + "v1.2.10", + "v1.9.0", + "v1.10.0", + + // Numeric identifiers exceeding integer limits. + "v18446744073709551615.0.0", + "v18446744073709551616.0.0", + "v1.2.3-18446744073709551615", + "v1.2.3-18446744073709551616", + + // ASCII ordering. + "v1.2.3-A", + "v1.2.3-a", + + // Empty identifiers. + "v1.2.3-.alpha", + "v1.2.3+.build", + "v1.2.3+build..1", + "v1.2.3+build.", + + // Invalid characters. + "v1.2.3-α", + "v1.2.3+α", + "v1.2.3", + "v1.2.3\n", + "v1.2.3\t", + "v1.2.3\x00", + " v1.2.3", } func TestSemverMatchesXMod(t *testing.T) { diff --git a/client/version/version_test.go b/client/version/version_test.go index f349f233..518791d9 100644 --- a/client/version/version_test.go +++ b/client/version/version_test.go @@ -35,4 +35,57 @@ func Test_Version(t *testing.T) { _, err := NewVersion("") assert.ErrorIs(t, err, ErrEmptyVersion) }) + t.Run("invalid semver", func(t *testing.T) { + v, err := NewVersion("v1.2.3.4") + assert.EqualError(t, err, "parsing version: invalid version: v1.2.3.4") + assert.Nil(t, v) + }) +} + +func TestMustNewVersion(t *testing.T) { + t.Parallel() + + assert.Equal(t, "v1.0", MustNewVersion("v1.0").String()) + assert.PanicsWithError(t, "parsing version: empty version", func() { + MustNewVersion("") + }) +} + +func TestCompare(t *testing.T) { + t.Parallel() + + tests := []struct { + a, b string + want int + }{ + {"v1.0.0", "v2.0.0", -1}, + {"v2.0.0", "v1.0.0", 1}, + {"v1.0.0", "v1.0.0", 0}, + {"v1", "v1.0.0", 0}, + {"v1.0.0-rc.1", "v1.0.0", -1}, + {"v1.0.0+foo", "v1.0.0+bar", 0}, + } + + for _, tc := range tests { + assert.Equal(t, tc.want, + Compare(MustNewVersion(tc.a), MustNewVersion(tc.b)), + "Compare(%q, %q)", tc.a, tc.b) + } +} + +type testVersion string + +func (v testVersion) String() string { + return string(v) +} + +func TestCompareInvalid(t *testing.T) { + t.Parallel() + + validVer := MustNewVersion("v1.0.0") + invalid := testVersion("invalid") + + assert.Equal(t, -1, Compare(invalid, validVer)) + assert.Equal(t, 1, Compare(validVer, invalid)) + assert.Zero(t, Compare(invalid, testVersion("also-invalid"))) } From cf04774baffa8218bf88fb77684f536e71a9920b Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 4 Dec 2026 17:34:54 +0100 Subject: [PATCH 2/2] client/version: remove golang.org/x/mod test dependency Replace differential semver tests with explicit validity and precedence assertions, retaining all existing test vectors and exhaustive pairwise comparisons. Replace differential fuzz testing with checks for comparator invariants, including reflexivity, antisymmetry, and transitivity. This removes the remaining dependency on golang.org/x/mod from the client module while retaining 100% statement coverage. Signed-off-by: Sebastiaan van Stijn --- client/go.mod | 1 - client/go.sum | 2 - client/version/semver_test.go | 187 +++++++++++++++++++++----------- plugin/go.sum | 2 - plugins/credentialhelper/go.sum | 2 - plugins/pass/go.sum | 2 - 6 files changed, 124 insertions(+), 72 deletions(-) diff --git a/client/go.mod b/client/go.mod index 370c0194..bdbdb24e 100644 --- a/client/go.mod +++ b/client/go.mod @@ -10,7 +10,6 @@ replace github.com/docker/secrets-engine/secrets => ../secrets require ( github.com/docker/secrets-engine/secrets v1.0.0 github.com/stretchr/testify v1.12.1 - golang.org/x/mod v0.41.0 google.golang.org/protobuf v1.36.11 ) diff --git a/client/go.sum b/client/go.sum index 090cfdde..223dc315 100644 --- a/client/go.sum +++ b/client/go.sum @@ -4,7 +4,5 @@ github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWD github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg= go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw= go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg= -golang.org/x/mod v0.41.0 h1:qJmnOUb4YB+FsEuM3HcWucdZASCPGhsX6uljO6pog0c= -golang.org/x/mod v0.41.0/go.mod h1:Ek9pY8RKWXwsWvd3rQiHYtMqkjSUV+s1Rj7j4H5Ur6o= google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE= google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco= diff --git a/client/version/semver_test.go b/client/version/semver_test.go index 8c955787..65c4f5b7 100644 --- a/client/version/semver_test.go +++ b/client/version/semver_test.go @@ -15,21 +15,16 @@ package version import ( + "cmp" "testing" "github.com/stretchr/testify/assert" - xsemver "golang.org/x/mod/semver" ) -var semverVectors = []string{ +var invalidVersions = []string{ "", "v", "1.0.0", - "v1", - "v1.0", - "v1.0.0", - "v1.2.3", - "v10.20.30", "v01", "v1.02", "v1.2.03", @@ -37,68 +32,24 @@ var semverVectors = []string{ "v1.", "v1..2", "v1.2.", - "v0", - "v0.0.0", "v1-pre", "v1.2-pre", "v1+meta", "v1.2+meta", "v1.2.3-", "v1.2.3+", - "v1.2.3-alpha", - "v1.2.3-alpha.1", - "v1.2.3-alpha.beta", "v1.2.3-alpha.01", - "v1.2.3-0", "v1.2.3-00", - "v1.2.3-0a", - "v1.2.3-01a", - "v1.2.3-1", - "v1.2.3-2", - "v1.2.3-10", - "v1.2.3-a-b", - "v1.2.3--", "v1.2.3-alpha..1", "v1.2.3-alpha.", - "v1.2.3-beta", - "v1.2.3-beta.2", - "v1.2.3-beta.11", - "v1.2.3-rc.1", - "v1.2.3+build", - "v1.2.3+build.01", "v1.2.3+build+x", - "v1.2.3-rc.1+build.5", "v1.2.3-rc_1", "v1.2.3+b_1", - "v2.0.0", - "v2.0.0-0.3.7", - "v1.0.0-x.7.z.92", "V1.0.0", "v1.0.0 ", "v-1", "v1.-1.0", - // Abbreviated versions and equivalent versions. - "v0.0", - "v1.2.3+other", - "v1.2.3-rc.1+other", - - // Numeric rather than lexical ordering. - "v1.2.9", - "v1.2.10", - "v1.9.0", - "v1.10.0", - - // Numeric identifiers exceeding integer limits. - "v18446744073709551615.0.0", - "v18446744073709551616.0.0", - "v1.2.3-18446744073709551615", - "v1.2.3-18446744073709551616", - - // ASCII ordering. - "v1.2.3-A", - "v1.2.3-a", - // Empty identifiers. "v1.2.3-.alpha", "v1.2.3+.build", @@ -115,26 +66,136 @@ var semverVectors = []string{ " v1.2.3", } -func TestSemverMatchesXMod(t *testing.T) { +// orderedVersions contains groups of valid versions in ascending +// precedence order. Versions within each group compare equal. +var orderedVersions = [][]string{ + // Abbreviated versions compare equal to their expanded forms. + {"v0", "v0.0", "v0.0.0"}, + {"v1.0.0-x.7.z.92"}, + {"v1", "v1.0", "v1.0.0"}, + + // Numeric pre-release identifiers are compared numerically. + {"v1.2.3-0"}, + {"v1.2.3-1"}, + {"v1.2.3-2"}, + {"v1.2.3-10"}, + + // Numeric identifiers are not limited to uint64. + {"v1.2.3-18446744073709551615"}, + {"v1.2.3-18446744073709551616"}, + + // Non-numeric identifiers sort after numeric identifiers + // and are compared in ASCII order. + {"v1.2.3--"}, + {"v1.2.3-01a"}, + {"v1.2.3-0a"}, + {"v1.2.3-A"}, + {"v1.2.3-a"}, + {"v1.2.3-a-b"}, + + // Pre-release identifiers are compared individually. + {"v1.2.3-alpha"}, + {"v1.2.3-alpha.1"}, + {"v1.2.3-alpha.beta"}, + {"v1.2.3-beta"}, + {"v1.2.3-beta.2"}, + {"v1.2.3-beta.11"}, + + // Build metadata does not affect precedence. + {"v1.2.3-rc.1", "v1.2.3-rc.1+build.5", "v1.2.3-rc.1+other"}, + {"v1.2.3", "v1.2.3+build", "v1.2.3+build.01", "v1.2.3+other"}, + + // Major, minor, and patch versions are compared numerically. + {"v1.2.9"}, + {"v1.2.10"}, + {"v1.9.0"}, + {"v1.10.0"}, + {"v2.0.0-0.3.7"}, + {"v2.0.0"}, + {"v10.20.30"}, + + // Version numbers are not limited to uint64. + {"v18446744073709551615.0.0"}, + {"v18446744073709551616.0.0"}, +} + +func TestParseSemver(t *testing.T) { t.Parallel() - for _, v := range semverVectors { + + for _, v := range invalidVersions { _, ok := parseSemver(v) - assert.Equal(t, xsemver.IsValid(v), ok, "valid(%q)", v) + assert.False(t, ok, "parseSemver(%q)", v) } - for _, a := range semverVectors { - for _, b := range semverVectors { - assert.Equal(t, xsemver.Compare(a, b), compareSemver(a, b), "compare(%q, %q)", a, b) + + for _, group := range orderedVersions { + for _, v := range group { + _, ok := parseSemver(v) + assert.True(t, ok, "parseSemver(%q)", v) } } } -func FuzzSemverMatchesXMod(f *testing.F) { - for _, v := range semverVectors { - f.Add(v, "v1.2.3-rc.1") +func TestCompareSemver(t *testing.T) { + t.Parallel() + + // Invalid versions compare equal to each other and less than + // any valid version. + groups := append([][]string{invalidVersions}, orderedVersions...) + + for i, groupA := range groups { + for j, groupB := range groups { + want := cmp.Compare(i, j) + for _, a := range groupA { + for _, b := range groupB { + assert.Equal(t, want, compareSemver(a, b), + "compareSemver(%q, %q)", a, b) + } + } + } + } +} + +func FuzzCompareSemver(f *testing.F) { + for _, v := range invalidVersions { + f.Add(v, "v1.2.3-rc.1", "v1.2.3") + } + + for _, group := range orderedVersions { + for _, v := range group { + f.Add(v, "v1.2.3-rc.1", "v1.2.3") + } } - f.Fuzz(func(t *testing.T, a, b string) { + + f.Fuzz(func(t *testing.T, a, b, c string) { + ab := compareSemver(a, b) + ba := compareSemver(b, a) + bc := compareSemver(b, c) + ac := compareSemver(a, c) + + // Reflexivity. + assert.Zero(t, compareSemver(a, a)) + + // Antisymmetry. + assert.Equal(t, -ab, ba) + + // Transitivity. + if ab <= 0 && bc <= 0 { + assert.LessOrEqual(t, ac, 0) + } + if ab >= 0 && bc >= 0 { + assert.GreaterOrEqual(t, ac, 0) + } + + // Valid versions sort after invalid versions. _, okA := parseSemver(a) - assert.Equal(t, xsemver.IsValid(a), okA, "valid(%q)", a) - assert.Equal(t, xsemver.Compare(a, b), compareSemver(a, b), "compare(%q, %q)", a, b) + _, okB := parseSemver(b) + switch { + case okA && !okB: + assert.Equal(t, 1, ab) + case !okA && okB: + assert.Equal(t, -1, ab) + case !okA && !okB: + assert.Zero(t, ab) + } }) } diff --git a/plugin/go.sum b/plugin/go.sum index 3a8ddea0..ee982700 100644 --- a/plugin/go.sum +++ b/plugin/go.sum @@ -6,8 +6,6 @@ github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWD github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg= go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw= go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg= -golang.org/x/mod v0.41.0 h1:qJmnOUb4YB+FsEuM3HcWucdZASCPGhsX6uljO6pog0c= -golang.org/x/mod v0.41.0/go.mod h1:Ek9pY8RKWXwsWvd3rQiHYtMqkjSUV+s1Rj7j4H5Ur6o= golang.org/x/sys v0.46.0 h1:noSf2Fq6F8DBgS+LysIkx7rIExoNHJsxOAtPp4rthXw= golang.org/x/sys v0.46.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE= diff --git a/plugins/credentialhelper/go.sum b/plugins/credentialhelper/go.sum index 52eb70db..42deaa21 100644 --- a/plugins/credentialhelper/go.sum +++ b/plugins/credentialhelper/go.sum @@ -8,8 +8,6 @@ github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWD github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg= go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw= go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg= -golang.org/x/mod v0.41.0 h1:qJmnOUb4YB+FsEuM3HcWucdZASCPGhsX6uljO6pog0c= -golang.org/x/mod v0.41.0/go.mod h1:Ek9pY8RKWXwsWvd3rQiHYtMqkjSUV+s1Rj7j4H5Ur6o= golang.org/x/sys v0.46.0 h1:noSf2Fq6F8DBgS+LysIkx7rIExoNHJsxOAtPp4rthXw= golang.org/x/sys v0.46.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE= diff --git a/plugins/pass/go.sum b/plugins/pass/go.sum index aa08f22a..cb6e50aa 100644 --- a/plugins/pass/go.sum +++ b/plugins/pass/go.sum @@ -24,8 +24,6 @@ github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWD github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg= go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw= go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg= -golang.org/x/mod v0.41.0 h1:qJmnOUb4YB+FsEuM3HcWucdZASCPGhsX6uljO6pog0c= -golang.org/x/mod v0.41.0/go.mod h1:Ek9pY8RKWXwsWvd3rQiHYtMqkjSUV+s1Rj7j4H5Ur6o= golang.org/x/sys v0.46.0 h1:noSf2Fq6F8DBgS+LysIkx7rIExoNHJsxOAtPp4rthXw= golang.org/x/sys v0.46.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE=