Skip to content

Commit 97b9533

Browse files
committed
Go: Make toolchain functions resilient to different version formats
1 parent a8d87ea commit 97b9533

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

go/extractor/toolchain/BUILD.bazel

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/extractor/toolchain/toolchain.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var goVersions = map[string]struct{}{}
2525

2626
// Adds an entry to the set of installed Go versions for the normalised `version` number.
2727
func addGoVersion(version string) {
28-
goVersions[semver.Canonical("v"+version)] = struct{}{}
28+
goVersions[semver.Canonical(util.FormatSemVer(version))] = struct{}{}
2929
}
3030

3131
// Returns the current Go version as returned by 'go version', e.g. go1.14.4
@@ -58,7 +58,7 @@ func GetEnvGoVersion() string {
5858

5959
// Determines whether, to our knowledge, `version` is available on the current system.
6060
func HasGoVersion(version string) bool {
61-
_, found := goVersions[semver.Canonical("v"+version)]
61+
_, found := goVersions[semver.Canonical(util.FormatSemVer(version))]
6262
return found
6363
}
6464

@@ -72,7 +72,7 @@ func InstallVersion(workingDir string, version string) bool {
7272
// Construct a command to invoke `go version` with `GOTOOLCHAIN=go1.N.0` to give
7373
// Go a valid toolchain version to download the toolchain we need; subsequent commands
7474
// should then work even with an invalid version that's still in `go.mod`
75-
toolchainArg := "GOTOOLCHAIN=go" + semver.Canonical("v" + version)[1:]
75+
toolchainArg := "GOTOOLCHAIN=go" + semver.Canonical(util.FormatSemVer(version))[1:]
7676
versionCmd := Version()
7777
versionCmd.Dir = workingDir
7878
versionCmd.Env = append(os.Environ(), toolchainArg)
@@ -112,9 +112,9 @@ func GoVersionToSemVer(goVersion string) string {
112112
// which is compatible with the SemVer specification
113113
rcIndex := strings.Index(goVersion, "rc")
114114
if rcIndex != -1 {
115-
return semver.Canonical("v"+goVersion[:rcIndex]) + "-" + goVersion[rcIndex:]
115+
return semver.Canonical(util.FormatSemVer(goVersion[:rcIndex])) + "-" + goVersion[rcIndex:]
116116
} else {
117-
return semver.Canonical("v" + goVersion)
117+
return semver.Canonical(util.FormatSemVer(goVersion))
118118
}
119119
}
120120

go/extractor/toolchain/toolchain_test.go

+39-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package toolchain
22

3-
import "testing"
3+
import (
4+
"testing"
5+
6+
"github.com/github/codeql-go/extractor/util"
7+
)
48

59
func TestParseGoVersion(t *testing.T) {
610
tests := map[string]string{
@@ -16,9 +20,41 @@ func TestParseGoVersion(t *testing.T) {
1620
}
1721

1822
func TestHasGoVersion(t *testing.T) {
19-
if HasGoVersion("1.21") {
20-
t.Error("Expected HasGoVersion(\"1.21\") to be false, but got true")
23+
versions := []string{"1.21", "v1.22", "1.22.3", "v1.21rc4"}
24+
25+
// All versions should be unknown.
26+
for _, version := range versions {
27+
if HasGoVersion(version) {
28+
t.Errorf("Expected HasGoVersion(\"%s\") to be false, but got true", version)
29+
}
30+
31+
if HasGoVersion(util.FormatSemVer(version)) {
32+
t.Errorf("Expected HasGoVersion(\"%s\") to be false, but got true", util.FormatSemVer(version))
33+
}
34+
35+
if HasGoVersion(util.UnformatSemVer(version)) {
36+
t.Errorf("Expected HasGoVersion(\"%s\") to be false, but got true", util.UnformatSemVer(version))
37+
}
38+
39+
// Add the version in preparation for the next part of the test.
40+
addGoVersion(version)
2141
}
42+
43+
// Now we should have all of the versions.
44+
for _, version := range versions {
45+
if !HasGoVersion(version) {
46+
t.Errorf("Expected HasGoVersion(\"%s\") to be true, but got false", version)
47+
}
48+
49+
if !HasGoVersion(util.FormatSemVer(version)) {
50+
t.Errorf("Expected HasGoVersion(\"%s\") to be true, but got false", util.FormatSemVer(version))
51+
}
52+
53+
if !HasGoVersion(util.UnformatSemVer(version)) {
54+
t.Errorf("Expected HasGoVersion(\"%s\") to be true, but got false", util.UnformatSemVer(version))
55+
}
56+
}
57+
2258
}
2359

2460
func testGoVersionToSemVer(t *testing.T, goVersion string, expectedSemVer string) {

0 commit comments

Comments
 (0)