Skip to content

improve ParseTorelant to be more tolerant with quirky inputs #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions v4/semver.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,22 @@ func Make(s string) (Version, error) {

// ParseTolerant allows for certain version specifications that do not strictly adhere to semver
// specs to be parsed by this library. It does so by normalizing versions before passing them to
// Parse(). It currently trims spaces, removes a "v" prefix, adds a 0 patch number to versions
// with only major and minor components specified, and removes leading 0s.
// Parse(). It currently trims spaces, removes a "v" prefix, adds 0s to missing minor and patch versions,
// and removes leading 0s. PreRelease/Build meta data is preserved.
func ParseTolerant(s string) (Version, error) {
s = strings.TrimSpace(s)
s = strings.TrimPrefix(s, "v")

//Extract PreRelease/Build meta data if any
index := strings.IndexRune(s, '-')
if buildIndex := strings.IndexRune(s, '+'); buildIndex != -1 && (buildIndex < index || index == -1) {
index = buildIndex
}
var meta string
if index != -1 {
meta = s[index:]
s = s[:index]
}
// Split into major.minor.(patch+pr+meta)
parts := strings.SplitN(s, ".", 3)
// Remove leading zeros.
Expand All @@ -254,15 +264,11 @@ func ParseTolerant(s string) (Version, error) {
}
}
// Fill up shortened versions.
if len(parts) < 3 {
if strings.ContainsAny(parts[len(parts)-1], "+-") {
return Version{}, errors.New("Short version cannot contain PreRelease/Build meta data")
}
for len(parts) < 3 {
parts = append(parts, "0")
}
for len(parts) < 3 {
parts = append(parts, "0")
}
s = strings.Join(parts, ".")
// Reconstruct the normalized version string
s = strings.Join(parts, ".") + meta

return Parse(s)
}
Expand Down
4 changes: 4 additions & 0 deletions v4/semver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ var tolerantFormatTests = []formatTest{
{Version{0, 0, 3, nil, nil}, "000.0.03"},
{Version{1, 2, 0, nil, nil}, "1.2"},
{Version{1, 0, 0, nil, nil}, "1"},
{Version{1, 0, 0, []PRVersion{prstr("alpha")}, nil}, "1-alpha"},
{Version{1, 2, 0, []PRVersion{prstr("alpha")}, nil}, "1.2-alpha"},
{Version{1, 0, 0, nil, []string{"build", "123"}}, "1+build.123"},
{Version{1, 2, 0, []PRVersion{prstr("alpha"), prstr("b-eta")}, []string{"123", "b-uild"}}, "1.2-alpha.b-eta+123.b-uild"},
}

func TestStringer(t *testing.T) {
Expand Down