Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testscript: add build:tag condition #150

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 echo 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'
48 changes: 27 additions & 21 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,32 +573,37 @@ func (ts *TestScript) applyScriptUpdates() {

// condition reports whether the given condition is satisfied.
func (ts *TestScript) condition(cond string) (bool, error) {
switch cond {
case "short":
switch {
case cond == "short":
return testing.Short(), nil
case "net":
case cond == "net":
return testenv.HasExternalNetwork(), nil
case "link":
case cond == "link":
return testenv.HasLink(), nil
case "symlink":
case cond == "symlink":
return testenv.HasSymlink(), nil
case runtime.GOOS, runtime.GOARCH:
return true, nil
default:
if imports.KnownArch[cond] || imports.KnownOS[cond] {
return false, nil
}
if strings.HasPrefix(cond, "exec:") {
prog := cond[len("exec:"):]
ok := execCache.Do(prog, func() interface{} {
_, err := execpath.Look(prog, ts.Getenv)
return err == nil
}).(bool)
return ok, nil
}
if ts.params.Condition != nil {
return ts.params.Condition(cond)
case imports.KnownOS[cond]:
return cond == runtime.GOOS, nil
case imports.KnownArch[cond]:
return cond == runtime.GOARCH, nil
case strings.HasPrefix(cond, "exec:"):
prog := cond[len("exec:"):]
ok := execCache.Do(prog, func() interface{} {
_, err := execpath.Look(prog, ts.Getenv)
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:
ts.Fatalf("unknown condition %q", cond)
panic("unreachable")
}
Expand Down