From 3caa6cfbd2616ae9194fd65277a9c68571cb9cc3 Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Thu, 4 Jun 2015 11:37:07 -0700 Subject: [PATCH] cmd/vet: add -tags flag This is needed to control which files to test in the usual manner. A followup CL on the main repo will add the flag to the go vet command. Updates golang/go#10228 Change-Id: I820d3c74657b58de5e92276627368dedf4e2096c Reviewed-on: https://go-review.googlesource.com/10692 Reviewed-by: Andrew Gerrand --- cmd/vet/main.go | 19 +++++++++++++++---- cmd/vet/testdata/tagtest/file1.go | 10 ++++++++++ cmd/vet/testdata/tagtest/file2.go | 10 ++++++++++ cmd/vet/vet_test.go | 28 ++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 cmd/vet/testdata/tagtest/file1.go create mode 100644 cmd/vet/testdata/tagtest/file2.go diff --git a/cmd/vet/main.go b/cmd/vet/main.go index 16b8842c..e4b68770 100644 --- a/cmd/vet/main.go +++ b/cmd/vet/main.go @@ -25,10 +25,13 @@ import ( "golang.org/x/tools/go/types" ) -// TODO: Need a flag to set build tags when parsing the package. +var ( + verbose = flag.Bool("v", false, "verbose") + testFlag = flag.Bool("test", false, "for testing only: sets -all and -shadow") + tags = flag.String("tags", "", "comma-separated list of build tags to apply when parsing") + tagList = []string{} // exploded version of tags flag; set in main +) -var verbose = flag.Bool("v", false, "verbose") -var testFlag = flag.Bool("test", false, "for testing only: sets -all and -shadow") var exitCode = 0 // "all" is here only for the appearance of backwards compatibility. @@ -198,6 +201,8 @@ func main() { } } + tagList = strings.Split(*tags, ",") + initPrintFlags() initUnusedFlags() @@ -246,7 +251,13 @@ func prefixDirectory(directory string, names []string) { // doPackageDir analyzes the single package found in the directory, if there is one, // plus a test package, if there is one. func doPackageDir(directory string) { - pkg, err := build.Default.ImportDir(directory, 0) + context := build.Default + if len(context.BuildTags) != 0 { + warnf("build tags %s previously set", context.BuildTags) + } + context.BuildTags = append(tagList, context.BuildTags...) + + pkg, err := context.ImportDir(directory, 0) if err != nil { // If it's just that there are no go source files, that's fine. if _, nogo := err.(*build.NoGoError); nogo { diff --git a/cmd/vet/testdata/tagtest/file1.go b/cmd/vet/testdata/tagtest/file1.go new file mode 100644 index 00000000..22a1509a --- /dev/null +++ b/cmd/vet/testdata/tagtest/file1.go @@ -0,0 +1,10 @@ +// Copyright 2015 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +build testtag + +package main + +func main() { +} diff --git a/cmd/vet/testdata/tagtest/file2.go b/cmd/vet/testdata/tagtest/file2.go new file mode 100644 index 00000000..ba7dd91b --- /dev/null +++ b/cmd/vet/testdata/tagtest/file2.go @@ -0,0 +1,10 @@ +// Copyright 2015 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +build !testtag + +package main + +func ignore() { +} diff --git a/cmd/vet/vet_test.go b/cmd/vet/vet_test.go index 65fefb95..33e54ae9 100644 --- a/cmd/vet/vet_test.go +++ b/cmd/vet/vet_test.go @@ -72,3 +72,31 @@ func run(c *exec.Cmd, t *testing.T) bool { } return !bytes.Contains(output, []byte("BUG")) } + +// TestTags verifies that the -tags argument controls which files to check. +func TestTags(t *testing.T) { + // go build + cmd := exec.Command("go", "build", "-o", binary) + run(cmd, t) + + // defer removal of vet + defer os.Remove(binary) + + args := []string{ + "-tags=testtag", + "-v", // We're going to look at the files it examines. + "testdata/tagtest", + } + cmd = exec.Command(filepath.Join(".", binary), args...) + output, err := cmd.CombinedOutput() + if err != nil { + t.Fatal(err) + } + // file1 has testtag and file2 has !testtag. + if !bytes.Contains(output, []byte("tagtest/file1.go")) { + t.Error("file1 was excluded, should be included") + } + if bytes.Contains(output, []byte("tagtest/file2.go")) { + t.Error("file2 was included, should be excluded") + } +}