github.com/drone/runner-go@v1.12.0/environ/calver_test.go (about)

     1  // Copyright 2019 Drone.IO Inc. All rights reserved.
     2  // Use of this source code is governed by the Polyform License
     3  // that can be found in the LICENSE file.
     4  
     5  package environ
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/google/go-cmp/cmp"
    11  )
    12  
    13  func TestCalver(t *testing.T) {
    14  	a := calversions("v19.1.0-beta.20190318")
    15  	b := map[string]string{
    16  		"DRONE_CALVER":             "19.1.0-beta.20190318",
    17  		"DRONE_CALVER_MAJOR":       "19",
    18  		"DRONE_CALVER_MAJOR_MINOR": "19.1",
    19  		"DRONE_CALVER_MINOR":       "1",
    20  		"DRONE_CALVER_MICRO":       "0",
    21  		"DRONE_CALVER_SHORT":       "19.1.0",
    22  		"DRONE_CALVER_MODIFIER":    "beta.20190318",
    23  	}
    24  	if diff := cmp.Diff(a, b); diff != "" {
    25  		t.Errorf("Unexpected calver variables")
    26  		t.Log(diff)
    27  	}
    28  }
    29  
    30  func TestCalverAlternate(t *testing.T) {
    31  	a := calversions("2019.01.0002")
    32  	b := map[string]string{
    33  		"DRONE_CALVER":             "2019.01.0002",
    34  		"DRONE_CALVER_MAJOR_MINOR": "2019.01",
    35  		"DRONE_CALVER_MAJOR":       "2019",
    36  		"DRONE_CALVER_MINOR":       "01",
    37  		"DRONE_CALVER_MICRO":       "0002",
    38  		"DRONE_CALVER_SHORT":       "2019.01.0002",
    39  	}
    40  	if diff := cmp.Diff(a, b); diff != "" {
    41  		t.Errorf("Unexpected calver variables")
    42  		t.Log(diff)
    43  	}
    44  }
    45  
    46  func TestCalver_Invalid(t *testing.T) {
    47  	tests := []string{
    48  		"1.2.3",
    49  		"1.2",
    50  		"1",
    51  		"0.12",
    52  		"0.12.1",
    53  	}
    54  	for _, s := range tests {
    55  		envs := calversions(s)
    56  		if len(envs) != 0 {
    57  			t.Errorf("Expect invalid calversion: %s", s)
    58  		}
    59  	}
    60  }
    61  
    62  func TestCalverParser(t *testing.T) {
    63  	tests := []struct {
    64  		s string
    65  		v *calver
    66  	}{
    67  		{"09.01.02", &calver{"09", "01", "02", ""}},
    68  		{"2009.01.02", &calver{"2009", "01", "02", ""}},
    69  		{"2009.1.2", &calver{"2009", "1", "2", ""}},
    70  		{"09.1.2", &calver{"09", "1", "2", ""}},
    71  		{"9.1.2", &calver{"9", "1", "2", ""}},
    72  		{"v19.1.0-beta.20190318", &calver{"19", "1", "0", "beta.20190318"}},
    73  
    74  		// invalid values
    75  		{"foo.bar.baz", nil},
    76  		{"foo.bar", nil},
    77  		{"foo.1", nil},
    78  		{"foo", nil},
    79  		{"1", nil},
    80  		{"1.foo", nil},
    81  	}
    82  
    83  	for _, test := range tests {
    84  		got, want := parseCalver(test.s), test.v
    85  		if diff := cmp.Diff(got, want); diff != "" {
    86  			t.Errorf("Unexpected calver %s", test.s)
    87  			t.Log(diff)
    88  		}
    89  	}
    90  }