From 24313847d41c3559138ddd35a8c1ece8d7bfe550 Mon Sep 17 00:00:00 2001 From: John Arundel Date: Sun, 24 Jul 2022 13:24:19 +0100 Subject: [PATCH] testscript: add 'unix' condition MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From Go 1.19, the build constraint 'unix' proposed in golang/go#20322 is satisfied by any sufficiently Unix-like value of GOOS, as defined by src/go/build/syslist.go. This commit adds a predefined 'unix' condition with the same meaning, available for use in test scripts. The condition is satisfied if the target GOOS is one of the list of Unix-like systems defined in 'imports.UnixOS'. Fixes #166. Co-authored-by: Daniel Martí --- testscript/doc.go | 6 ++++++ testscript/testdata/cond.txt | 28 ++++++++++++++++++++-------- testscript/testscript.go | 2 ++ 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/testscript/doc.go b/testscript/doc.go index 10ae6ace..5cee2023 100644 --- a/testscript/doc.go +++ b/testscript/doc.go @@ -108,6 +108,12 @@ should only run when the condition is satisfied. The predefined conditions are: - [gc] for whether Go was built with gc - [gccgo] for whether Go was built with gccgo - [go1.x] for whether the Go version is 1.x or later + - [unix] for whether the OS is Unix-like (that is, would match the 'unix' build + constraint) + +Any known values of GOOS and GOARCH can also be used as conditions. They will be +satisfied if the target OS or architecture match the specified value. For example, +the condition [darwin] is true if GOOS=darwin, and [amd64] is true if GOARCH=amd64. A condition can be negated: [!short] means to run the rest of the line when testing.Short() is false. diff --git a/testscript/testdata/cond.txt b/testscript/testdata/cond.txt index 60b62c2a..05612d64 100644 --- a/testscript/testdata/cond.txt +++ b/testscript/testdata/cond.txt @@ -1,11 +1,23 @@ -[!exec:sh] skip 'sh not found in $PATH' - # test that exactly one of gc and gccgo is set -[gc] exec sh -c 'echo gc >> go-compiler' -[gccgo] exec sh -c 'echo gccgo >> go-compiler' -grep '\A(gc|gccgo)\n\z' go-compiler +[gc] mkdir gc_true +[gccgo] mkdir gccgo_true + +[gc] ! exists gccgo_true +[!gc] exists gccgo_true +[gccgo] ! exists gc_true +[!gccgo] exists gc_true # test that go version build tags are set -[go1.1] exec sh -c 'echo go1.1 >> go-version' -[go2.1] exec sh -c 'echo go2.1 >> go-version' -grep '\Ago1\.1\n\z' go-version \ No newline at end of file +[go1.1] mkdir go1.x +[go2.1] mkdir go2.x + +exists go1.x +! exists go2.x + +# unix should be true on Linux and MacOS, but not on Windows. +# Both platforms are tested on CI. +[unix] mkdir unix_true + +[linux] exists unix_true +[darwin] exists unix_true +[windows] ! exists unix_true diff --git a/testscript/testscript.go b/testscript/testscript.go index bd05977a..a04ca81d 100644 --- a/testscript/testscript.go +++ b/testscript/testscript.go @@ -601,6 +601,8 @@ func (ts *TestScript) condition(cond string) (bool, error) { return testenv.HasSymlink(), nil case imports.KnownOS[cond]: return cond == runtime.GOOS, nil + case cond == "unix": + return imports.UnixOS[runtime.GOOS], nil case imports.KnownArch[cond]: return cond == runtime.GOARCH, nil case strings.HasPrefix(cond, "exec:"):