github.com/euank/go@v0.0.0-20160829210321-495514729181/src/cmd/vet/vet_test.go (about) 1 // Copyright 2013 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package main_test 6 7 import ( 8 "bytes" 9 "flag" 10 "fmt" 11 "internal/testenv" 12 "os" 13 "os/exec" 14 "path/filepath" 15 "runtime" 16 "testing" 17 ) 18 19 const ( 20 dataDir = "testdata" 21 binary = "testvet.exe" 22 ) 23 24 // We implement TestMain so remove the test binary when all is done. 25 func TestMain(m *testing.M) { 26 flag.Parse() 27 result := m.Run() 28 os.Remove(binary) 29 os.Exit(result) 30 } 31 32 func MustHavePerl(t *testing.T) { 33 switch runtime.GOOS { 34 case "plan9", "windows": 35 t.Skipf("skipping test: perl not available on %s", runtime.GOOS) 36 } 37 if _, err := exec.LookPath("perl"); err != nil { 38 t.Skipf("skipping test: perl not found in path") 39 } 40 } 41 42 var ( 43 built = false // We have built the binary. 44 failed = false // We have failed to build the binary, don't try again. 45 ) 46 47 func Build(t *testing.T) { 48 testenv.MustHaveGoBuild(t) 49 MustHavePerl(t) 50 if built { 51 return 52 } 53 if failed { 54 t.Skip("cannot run on this environment") 55 } 56 cmd := exec.Command("go", "build", "-o", binary) 57 output, err := cmd.CombinedOutput() 58 if err != nil { 59 failed = true 60 fmt.Fprintf(os.Stderr, "%s\n", output) 61 t.Fatal(err) 62 } 63 built = true 64 } 65 66 func Vet(t *testing.T, files []string) { 67 errchk := filepath.Join(runtime.GOROOT(), "test", "errchk") 68 flags := []string{ 69 "./" + binary, 70 "-printfuncs=Warn:1,Warnf:1", 71 "-all", 72 "-shadow", 73 } 74 cmd := exec.Command(errchk, append(flags, files...)...) 75 if !run(cmd, t) { 76 t.Fatal("vet command failed") 77 } 78 } 79 80 // Run this shell script, but do it in Go so it can be run by "go test". 81 // go build -o testvet 82 // $(GOROOT)/test/errchk ./testvet -shadow -printfuncs='Warn:1,Warnf:1' testdata/*.go testdata/*.s 83 // rm testvet 84 // 85 86 func TestVet(t *testing.T) { 87 Build(t) 88 89 // errchk ./testvet 90 gos, err := filepath.Glob(filepath.Join(dataDir, "*.go")) 91 if err != nil { 92 t.Fatal(err) 93 } 94 asms, err := filepath.Glob(filepath.Join(dataDir, "*.s")) 95 if err != nil { 96 t.Fatal(err) 97 } 98 files := append(gos, asms...) 99 Vet(t, files) 100 } 101 102 func TestDivergentPackagesExamples(t *testing.T) { 103 Build(t) 104 // errchk ./testvet 105 Vet(t, []string{"testdata/divergent"}) 106 } 107 108 func TestIncompleteExamples(t *testing.T) { 109 Build(t) 110 // errchk ./testvet 111 Vet(t, []string{"testdata/incomplete/examples_test.go"}) 112 } 113 114 func run(c *exec.Cmd, t *testing.T) bool { 115 output, err := c.CombinedOutput() 116 os.Stderr.Write(output) 117 if err != nil { 118 t.Fatal(err) 119 } 120 // Errchk delights by not returning non-zero status if it finds errors, so we look at the output. 121 // It prints "BUG" if there is a failure. 122 if !c.ProcessState.Success() { 123 return false 124 } 125 return !bytes.Contains(output, []byte("BUG")) 126 } 127 128 // TestTags verifies that the -tags argument controls which files to check. 129 func TestTags(t *testing.T) { 130 Build(t) 131 args := []string{ 132 "-tags=testtag", 133 "-v", // We're going to look at the files it examines. 134 "testdata/tagtest", 135 } 136 cmd := exec.Command("./"+binary, args...) 137 output, err := cmd.CombinedOutput() 138 if err != nil { 139 t.Fatal(err) 140 } 141 // file1 has testtag and file2 has !testtag. 142 if !bytes.Contains(output, []byte(filepath.Join("tagtest", "file1.go"))) { 143 t.Error("file1 was excluded, should be included") 144 } 145 if bytes.Contains(output, []byte(filepath.Join("tagtest", "file2.go"))) { 146 t.Error("file2 was included, should be excluded") 147 } 148 }