sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/plugins/golang/go_version_test.go (about)

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package golang
    18  
    19  import (
    20  	"sort"
    21  
    22  	. "github.com/onsi/ginkgo/v2"
    23  	. "github.com/onsi/gomega"
    24  )
    25  
    26  var _ = Describe("GoVersion", func() {
    27  	Context("parse", func() {
    28  		var v GoVersion
    29  
    30  		BeforeEach(func() {
    31  			v = GoVersion{}
    32  		})
    33  
    34  		DescribeTable("should succeed for valid versions",
    35  			func(version string, expected GoVersion) {
    36  				Expect(v.parse(version)).NotTo(HaveOccurred())
    37  				Expect(v.major).To(Equal(expected.major))
    38  				Expect(v.minor).To(Equal(expected.minor))
    39  				Expect(v.patch).To(Equal(expected.patch))
    40  				Expect(v.prerelease).To(Equal(expected.prerelease))
    41  			},
    42  			Entry("for minor release", "go1.15", GoVersion{
    43  				major: 1,
    44  				minor: 15,
    45  			}),
    46  			Entry("for patch release", "go1.15.1", GoVersion{
    47  				major: 1,
    48  				minor: 15,
    49  				patch: 1,
    50  			}),
    51  			Entry("for alpha release", "go1.15alpha1", GoVersion{
    52  				major:      1,
    53  				minor:      15,
    54  				prerelease: "alpha1",
    55  			}),
    56  			Entry("for beta release", "go1.15beta1", GoVersion{
    57  				major:      1,
    58  				minor:      15,
    59  				prerelease: "beta1",
    60  			}),
    61  			Entry("for release candidate", "go1.15rc1", GoVersion{
    62  				major:      1,
    63  				minor:      15,
    64  				prerelease: "rc1",
    65  			}),
    66  		)
    67  
    68  		DescribeTable("should fail for invalid versions",
    69  			func(version string) { Expect(v.parse(version)).To(HaveOccurred()) },
    70  			Entry("for invalid prefix", "g1.15"),
    71  			Entry("for missing major version", "go.15"),
    72  			Entry("for missing minor version", "go1."),
    73  			Entry("for patch and prerelease version", "go1.15.1rc1"),
    74  			Entry("for invalid major version", "goa.15"),
    75  			Entry("for invalid minor version", "go1.a"),
    76  			Entry("for invalid patch version", "go1.15.a"),
    77  		)
    78  	})
    79  
    80  	Context("Compare", func() {
    81  		// Test Compare() by sorting a list.
    82  		var (
    83  			versions = []GoVersion{
    84  				{major: 1, minor: 15, prerelease: "rc2"},
    85  				{major: 1, minor: 15, patch: 1},
    86  				{major: 1, minor: 16},
    87  				{major: 1, minor: 15, prerelease: "beta1"},
    88  				{major: 1, minor: 15, prerelease: "alpha2"},
    89  				{major: 2, minor: 0},
    90  				{major: 1, minor: 15, prerelease: "alpha1"},
    91  				{major: 1, minor: 13},
    92  				{major: 1, minor: 15, prerelease: "rc1"},
    93  				{major: 1, minor: 15},
    94  				{major: 1, minor: 15, patch: 2},
    95  				{major: 1, minor: 14},
    96  				{major: 1, minor: 15, prerelease: "beta2"},
    97  				{major: 0, minor: 123},
    98  			}
    99  
   100  			sortedVersions = []GoVersion{
   101  				{major: 0, minor: 123},
   102  				{major: 1, minor: 13},
   103  				{major: 1, minor: 14},
   104  				{major: 1, minor: 15, prerelease: "alpha1"},
   105  				{major: 1, minor: 15, prerelease: "alpha2"},
   106  				{major: 1, minor: 15, prerelease: "beta1"},
   107  				{major: 1, minor: 15, prerelease: "beta2"},
   108  				{major: 1, minor: 15, prerelease: "rc1"},
   109  				{major: 1, minor: 15, prerelease: "rc2"},
   110  				{major: 1, minor: 15},
   111  				{major: 1, minor: 15, patch: 1},
   112  				{major: 1, minor: 15, patch: 2},
   113  				{major: 1, minor: 16},
   114  				{major: 2, minor: 0},
   115  			}
   116  		)
   117  
   118  		It("sorts a valid list of versions correctly", func() {
   119  			sort.Slice(versions, func(i int, j int) bool {
   120  				return versions[i].Compare(versions[j]) == -1
   121  			})
   122  			Expect(versions).To(Equal(sortedVersions))
   123  		})
   124  	})
   125  })
   126  
   127  var _ = Describe("checkGoVersion", func() {
   128  	goVerMin := MustParse("go1.13")
   129  	goVerMax := MustParse("go2.0alpha1")
   130  
   131  	DescribeTable("should return no error for supported go versions",
   132  		func(version string) { Expect(checkGoVersion(version, goVerMin, goVerMax)).To(Succeed()) },
   133  		Entry("for go 1.13", "go1.13"),
   134  		Entry("for go 1.13.1", "go1.13.1"),
   135  		Entry("for go 1.13.2", "go1.13.2"),
   136  		Entry("for go 1.13.3", "go1.13.3"),
   137  		Entry("for go 1.13.4", "go1.13.4"),
   138  		Entry("for go 1.13.5", "go1.13.5"),
   139  		Entry("for go 1.13.6", "go1.13.6"),
   140  		Entry("for go 1.13.7", "go1.13.7"),
   141  		Entry("for go 1.13.8", "go1.13.8"),
   142  		Entry("for go 1.13.9", "go1.13.9"),
   143  		Entry("for go 1.13.10", "go1.13.10"),
   144  		Entry("for go 1.13.11", "go1.13.11"),
   145  		Entry("for go 1.13.12", "go1.13.12"),
   146  		Entry("for go 1.13.13", "go1.13.13"),
   147  		Entry("for go 1.13.14", "go1.13.14"),
   148  		Entry("for go 1.13.15", "go1.13.15"),
   149  		Entry("for go 1.14beta1", "go1.14beta1"),
   150  		Entry("for go 1.14rc1", "go1.14rc1"),
   151  		Entry("for go 1.14", "go1.14"),
   152  		Entry("for go 1.14.1", "go1.14.1"),
   153  		Entry("for go 1.14.2", "go1.14.2"),
   154  		Entry("for go 1.14.3", "go1.14.3"),
   155  		Entry("for go 1.14.4", "go1.14.4"),
   156  		Entry("for go 1.14.5", "go1.14.5"),
   157  		Entry("for go 1.14.6", "go1.14.6"),
   158  		Entry("for go 1.14.7", "go1.14.7"),
   159  		Entry("for go 1.14.8", "go1.14.8"),
   160  		Entry("for go 1.14.9", "go1.14.9"),
   161  		Entry("for go 1.14.10", "go1.14.10"),
   162  		Entry("for go 1.14.11", "go1.14.11"),
   163  		Entry("for go 1.14.12", "go1.14.12"),
   164  		Entry("for go 1.14.13", "go1.14.13"),
   165  		Entry("for go 1.14.14", "go1.14.14"),
   166  		Entry("for go 1.14.15", "go1.14.15"),
   167  		Entry("for go 1.15beta1", "go1.15beta1"),
   168  		Entry("for go 1.15rc1", "go1.15rc1"),
   169  		Entry("for go 1.15rc2", "go1.15rc2"),
   170  		Entry("for go 1.15", "go1.15"),
   171  		Entry("for go 1.15.1", "go1.15.1"),
   172  		Entry("for go 1.15.2", "go1.15.2"),
   173  		Entry("for go 1.15.3", "go1.15.3"),
   174  		Entry("for go 1.15.4", "go1.15.4"),
   175  		Entry("for go 1.15.5", "go1.15.5"),
   176  		Entry("for go 1.15.6", "go1.15.6"),
   177  		Entry("for go 1.15.7", "go1.15.7"),
   178  		Entry("for go 1.15.8", "go1.15.8"),
   179  		Entry("for go 1.16", "go1.16"),
   180  		Entry("for go 1.16.1", "go1.16.1"),
   181  		Entry("for go 1.16.2", "go1.16.2"),
   182  		Entry("for go 1.16.3", "go1.16.3"),
   183  		Entry("for go 1.16.4", "go1.16.4"),
   184  		Entry("for go 1.16.5", "go1.16.5"),
   185  		Entry("for go 1.16.6", "go1.16.6"),
   186  		Entry("for go 1.16.7", "go1.16.7"),
   187  		Entry("for go 1.16.8", "go1.16.8"),
   188  		Entry("for go 1.16.9", "go1.16.9"),
   189  		Entry("for go 1.16.10", "go1.16.10"),
   190  		Entry("for go 1.16.11", "go1.16.11"),
   191  		Entry("for go 1.16.12", "go1.16.12"),
   192  		Entry("for go 1.17.1", "go1.17.1"),
   193  		Entry("for go 1.17.2", "go1.17.2"),
   194  		Entry("for go 1.17.3", "go1.17.3"),
   195  		Entry("for go 1.17.4", "go1.17.4"),
   196  		Entry("for go 1.17.5", "go1.17.5"),
   197  		Entry("for go 1.18.1", "go1.18.1"),
   198  		Entry("for go.1.19", "go1.19"),
   199  		Entry("for go.1.19.1", "go1.19.1"),
   200  		Entry("for go.1.20", "go1.20"),
   201  		Entry("for go.1.21", "go1.21"),
   202  	)
   203  
   204  	DescribeTable("should return an error for non-supported go versions",
   205  		func(version string) { Expect(checkGoVersion(version, goVerMin, goVerMax)).NotTo(Succeed()) },
   206  		Entry("for invalid go versions", "go"),
   207  		Entry("for go 1.13beta1", "go1.13beta1"),
   208  		Entry("for go 1.13rc1", "go1.13rc1"),
   209  		Entry("for go 1.13rc2", "go1.13rc2"),
   210  		Entry("for go 2.0alpha1", "go2.0alpha1"),
   211  		Entry("for go 2.0.0", "go2.0.0"),
   212  	)
   213  })