Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'unix' condition #169

Merged
merged 2 commits into from Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 15 additions & 4 deletions imports/build.go
Expand Up @@ -195,17 +195,28 @@ func MatchFile(name string, tags map[string]bool) bool {
return true
}

var KnownOS = make(map[string]bool)
var KnownArch = make(map[string]bool)
var (
KnownOS = make(map[string]bool)
UnixOS = make(map[string]bool)
KnownArch = make(map[string]bool)
)

func init() {
for _, v := range strings.Fields(goosList) {
KnownOS[v] = true
}
for _, v := range strings.Fields(unixList) {
UnixOS[v] = true
}
for _, v := range strings.Fields(goarchList) {
KnownArch[v] = true
}
}

const goosList = "aix android darwin dragonfly freebsd hurd illumos ios js linux nacl netbsd openbsd plan9 solaris windows zos "
const goarchList = "386 amd64 amd64p32 arm armbe arm64 arm64be loong64 mips mipsle mips64 mips64le mips64p32 mips64p32le ppc ppc64 ppc64le riscv riscv64 s390 s390x sparc sparc64 wasm "
// These values come from Go's src/go/build/syslist.go and should be kept in
// sync with that file.
const (
mvdan marked this conversation as resolved.
Show resolved Hide resolved
goosList = "aix android darwin dragonfly freebsd hurd illumos ios js linux nacl netbsd openbsd plan9 solaris windows zos "
unixList = "aix android darwin dragonfly freebsd hurd illumos ios linux netbsd openbsd solaris "
goarchList = "386 amd64 amd64p32 arm armbe arm64 arm64be loong64 mips mipsle mips64 mips64le mips64p32 mips64p32le ppc ppc64 ppc64le riscv riscv64 s390 s390x sparc sparc64 wasm "
)
6 changes: 6 additions & 0 deletions testscript/doc.go
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
@@ -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
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