add test for Go version checking (#140)

Add tests for Go version checking

Fix panic if go version has invalid format

Fixes: #121
Co-authored-by: Daniel Martí <mvdan@mvdan.cc>
pull/141/head
pagran 4 years ago committed by GitHub
parent 25b73afaec
commit 00c1d5b11d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -247,11 +247,11 @@ func goVersionOK() bool {
out, err := exec.Command("go", "version").CombinedOutput()
if err != nil {
fmt.Printf(`Can't get go version: %v
fmt.Fprintf(os.Stderr, `Can't get Go version: %v
This is likely due to go not being installed/setup correctly.
How to install go: https://golang.org/doc/install
How to install Go: https://golang.org/doc/install
`, err)
return false
}
@ -263,25 +263,31 @@ How to install go: https://golang.org/doc/install
if tag == "devel" {
commitAndDate := rawVersion[tagIdx+1:]
// Remove commit hash and architecture from version
date := commitAndDate[strings.IndexByte(commitAndDate, ' ')+1 : strings.LastIndexByte(commitAndDate, ' ')]
startDateIdx := strings.IndexByte(commitAndDate, ' ') + 1
endDateIdx := strings.LastIndexByte(commitAndDate, ' ')
if endDateIdx <= 0 {
fmt.Fprintf(os.Stderr, "Can't recognize devel build timestamp")
return false
}
date := commitAndDate[startDateIdx:endDateIdx]
versionDate, err := time.Parse(gitTimeFormat, date)
if err != nil {
fmt.Printf("Can't recognize devel build timestamp: %v\n", err)
return true
fmt.Fprintf(os.Stderr, "Can't recognize devel build timestamp: %v\n", err)
return false
}
if versionDate.After(minGoVersionDate) {
return true
}
fmt.Printf("You use the old unstable %q Go version, please upgrade Go to %s\n", rawVersion, supportedGoVersions)
fmt.Fprintf(os.Stderr, "You use the old unstable %q Go version, please upgrade Go to %s\n", rawVersion, supportedGoVersions)
return false
}
version := "v" + strings.TrimPrefix(tag, "go")
if semver.Compare(version, minGoVersion) < 0 {
fmt.Printf("Outdated Go version %q is used, please upgrade Go to %s\n", version, supportedGoVersions)
fmt.Fprintf(os.Stderr, "Outdated Go version %q is used, please upgrade Go to %s\n", version, supportedGoVersions)
return false
}
@ -295,7 +301,7 @@ func mainErr(args []string) error {
flagSet.Usage()
case "build", "test":
if !goVersionOK() {
return nil
os.Exit(1)
}
// Split the flags from the package arguments, since we'll need
// to run 'go list' on the same set of packages.

@ -0,0 +1,53 @@
# TODO: Place file direct to .bin directory
cp go.sh .bin/go
cp go.bat .bin/go.bat
chmod 777 .bin/go
# Check incorrect go install
env GO_VERSION=''
! garble build
stderr 'Can''t get Go version'
[short] stop
# Check invalid devel format
env GO_VERSION='go version devel someinvalidversion'
! garble build
stderr 'Can''t recognize devel build timestamp'
# Check invalid devel date
env GO_VERSION='go version devel +afb5fca Sun Sep 99 99:99:99 9999 +0000 linux/amd64'
! garble build
stderr 'Can''t recognize devel build timestamp: parsing time'
# Check invalid old devel version
env GO_VERSION='go version devel +afb5fca Sun Aug 07 00:00:00 2020 +0000 linux/amd64'
! garble build
stderr 'You use the old unstable'
# Check invalid new devel version
env GO_VERSION='go version devel +afb5fca Sun Sep 13 07:54:42 2020 +0000 linux/amd64'
! garble build
! stderr 'You use the old unstable'
# Check old version
env GO_VERSION='go version go1.14 windows/amd64'
! garble build
stderr 'Outdated Go version'
# Check supported version
env GO_VERSION='go version go1.15.2 windows/amd64'
! garble build
! stderr 'Outdated Go version'
-- main.go --
package main
-- go.sh --
#!/bin/sh
[ -z "$GO_VERSION" ] && exit 1 || echo "$GO_VERSION"
-- go.bat --
@echo off
IF DEFINED GO_VERSION (echo %GO_VERSION%) ELSE (exit 1)
Loading…
Cancel
Save