Skip to content

Commit 2431384

Browse files
bitfieldmvdan
andcommitted
testscript: add 'unix' condition
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]>
1 parent 8da7db8 commit 2431384

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

testscript/doc.go

+6
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ should only run when the condition is satisfied. The predefined conditions are:
108108
- [gc] for whether Go was built with gc
109109
- [gccgo] for whether Go was built with gccgo
110110
- [go1.x] for whether the Go version is 1.x or later
111+
- [unix] for whether the OS is Unix-like (that is, would match the 'unix' build
112+
constraint)
113+
114+
Any known values of GOOS and GOARCH can also be used as conditions. They will be
115+
satisfied if the target OS or architecture match the specified value. For example,
116+
the condition [darwin] is true if GOOS=darwin, and [amd64] is true if GOARCH=amd64.
111117
112118
A condition can be negated: [!short] means to run the rest of the line
113119
when testing.Short() is false.

testscript/testdata/cond.txt

+20-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
1-
[!exec:sh] skip 'sh not found in $PATH'
2-
31
# test that exactly one of gc and gccgo is set
4-
[gc] exec sh -c 'echo gc >> go-compiler'
5-
[gccgo] exec sh -c 'echo gccgo >> go-compiler'
6-
grep '\A(gc|gccgo)\n\z' go-compiler
2+
[gc] mkdir gc_true
3+
[gccgo] mkdir gccgo_true
4+
5+
[gc] ! exists gccgo_true
6+
[!gc] exists gccgo_true
7+
[gccgo] ! exists gc_true
8+
[!gccgo] exists gc_true
79

810
# test that go version build tags are set
9-
[go1.1] exec sh -c 'echo go1.1 >> go-version'
10-
[go2.1] exec sh -c 'echo go2.1 >> go-version'
11-
grep '\Ago1\.1\n\z' go-version
11+
[go1.1] mkdir go1.x
12+
[go2.1] mkdir go2.x
13+
14+
exists go1.x
15+
! exists go2.x
16+
17+
# unix should be true on Linux and MacOS, but not on Windows.
18+
# Both platforms are tested on CI.
19+
[unix] mkdir unix_true
20+
21+
[linux] exists unix_true
22+
[darwin] exists unix_true
23+
[windows] ! exists unix_true

testscript/testscript.go

+2
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,8 @@ func (ts *TestScript) condition(cond string) (bool, error) {
601601
return testenv.HasSymlink(), nil
602602
case imports.KnownOS[cond]:
603603
return cond == runtime.GOOS, nil
604+
case cond == "unix":
605+
return imports.UnixOS[runtime.GOOS], nil
604606
case imports.KnownArch[cond]:
605607
return cond == runtime.GOARCH, nil
606608
case strings.HasPrefix(cond, "exec:"):

0 commit comments

Comments
 (0)