mirror of
https://github.com/golang/go
synced 2024-11-18 12:54:44 -07:00
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 <adg@golang.org>
This commit is contained in:
parent
578c521fc2
commit
3caa6cfbd2
@ -25,10 +25,13 @@ import (
|
|||||||
"golang.org/x/tools/go/types"
|
"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
|
var exitCode = 0
|
||||||
|
|
||||||
// "all" is here only for the appearance of backwards compatibility.
|
// "all" is here only for the appearance of backwards compatibility.
|
||||||
@ -198,6 +201,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tagList = strings.Split(*tags, ",")
|
||||||
|
|
||||||
initPrintFlags()
|
initPrintFlags()
|
||||||
initUnusedFlags()
|
initUnusedFlags()
|
||||||
|
|
||||||
@ -246,7 +251,13 @@ func prefixDirectory(directory string, names []string) {
|
|||||||
// doPackageDir analyzes the single package found in the directory, if there is one,
|
// doPackageDir analyzes the single package found in the directory, if there is one,
|
||||||
// plus a test package, if there is one.
|
// plus a test package, if there is one.
|
||||||
func doPackageDir(directory string) {
|
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 err != nil {
|
||||||
// If it's just that there are no go source files, that's fine.
|
// If it's just that there are no go source files, that's fine.
|
||||||
if _, nogo := err.(*build.NoGoError); nogo {
|
if _, nogo := err.(*build.NoGoError); nogo {
|
||||||
|
10
cmd/vet/testdata/tagtest/file1.go
vendored
Normal file
10
cmd/vet/testdata/tagtest/file1.go
vendored
Normal file
@ -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() {
|
||||||
|
}
|
10
cmd/vet/testdata/tagtest/file2.go
vendored
Normal file
10
cmd/vet/testdata/tagtest/file2.go
vendored
Normal file
@ -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() {
|
||||||
|
}
|
@ -72,3 +72,31 @@ func run(c *exec.Cmd, t *testing.T) bool {
|
|||||||
}
|
}
|
||||||
return !bytes.Contains(output, []byte("BUG"))
|
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user