mirror of
https://github.com/golang/go
synced 2024-10-14 11:53:56 +00:00
cmd/vet: use the test binary as 'vet' instead of rebuilding it
This not only reduces the latency of the test, but also respects build flags like '-race' and '-cover' passed to the 'go test' command. Change-Id: I4914870396f90097f45d00012e5601fe42655899 Reviewed-on: https://go-review.googlesource.com/c/go/+/450710 Run-TryBot: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Bryan Mills <bcmills@google.com> Reviewed-by: Than McIntosh <thanm@google.com>
This commit is contained in:
parent
ddf78189d4
commit
509ac2ca6d
|
@ -2,7 +2,7 @@
|
||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package main_test
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
@ -21,60 +21,46 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
var binary string
|
// TestMain executes the test binary as the vet command if
|
||||||
|
// GO_VETTEST_IS_VET is set, and runs the tests otherwise.
|
||||||
// We implement TestMain so remove the test binary when all is done.
|
|
||||||
func TestMain(m *testing.M) {
|
func TestMain(m *testing.M) {
|
||||||
os.Exit(testMain(m))
|
if os.Getenv("GO_VETTEST_IS_VET") != "" {
|
||||||
|
main()
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
os.Setenv("GO_VETTEST_IS_VET", "1") // Set for subprocesses to inherit.
|
||||||
|
os.Exit(m.Run())
|
||||||
}
|
}
|
||||||
|
|
||||||
func testMain(m *testing.M) int {
|
// vetPath returns the path to the "vet" binary to run.
|
||||||
dir, err := os.MkdirTemp("", "vet_test")
|
func vetPath(t testing.TB) string {
|
||||||
if err != nil {
|
t.Helper()
|
||||||
fmt.Fprintln(os.Stderr, err)
|
testenv.MustHaveExec(t)
|
||||||
return 1
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(dir)
|
|
||||||
binary = filepath.Join(dir, "testvet.exe")
|
|
||||||
|
|
||||||
return m.Run()
|
vetPathOnce.Do(func() {
|
||||||
|
vetExePath, vetPathErr = os.Executable()
|
||||||
|
})
|
||||||
|
if vetPathErr != nil {
|
||||||
|
t.Fatal(vetPathErr)
|
||||||
|
}
|
||||||
|
return vetExePath
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
buildMu sync.Mutex // guards following
|
vetPathOnce sync.Once
|
||||||
built = false // We have built the binary.
|
vetExePath string
|
||||||
failed = false // We have failed to build the binary, don't try again.
|
vetPathErr error
|
||||||
)
|
)
|
||||||
|
|
||||||
func Build(t *testing.T) {
|
|
||||||
buildMu.Lock()
|
|
||||||
defer buildMu.Unlock()
|
|
||||||
if built {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if failed {
|
|
||||||
t.Skip("cannot run on this environment")
|
|
||||||
}
|
|
||||||
testenv.MustHaveGoBuild(t)
|
|
||||||
cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", binary)
|
|
||||||
output, err := cmd.CombinedOutput()
|
|
||||||
if err != nil {
|
|
||||||
failed = true
|
|
||||||
fmt.Fprintf(os.Stderr, "%s\n", output)
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
built = true
|
|
||||||
}
|
|
||||||
|
|
||||||
func vetCmd(t *testing.T, arg, pkg string) *exec.Cmd {
|
func vetCmd(t *testing.T, arg, pkg string) *exec.Cmd {
|
||||||
cmd := exec.Command(testenv.GoToolPath(t), "vet", "-vettool="+binary, arg, path.Join("cmd/vet/testdata", pkg))
|
cmd := exec.Command(testenv.GoToolPath(t), "vet", "-vettool="+vetPath(t), arg, path.Join("cmd/vet/testdata", pkg))
|
||||||
cmd.Env = os.Environ()
|
cmd.Env = os.Environ()
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestVet(t *testing.T) {
|
func TestVet(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
Build(t)
|
|
||||||
for _, pkg := range []string{
|
for _, pkg := range []string{
|
||||||
"asm",
|
"asm",
|
||||||
"assign",
|
"assign",
|
||||||
|
@ -163,7 +149,6 @@ func errchk(c *exec.Cmd, files []string, t *testing.T) {
|
||||||
// TestTags verifies that the -tags argument controls which files to check.
|
// TestTags verifies that the -tags argument controls which files to check.
|
||||||
func TestTags(t *testing.T) {
|
func TestTags(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
Build(t)
|
|
||||||
for tag, wantFile := range map[string]int{
|
for tag, wantFile := range map[string]int{
|
||||||
"testtag": 1, // file1
|
"testtag": 1, // file1
|
||||||
"x testtag y": 1,
|
"x testtag y": 1,
|
||||||
|
|
Loading…
Reference in a new issue