github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/helper/constraints/semver/constraints_test.go (about)

     1  package semver
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/go-version"
     7  )
     8  
     9  // This file is a copy of github.com/hashicorp/go-version/constraint_test.go
    10  // with minimal changes to demonstrate differences. Diffing the files should
    11  // illustrate behavior differences in Constraint and version.Constraint.
    12  
    13  func TestNewConstraint(t *testing.T) {
    14  	cases := []struct {
    15  		input string
    16  		count int
    17  		err   bool
    18  	}{
    19  		{">= 1.2", 1, false},
    20  		{"1.0", 1, false},
    21  		{">= 1.x", 0, true},
    22  		{">= 1.2, < 1.0", 2, false},
    23  
    24  		// Out of bounds
    25  		{"11387778780781445675529500000000000000000", 0, true},
    26  
    27  		// Semver only
    28  		{">= 1.0beta1", 0, true},
    29  
    30  		// No pessimistic operator
    31  		{"~> 1.0", 0, true},
    32  	}
    33  
    34  	for _, tc := range cases {
    35  		v, err := NewConstraint(tc.input)
    36  		if tc.err && err == nil {
    37  			t.Fatalf("expected error for input: %s", tc.input)
    38  		} else if !tc.err && err != nil {
    39  			t.Fatalf("error for input %s: %s", tc.input, err)
    40  		}
    41  
    42  		if len(v) != tc.count {
    43  			t.Fatalf("input: %s\nexpected len: %d\nactual: %d",
    44  				tc.input, tc.count, len(v))
    45  		}
    46  	}
    47  }
    48  
    49  func TestConstraintCheck(t *testing.T) {
    50  	cases := []struct {
    51  		constraint string
    52  		version    string
    53  		check      bool
    54  	}{
    55  		{">= 1.0, < 1.2", "1.1.5", true},
    56  		{"< 1.0, < 1.2", "1.1.5", false},
    57  		{"= 1.0", "1.1.5", false},
    58  		{"= 1.0", "1.0.0", true},
    59  		{"1.0", "1.0.0", true},
    60  
    61  		// Assert numbers are *not* compared lexically as in #4729
    62  		{"> 10", "8", false},
    63  
    64  		// Pre-releases are ordered according to Semver v2
    65  		{"> 2.0", "2.1.0-beta", true},
    66  		{"> 2.1.0-a", "2.1.0-beta", true},
    67  		{"> 2.1.0-a", "2.1.1-beta", true},
    68  		{"> 2.0.0", "2.1.0-beta", true},
    69  		{"> 2.1.0-a", "2.1.1", true},
    70  		{"> 2.1.0-a", "2.1.1-beta", true},
    71  		{"> 2.1.0-a", "2.1.0", true},
    72  		{"<= 2.1.0-a", "2.0.0", true},
    73  		{">= 0.6.1", "1.3.0-beta1", true},
    74  		{"> 1.0-beta1", "1.0-rc1", true},
    75  
    76  		// Meta components are ignored according to Semver v2
    77  		{">= 0.6.1", "1.3.0-beta1+ent", true},
    78  		{">= 1.3.0-beta1", "1.3.0-beta1+ent", true},
    79  		{"> 1.3.0-beta1+cgo", "1.3.0-beta1+ent", false},
    80  		{"= 1.3.0-beta1+cgo", "1.3.0-beta1+ent", true},
    81  	}
    82  
    83  	for _, tc := range cases {
    84  		c, err := NewConstraint(tc.constraint)
    85  		if err != nil {
    86  			t.Fatalf("err: %s", err)
    87  		}
    88  
    89  		v, err := version.NewSemver(tc.version)
    90  		if err != nil {
    91  			t.Fatalf("err: %s", err)
    92  		}
    93  
    94  		actual := c.Check(v)
    95  		expected := tc.check
    96  		if actual != expected {
    97  			t.Fatalf("Version: %s\nConstraint: %s\nExpected: %#v",
    98  				tc.version, tc.constraint, expected)
    99  		}
   100  	}
   101  }
   102  
   103  func TestConstraintsString(t *testing.T) {
   104  	cases := []struct {
   105  		constraint string
   106  		result     string
   107  	}{
   108  		{">= 1.0, < 1.2", ""},
   109  	}
   110  
   111  	for _, tc := range cases {
   112  		c, err := NewConstraint(tc.constraint)
   113  		if err != nil {
   114  			t.Fatalf("err: %s", err)
   115  		}
   116  
   117  		actual := c.String()
   118  		expected := tc.result
   119  		if expected == "" {
   120  			expected = tc.constraint
   121  		}
   122  
   123  		if actual != expected {
   124  			t.Fatalf("Constraint: %s\nExpected: %#v\nActual: %s",
   125  				tc.constraint, expected, actual)
   126  		}
   127  	}
   128  }