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 Jan 12, 2022
1 parent 2247aee commit a3987ea
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
8 changes: 8 additions & 0 deletions testscript/doc.go
Expand Up @@ -105,6 +105,14 @@ 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. Note that the "build:" prefix
distinguishes this set of conditions from the conditions recognized
by the gotooltest package - that package has no "build:" prefix and
guards based on whether the go tool supports a given tag, but the
"build:" tag guards based on whether the binary that's imported the
testscript package supports the tag.
A condition can be negated: [!short] means to run the rest of the line
when testing.Short() is false.
Expand Down
9 changes: 9 additions & 0 deletions testscript/testdata/build.txt
@@ -0,0 +1,9 @@
[!exec:sh] skip 'sh not found in $PATH'

# test that the build:tag condition works for set build tags
[build:go1.16] exec cat ok
stdout ok
[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 @@ -592,6 +593,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 a3987ea

Please sign in to comment.