github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/tools/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 // No testdata on Android. 6 7 // +build !android 8 9 package main_test 10 11 import ( 12 "bytes" 13 "os" 14 "os/exec" 15 "path/filepath" 16 "runtime" 17 "testing" 18 ) 19 20 const ( 21 dataDir = "testdata" 22 binary = "testvet.exe" 23 ) 24 25 func CanRun(t *testing.T) bool { 26 // Plan 9 and Windows systems can't be guaranteed to have Perl and so can't run errchk. 27 switch runtime.GOOS { 28 case "plan9", "windows": 29 t.Skip("skipping test; no Perl on %q", runtime.GOOS) 30 return false 31 } 32 return true 33 } 34 35 func Build(t *testing.T) { 36 // go build 37 cmd := exec.Command("go", "build", "-o", binary) 38 run(cmd, t) 39 } 40 41 func Vet(t *testing.T, files []string) { 42 errchk := filepath.Join(runtime.GOROOT(), "test", "errchk") 43 flags := []string{ 44 "./" + binary, 45 "-printfuncs=Warn:1,Warnf:1", 46 "-test", // TODO: Delete once -shadow is part of -all. 47 } 48 cmd := exec.Command(errchk, append(flags, files...)...) 49 if !run(cmd, t) { 50 t.Fatal("vet command failed") 51 } 52 } 53 54 // Run this shell script, but do it in Go so it can be run by "go test". 55 // go build -o testvet 56 // $(GOROOT)/test/errchk ./testvet -shadow -printfuncs='Warn:1,Warnf:1' testdata/*.go testdata/*.s 57 // rm testvet 58 // 59 60 func TestVet(t *testing.T) { 61 if !CanRun(t) { 62 t.Skip("cannot run on this environment") 63 } 64 Build(t) 65 defer os.Remove(binary) 66 67 // errchk ./testvet 68 gos, err := filepath.Glob(filepath.Join(dataDir, "*.go")) 69 if err != nil { 70 t.Fatal(err) 71 } 72 asms, err := filepath.Glob(filepath.Join(dataDir, "*.s")) 73 if err != nil { 74 t.Fatal(err) 75 } 76 files := append(gos, asms...) 77 Vet(t, files) 78 } 79 80 func TestDivergentPackagesExamples(t *testing.T) { 81 if !CanRun(t) { 82 t.Skip("cannot run on this environment") 83 } 84 Build(t) 85 defer os.Remove(binary) 86 87 // errchk ./testvet 88 Vet(t, []string{"testdata/divergent/buf.go", "testdata/divergent/buf_test.go"}) 89 } 90 91 func TestIncompleteExamples(t *testing.T) { 92 if !CanRun(t) { 93 t.Skip("cannot run on this environment") 94 } 95 Build(t) 96 defer os.Remove(binary) 97 98 // errchk ./testvet 99 Vet(t, []string{"testdata/incomplete/examples_test.go"}) 100 } 101 102 func run(c *exec.Cmd, t *testing.T) bool { 103 output, err := c.CombinedOutput() 104 os.Stderr.Write(output) 105 if err != nil { 106 t.Fatal(err) 107 } 108 // Errchk delights by not returning non-zero status if it finds errors, so we look at the output. 109 // It prints "BUG" if there is a failure. 110 if !c.ProcessState.Success() { 111 return false 112 } 113 return !bytes.Contains(output, []byte("BUG")) 114 } 115 116 // TestTags verifies that the -tags argument controls which files to check. 117 func TestTags(t *testing.T) { 118 // go build 119 cmd := exec.Command("go", "build", "-o", binary) 120 run(cmd, t) 121 122 defer os.Remove(binary) 123 124 args := []string{ 125 "-tags=testtag", 126 "-v", // We're going to look at the files it examines. 127 "testdata/tagtest", 128 } 129 cmd = exec.Command("./"+binary, args...) 130 output, err := cmd.CombinedOutput() 131 if err != nil { 132 t.Fatal(err) 133 } 134 // file1 has testtag and file2 has !testtag. 135 if !bytes.Contains(output, []byte(filepath.Join("tagtest", "file1.go"))) { 136 t.Error("file1 was excluded, should be included") 137 } 138 if bytes.Contains(output, []byte(filepath.Join("tagtest", "file2.go"))) { 139 t.Error("file2 was included, should be excluded") 140 } 141 }