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