Skip to content

Commit

Permalink
Add support for two digit versions - vX.Y
Browse files Browse the repository at this point in the history
The version parsing library already supports them, but it normalizes
all version strings to have three components. This fix properly handles
versions that have been normalized in that way.

Fixes #24
  • Loading branch information
tomdee committed Aug 23, 2018
1 parent 349ab84 commit 548a710
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 29 deletions.
24 changes: 9 additions & 15 deletions tag.go
Expand Up @@ -27,22 +27,23 @@ func isTagConstraintSpecificTag(tagConstraint string) (bool, string) {
}

func getLatestAcceptableTag(tagConstraint string, tags []string) (string, *FetchError) {
var latestTag string

if len(tags) == 0 {
return latestTag, nil
return "", nil
}

// Sort all tags
// Our use of the library go-version means that each tag will each be represented as a *version.Version
// go-version normalizes the versions so store off a mapping from the normalized version back to the original tag.
versions := make([]*version.Version, len(tags))
verToTag := make(map[*version.Version]string)
for i, tag := range tags {
v, err := version.NewVersion(tag)
if err != nil {
return latestTag, wrapError(err)
return "", wrapError(err)
}

versions[i] = v
verToTag[v] = tag
}
sort.Sort(version.Collection(versions))

Expand All @@ -56,9 +57,9 @@ func getLatestAcceptableTag(tagConstraint string, tags []string) (string, *Fetch
if err != nil {
// Explicitly check for a malformed tag value so we can return a nice error to the user
if strings.Contains(err.Error(), "Malformed constraint") {
return latestTag, newError(INVALID_TAG_CONSTRAINT_EXPRESSION, err.Error())
return "", newError(INVALID_TAG_CONSTRAINT_EXPRESSION, err.Error())
} else {
return latestTag, wrapError(err)
return "", wrapError(err)
}
}

Expand All @@ -71,15 +72,8 @@ func getLatestAcceptableTag(tagConstraint string, tags []string) (string, *Fetch

// check constraint against latest acceptable version
if !constraints.Check(latestAcceptableVersion) {
return latestTag, wrapError(errors.New("Tag does not exist"))
}

// The tag name may have started with a "v". If so, re-apply that string now
for _, originalTagName := range tags {
if strings.Contains(originalTagName, latestAcceptableVersion.String()) {
latestTag = originalTagName
}
return "", wrapError(errors.New("Tag does not exist"))
}

return latestTag, nil
return verToTag[latestAcceptableVersion], nil
}
33 changes: 19 additions & 14 deletions tag_test.go
Expand Up @@ -14,16 +14,19 @@ func TestGetLatestAcceptableTag(t *testing.T) {
}{
{"1.0.7", []string{"1.0.7"}, "1.0.7"},

{"~> 1.0.0", []string{"1.0.5","1.0.6","1.0.7","1.0.8","1.0.9","1.1.0","1.2.3"}, "1.0.9"},
{"~> 1.0.7", []string{"1.0.5","1.0.6","1.0.7","1.0.8","1.0.9","1.1.0","1.2.3"}, "1.0.9"},
{"~> 1.1.0", []string{"1.0.5","1.0.6","1.0.7","1.0.8","1.0.9","1.1.0","1.2.3"}, "1.1.0"},
{"~> 1.1.1", []string{"1.0.5","1.0.6","1.0.7","1.0.8","1.0.9","1.1.0","1.1.1","1.1.2","1.1.3","1.2.3","1.4.0","2.0.0","2.1.0"}, "1.1.3"},
{"~> 1.2.1", []string{"1.0.5","1.0.6","1.0.7","1.0.8","1.0.9","1.1.0","1.1.1","1.1.2","1.1.3","1.2.3","1.4.0","2.0.0","2.1.0"}, "1.2.3"},
{"~> 1.1", []string{"1.0.5","1.0.6","1.0.7","1.0.8","1.0.9","1.1.0","1.1.1","1.1.2","1.1.3","1.2.3","1.4.0","2.0.0","2.1.0"}, "1.4.0"},
{"~> 1.2", []string{"1.0.5","1.0.6","1.0.7","1.0.8","1.0.9","1.1.0","1.1.1","1.1.2","1.1.3","1.2.3","1.4.0","2.0.0","2.1.0"}, "1.4.0"},
{"~> 1.3", []string{"1.0.5","1.0.6","1.0.7","1.0.8","1.0.9","1.1.0","1.1.1","1.1.2","1.1.3","1.2.3","1.4.0","2.0.0","2.1.0"}, "1.4.0"},

{">= 1.3", []string{"1.0.5","1.0.6","1.0.7","1.0.8","1.0.9","1.1.0","1.1.1","1.1.2","1.1.3","1.2.3","1.4.0","2.0.0","2.1.0"}, "2.1.0"},
{"~> 1.0.0", []string{"1.0.5", "1.0.6", "1.0.7", "1.0.8", "1.0.9", "1.1.0", "1.2.3"}, "1.0.9"},
{"~> 1.0.7", []string{"1.0.5", "1.0.6", "1.0.7", "1.0.8", "1.0.9", "1.1.0", "1.2.3"}, "1.0.9"},
{"~> 1.1.0", []string{"1.0.5", "1.0.6", "1.0.7", "1.0.8", "1.0.9", "1.1.0", "1.2.3"}, "1.1.0"},
{"~> 1.1.1", []string{"1.0.5", "1.0.6", "1.0.7", "1.0.8", "1.0.9", "1.1.0", "1.1.1", "1.1.2", "1.1.3", "1.2.3", "1.4.0", "2.0.0", "2.1.0"}, "1.1.3"},
{"~> 1.2.1", []string{"1.0.5", "1.0.6", "1.0.7", "1.0.8", "1.0.9", "1.1.0", "1.1.1", "1.1.2", "1.1.3", "1.2.3", "1.4.0", "2.0.0", "2.1.0"}, "1.2.3"},
{"~> 1.1", []string{"1.0.5", "1.0.6", "1.0.7", "1.0.8", "1.0.9", "1.1.0", "1.1.1", "1.1.2", "1.1.3", "1.2.3", "1.4.0", "2.0.0", "2.1.0", "1.3", "2.3"}, "1.4.0"},
{"~> 1.2", []string{"1.0.5", "1.0.6", "1.0.7", "1.0.8", "1.0.9", "1.1.0", "1.1.1", "1.1.2", "1.1.3", "1.2.3", "1.4.0", "2.0.0", "2.1.0", "1.3", "2.3"}, "1.4.0"},
{"~> 1.3", []string{"1.0.5", "1.0.6", "1.0.7", "1.0.8", "1.0.9", "1.1.0", "1.1.1", "1.1.2", "1.1.3", "1.2.3", "1.4.0", "2.0.0", "2.1.0"}, "1.4.0"},
{"~> 1.3", []string{"1.0.5", "1.0.6", "1.0.7", "1.0.8", "1.0.9", "1.1.0", "1.1.1", "1.1.2", "1.1.3", "1.2.3", "1.4.0", "2.0.0", "2.1.0", "1.5"}, "1.5"},
{"~> 1.3", []string{"1.2", "1.3", "1.3.1"}, "1.3.1"},
{">= 1.3", []string{"1.0.5", "1.0.6", "1.0.7", "1.0.8", "1.0.9", "1.1.0", "1.1.1", "1.1.2", "1.1.3", "1.2.3", "1.4.0", "2.0.0", "2.1.0"}, "2.1.0"},

{">= 1.5", []string{"1.1", "1.2", "1.3", "1.4", "2.0", "2.1"}, "2.1"},

{"v1.0.7", []string{"v1.0.7"}, "v1.0.7"},
{"v1.0.7", []string{}, ""},
Expand Down Expand Up @@ -85,9 +88,11 @@ func TestGetLatestAcceptableTagOnEmptyConstraint(t *testing.T) {
tags []string
expectedTag string
}{
{"", []string{"v0.0.1","v0.0.2","v0.0.3"}, "v0.0.3"},
{"", []string{"1.0.5","1.0.6","1.0.7","1.0.8","1.0.9","1.1.0","1.2.3"}, "1.2.3"},
{"", []string{"1.0.5","1.0.6","1.0.7","1.0.8","1.0.9","1.1.0","1.1.1","1.1.2","1.1.3","1.2.3","1.4.0","2.0.0","2.1.0"}, "2.1.0"},
{"", []string{"v0.0.1", "v0.0.2", "v0.0.3"}, "v0.0.3"},
{"", []string{"1.0.5", "1.0.6", "1.0.7", "1.0.8", "1.0.9", "1.1.0", "1.2.3"}, "1.2.3"},
{"", []string{"v0.5", "2.6", "1.4", "v1.0.8", "v3.1.4", "1.2.3"}, "v3.1.4"},
{"", []string{"v0.5", "2.6", "1.4", "v1.0.8", "v3.1.4", "1.2.3", "4.0"}, "4.0"},
{"", []string{"1.0.5", "1.0.6", "1.0.7", "1.0.8", "1.0.9", "1.1.0", "1.1.1", "1.1.2", "1.1.3", "1.2.3", "1.4.0", "2.0.0", "2.1.0"}, "2.1.0"},
{"", []string{}, ""},
}

Expand Down Expand Up @@ -125,7 +130,7 @@ func TestGetLatestAcceptableTagNoSuchTag(t *testing.T) {
t.Parallel()
cases := []struct {
tagConstraint string
tags []string
tags []string
}{
{"~> 0.0.4", []string{"0.0.1", "0.0.2", "0.0.3"}},
{"> 0.0.4", []string{"0.0.1", "0.0.2", "0.0.3"}},
Expand Down

0 comments on commit 548a710

Please sign in to comment.