Skip to content

Commit 17fa7fe

Browse files
committed
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 rogpeppe#166.
1 parent f4e7d68 commit 17fa7fe

File tree

5 files changed

+83
-1
lines changed

5 files changed

+83
-1
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

+7-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,10 @@ grep '\A(gc|gccgo)\n\z' go-compiler
88
# test that go version build tags are set
99
[go1.1] exec sh -c 'echo go1.1 >> go-version'
1010
[go2.1] exec sh -c 'echo go2.1 >> go-version'
11-
grep '\Ago1\.1\n\z' go-version
11+
grep '\Ago1\.1\n\z' go-version
12+
13+
# test that either 'unix' or '!unix' is true
14+
[unix] exec sh -c 'echo unix >> unixness'
15+
[!unix] exec sh -c 'echo not unix >> unixness'
16+
[unix] grep '\Aunix\n\z' unixness
17+
[!unix] grep '\Anot unix\n\z' unixness

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:"):

testscript/testscript_nonunix_test.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build !(aix || android || darwin || dragonfly || freebsd || hurd || illumos || ios || linux || netbsd || openbsd || solaris)
6+
7+
package testscript
8+
9+
import (
10+
"runtime"
11+
"testing"
12+
)
13+
14+
func TestConditionUnixIsFalseOnNonUnixySystems(t *testing.T) {
15+
t.Parallel()
16+
ts := &TestScript{
17+
t: &fakeT{},
18+
}
19+
defer func() {
20+
if err := recover(); err != nil {
21+
if err != errAbort {
22+
panic(err)
23+
}
24+
t.Fatal("unrecognised condition [unix]")
25+
}
26+
}()
27+
ok, err := ts.condition("unix")
28+
if err != nil {
29+
t.Fatal(err)
30+
}
31+
if ok {
32+
t.Errorf("want false for condition [unix] when GOOS=%s, got true", runtime.GOOS)
33+
}
34+
}

testscript/testscript_unix_test.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build aix || android || darwin || dragonfly || freebsd || hurd || illumos || ios || linux || netbsd || openbsd || solaris
6+
7+
package testscript
8+
9+
import (
10+
"runtime"
11+
"testing"
12+
)
13+
14+
func TestConditionUnixIsTrueOnUnixySystems(t *testing.T) {
15+
t.Parallel()
16+
ts := &TestScript{
17+
t: &fakeT{},
18+
}
19+
defer func() {
20+
if err := recover(); err != nil {
21+
if err != errAbort {
22+
panic(err)
23+
}
24+
t.Fatal("unrecognised condition [unix]")
25+
}
26+
}()
27+
ok, err := ts.condition("unix")
28+
if err != nil {
29+
t.Fatal(err)
30+
}
31+
if !ok {
32+
t.Errorf("want true for condition [unix] when GOOS=%s, got false", runtime.GOOS)
33+
}
34+
}

0 commit comments

Comments
 (0)