Skip to content

Commit

Permalink
testscript: add build:tag condition
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Dec 15, 2021
1 parent f883332 commit 157e235
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
2 changes: 2 additions & 0 deletions testscript/doc.go
Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions 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'
9 changes: 9 additions & 0 deletions testscript/testscript.go
Expand Up @@ -12,6 +12,7 @@ import (
"context"
"flag"
"fmt"
"go/build"
"io/ioutil"
"os"
"os/exec"
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 157e235

Please sign in to comment.