From bf87b9f0f503f69b7623be389fba66a5f48005d2 Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Wed, 22 May 2013 10:20:50 -0700 Subject: [PATCH] go.tools/cmd/vet: use "go test" to test - remove Makefile - move test data into a subdirectory - encapsulate the invocation of errchk into a standard Test using os.exec R=golang-dev, minux.ma, rsc CC=golang-dev https://golang.org/cl/9509045 --- cmd/vet/Makefile | 14 ---- cmd/vet/{test_asm.go => testdata/asm.go} | 2 +- cmd/vet/{test_asm1.s => testdata/asm1.s} | 0 cmd/vet/{test_asm2.s => testdata/asm2.s} | 0 cmd/vet/{test_asm3.s => testdata/asm3.s} | 0 .../{test_assign.go => testdata/assign.go} | 4 +- .../{test_atomic.go => testdata/atomic.go} | 4 +- .../buildtag.go} | 3 +- .../buildtag_bad.go} | 0 .../deadcode.go} | 3 +- .../{test_method.go => testdata/method.go} | 4 +- cmd/vet/{test_print.go => testdata/print.go} | 4 +- .../rangeloop.go} | 4 +- .../structtag.go} | 4 +- .../{test_taglit.go => testdata/taglit.go} | 4 +- cmd/vet/vet_test.go | 64 +++++++++++++++++++ 16 files changed, 74 insertions(+), 40 deletions(-) delete mode 100644 cmd/vet/Makefile rename cmd/vet/{test_asm.go => testdata/asm.go} (97%) rename cmd/vet/{test_asm1.s => testdata/asm1.s} (100%) rename cmd/vet/{test_asm2.s => testdata/asm2.s} (100%) rename cmd/vet/{test_asm3.s => testdata/asm3.s} (100%) rename cmd/vet/{test_assign.go => testdata/assign.go} (93%) rename cmd/vet/{test_atomic.go => testdata/atomic.go} (97%) rename cmd/vet/{test_buildtag.go => testdata/buildtag.go} (92%) rename cmd/vet/{test_buildtag_bad.go => testdata/buildtag_bad.go} (100%) rename cmd/vet/{test_deadcode.go => testdata/deadcode.go} (99%) rename cmd/vet/{test_method.go => testdata/method.go} (93%) rename cmd/vet/{test_print.go => testdata/print.go} (99%) rename cmd/vet/{test_rangeloop.go => testdata/rangeloop.go} (97%) rename cmd/vet/{test_structtag.go => testdata/structtag.go} (91%) rename cmd/vet/{test_taglit.go => testdata/taglit.go} (97%) create mode 100644 cmd/vet/vet_test.go diff --git a/cmd/vet/Makefile b/cmd/vet/Makefile deleted file mode 100644 index 3e0a418266..0000000000 --- a/cmd/vet/Makefile +++ /dev/null @@ -1,14 +0,0 @@ -# Copyright 2010 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. - -# Assumes go/types is installed -test testshort: - go build - $(GOROOT)/test/errchk ./vet -printfuncs='Warn:1,Warnf:1' test_*.go test_*.s - -# Install command where the go tool can find it. -install: - go build -o _vet - cp _vet $(GOROOT)/pkg/tool/$(GOOS)_$(GOARCH)/vet - rm -f _vet diff --git a/cmd/vet/test_asm.go b/cmd/vet/testdata/asm.go similarity index 97% rename from cmd/vet/test_asm.go rename to cmd/vet/testdata/asm.go index 098bdd15c6..18254a40eb 100644 --- a/cmd/vet/test_asm.go +++ b/cmd/vet/testdata/asm.go @@ -6,7 +6,7 @@ // This file contains declarations to test the assembly in test_asm.s. -package main +package testdata func arg1(x int8, y uint8) func arg2(x int16, y uint16) diff --git a/cmd/vet/test_asm1.s b/cmd/vet/testdata/asm1.s similarity index 100% rename from cmd/vet/test_asm1.s rename to cmd/vet/testdata/asm1.s diff --git a/cmd/vet/test_asm2.s b/cmd/vet/testdata/asm2.s similarity index 100% rename from cmd/vet/test_asm2.s rename to cmd/vet/testdata/asm2.s diff --git a/cmd/vet/test_asm3.s b/cmd/vet/testdata/asm3.s similarity index 100% rename from cmd/vet/test_asm3.s rename to cmd/vet/testdata/asm3.s diff --git a/cmd/vet/test_assign.go b/cmd/vet/testdata/assign.go similarity index 93% rename from cmd/vet/test_assign.go rename to cmd/vet/testdata/assign.go index 8e0f45e532..32ba8683c1 100644 --- a/cmd/vet/test_assign.go +++ b/cmd/vet/testdata/assign.go @@ -4,9 +4,7 @@ // This file contains tests for the useless-assignment checker. -// +build vet_test - -package main +package testdata type ST struct { x int diff --git a/cmd/vet/test_atomic.go b/cmd/vet/testdata/atomic.go similarity index 97% rename from cmd/vet/test_atomic.go rename to cmd/vet/testdata/atomic.go index 9231e9dc0d..7a6dcb8c88 100644 --- a/cmd/vet/test_atomic.go +++ b/cmd/vet/testdata/atomic.go @@ -2,11 +2,9 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build vet_test - // This file contains tests for the atomic checker. -package main +package testdata import ( "sync/atomic" diff --git a/cmd/vet/test_buildtag.go b/cmd/vet/testdata/buildtag.go similarity index 92% rename from cmd/vet/test_buildtag.go rename to cmd/vet/testdata/buildtag.go index d7174ade21..7959c5a71c 100644 --- a/cmd/vet/test_buildtag.go +++ b/cmd/vet/testdata/buildtag.go @@ -4,11 +4,10 @@ // This file contains tests for the buildtag checker. -// +build vet_test // +builder // ERROR "possible malformed \+build comment" // +build !ignore -package main +package testdata // +build toolate // ERROR "build comment appears too late in file" diff --git a/cmd/vet/test_buildtag_bad.go b/cmd/vet/testdata/buildtag_bad.go similarity index 100% rename from cmd/vet/test_buildtag_bad.go rename to cmd/vet/testdata/buildtag_bad.go diff --git a/cmd/vet/test_deadcode.go b/cmd/vet/testdata/deadcode.go similarity index 99% rename from cmd/vet/test_deadcode.go rename to cmd/vet/testdata/deadcode.go index d08e57782f..3028c98bc3 100644 --- a/cmd/vet/test_deadcode.go +++ b/cmd/vet/testdata/deadcode.go @@ -2,12 +2,11 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build vet_test // +build ignore // This file contains tests for the dead code checker. -package main +package testdata type T int diff --git a/cmd/vet/test_method.go b/cmd/vet/testdata/method.go similarity index 93% rename from cmd/vet/test_method.go rename to cmd/vet/testdata/method.go index 41de62bb1d..52b500df27 100644 --- a/cmd/vet/test_method.go +++ b/cmd/vet/testdata/method.go @@ -4,11 +4,9 @@ // This file contains tests for the canonical method checker. -// +build vet_test - // This file contains the code to check canonical methods. -package main +package testdata import ( "fmt" diff --git a/cmd/vet/test_print.go b/cmd/vet/testdata/print.go similarity index 99% rename from cmd/vet/test_print.go rename to cmd/vet/testdata/print.go index 8b41e6c69b..72587feebf 100644 --- a/cmd/vet/test_print.go +++ b/cmd/vet/testdata/print.go @@ -2,11 +2,9 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build vet_test - // This file contains tests for the printf checker. -package main +package testdata import ( "fmt" diff --git a/cmd/vet/test_rangeloop.go b/cmd/vet/testdata/rangeloop.go similarity index 97% rename from cmd/vet/test_rangeloop.go rename to cmd/vet/testdata/rangeloop.go index 941fd72aaa..331e5d0844 100644 --- a/cmd/vet/test_rangeloop.go +++ b/cmd/vet/testdata/rangeloop.go @@ -4,9 +4,7 @@ // This file contains tests for the rangeloop checker. -// +build vet_test - -package main +package testdata func RangeLoopTests() { var s []int diff --git a/cmd/vet/test_structtag.go b/cmd/vet/testdata/structtag.go similarity index 91% rename from cmd/vet/test_structtag.go rename to cmd/vet/testdata/structtag.go index 08cf737fd8..502c58b08b 100644 --- a/cmd/vet/test_structtag.go +++ b/cmd/vet/testdata/structtag.go @@ -4,11 +4,9 @@ // This file contains tests for the structtag checker. -// +build vet_test - // This file contains the test for canonical struct tags. -package main +package testdata type StructTagTest struct { X int "hello" // ERROR "not compatible with reflect.StructTag.Get" diff --git a/cmd/vet/test_taglit.go b/cmd/vet/testdata/taglit.go similarity index 97% rename from cmd/vet/test_taglit.go rename to cmd/vet/testdata/taglit.go index f34062f18e..57219149b8 100644 --- a/cmd/vet/test_taglit.go +++ b/cmd/vet/testdata/taglit.go @@ -4,11 +4,9 @@ // This file contains tests for the untagged struct literal checker. -// +build vet_test - // This file contains the test for untagged struct literals. -package main +package testdata import ( "flag" diff --git a/cmd/vet/vet_test.go b/cmd/vet/vet_test.go new file mode 100644 index 0000000000..f9be1b3f1d --- /dev/null +++ b/cmd/vet/vet_test.go @@ -0,0 +1,64 @@ +// Copyright 2013 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. + +package main_test + +import ( + "os" + "os/exec" + "path/filepath" + "runtime" + "testing" +) + +const ( + dataDir = "testdata" + binary = "testvet" +) + +// Run this shell script, but do it in Go so it can be run by "go test". +// go build -o testvet +// $(GOROOT)/test/errchk ./testvet -printfuncs='Warn:1,Warnf:1' testdata/*.go testdata/*.s +// rm testvet +// +func TestVet(t *testing.T) { + // Windows systems can't be guaranteed to have Perl and so can't run errchk. + if runtime.GOOS == "windows" { + t.Skip("skipping test; no Perl on Windows") + } + + // go build + cmd := exec.Command("go", "build", "-o", binary) + run(cmd, t) + + // defer removal of vet + defer os.Remove(binary) + + // errchk ./testvet + gos, err := filepath.Glob(filepath.Join(dataDir, "*.go")) + if err != nil { + t.Fatal(err) + } + asms, err := filepath.Glob(filepath.Join(dataDir, "*.s")) + if err != nil { + t.Fatal(err) + } + files := append(gos, asms...) + errchk := filepath.Join(runtime.GOROOT(), "test", "errchk") + flags := []string{ + binary, + "-printfuncs=Warn:1,Warnf:1", + } + cmd = exec.Command(errchk, append(flags, files...)...) + run(cmd, t) +} + +func run(c *exec.Cmd, t *testing.T) { + c.Stdout = os.Stdout + c.Stderr = os.Stderr + err := c.Run() + if err != nil { + t.Fatal(err) + } +}