From: Rémy Oudompheng Date: Mon, 28 Jan 2013 20:29:45 +0000 (+0100) Subject: test: add support for build tags. X-Git-Tag: go1.1rc2~1276 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=4f6a2b9840f7894086a04e42ebf497b2d8fdbd33;p=gostls13.git 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 --- diff --git a/test/fixedbugs/bug385_32.go b/test/fixedbugs/bug385_32.go index 5ac4136e7d..724ed93262 100644 --- a/test/fixedbugs/bug385_32.go +++ b/test/fixedbugs/bug385_32.go @@ -1,7 +1,5 @@ -// [ $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 +} diff --git a/test/fixedbugs/bug385_64.go b/test/fixedbugs/bug385_64.go index f8ccb42a9b..b5621b2103 100644 --- a/test/fixedbugs/bug385_64.go +++ b/test/fixedbugs/bug385_64.go @@ -1,7 +1,5 @@ -// [ $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 diff --git a/test/run.go b/test/run.go index fb528fa4ce..bc545df10b 100644 --- a/test/run.go +++ b/test/run.go @@ -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")) +} diff --git a/test/sigchld.go b/test/sigchld.go index c1cfc2a8d0..a60d28deaa 100644 --- a/test/sigchld.go +++ b/test/sigchld.go @@ -1,8 +1,5 @@ -// [ "$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 diff --git a/test/testlib b/test/testlib index b58e8831c3..de138b1d19 100644 --- a/test/testlib +++ b/test/testlib @@ -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 }