2013-05-22 11:20:50 -06:00
|
|
|
// 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.
|
|
|
|
|
2015-08-05 17:42:54 -06:00
|
|
|
// No testdata on Android.
|
|
|
|
|
|
|
|
// +build !android
|
|
|
|
|
2013-05-22 11:20:50 -06:00
|
|
|
package main_test
|
|
|
|
|
|
|
|
import (
|
2013-05-29 09:28:19 -06:00
|
|
|
"bytes"
|
2013-05-22 11:20:50 -06:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
dataDir = "testdata"
|
2015-07-28 23:41:15 -06:00
|
|
|
binary = "testvet.exe"
|
2013-05-22 11:20:50 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// Run this shell script, but do it in Go so it can be run by "go test".
|
|
|
|
// go build -o testvet
|
2013-06-21 12:27:53 -06:00
|
|
|
// $(GOROOT)/test/errchk ./testvet -shadow -printfuncs='Warn:1,Warnf:1' testdata/*.go testdata/*.s
|
2013-05-22 11:20:50 -06:00
|
|
|
// rm testvet
|
|
|
|
//
|
|
|
|
func TestVet(t *testing.T) {
|
2014-02-20 13:43:24 -07:00
|
|
|
// Plan 9 and Windows systems can't be guaranteed to have Perl and so can't run errchk.
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "plan9", "windows":
|
|
|
|
t.Skip("skipping test; no Perl on %q", runtime.GOOS)
|
2013-05-22 11:20:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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{
|
2013-05-29 13:00:51 -06:00
|
|
|
"./" + binary,
|
2013-05-22 11:20:50 -06:00
|
|
|
"-printfuncs=Warn:1,Warnf:1",
|
2013-06-21 12:27:53 -06:00
|
|
|
"-test", // TODO: Delete once -shadow is part of -all.
|
2013-05-22 11:20:50 -06:00
|
|
|
}
|
|
|
|
cmd = exec.Command(errchk, append(flags, files...)...)
|
2013-05-28 14:51:47 -06:00
|
|
|
if !run(cmd, t) {
|
|
|
|
t.Fatal("vet command failed")
|
|
|
|
}
|
2013-05-22 11:20:50 -06:00
|
|
|
}
|
|
|
|
|
2013-05-28 14:51:47 -06:00
|
|
|
func run(c *exec.Cmd, t *testing.T) bool {
|
|
|
|
output, err := c.CombinedOutput()
|
|
|
|
os.Stderr.Write(output)
|
2013-05-22 11:20:50 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-05-28 14:51:47 -06:00
|
|
|
// Errchk delights by not returning non-zero status if it finds errors, so we look at the output.
|
2013-05-29 09:28:19 -06:00
|
|
|
// It prints "BUG" if there is a failure.
|
|
|
|
if !c.ProcessState.Success() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return !bytes.Contains(output, []byte("BUG"))
|
2013-05-22 11:20:50 -06:00
|
|
|
}
|
2015-06-04 12:37:07 -06:00
|
|
|
|
|
|
|
// 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",
|
|
|
|
}
|
2015-06-04 15:15:26 -06:00
|
|
|
cmd = exec.Command("./"+binary, args...)
|
2015-06-04 12:37:07 -06:00
|
|
|
output, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// file1 has testtag and file2 has !testtag.
|
2015-07-28 23:41:15 -06:00
|
|
|
if !bytes.Contains(output, []byte(filepath.Join("tagtest", "file1.go"))) {
|
2015-06-04 12:37:07 -06:00
|
|
|
t.Error("file1 was excluded, should be included")
|
|
|
|
}
|
2015-07-28 23:41:15 -06:00
|
|
|
if bytes.Contains(output, []byte(filepath.Join("tagtest", "file2.go"))) {
|
2015-06-04 12:37:07 -06:00
|
|
|
t.Error("file2 was included, should be excluded")
|
|
|
|
}
|
|
|
|
}
|