gitlab.com/Raven-IO/raven-delve@v1.22.4/pkg/goversion/version_test.go (about)

     1  package goversion
     2  
     3  import (
     4  	"runtime"
     5  	"testing"
     6  )
     7  
     8  func parseVer(t *testing.T, verStr string) GoVersion {
     9  	pver, ok := Parse(verStr)
    10  	if !ok {
    11  		t.Fatalf("Could not parse version string <%s>", verStr)
    12  	}
    13  	return pver
    14  }
    15  
    16  func versionAfterOrEqual(t *testing.T, verStr string, ver GoVersion) {
    17  	t.Helper()
    18  	pver := parseVer(t, verStr)
    19  	if !pver.AfterOrEqual(ver) {
    20  		t.Fatalf("Version <%s> parsed as %v not after %v", verStr, pver, ver)
    21  	}
    22  	t.Logf("version string <%s> → %v", verStr, ver)
    23  }
    24  
    25  func versionAfterOrEqual2(t *testing.T, verStr1, verStr2 string) {
    26  	t.Helper()
    27  	pver1 := parseVer(t, verStr1)
    28  	pver2 := parseVer(t, verStr2)
    29  	if !pver1.AfterOrEqual(pver2) {
    30  		t.Fatalf("Version <%s> %#v not after or equal to <%s> %#v", verStr1, pver1, verStr2, pver2)
    31  	}
    32  }
    33  
    34  func versionEqual(t *testing.T, verStr string, ver GoVersion) {
    35  	t.Helper()
    36  	pver := parseVer(t, verStr)
    37  	if pver != ver {
    38  		t.Fatalf("Version <%s> parsed as %v not equal to %v", verStr, pver, ver)
    39  	}
    40  	t.Logf("version string <%s> → %v", verStr, ver)
    41  }
    42  
    43  func TestParseVersionStringAfterOrEqual(t *testing.T) {
    44  	versionAfterOrEqual(t, "go1.4", GoVersion{1, 4, 0, "", ""})
    45  	versionAfterOrEqual(t, "go1.5.0", GoVersion{1, 5, 0, "", ""})
    46  	versionAfterOrEqual(t, "go1.4.2", GoVersion{1, 4, 2, "", ""})
    47  	versionAfterOrEqual(t, "go1.5beta2", GoVersion{1, 5, betaRev(2), "", ""})
    48  	versionAfterOrEqual(t, "go1.5rc2", GoVersion{1, 5, rcRev(2), "", ""})
    49  	versionAfterOrEqual(t, "go1.6.1 (appengine-1.9.37)", GoVersion{1, 6, 1, "", ""})
    50  	versionAfterOrEqual(t, "go1.8.1.typealias", GoVersion{1, 6, 1, "", ""})
    51  	versionAfterOrEqual(t, "go1.8b1", GoVersion{1, 8, 0, "", ""})
    52  	versionAfterOrEqual(t, "go1.16.4b7", GoVersion{1, 16, 4, "", ""})
    53  	ver, ok := Parse("devel +17efbfc Tue Jul 28 17:39:19 2015 +0000 linux/amd64")
    54  	if !ok {
    55  		t.Fatalf("Could not parse devel version string")
    56  	}
    57  	if !ver.IsDevel() {
    58  		t.Fatalf("Devel version string not correctly recognized")
    59  	}
    60  
    61  	versionAfterOrEqual2(t, "go1.16", "go1.16b1")
    62  	versionAfterOrEqual2(t, "go1.16", "go1.16rc1")
    63  	versionAfterOrEqual2(t, "go1.16rc1", "go1.16beta1")
    64  	versionAfterOrEqual2(t, "go1.16beta2", "go1.16beta1")
    65  	versionAfterOrEqual2(t, "go1.16rc10", "go1.16rc8")
    66  }
    67  
    68  func TestParseVersionStringEqual(t *testing.T) {
    69  	versionEqual(t, "go1.4", GoVersion{1, 4, 0, "", ""})
    70  	versionEqual(t, "go1.5.0", GoVersion{1, 5, 0, "", ""})
    71  	versionEqual(t, "go1.4.2", GoVersion{1, 4, 2, "", ""})
    72  	versionEqual(t, "go1.5beta2", GoVersion{1, 5, betaRev(2), "", ""})
    73  	versionEqual(t, "go1.5rc2", GoVersion{1, 5, rcRev(2), "", ""})
    74  	versionEqual(t, "go1.6.1 (appengine-1.9.37)", GoVersion{1, 6, 1, "", ""})
    75  	versionEqual(t, "go1.8.1.typealias", GoVersion{1, 8, 1, "typealias", ""})
    76  	versionEqual(t, "go1.8b1", GoVersion{1, 8, 0, "", ""})
    77  	versionEqual(t, "go1.16.4b7", GoVersion{1, 16, 4, "", ""})
    78  	versionEqual(t, "go1.21.1-something", GoVersion{1, 21, 1, "", "something"})
    79  	versionEqual(t, "devel +17efbfc Tue Jul 28 17:39:19 2015 +0000 linux/amd64", GoVersion{Major: -1})
    80  }
    81  
    82  func TestRoundtrip(t *testing.T) {
    83  	for _, verStr := range []string{
    84  		"go1.4",
    85  		"go1.4.2",
    86  		"go1.5beta2",
    87  		"go1.5rc2",
    88  		"go1.8.1.typealias",
    89  		"go1.21.1-something",
    90  		"go1.21.0",
    91  	} {
    92  		pver := parseVer(t, verStr)
    93  		if pver.String() != verStr {
    94  			t.Fatalf("roundtrip mismatch <%s> -> %#v -> <%s>", verStr, pver, pver.String())
    95  		}
    96  	}
    97  }
    98  
    99  func TestInstalled(t *testing.T) {
   100  	installedVersion, ok := Installed()
   101  	if !ok {
   102  		t.Fatalf("could not parse output of go version")
   103  	}
   104  	runtimeVersion, ok := Parse(runtime.Version())
   105  	if !ok {
   106  		t.Fatalf("could not parse output of runtime.Version() %q", runtime.Version())
   107  	}
   108  
   109  	t.Logf("installed: %v", installedVersion)
   110  	t.Logf("runtime: %v", runtimeVersion)
   111  
   112  	if installedVersion != runtimeVersion {
   113  		t.Fatalf("version mismatch %#v %#v", installedVersion, runtimeVersion)
   114  	}
   115  }