Skip to content

Commit

Permalink
Add a prerelease method to version
Browse files Browse the repository at this point in the history
This method returns the joined components of the prerelease field. It's
intended use is really to see if there is a prerelease or not, so a bool could
also work, possibly.
  • Loading branch information
jbowes committed Sep 11, 2021
1 parent 801194f commit 1c9530a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
13 changes: 13 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,16 @@ func (v *buildlessVersion) comparePre(other *Version) int {

// build is not used for semver comparison.
}

// Prerelease returns the prerelease portion of a Version. If the version had
// no prerelease or is nil, the empty string is returned.
//
// Note: This method is considered experimental, and may be removed or changed
// before v1.0.0
func (v *Version) Prerelease() string {
if v == nil {
return ""
}

return strings.Join(v.pre, ".")
}
26 changes: 26 additions & 0 deletions version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,29 @@ func TestCompare_nil(t *testing.T) {
t.Errorf("two nil versions are not equal")
}
}

func TestVersion_Prerelease(t *testing.T) {
mustParse := func(s string) *semver.Version {
v, _ := semver.Parse(s)
return v
}

tcs := map[string]struct {
in *semver.Version
out string
}{
"no prerelease": {mustParse("1.0.0"), ""},
"prerelease": {mustParse("1.0.0-alpha"), "alpha"},
"multi-part prerelease": {mustParse("1.0.0-rc.0"), "rc.0"},
"nil version": {nil, ""},
}

for name, tc := range tcs {
t.Run(name, func(t *testing.T) {
out := tc.in.Prerelease()
if out != tc.out {
t.Error("bad prerelease. got:", out, "wanted:", tc.out)
}
})
}
}

0 comments on commit 1c9530a

Please sign in to comment.