Skip to content

Commit

Permalink
testscript: add 'unix' condition
Browse files Browse the repository at this point in the history
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í <[email protected]>
  • Loading branch information
bitfield and mvdan committed Jul 28, 2022
1 parent 8da7db8 commit 2431384
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
6 changes: 6 additions & 0 deletions testscript/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
28 changes: 20 additions & 8 deletions testscript/testdata/cond.txt
Original file line number Diff line number Diff line change
@@ -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
[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
2 changes: 2 additions & 0 deletions testscript/testscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:"):
Expand Down

0 comments on commit 2431384

Please sign in to comment.