diff --git a/pkg/util/version/version.go b/pkg/util/version/version.go index 327f2e67f4..e8cd0cecfa 100644 --- a/pkg/util/version/version.go +++ b/pkg/util/version/version.go @@ -121,11 +121,39 @@ func MustParseSemantic(str string) *Version { return v } +// Major returns the major release number +func (v *Version) Major() uint { + return v.components[0] +} + +// Minor returns the minor release number +func (v *Version) Minor() uint { + return v.components[1] +} + +// Patch returns the patch release number if v is a Semantic Version, or 0 +func (v *Version) Patch() uint { + if len(v.components) < 3 { + return 0 + } + return v.components[2] +} + // BuildMetadata returns the build metadata, if v is a Semantic Version, or "" func (v *Version) BuildMetadata() string { return v.buildMetadata } +// PreRelease returns the prerelease metadata, if v is a Semantic Version, or "" +func (v *Version) PreRelease() string { + return v.preRelease +} + +// Components returns the version number components +func (v *Version) Components() []uint { + return v.components +} + // String converts a Version back to a string; note that for versions parsed with // ParseGeneric, this will not include the trailing uninterpreted portion of the version // number. diff --git a/pkg/util/version/version_test.go b/pkg/util/version/version_test.go index 555c59b4b7..eb231b1e2b 100644 --- a/pkg/util/version/version_test.go +++ b/pkg/util/version/version_test.go @@ -18,6 +18,7 @@ package version import ( "fmt" + "reflect" "testing" ) @@ -257,3 +258,75 @@ func TestBadGenericVersions(t *testing.T) { } } } + +func TestComponents(t *testing.T) { + + var tests = []struct { + version string + semver bool + expectedComponents []uint + expectedMajor uint + expectedMinor uint + expectedPatch uint + expectedPreRelease string + expectedBuildMetadata string + }{ + { + version: "1.0.2", + semver: true, + expectedComponents: []uint{1, 0, 2}, + expectedMajor: 1, + expectedMinor: 0, + expectedPatch: 2, + }, + { + version: "1.0.2-alpha+001", + semver: true, + expectedComponents: []uint{1, 0, 2}, + expectedMajor: 1, + expectedMinor: 0, + expectedPatch: 2, + expectedPreRelease: "alpha", + expectedBuildMetadata: "001", + }, + { + version: "1.2", + semver: false, + expectedComponents: []uint{1, 2}, + expectedMajor: 1, + expectedMinor: 2, + }, + { + version: "1.0.2-beta+exp.sha.5114f85", + semver: true, + expectedComponents: []uint{1, 0, 2}, + expectedMajor: 1, + expectedMinor: 0, + expectedPatch: 2, + expectedPreRelease: "beta", + expectedBuildMetadata: "exp.sha.5114f85", + }, + } + + for _, test := range tests { + version, _ := parse(test.version, test.semver) + if !reflect.DeepEqual(test.expectedComponents, version.Components()) { + t.Error("parse returned un'expected components") + } + if test.expectedMajor != version.Major() { + t.Errorf("parse returned version.Major %d, expected %d", test.expectedMajor, version.Major()) + } + if test.expectedMinor != version.Minor() { + t.Errorf("parse returned version.Minor %d, expected %d", test.expectedMinor, version.Minor()) + } + if test.expectedPatch != version.Patch() { + t.Errorf("parse returned version.Patch %d, expected %d", test.expectedPatch, version.Patch()) + } + if test.expectedPreRelease != version.PreRelease() { + t.Errorf("parse returned version.PreRelease %s, expected %s", test.expectedPreRelease, version.PreRelease()) + } + if test.expectedBuildMetadata != version.BuildMetadata() { + t.Errorf("parse returned version.BuildMetadata %s, expected %s", test.expectedBuildMetadata, version.BuildMetadata()) + } + } +}