github.com/razvanm/vanadium-go-1.3@v0.0.0-20160721203343-4a65068e5915/src/cmd/api/goapi_test.go (about) 1 // +build api_tool 2 3 // Copyright 2011 The Go Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file. 6 7 package main 8 9 import ( 10 "bytes" 11 "flag" 12 "fmt" 13 "go/build" 14 "io/ioutil" 15 "os" 16 "os/exec" 17 "path/filepath" 18 "sort" 19 "strings" 20 "testing" 21 ) 22 23 var ( 24 updateGolden = flag.Bool("updategolden", false, "update golden files") 25 ) 26 27 func TestGolden(t *testing.T) { 28 td, err := os.Open("testdata/src/pkg") 29 if err != nil { 30 t.Fatal(err) 31 } 32 fis, err := td.Readdir(0) 33 if err != nil { 34 t.Fatal(err) 35 } 36 for _, fi := range fis { 37 if !fi.IsDir() { 38 continue 39 } 40 41 goldenFile := filepath.Join("testdata", "src", fi.Name(), "golden.txt") 42 w := NewWalker(nil, "testdata/src/pkg") 43 w.export(w.Import(fi.Name())) 44 45 if *updateGolden { 46 os.Remove(goldenFile) 47 f, err := os.Create(goldenFile) 48 if err != nil { 49 t.Fatal(err) 50 } 51 for _, feat := range w.Features() { 52 fmt.Fprintf(f, "%s\n", feat) 53 } 54 f.Close() 55 } 56 57 bs, err := ioutil.ReadFile(goldenFile) 58 if err != nil { 59 t.Fatalf("opening golden.txt for package %q: %v", fi.Name(), err) 60 } 61 wanted := strings.Split(string(bs), "\n") 62 sort.Strings(wanted) 63 for _, feature := range wanted { 64 if feature == "" { 65 continue 66 } 67 _, ok := w.features[feature] 68 if !ok { 69 t.Errorf("package %s: missing feature %q", fi.Name(), feature) 70 } 71 delete(w.features, feature) 72 } 73 74 for _, feature := range w.Features() { 75 t.Errorf("package %s: extra feature not in golden file: %q", fi.Name(), feature) 76 } 77 } 78 } 79 80 func TestCompareAPI(t *testing.T) { 81 tests := []struct { 82 name string 83 features, required, optional, exception []string 84 ok bool // want 85 out string // want 86 }{ 87 { 88 name: "feature added", 89 features: []string{"A", "B", "C", "D", "E", "F"}, 90 required: []string{"B", "D"}, 91 ok: true, 92 out: "+A\n+C\n+E\n+F\n", 93 }, 94 { 95 name: "feature removed", 96 features: []string{"C", "A"}, 97 required: []string{"A", "B", "C"}, 98 ok: false, 99 out: "-B\n", 100 }, 101 { 102 name: "feature added then removed", 103 features: []string{"A", "C"}, 104 optional: []string{"B"}, 105 required: []string{"A", "C"}, 106 ok: true, 107 out: "±B\n", 108 }, 109 { 110 name: "exception removal", 111 required: []string{"A", "B", "C"}, 112 features: []string{"A", "C"}, 113 exception: []string{"B"}, 114 ok: true, 115 out: "", 116 }, 117 { 118 // http://golang.org/issue/4303 119 name: "contexts reconverging", 120 required: []string{ 121 "A", 122 "pkg syscall (darwin-386), type RawSockaddrInet6 struct", 123 "pkg syscall (darwin-amd64), type RawSockaddrInet6 struct", 124 }, 125 features: []string{ 126 "A", 127 "pkg syscall, type RawSockaddrInet6 struct", 128 }, 129 ok: true, 130 out: "+pkg syscall, type RawSockaddrInet6 struct\n", 131 }, 132 } 133 for _, tt := range tests { 134 buf := new(bytes.Buffer) 135 gotok := compareAPI(buf, tt.features, tt.required, tt.optional, tt.exception) 136 if gotok != tt.ok { 137 t.Errorf("%s: ok = %v; want %v", tt.name, gotok, tt.ok) 138 } 139 if got := buf.String(); got != tt.out { 140 t.Errorf("%s: output differs\nGOT:\n%s\nWANT:\n%s", tt.name, got, tt.out) 141 } 142 } 143 } 144 145 func TestSkipInternal(t *testing.T) { 146 tests := []struct { 147 pkg string 148 want bool 149 }{ 150 {"net/http", true}, 151 {"net/http/internal-foo", true}, 152 {"net/http/internal", false}, 153 {"net/http/internal/bar", false}, 154 {"internal/foo", false}, 155 {"internal", false}, 156 } 157 for _, tt := range tests { 158 got := !internalPkg.MatchString(tt.pkg) 159 if got != tt.want { 160 t.Errorf("%s is internal = %v; want %v", tt.pkg, got, tt.want) 161 } 162 } 163 } 164 165 func BenchmarkAll(b *testing.B) { 166 stds, err := exec.Command("go", "list", "std").Output() 167 if err != nil { 168 b.Fatal(err) 169 } 170 b.ResetTimer() 171 pkgNames := strings.Fields(string(stds)) 172 173 for _, c := range contexts { 174 c.Compiler = build.Default.Compiler 175 } 176 177 for i := 0; i < b.N; i++ { 178 for _, context := range contexts { 179 w := NewWalker(context, filepath.Join(build.Default.GOROOT, "src")) 180 for _, name := range pkgNames { 181 if name != "unsafe" && !strings.HasPrefix(name, "cmd/") { 182 w.export(w.Import(name)) 183 } 184 } 185 w.Features() 186 } 187 } 188 }