github.com/sbinet/go@v0.0.0-20160827155028-54d7de7dd62b/src/go/build/deps_test.go (about) 1 // Copyright 2012 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 // This file exercises the import parser but also checks that 6 // some low-level packages do not have new dependencies added. 7 8 package build 9 10 import ( 11 "bytes" 12 "fmt" 13 "io/ioutil" 14 "os" 15 "path/filepath" 16 "runtime" 17 "sort" 18 "strconv" 19 "strings" 20 "testing" 21 ) 22 23 // pkgDeps defines the expected dependencies between packages in 24 // the Go source tree. It is a statement of policy. 25 // Changes should not be made to this map without prior discussion. 26 // 27 // The map contains two kinds of entries: 28 // 1) Lower-case keys are standard import paths and list the 29 // allowed imports in that package. 30 // 2) Upper-case keys define aliases for package sets, which can then 31 // be used as dependencies by other rules. 32 // 33 // DO NOT CHANGE THIS DATA TO FIX BUILDS. 34 // 35 var pkgDeps = map[string][]string{ 36 // L0 is the lowest level, core, nearly unavoidable packages. 37 "errors": {}, 38 "io": {"errors", "sync"}, 39 "runtime": {"unsafe", "runtime/internal/atomic", "runtime/internal/sys"}, 40 "runtime/internal/sys": {}, 41 "runtime/internal/atomic": {"unsafe", "runtime/internal/sys"}, 42 "internal/race": {"runtime", "unsafe"}, 43 "sync": {"internal/race", "runtime", "sync/atomic", "unsafe"}, 44 "sync/atomic": {"unsafe"}, 45 "unsafe": {}, 46 47 "L0": { 48 "errors", 49 "io", 50 "runtime", 51 "runtime/internal/atomic", 52 "sync", 53 "sync/atomic", 54 "unsafe", 55 }, 56 57 // L1 adds simple functions and strings processing, 58 // but not Unicode tables. 59 "math": {"unsafe"}, 60 "math/cmplx": {"math"}, 61 "math/rand": {"L0", "math"}, 62 "sort": {}, 63 "strconv": {"L0", "unicode/utf8", "math"}, 64 "unicode/utf16": {}, 65 "unicode/utf8": {}, 66 67 "L1": { 68 "L0", 69 "math", 70 "math/cmplx", 71 "math/rand", 72 "sort", 73 "strconv", 74 "unicode/utf16", 75 "unicode/utf8", 76 }, 77 78 // L2 adds Unicode and strings processing. 79 "bufio": {"L0", "unicode/utf8", "bytes"}, 80 "bytes": {"L0", "unicode", "unicode/utf8"}, 81 "path": {"L0", "unicode/utf8", "strings"}, 82 "strings": {"L0", "unicode", "unicode/utf8"}, 83 "unicode": {}, 84 85 "L2": { 86 "L1", 87 "bufio", 88 "bytes", 89 "path", 90 "strings", 91 "unicode", 92 }, 93 94 // L3 adds reflection and some basic utility packages 95 // and interface definitions, but nothing that makes 96 // system calls. 97 "crypto": {"L2", "hash"}, // interfaces 98 "crypto/cipher": {"L2", "crypto/subtle"}, // interfaces 99 "crypto/subtle": {}, 100 "encoding/base32": {"L2"}, 101 "encoding/base64": {"L2"}, 102 "encoding/binary": {"L2", "reflect"}, 103 "hash": {"L2"}, // interfaces 104 "hash/adler32": {"L2", "hash"}, 105 "hash/crc32": {"L2", "hash"}, 106 "hash/crc64": {"L2", "hash"}, 107 "hash/fnv": {"L2", "hash"}, 108 "image": {"L2", "image/color"}, // interfaces 109 "image/color": {"L2"}, // interfaces 110 "image/color/palette": {"L2", "image/color"}, 111 "reflect": {"L2"}, 112 113 "L3": { 114 "L2", 115 "crypto", 116 "crypto/cipher", 117 "crypto/subtle", 118 "encoding/base32", 119 "encoding/base64", 120 "encoding/binary", 121 "hash", 122 "hash/adler32", 123 "hash/crc32", 124 "hash/crc64", 125 "hash/fnv", 126 "image", 127 "image/color", 128 "image/color/palette", 129 "reflect", 130 }, 131 132 // End of linear dependency definitions. 133 134 // Operating system access. 135 "syscall": {"L0", "internal/race", "internal/syscall/windows/sysdll", "unicode/utf16"}, 136 "internal/syscall/unix": {"L0", "syscall"}, 137 "internal/syscall/windows": {"L0", "syscall", "internal/syscall/windows/sysdll"}, 138 "internal/syscall/windows/registry": {"L0", "syscall", "internal/syscall/windows/sysdll", "unicode/utf16"}, 139 "time": { 140 // "L0" without the "io" package: 141 "errors", 142 "runtime", 143 "runtime/internal/atomic", 144 "sync", 145 "sync/atomic", 146 "unsafe", 147 // Other time dependencies: 148 "internal/syscall/windows/registry", 149 "syscall", 150 }, 151 152 "os": {"L1", "os", "syscall", "time", "internal/syscall/windows"}, 153 "path/filepath": {"L2", "os", "syscall"}, 154 "io/ioutil": {"L2", "os", "path/filepath", "time"}, 155 "os/exec": {"L2", "os", "context", "path/filepath", "syscall"}, 156 "os/signal": {"L2", "os", "syscall"}, 157 158 // OS enables basic operating system functionality, 159 // but not direct use of package syscall, nor os/signal. 160 "OS": { 161 "io/ioutil", 162 "os", 163 "os/exec", 164 "path/filepath", 165 "time", 166 }, 167 168 // Formatted I/O: few dependencies (L1) but we must add reflect. 169 "fmt": {"L1", "os", "reflect"}, 170 "log": {"L1", "os", "fmt", "time"}, 171 172 // Packages used by testing must be low-level (L2+fmt). 173 "regexp": {"L2", "regexp/syntax"}, 174 "regexp/syntax": {"L2"}, 175 "runtime/debug": {"L2", "fmt", "io/ioutil", "os", "time"}, 176 "runtime/pprof": {"L2", "fmt", "os", "text/tabwriter"}, 177 "runtime/trace": {"L0"}, 178 "text/tabwriter": {"L2"}, 179 180 "testing": {"L2", "flag", "fmt", "os", "runtime/debug", "runtime/pprof", "runtime/trace", "time"}, 181 "testing/iotest": {"L2", "log"}, 182 "testing/quick": {"L2", "flag", "fmt", "reflect"}, 183 "internal/testenv": {"L2", "OS", "flag", "testing", "syscall"}, 184 185 // L4 is defined as L3+fmt+log+time, because in general once 186 // you're using L3 packages, use of fmt, log, or time is not a big deal. 187 "L4": { 188 "L3", 189 "fmt", 190 "log", 191 "time", 192 }, 193 194 // Go parser. 195 "go/ast": {"L4", "OS", "go/scanner", "go/token"}, 196 "go/doc": {"L4", "go/ast", "go/token", "regexp", "text/template"}, 197 "go/parser": {"L4", "OS", "go/ast", "go/scanner", "go/token"}, 198 "go/printer": {"L4", "OS", "go/ast", "go/scanner", "go/token", "text/tabwriter"}, 199 "go/scanner": {"L4", "OS", "go/token"}, 200 "go/token": {"L4"}, 201 202 "GOPARSER": { 203 "go/ast", 204 "go/doc", 205 "go/parser", 206 "go/printer", 207 "go/scanner", 208 "go/token", 209 }, 210 211 "go/format": {"L4", "GOPARSER", "internal/format"}, 212 "internal/format": {"L4", "GOPARSER"}, 213 214 // Go type checking. 215 "go/constant": {"L4", "go/token", "math/big"}, 216 "go/importer": {"L4", "go/internal/gcimporter", "go/internal/gccgoimporter", "go/types"}, 217 "go/internal/gcimporter": {"L4", "OS", "go/build", "go/constant", "go/token", "go/types", "text/scanner"}, 218 "go/internal/gccgoimporter": {"L4", "OS", "debug/elf", "go/constant", "go/token", "go/types", "text/scanner"}, 219 "go/types": {"L4", "GOPARSER", "container/heap", "go/constant"}, 220 221 // One of a kind. 222 "archive/tar": {"L4", "OS", "syscall"}, 223 "archive/zip": {"L4", "OS", "compress/flate"}, 224 "container/heap": {"sort"}, 225 "compress/bzip2": {"L4"}, 226 "compress/flate": {"L4"}, 227 "compress/gzip": {"L4", "compress/flate"}, 228 "compress/lzw": {"L4"}, 229 "compress/zlib": {"L4", "compress/flate"}, 230 "context": {"errors", "fmt", "reflect", "sync", "time"}, 231 "database/sql": {"L4", "container/list", "database/sql/driver"}, 232 "database/sql/driver": {"L4", "time"}, 233 "debug/dwarf": {"L4"}, 234 "debug/elf": {"L4", "OS", "debug/dwarf", "compress/zlib"}, 235 "debug/gosym": {"L4"}, 236 "debug/macho": {"L4", "OS", "debug/dwarf"}, 237 "debug/pe": {"L4", "OS", "debug/dwarf"}, 238 "debug/plan9obj": {"L4", "OS"}, 239 "encoding": {"L4"}, 240 "encoding/ascii85": {"L4"}, 241 "encoding/asn1": {"L4", "math/big"}, 242 "encoding/csv": {"L4"}, 243 "encoding/gob": {"L4", "OS", "encoding"}, 244 "encoding/hex": {"L4"}, 245 "encoding/json": {"L4", "encoding"}, 246 "encoding/pem": {"L4"}, 247 "encoding/xml": {"L4", "encoding"}, 248 "flag": {"L4", "OS"}, 249 "go/build": {"L4", "OS", "GOPARSER"}, 250 "html": {"L4"}, 251 "image/draw": {"L4", "image/internal/imageutil"}, 252 "image/gif": {"L4", "compress/lzw", "image/color/palette", "image/draw"}, 253 "image/internal/imageutil": {"L4"}, 254 "image/jpeg": {"L4", "image/internal/imageutil"}, 255 "image/png": {"L4", "compress/zlib"}, 256 "index/suffixarray": {"L4", "regexp"}, 257 "internal/singleflight": {"sync"}, 258 "internal/trace": {"L4", "OS"}, 259 "math/big": {"L4"}, 260 "mime": {"L4", "OS", "syscall", "internal/syscall/windows/registry"}, 261 "mime/quotedprintable": {"L4"}, 262 "net/internal/socktest": {"L4", "OS", "syscall"}, 263 "net/url": {"L4"}, 264 "text/scanner": {"L4", "OS"}, 265 "text/template/parse": {"L4"}, 266 267 "html/template": { 268 "L4", "OS", "encoding/json", "html", "text/template", 269 "text/template/parse", 270 }, 271 "text/template": { 272 "L4", "OS", "net/url", "text/template/parse", 273 }, 274 275 // Cgo. 276 // If you add a dependency on CGO, you must add the package to 277 // cgoPackages in cmd/dist/test.go. 278 "runtime/cgo": {"L0", "C"}, 279 "CGO": {"C", "runtime/cgo"}, 280 281 // Fake entry to satisfy the pseudo-import "C" 282 // that shows up in programs that use cgo. 283 "C": {}, 284 285 // Race detector/MSan uses cgo. 286 "runtime/race": {"C"}, 287 "runtime/msan": {"C"}, 288 289 // Plan 9 alone needs io/ioutil and os. 290 "os/user": {"L4", "CGO", "io/ioutil", "os", "syscall"}, 291 292 // Basic networking. 293 // Because net must be used by any package that wants to 294 // do networking portably, it must have a small dependency set: just L0+basic os. 295 "net": { 296 "L0", "CGO", 297 "context", "math/rand", "os", "sort", "syscall", "time", 298 "internal/nettrace", 299 "internal/syscall/windows", "internal/singleflight", "internal/race", 300 "golang_org/x/net/route", 301 }, 302 303 // NET enables use of basic network-related packages. 304 "NET": { 305 "net", 306 "mime", 307 "net/textproto", 308 "net/url", 309 }, 310 311 // Uses of networking. 312 "log/syslog": {"L4", "OS", "net"}, 313 "net/mail": {"L4", "NET", "OS", "mime"}, 314 "net/textproto": {"L4", "OS", "net"}, 315 316 // Core crypto. 317 "crypto/aes": {"L3"}, 318 "crypto/des": {"L3"}, 319 "crypto/hmac": {"L3"}, 320 "crypto/md5": {"L3"}, 321 "crypto/rc4": {"L3"}, 322 "crypto/sha1": {"L3"}, 323 "crypto/sha256": {"L3"}, 324 "crypto/sha512": {"L3"}, 325 326 "CRYPTO": { 327 "crypto/aes", 328 "crypto/des", 329 "crypto/hmac", 330 "crypto/md5", 331 "crypto/rc4", 332 "crypto/sha1", 333 "crypto/sha256", 334 "crypto/sha512", 335 }, 336 337 // Random byte, number generation. 338 // This would be part of core crypto except that it imports 339 // math/big, which imports fmt. 340 "crypto/rand": {"L4", "CRYPTO", "OS", "math/big", "syscall", "internal/syscall/unix"}, 341 342 // Mathematical crypto: dependencies on fmt (L4) and math/big. 343 // We could avoid some of the fmt, but math/big imports fmt anyway. 344 "crypto/dsa": {"L4", "CRYPTO", "math/big"}, 345 "crypto/ecdsa": {"L4", "CRYPTO", "crypto/elliptic", "math/big", "encoding/asn1"}, 346 "crypto/elliptic": {"L4", "CRYPTO", "math/big"}, 347 "crypto/rsa": {"L4", "CRYPTO", "crypto/rand", "math/big"}, 348 349 "CRYPTO-MATH": { 350 "CRYPTO", 351 "crypto/dsa", 352 "crypto/ecdsa", 353 "crypto/elliptic", 354 "crypto/rand", 355 "crypto/rsa", 356 "encoding/asn1", 357 "math/big", 358 }, 359 360 // SSL/TLS. 361 "crypto/tls": { 362 "L4", "CRYPTO-MATH", "OS", 363 "container/list", "crypto/x509", "encoding/pem", "net", "syscall", 364 }, 365 "crypto/x509": { 366 "L4", "CRYPTO-MATH", "OS", "CGO", 367 "crypto/x509/pkix", "encoding/pem", "encoding/hex", "net", "syscall", 368 }, 369 "crypto/x509/pkix": {"L4", "CRYPTO-MATH"}, 370 371 // Simple net+crypto-aware packages. 372 "mime/multipart": {"L4", "OS", "mime", "crypto/rand", "net/textproto", "mime/quotedprintable"}, 373 "net/smtp": {"L4", "CRYPTO", "NET", "crypto/tls"}, 374 375 // HTTP, kingpin of dependencies. 376 "net/http": { 377 "L4", "NET", "OS", 378 "context", "compress/gzip", "container/list", "crypto/tls", 379 "mime/multipart", "runtime/debug", 380 "net/http/internal", 381 "golang_org/x/net/http2/hpack", 382 "golang_org/x/net/lex/httplex", 383 "internal/nettrace", 384 "net/http/httptrace", 385 }, 386 "net/http/internal": {"L4"}, 387 "net/http/httptrace": {"context", "internal/nettrace", "net", "reflect", "time"}, 388 389 // HTTP-using packages. 390 "expvar": {"L4", "OS", "encoding/json", "net/http"}, 391 "net/http/cgi": {"L4", "NET", "OS", "crypto/tls", "net/http", "regexp"}, 392 "net/http/cookiejar": {"L4", "NET", "net/http"}, 393 "net/http/fcgi": {"L4", "NET", "OS", "net/http", "net/http/cgi"}, 394 "net/http/httptest": {"L4", "NET", "OS", "crypto/tls", "flag", "net/http", "net/http/internal"}, 395 "net/http/httputil": {"L4", "NET", "OS", "net/http", "net/http/internal"}, 396 "net/http/pprof": {"L4", "OS", "html/template", "net/http", "runtime/pprof", "runtime/trace"}, 397 "net/rpc": {"L4", "NET", "encoding/gob", "html/template", "net/http"}, 398 "net/rpc/jsonrpc": {"L4", "NET", "encoding/json", "net/rpc"}, 399 } 400 401 // isMacro reports whether p is a package dependency macro 402 // (uppercase name). 403 func isMacro(p string) bool { 404 return 'A' <= p[0] && p[0] <= 'Z' 405 } 406 407 func allowed(pkg string) map[string]bool { 408 m := map[string]bool{} 409 var allow func(string) 410 allow = func(p string) { 411 if m[p] { 412 return 413 } 414 m[p] = true // set even for macros, to avoid loop on cycle 415 416 // Upper-case names are macro-expanded. 417 if isMacro(p) { 418 for _, pp := range pkgDeps[p] { 419 allow(pp) 420 } 421 } 422 } 423 for _, pp := range pkgDeps[pkg] { 424 allow(pp) 425 } 426 return m 427 } 428 429 // listStdPkgs returns the same list of packages as "go list std". 430 func listStdPkgs(goroot string) ([]string, error) { 431 // Based on cmd/go's matchPackages function. 432 var pkgs []string 433 434 src := filepath.Join(goroot, "src") + string(filepath.Separator) 435 walkFn := func(path string, fi os.FileInfo, err error) error { 436 if err != nil || !fi.IsDir() || path == src { 437 return nil 438 } 439 440 base := filepath.Base(path) 441 if strings.HasPrefix(base, ".") || strings.HasPrefix(base, "_") || base == "testdata" { 442 return filepath.SkipDir 443 } 444 445 name := filepath.ToSlash(path[len(src):]) 446 if name == "builtin" || name == "cmd" || strings.Contains(name, "golang_org") { 447 return filepath.SkipDir 448 } 449 450 pkgs = append(pkgs, name) 451 return nil 452 } 453 if err := filepath.Walk(src, walkFn); err != nil { 454 return nil, err 455 } 456 return pkgs, nil 457 } 458 459 func TestDependencies(t *testing.T) { 460 iOS := runtime.GOOS == "darwin" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64") 461 if runtime.GOOS == "nacl" || iOS { 462 // Tests run in a limited file system and we do not 463 // provide access to every source file. 464 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH) 465 } 466 467 ctxt := Default 468 all, err := listStdPkgs(ctxt.GOROOT) 469 if err != nil { 470 t.Fatal(err) 471 } 472 sort.Strings(all) 473 474 for _, pkg := range all { 475 imports, err := findImports(pkg) 476 if err != nil { 477 t.Error(err) 478 continue 479 } 480 ok := allowed(pkg) 481 var bad []string 482 for _, imp := range imports { 483 if !ok[imp] { 484 bad = append(bad, imp) 485 } 486 } 487 if bad != nil { 488 t.Errorf("unexpected dependency: %s imports %v", pkg, bad) 489 } 490 } 491 } 492 493 var buildIgnore = []byte("\n// +build ignore") 494 495 func findImports(pkg string) ([]string, error) { 496 dir := filepath.Join(Default.GOROOT, "src", pkg) 497 files, err := ioutil.ReadDir(dir) 498 if err != nil { 499 return nil, err 500 } 501 var imports []string 502 var haveImport = map[string]bool{} 503 for _, file := range files { 504 name := file.Name() 505 if !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") { 506 continue 507 } 508 f, err := os.Open(filepath.Join(dir, name)) 509 if err != nil { 510 return nil, err 511 } 512 var imp []string 513 data, err := readImports(f, false, &imp) 514 f.Close() 515 if err != nil { 516 return nil, fmt.Errorf("reading %v: %v", name, err) 517 } 518 if bytes.Contains(data, buildIgnore) { 519 continue 520 } 521 for _, quoted := range imp { 522 path, err := strconv.Unquote(quoted) 523 if err != nil { 524 continue 525 } 526 if !haveImport[path] { 527 haveImport[path] = true 528 imports = append(imports, path) 529 } 530 } 531 } 532 sort.Strings(imports) 533 return imports, nil 534 }