Commit 4f6a2b98 authored by Rémy Oudompheng's avatar Rémy Oudompheng

test: add support for build tags.

This enables a few tests that were only executed
unconditionnally.

R=rsc, minux.ma, bradfitz
CC=golang-dev
https://golang.org/cl/7103051
parent cf1f5424
// [ $A == 6 ] || errchk $G -e $D/$F.go
// NOTE: This test is not run by 'run.go' and so not run by all.bash.
// To run this test you must use the ./run shell script.
// +build 386 arm
// errorcheck
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
......@@ -14,4 +12,4 @@ func main() {
var arr [1000200030]int // ERROR "type .* too large"
arr_bkup := arr
_ = arr_bkup
}
\ No newline at end of file
}
// [ $A != 6 ] || errchk $G -e $D/$F.go
// NOTE: This test is not run by 'run.go' and so not run by all.bash.
// To run this test you must use the ./run shell script.
// +build amd64
// errorcheck
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
......
......@@ -299,6 +299,50 @@ func goDirPackages(longdir string) ([][]string, error) {
return pkgs, nil
}
// shouldTest looks for build tags in a source file and returns
// whether the file should be used according to the tags.
func shouldTest(src string, goos, goarch string) (ok bool, whyNot string) {
if idx := strings.Index(src, "\npackage"); idx >= 0 {
src = src[:idx]
}
notgoos := "!" + goos
notgoarch := "!" + goarch
for _, line := range strings.Split(src, "\n") {
line = strings.TrimSpace(line)
if strings.HasPrefix(line, "//") {
line = line[2:]
} else {
continue
}
line = strings.TrimSpace(line)
if len(line) == 0 || line[0] != '+' {
continue
}
words := strings.Fields(line)
if words[0] == "+build" {
for _, word := range words {
switch word {
case goos, goarch:
return true, ""
case notgoos, notgoarch:
continue
default:
if word[0] == '!' {
// NOT something-else
return true, ""
}
}
}
// no matching tag found.
return false, line
}
}
// no build tags.
return true, ""
}
func init() { checkShouldTest() }
// run runs a test.
func (t *test) run() {
defer close(t.donec)
......@@ -318,7 +362,18 @@ func (t *test) run() {
t.err = errors.New("double newline not found")
return
}
if ok, why := shouldTest(t.src, runtime.GOOS, runtime.GOARCH); !ok {
t.action = "skip"
if *showSkips {
fmt.Printf("%-20s %-20s: %s\n", t.action, t.goFileName(), why)
}
return
}
action := t.src[:pos]
if nl := strings.Index(action, "\n"); nl >= 0 && strings.Contains(action[:nl], "+build") {
// skip first line
action = action[nl+1:]
}
if strings.HasPrefix(action, "//") {
action = action[2:]
}
......@@ -732,17 +787,14 @@ func (t *test) wantedErrors(file, short string) (errs []wantedError) {
}
var skipOkay = map[string]bool{
"linkx.go": true,
"sigchld.go": true,
"sinit.go": true,
"fixedbugs/bug248.go": true, // combines errorcheckdir and rundir in the same dir.
"fixedbugs/bug302.go": true, // tests both .$O and .a imports.
"fixedbugs/bug345.go": true, // needs the appropriate flags in gc invocation.
"fixedbugs/bug369.go": true, // needs compiler flags.
"fixedbugs/bug385_32.go": true, // arch-specific errors.
"fixedbugs/bug385_64.go": true, // arch-specific errors.
"fixedbugs/bug429.go": true,
"bugs/bug395.go": true,
"linkx.go": true, // like "run" but wants linker flags
"sinit.go": true,
"fixedbugs/bug248.go": true, // combines errorcheckdir and rundir in the same dir.
"fixedbugs/bug302.go": true, // tests both .$O and .a imports.
"fixedbugs/bug345.go": true, // needs the appropriate flags in gc invocation.
"fixedbugs/bug369.go": true, // needs compiler flags.
"fixedbugs/bug429.go": true, // like "run" but program should fail
"bugs/bug395.go": true,
}
// defaultRunOutputLimit returns the number of runoutput tests that
......@@ -756,3 +808,18 @@ func defaultRunOutputLimit() int {
}
return cpu
}
// checkShouldTest runs canity checks on the shouldTest function.
func checkShouldTest() {
assert := func(ok bool, _ string) {
if !ok {
panic("fail")
}
}
assertNot := func(ok bool, _ string) { assert(!ok, "") }
assert(shouldTest("// +build linux", "linux", "arm"))
assert(shouldTest("// +build !windows", "linux", "arm"))
assertNot(shouldTest("// +build !windows", "windows", "amd64"))
assertNot(shouldTest("// +build arm 386", "linux", "amd64"))
assert(shouldTest("// This is a test.", "os", "arch"))
}
// [ "$GOOS" == windows ] ||
// ($G $D/$F.go && $L $F.$A && ./$A.out 2>&1 | cmp - $D/$F.out)
// NOTE: This test is not run by 'run.go' and so not run by all.bash.
// To run this test you must use the ./run shell script.
// +build !windows
// cmpout
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
......
......@@ -16,6 +16,31 @@ pkgs() {
done | sort
}
# +build aborts execution if the supplied tags don't match,
# i.e. none of the tags (x or !x) matches GOARCH or GOOS.
+build() {
if (( $# == 0 )); then
return
fi
for tag; do
case $tag in
$GOARCH|$GOOS)
#echo >&2 "match $tag in $1"
return # don't exclude.
;;
'!'$GOARCH|'!'$GOOS)
;;
'!'*)
# not x where x is neither GOOS nor GOARCH.
#echo >&2 "match $tag in $1"
return # don't exclude
;;
esac
done
# no match.
exit 0
}
compile() {
$G $D/$F.go
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment