diff --git a/testscript/doc.go b/testscript/doc.go index 48a02c97..b5a61bd9 100644 --- a/testscript/doc.go +++ b/testscript/doc.go @@ -105,6 +105,8 @@ 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) + - [build:tag] for whether the release build tag is set; user-defined build tags + are not currently supported A condition can be negated: [!short] means to run the rest of the line when testing.Short() is false. diff --git a/testscript/testdata/build.txt b/testscript/testdata/build.txt new file mode 100644 index 00000000..1ea31c7d --- /dev/null +++ b/testscript/testdata/build.txt @@ -0,0 +1,8 @@ +[!exec:sh] skip 'sh not found in $PATH' + +# test that the build:tag condition works for set build tags +[build:go1.16] exec sh -c 'exit 0' +[build:go1.17] exec sh -c 'exit 0' + +# test that the build:tag condition works for unset build tags +[build:go2] exec sh -c 'exit 1' diff --git a/testscript/testscript.go b/testscript/testscript.go index a9382530..8970c94c 100644 --- a/testscript/testscript.go +++ b/testscript/testscript.go @@ -12,6 +12,7 @@ import ( "context" "flag" "fmt" + "go/build" "io/ioutil" "os" "os/exec" @@ -572,6 +573,14 @@ func (ts *TestScript) condition(cond string) (bool, error) { return err == nil }).(bool) return ok, nil + case strings.HasPrefix(cond, "build:"): + tag := cond[len("build:"):] + for _, releaseTag := range build.Default.ReleaseTags { + if releaseTag == tag { + return true, nil + } + } + return false, nil case ts.params.Condition != nil: return ts.params.Condition(cond) default: