Skip to content

Commit

Permalink
Go: Make toolchain functions resilient to different version formats
Browse files Browse the repository at this point in the history
  • Loading branch information
mbg committed May 8, 2024
1 parent a8d87ea commit 97b9533
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
1 change: 1 addition & 0 deletions go/extractor/toolchain/BUILD.bazel

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions go/extractor/toolchain/toolchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var goVersions = map[string]struct{}{}

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

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

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

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

Expand Down
42 changes: 39 additions & 3 deletions go/extractor/toolchain/toolchain_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package toolchain

import "testing"
import (
"testing"

"github.com/github/codeql-go/extractor/util"
)

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

func TestHasGoVersion(t *testing.T) {
if HasGoVersion("1.21") {
t.Error("Expected HasGoVersion(\"1.21\") to be false, but got true")
versions := []string{"1.21", "v1.22", "1.22.3", "v1.21rc4"}

// All versions should be unknown.
for _, version := range versions {
if HasGoVersion(version) {
t.Errorf("Expected HasGoVersion(\"%s\") to be false, but got true", version)
}

if HasGoVersion(util.FormatSemVer(version)) {
t.Errorf("Expected HasGoVersion(\"%s\") to be false, but got true", util.FormatSemVer(version))
}

if HasGoVersion(util.UnformatSemVer(version)) {
t.Errorf("Expected HasGoVersion(\"%s\") to be false, but got true", util.UnformatSemVer(version))
}

// Add the version in preparation for the next part of the test.
addGoVersion(version)
}

// Now we should have all of the versions.
for _, version := range versions {
if !HasGoVersion(version) {
t.Errorf("Expected HasGoVersion(\"%s\") to be true, but got false", version)
}

if !HasGoVersion(util.FormatSemVer(version)) {
t.Errorf("Expected HasGoVersion(\"%s\") to be true, but got false", util.FormatSemVer(version))
}

if !HasGoVersion(util.UnformatSemVer(version)) {
t.Errorf("Expected HasGoVersion(\"%s\") to be true, but got false", util.UnformatSemVer(version))
}
}

}

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

0 comments on commit 97b9533

Please sign in to comment.