@ -17,32 +17,25 @@ limitations under the License.
package registry
package registry
import (
import (
"strings"
"testing"
"testing"
"github.com/stretchr/testify/require"
)
)
func TestGetTagMatchingVersionOrConstraint_ExactMatch ( t * testing . T ) {
func TestGetTagMatchingVersionOrConstraint_ExactMatch ( t * testing . T ) {
tags := [ ] string { "1.0.0" , "1.2.3" , "2.0.0" }
tags := [ ] string { "1.0.0" , "1.2.3" , "2.0.0" }
got , err := GetTagMatchingVersionOrConstraint ( tags , "1.2.3" )
got , err := GetTagMatchingVersionOrConstraint ( tags , "1.2.3" )
if err != nil {
require . NoError ( t , err )
t . Fatalf ( "unexpected error: %v" , err )
require . Equal ( t , "1.2.3" , got , "expected exact match" )
}
if got != "1.2.3" {
t . Fatalf ( "expected exact match '1.2.3', got %q" , got )
}
}
}
func TestGetTagMatchingVersionOrConstraint_EmptyVersionWildcard ( t * testing . T ) {
func TestGetTagMatchingVersionOrConstraint_EmptyVersionWildcard ( t * testing . T ) {
// Includes a non-semver tag which should be skipped
// Includes a non-semver tag which should be skipped
tags := [ ] string { "latest" , "0.9.0" , "1.0.0" }
tags := [ ] string { "latest" , "0.9.0" , "1.0.0" }
got , err := GetTagMatchingVersionOrConstraint ( tags , "" )
got , err := GetTagMatchingVersionOrConstraint ( tags , "" )
if err != nil {
require . NoError ( t , err )
t . Fatalf ( "unexpected error: %v" , err )
}
// Should pick the first valid semver tag in order, which is 0.9.0
// Should pick the first valid semver tag in order, which is 0.9.0
if got != "0.9.0" {
require . Equal ( t , "0.9.0" , got )
t . Fatalf ( "expected '0.9.0', got %q" , got )
}
}
}
func TestGetTagMatchingVersionOrConstraint_ConstraintRange ( t * testing . T ) {
func TestGetTagMatchingVersionOrConstraint_ConstraintRange ( t * testing . T ) {
@ -50,73 +43,47 @@ func TestGetTagMatchingVersionOrConstraint_ConstraintRange(t *testing.T) {
// Caret range
// Caret range
got , err := GetTagMatchingVersionOrConstraint ( tags , "^1.0.0" )
got , err := GetTagMatchingVersionOrConstraint ( tags , "^1.0.0" )
if err != nil {
require . NoError ( t , err )
t . Fatalf ( "unexpected error: %v" , err )
require . Equal ( t , "1.0.0" , got , "first match in order" )
}
if got != "1.0.0" { // first match in order
t . Fatalf ( "expected '1.0.0', got %q" , got )
}
// Compound range
// Compound range
got , err = GetTagMatchingVersionOrConstraint ( tags , ">=1.0.0 <2.0.0" )
got , err = GetTagMatchingVersionOrConstraint ( tags , ">=1.0.0 <2.0.0" )
if err != nil {
require . NoError ( t , err )
t . Fatalf ( "unexpected error: %v" , err )
require . Equal ( t , "1.0.0" , got )
}
if got != "1.0.0" {
t . Fatalf ( "expected '1.0.0', got %q" , got )
}
}
}
func TestGetTagMatchingVersionOrConstraint_InvalidConstraint ( t * testing . T ) {
func TestGetTagMatchingVersionOrConstraint_InvalidConstraint ( t * testing . T ) {
tags := [ ] string { "1.0.0" }
tags := [ ] string { "1.0.0" }
_ , err := GetTagMatchingVersionOrConstraint ( tags , ">a1" )
_ , err := GetTagMatchingVersionOrConstraint ( tags , ">a1" )
if err == nil {
require . Error ( t , err , "expected error for invalid constraint" )
t . Fatal ( "expected error for invalid constraint" )
}
}
}
func TestGetTagMatchingVersionOrConstraint_NoMatches ( t * testing . T ) {
func TestGetTagMatchingVersionOrConstraint_NoMatches ( t * testing . T ) {
tags := [ ] string { "0.1.0" , "0.2.0" }
tags := [ ] string { "0.1.0" , "0.2.0" }
_ , err := GetTagMatchingVersionOrConstraint ( tags , ">=1.0.0" )
_ , err := GetTagMatchingVersionOrConstraint ( tags , ">=1.0.0" )
if err == nil {
require . Error ( t , err , "expected error when no tags match" )
t . Fatal ( "expected error when no tags match" )
require . Contains ( t , err . Error ( ) , ">=1.0.0" , "expected error to contain version string" )
}
if ! strings . Contains ( err . Error ( ) , ">=1.0.0" ) {
t . Fatalf ( "expected error to contain version string, got: %v" , err )
}
}
}
func TestGetTagMatchingVersionOrConstraint_SkipsNonSemverTags ( t * testing . T ) {
func TestGetTagMatchingVersionOrConstraint_SkipsNonSemverTags ( t * testing . T ) {
tags := [ ] string { "alpha" , "1.0.0" , "beta" , "1.1.0" }
tags := [ ] string { "alpha" , "1.0.0" , "beta" , "1.1.0" }
got , err := GetTagMatchingVersionOrConstraint ( tags , ">=1.0.0 <2.0.0" )
got , err := GetTagMatchingVersionOrConstraint ( tags , ">=1.0.0 <2.0.0" )
if err != nil {
require . NoError ( t , err )
t . Fatalf ( "unexpected error: %v" , err )
require . Equal ( t , "1.0.0" , got )
}
if got != "1.0.0" {
t . Fatalf ( "expected '1.0.0', got %q" , got )
}
}
}
func TestGetTagMatchingVersionOrConstraint_OrderMatters_FirstMatchReturned ( t * testing . T ) {
func TestGetTagMatchingVersionOrConstraint_OrderMatters_FirstMatchReturned ( t * testing . T ) {
// Both 1.2.0 and 1.3.0 satisfy >=1.2.0 <2.0.0, but the function returns the first in input order
// Both 1.2.0 and 1.3.0 satisfy >=1.2.0 <2.0.0, but the function returns the first in input order
tags := [ ] string { "1.3.0" , "1.2.0" }
tags := [ ] string { "1.3.0" , "1.2.0" }
got , err := GetTagMatchingVersionOrConstraint ( tags , ">=1.2.0 <2.0.0" )
got , err := GetTagMatchingVersionOrConstraint ( tags , ">=1.2.0 <2.0.0" )
if err != nil {
require . NoError ( t , err )
t . Fatalf ( "unexpected error: %v" , err )
require . Equal ( t , "1.3.0" , got , "first satisfying tag" )
}
if got != "1.3.0" {
t . Fatalf ( "expected '1.3.0' (first satisfying tag), got %q" , got )
}
}
}
func TestGetTagMatchingVersionOrConstraint_ExactMatchHasPrecedence ( t * testing . T ) {
func TestGetTagMatchingVersionOrConstraint_ExactMatchHasPrecedence ( t * testing . T ) {
// Exact match should be returned even if another earlier tag would match the parsed constraint
// Exact match should be returned even if another earlier tag would match the parsed constraint
tags := [ ] string { "1.3.0" , "1.2.3" }
tags := [ ] string { "1.3.0" , "1.2.3" }
got , err := GetTagMatchingVersionOrConstraint ( tags , "1.2.3" )
got , err := GetTagMatchingVersionOrConstraint ( tags , "1.2.3" )
if err != nil {
require . NoError ( t , err )
t . Fatalf ( "unexpected error: %v" , err )
require . Equal ( t , "1.2.3" , got , "expected exact match" )
}
if got != "1.2.3" {
t . Fatalf ( "expected exact match '1.2.3', got %q" , got )
}
}
}