github.com/miolini/go@v0.0.0-20160405192216-fca68c8cb408/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 } 38 39 var ( 40 built = false // We have built the binary. 41 failed = false // We have failed to build the binary, don't try again. 42 ) 43 44 func Build(t *testing.T) { 45 if built { 46 return 47 } 48 testenv.MustHaveGoBuild(t) 49 MustHavePerl(t) 50 if failed { 51 t.Skip("cannot run on this environment") 52 } 53 cmd := exec.Command("go", "build", "-o", binary) 54 output, err := cmd.CombinedOutput() 55 if err != nil { 56 failed = true 57 fmt.Fprintf(os.Stderr, "%s\n", output) 58 t.Fatal(err) 59 } 60 built = true 61 } 62 63 func Vet(t *testing.T, files []string) { 64 errchk := filepath.Join(runtime.GOROOT(), "test", "errchk") 65 flags := []string{ 66 "./" + binary, 67 "-printfuncs=Warn:1,Warnf:1", 68 "-all", 69 "-shadow", 70 } 71 cmd := exec.Command(errchk, append(flags, files...)...) 72 if !run(cmd, t) { 73 t.Fatal("vet command failed") 74 } 75 } 76 77 // Run this shell script, but do it in Go so it can be run by "go test". 78 // go build -o testvet 79 // $(GOROOT)/test/errchk ./testvet -shadow -printfuncs='Warn:1,Warnf:1' testdata/*.go testdata/*.s 80 // rm testvet 81 // 82 83 func TestVet(t *testing.T) { 84 Build(t) 85 86 // errchk ./testvet 87 gos, err := filepath.Glob(filepath.Join(dataDir, "*.go")) 88 if err != nil { 89 t.Fatal(err) 90 } 91 asms, err := filepath.Glob(filepath.Join(dataDir, "*.s")) 92 if err != nil { 93 t.Fatal(err) 94 } 95 files := append(gos, asms...) 96 Vet(t, files) 97 } 98 99 func TestDivergentPackagesExamples(t *testing.T) { 100 Build(t) 101 // errchk ./testvet 102 Vet(t, []string{"testdata/divergent/buf.go", "testdata/divergent/buf_test.go"}) 103 } 104 105 func TestIncompleteExamples(t *testing.T) { 106 Build(t) 107 // errchk ./testvet 108 Vet(t, []string{"testdata/incomplete/examples_test.go"}) 109 } 110 111 func run(c *exec.Cmd, t *testing.T) bool { 112 output, err := c.CombinedOutput() 113 os.Stderr.Write(output) 114 if err != nil { 115 t.Fatal(err) 116 } 117 // Errchk delights by not returning non-zero status if it finds errors, so we look at the output. 118 // It prints "BUG" if there is a failure. 119 if !c.ProcessState.Success() { 120 return false 121 } 122 return !bytes.Contains(output, []byte("BUG")) 123 } 124 125 // TestTags verifies that the -tags argument controls which files to check. 126 func TestTags(t *testing.T) { 127 Build(t) 128 args := []string{ 129 "-tags=testtag", 130 "-v", // We're going to look at the files it examines. 131 "testdata/tagtest", 132 } 133 cmd := exec.Command("./"+binary, args...) 134 output, err := cmd.CombinedOutput() 135 if err != nil { 136 t.Fatal(err) 137 } 138 // file1 has testtag and file2 has !testtag. 139 if !bytes.Contains(output, []byte(filepath.Join("tagtest", "file1.go"))) { 140 t.Error("file1 was excluded, should be included") 141 } 142 if bytes.Contains(output, []byte(filepath.Join("tagtest", "file2.go"))) { 143 t.Error("file2 was included, should be excluded") 144 } 145 }