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.
|
|
|
|
|
|
|
|
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"
|
|
|
|
binary = "testvet"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
// 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{
|
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
|
|
|
}
|