Skip to content

Commit 2b31abc

Browse files
mdempskygopherbot
authored andcommitted
test: add //go:build support to run.go
gofmt is rewriting +build comments into //go:build anyway, so update the test script to support both. Change-Id: Ia6d950cfaa2fca9f184b8b2d3625a551bff88dde Reviewed-on: https://go-review.googlesource.com/c/go/+/399794 Run-TryBot: Matthew Dempsky <[email protected]> Auto-Submit: Matthew Dempsky <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent f2d9ab2 commit 2b31abc

File tree

1 file changed

+13
-38
lines changed

1 file changed

+13
-38
lines changed

test/run.go

+13-38
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"flag"
1515
"fmt"
1616
"go/build"
17+
"go/build/constraint"
1718
"hash/fnv"
1819
"io"
1920
"io/fs"
@@ -462,57 +463,31 @@ func shouldTest(src string, goos, goarch string) (ok bool, whyNot string) {
462463
return true, ""
463464
}
464465
for _, line := range strings.Split(src, "\n") {
465-
line = strings.TrimSpace(line)
466-
if strings.HasPrefix(line, "//") {
467-
line = line[2:]
468-
} else {
469-
continue
470-
}
471-
line = strings.TrimSpace(line)
472-
if len(line) == 0 || line[0] != '+' {
473-
continue
466+
if strings.HasPrefix(line, "package ") {
467+
break
474468
}
475-
gcFlags := os.Getenv("GO_GCFLAGS")
476-
ctxt := &context{
477-
GOOS: goos,
478-
GOARCH: goarch,
479-
cgoEnabled: cgoEnabled,
480-
noOptEnv: strings.Contains(gcFlags, "-N") || strings.Contains(gcFlags, "-l"),
481-
}
482-
483-
words := strings.Fields(line)
484-
if words[0] == "+build" {
485-
ok := false
486-
for _, word := range words[1:] {
487-
if ctxt.match(word) {
488-
ok = true
489-
break
490-
}
469+
470+
if expr, err := constraint.Parse(line); err == nil {
471+
gcFlags := os.Getenv("GO_GCFLAGS")
472+
ctxt := &context{
473+
GOOS: goos,
474+
GOARCH: goarch,
475+
cgoEnabled: cgoEnabled,
476+
noOptEnv: strings.Contains(gcFlags, "-N") || strings.Contains(gcFlags, "-l"),
491477
}
492-
if !ok {
493-
// no matching tag found.
478+
479+
if !expr.Eval(ctxt.match) {
494480
return false, line
495481
}
496482
}
497483
}
498-
// no build tags
499484
return true, ""
500485
}
501486

502487
func (ctxt *context) match(name string) bool {
503488
if name == "" {
504489
return false
505490
}
506-
if first, rest, ok := strings.Cut(name, ","); ok {
507-
// comma-separated list
508-
return ctxt.match(first) && ctxt.match(rest)
509-
}
510-
if strings.HasPrefix(name, "!!") { // bad syntax, reject always
511-
return false
512-
}
513-
if strings.HasPrefix(name, "!") { // negation
514-
return len(name) > 1 && !ctxt.match(name[1:])
515-
}
516491

517492
// Tags must be letters, digits, underscores or dots.
518493
// Unlike in Go identifiers, all digits are fine (e.g., "386").

0 commit comments

Comments
 (0)