github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/util/version/version_test.go (about)

     1  // Copyright 2018 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package version
    12  
    13  import (
    14  	"fmt"
    15  	"testing"
    16  )
    17  
    18  func TestGetters(t *testing.T) {
    19  	v, err := Parse("v1.2.3-beta+md")
    20  	if err != nil {
    21  		t.Fatal(err)
    22  	}
    23  	str := fmt.Sprintf(
    24  		"%d %d %d %s %s", v.Major(), v.Minor(), v.Patch(), v.PreRelease(), v.Metadata(),
    25  	)
    26  	exp := "1 2 3 beta md"
    27  	if str != exp {
    28  		t.Errorf("got '%s', expected '%s'", str, exp)
    29  	}
    30  }
    31  
    32  func TestValid(t *testing.T) {
    33  	testData := []string{
    34  		"v0.0.0",
    35  		"v0.0.1",
    36  		"v0.1.0",
    37  		"v1.0.0",
    38  		"v1.0.0-alpha",
    39  		"v1.0.0-beta.20190101",
    40  		"v1.0.0-rc1-with-hyphen",
    41  		"v1.0.0-rc2.dot.dot",
    42  		"v1.2.3+metadata",
    43  		"v1.2.3+metadata-with-hyphen",
    44  		"v1.2.3+metadata.with.dots",
    45  		"v1.1.2-beta.20190101+metadata",
    46  		"v1.2.3-rc1-with-hyphen+metadata-with-hyphen",
    47  	}
    48  	for _, str := range testData {
    49  		v, err := Parse(str)
    50  		if err != nil {
    51  			t.Errorf("%s: %s", str, err)
    52  		}
    53  		if v.String() != str {
    54  			t.Errorf("%s roundtripped to %s", str, v.String())
    55  		}
    56  	}
    57  }
    58  
    59  func TestInvalid(t *testing.T) {
    60  	testData := []string{
    61  		"v1",
    62  		"v1.2",
    63  		"v1.2-beta",
    64  		"v1x2.3",
    65  		"v1.2x3",
    66  		"1.0.0",
    67  		" v1.0.0",
    68  		"v1.0.0  ",
    69  		"v1.2.beta",
    70  		"v1.2-beta",
    71  		"v1.2.3.beta",
    72  		"v1.2.3-beta$",
    73  		"v1.2.3-bet;a",
    74  		"v1.2.3+metadata%",
    75  		"v01.2.3",
    76  		"v1.02.3",
    77  		"v1.2.03",
    78  	}
    79  	for _, str := range testData {
    80  		if _, err := Parse(str); err == nil {
    81  			t.Errorf("expected error for %s", str)
    82  		}
    83  	}
    84  }
    85  
    86  func TestCompare(t *testing.T) {
    87  	testData := []struct {
    88  		a, b string
    89  		cmp  int
    90  	}{
    91  		{"v1.0.0", "v1.0.0", 0},
    92  		{"v1.0.0", "v1.0.1", -1},
    93  		{"v1.2.3", "v1.3.0", -1},
    94  		{"v1.2.3", "v2.0.0", -1},
    95  		{"v1.0.0+metadata", "v1.0.0", 0},
    96  		{"v1.0.0+metadata", "v1.0.0+other.metadata", 0},
    97  		{"v1.0.1+metadata", "v1.0.0+other.metadata", +1},
    98  		{"v1.0.0", "v1.0.0-alpha", +1},
    99  		{"v1.0.0", "v1.0.0-rc2", +1},
   100  		{"v1.0.0-alpha", "v1.0.0-beta", -1},
   101  		{"v1.0.0-beta", "v1.0.0-rc.2", -1},
   102  		{"v1.0.0-rc.2", "v1.0.0-rc.10", -1},
   103  		{"v1.0.1", "v1.0.0-alpha", +1},
   104  
   105  		// Tests below taken from coreos/go-semver, which carries the following
   106  		// copyright:
   107  		//
   108  		// Copyright 2013-2015 CoreOS, Inc.
   109  		// Copyright 2018 The Cockroach Authors.
   110  		//
   111  		// Licensed under the Apache License, Version 2.0 (the "License");
   112  		// you may not use this file except in compliance with the License.
   113  		// You may obtain a copy of the License at
   114  		//
   115  		//     http://www.apache.org/licenses/LICENSE-2.0
   116  		//
   117  		// Unless required by applicable law or agreed to in writing, software
   118  		// distributed under the License is distributed on an "AS IS" BASIS,
   119  		// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   120  		// See the License for the specific language governing permissions and
   121  		// limitations under the License.
   122  		{"v0.0.0", "v0.0.0-foo", +1},
   123  		{"v0.0.1", "v0.0.0", +1},
   124  		{"v1.0.0", "v0.9.9", +1},
   125  		{"v0.10.0", "v0.9.0", +1},
   126  		{"v0.99.0", "v0.10.0", +1},
   127  		{"v2.0.0", "v1.2.3", +1},
   128  		{"v0.0.0", "v0.0.0-foo", +1},
   129  		{"v0.0.1", "v0.0.0", +1},
   130  		{"v1.0.0", "v0.9.9", +1},
   131  		{"v0.10.0", "v0.9.0", +1},
   132  		{"v0.99.0", "v0.10.0", +1},
   133  		{"v2.0.0", "v1.2.3", +1},
   134  		{"v0.0.0", "v0.0.0-foo", +1},
   135  		{"v0.0.1", "v0.0.0", +1},
   136  		{"v1.0.0", "v0.9.9", +1},
   137  		{"v0.10.0", "v0.9.0", +1},
   138  		{"v0.99.0", "v0.10.0", +1},
   139  		{"v2.0.0", "v1.2.3", +1},
   140  		{"v1.2.3", "v1.2.3-asdf", +1},
   141  		{"v1.2.3", "v1.2.3-4", +1},
   142  		{"v1.2.3", "v1.2.3-4-foo", +1},
   143  		{"v1.2.3-5-foo", "v1.2.3-5", +1},
   144  		{"v1.2.3-5", "v1.2.3-4", +1},
   145  		{"v1.2.3-5-foo", "v1.2.3-5-Foo", +1},
   146  		{"v3.0.0", "v2.7.2+asdf", +1},
   147  		{"v3.0.0+foobar", "v2.7.2", +1},
   148  		{"v1.2.3-a.10", "v1.2.3-a.5", +1},
   149  		{"v1.2.3-a.b", "v1.2.3-a.5", +1},
   150  		{"v1.2.3-a.b", "v1.2.3-a", +1},
   151  		{"v1.2.3-a.b.c.10.d.5", "v1.2.3-a.b.c.5.d.100", +1},
   152  		{"v1.0.0", "v1.0.0-rc.1", +1},
   153  		{"v1.0.0-rc.2", "v1.0.0-rc.1", +1},
   154  		{"v1.0.0-rc.1", "v1.0.0-beta.11", +1},
   155  		{"v1.0.0-beta.11", "v1.0.0-beta.2", +1},
   156  		{"v1.0.0-beta.2", "v1.0.0-beta", +1},
   157  		{"v1.0.0-beta", "v1.0.0-alpha.beta", +1},
   158  		{"v1.0.0-alpha.beta", "v1.0.0-alpha.1", +1},
   159  		{"v1.0.0-alpha.1", "v1.0.0-alpha", +1},
   160  	}
   161  	for _, tc := range testData {
   162  		a, err := Parse(tc.a)
   163  		if err != nil {
   164  			t.Fatal(err)
   165  		}
   166  		b, err := Parse(tc.b)
   167  		if err != nil {
   168  			t.Fatal(err)
   169  		}
   170  		if cmp := a.Compare(b); cmp != tc.cmp {
   171  			t.Errorf("'%s' vs '%s': expected %d, got %d", tc.a, tc.b, tc.cmp, cmp)
   172  		}
   173  		if cmp := b.Compare(a); cmp != -tc.cmp {
   174  			t.Errorf("'%s' vs '%s': expected %d, got %d", tc.b, tc.a, -tc.cmp, cmp)
   175  		}
   176  	}
   177  }