Skip to content

Commit

Permalink
testscript: add go:version condition
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Dec 15, 2021
1 parent 65aafbb commit 99120a2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions testscript/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ should only run when the condition is satisfied. The predefined conditions are:
- [link] for whether the OS has hard link support
- [symlink] for whether the OS has symbolic link support
- [exec:prog] for whether prog is available for execution (found by exec.LookPath)
- [go:version] for whether the Go version is at least the given version
A condition can be negated: [!short] means to run the rest of the line
when testing.Short() is false.
Expand Down
8 changes: 8 additions & 0 deletions testscript/testdata/goversion.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[!exec:sh] skip 'sh not found in $PATH'

# test that the go: condition works for minimum supported versions of Go
[go:1.16] exec sh -c 'exit 0'
[go:1.17.1] exec sh -c 'exit 0'

# test that the go:condition works for future versions of Go
[go:2] exec sh -c 'exit 1'
13 changes: 13 additions & 0 deletions testscript/testscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/rogpeppe/go-internal/imports"
"github.com/rogpeppe/go-internal/internal/os/execpath"
"github.com/rogpeppe/go-internal/par"
"github.com/rogpeppe/go-internal/semver"
"github.com/rogpeppe/go-internal/testenv"
"github.com/rogpeppe/go-internal/txtar"
)
Expand Down Expand Up @@ -572,6 +573,18 @@ func (ts *TestScript) condition(cond string) (bool, error) {
return err == nil
}).(bool)
return ok, nil
case strings.HasPrefix(cond, "go:"):
runtimeVersion := runtime.Version()
if !strings.HasPrefix(runtimeVersion, "go") {
// If the runtime version does not start with "go", then it is a
// commit hash and date at the time of the build, so we have no way
// to know if the runtime version is greater than or equal to the
// minimum version. So, conservatively assume that it is not.
return false, nil
}
goVersion := "v" + strings.TrimPrefix(runtimeVersion, "go")
minVersion := "v" + strings.TrimPrefix(cond, "go:")
return semver.Compare(goVersion, minVersion) >= 0, nil
case ts.params.Condition != nil:
return ts.params.Condition(cond)
default:
Expand Down

0 comments on commit 99120a2

Please sign in to comment.