github.com/mdempsky/go@v0.0.0-20151201204031-5dd372bd1e70/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 "-test", // TODO: Delete once -shadow is part of -all. 69 } 70 cmd := exec.Command(errchk, append(flags, files...)...) 71 if !run(cmd, t) { 72 t.Fatal("vet command failed") 73 } 74 } 75 76 // Run this shell script, but do it in Go so it can be run by "go test". 77 // go build -o testvet 78 // $(GOROOT)/test/errchk ./testvet -shadow -printfuncs='Warn:1,Warnf:1' testdata/*.go testdata/*.s 79 // rm testvet 80 // 81 82 func TestVet(t *testing.T) { 83 Build(t) 84 85 // errchk ./testvet 86 gos, err := filepath.Glob(filepath.Join(dataDir, "*.go")) 87 if err != nil { 88 t.Fatal(err) 89 } 90 asms, err := filepath.Glob(filepath.Join(dataDir, "*.s")) 91 if err != nil { 92 t.Fatal(err) 93 } 94 files := append(gos, asms...) 95 Vet(t, files) 96 } 97 98 func TestDivergentPackagesExamples(t *testing.T) { 99 Build(t) 100 // errchk ./testvet 101 Vet(t, []string{"testdata/divergent/buf.go", "testdata/divergent/buf_test.go"}) 102 } 103 104 func TestIncompleteExamples(t *testing.T) { 105 Build(t) 106 // errchk ./testvet 107 Vet(t, []string{"testdata/incomplete/examples_test.go"}) 108 } 109 110 func run(c *exec.Cmd, t *testing.T) bool { 111 output, err := c.CombinedOutput() 112 os.Stderr.Write(output) 113 if err != nil { 114 t.Fatal(err) 115 } 116 // Errchk delights by not returning non-zero status if it finds errors, so we look at the output. 117 // It prints "BUG" if there is a failure. 118 if !c.ProcessState.Success() { 119 return false 120 } 121 return !bytes.Contains(output, []byte("BUG")) 122 } 123 124 // TestTags verifies that the -tags argument controls which files to check. 125 func TestTags(t *testing.T) { 126 Build(t) 127 args := []string{ 128 "-tags=testtag", 129 "-v", // We're going to look at the files it examines. 130 "testdata/tagtest", 131 } 132 cmd := exec.Command("./"+binary, args...) 133 output, err := cmd.CombinedOutput() 134 if err != nil { 135 t.Fatal(err) 136 } 137 // file1 has testtag and file2 has !testtag. 138 if !bytes.Contains(output, []byte(filepath.Join("tagtest", "file1.go"))) { 139 t.Error("file1 was excluded, should be included") 140 } 141 if bytes.Contains(output, []byte(filepath.Join("tagtest", "file2.go"))) { 142 t.Error("file2 was included, should be excluded") 143 } 144 }