From 2247aeeeda1f474b556fdf456c98c001f3db2edb Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Wed, 15 Dec 2021 20:54:22 +0100 Subject: [PATCH 1/2] testscript: tidy up condition logic --- testscript/testscript.go | 39 ++++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/testscript/testscript.go b/testscript/testscript.go index ea5f51e2..cf9a6724 100644 --- a/testscript/testscript.go +++ b/testscript/testscript.go @@ -572,32 +572,29 @@ 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 + 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 ts.params.Condition != nil: + return ts.params.Condition(cond) 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) - } ts.Fatalf("unknown condition %q", cond) panic("unreachable") } From 780a119a76ad4b9a9134219257becabf9eff68d6 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Wed, 15 Dec 2021 22:14:58 +0100 Subject: [PATCH 2/2] testscript: add build:tag condition --- testscript/doc.go | 8 ++++++++ testscript/testdata/build.txt | 9 +++++++++ testscript/testscript.go | 9 +++++++++ 3 files changed, 26 insertions(+) create mode 100644 testscript/testdata/build.txt diff --git a/testscript/doc.go b/testscript/doc.go index d4fe1869..d1ef5895 100644 --- a/testscript/doc.go +++ b/testscript/doc.go @@ -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. diff --git a/testscript/testdata/build.txt b/testscript/testdata/build.txt new file mode 100644 index 00000000..b4710925 --- /dev/null +++ b/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' diff --git a/testscript/testscript.go b/testscript/testscript.go index cf9a6724..dd309058 100644 --- a/testscript/testscript.go +++ b/testscript/testscript.go @@ -12,6 +12,7 @@ import ( "context" "flag" "fmt" + "go/build" "io/ioutil" "os" "os/exec" @@ -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: