]> Cypherpunks.ru repositories - gostls13.git/blob - src/go/build/deps_test.go
[dev.boringcrypto] all: merge master into dev.boringcrypto
[gostls13.git] / src / go / build / deps_test.go
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         "go/token"
14         "internal/testenv"
15         "io/fs"
16         "os"
17         "path/filepath"
18         "runtime"
19         "sort"
20         "strings"
21         "testing"
22 )
23
24 // depsRules defines the expected dependencies between packages in
25 // the Go source tree. It is a statement of policy.
26 //
27 // DO NOT CHANGE THIS DATA TO FIX BUILDS.
28 // Existing packages should not have their constraints relaxed
29 // without prior discussion.
30 // Negative assertions should almost never be removed.
31 //
32 // The general syntax of a rule is:
33 //
34 //              a, b < c, d;
35 //
36 // which means c and d come after a and b in the partial order
37 // (that is, c and d can import a and b),
38 // but doesn't provide a relative order between a vs b or c vs d.
39 //
40 // The rules can chain together, as in:
41 //
42 //              e < f, g < h;
43 //
44 // which is equivalent to
45 //
46 //              e < f, g;
47 //              f, g < h;
48 //
49 // Except for the special bottom element "NONE", each name
50 // must appear exactly once on the right-hand side of a rule.
51 // That rule serves as the definition of the allowed dependencies
52 // for that name. The definition must appear before any uses
53 // of the name on the left-hand side of a rule. (That is, the
54 // rules themselves must be ordered according to the partial
55 // order, for easier reading by people.)
56 //
57 // Negative assertions double-check the partial order:
58 //
59 //              i !< j
60 //
61 // means that it must NOT be the case that i < j.
62 // Negative assertions may appear anywhere in the rules,
63 // even before i and j have been defined.
64 //
65 // Comments begin with #.
66 //
67 // All-caps names are pseudo-names for specific points
68 // in the dependency lattice.
69 //
70 var depsRules = `
71         # No dependencies allowed for any of these packages.
72         NONE
73         < constraints, container/list, container/ring,
74           internal/cfg, internal/cpu, internal/goarch,
75           internal/goexperiment, internal/goos,
76           internal/goversion, internal/nettrace,
77           unicode/utf8, unicode/utf16, unicode,
78           unsafe;
79
80         # These packages depend only on internal/goarch and unsafe.
81         internal/goarch, unsafe
82         < internal/abi;
83
84         # RUNTIME is the core runtime group of packages, all of them very light-weight.
85         internal/abi, internal/cpu, internal/goarch,
86         internal/goexperiment, internal/goos, unsafe
87         < internal/bytealg
88         < internal/itoa
89         < internal/unsafeheader
90         < runtime/internal/sys
91         < runtime/internal/syscall
92         < runtime/internal/atomic
93         < runtime/internal/math
94         < runtime
95         < sync/atomic
96         < internal/race
97         < sync
98         < internal/reflectlite
99         < errors
100         < internal/oserror, math/bits
101         < RUNTIME;
102
103         RUNTIME
104         < sort
105         < container/heap;
106
107         RUNTIME
108         < io;
109
110         syscall !< io;
111         reflect !< sort;
112
113         RUNTIME, unicode/utf8
114         < path;
115
116         unicode !< path;
117
118         # SYSCALL is RUNTIME plus the packages necessary for basic system calls.
119         RUNTIME, unicode/utf8, unicode/utf16
120         < internal/syscall/windows/sysdll, syscall/js
121         < syscall
122         < internal/syscall/unix, internal/syscall/windows, internal/syscall/windows/registry
123         < internal/syscall/execenv
124         < SYSCALL;
125
126         # TIME is SYSCALL plus the core packages about time, including context.
127         SYSCALL
128         < time/tzdata
129         < time
130         < context
131         < TIME;
132
133         TIME, io, path, sort
134         < io/fs;
135
136         # MATH is RUNTIME plus the basic math packages.
137         RUNTIME
138         < math
139         < MATH;
140
141         unicode !< math;
142
143         MATH
144         < math/cmplx;
145
146         MATH
147         < math/rand;
148
149         MATH
150         < runtime/metrics;
151
152         MATH, unicode/utf8
153         < strconv;
154
155         unicode !< strconv;
156
157         # STR is basic string and buffer manipulation.
158         RUNTIME, io, unicode/utf8, unicode/utf16, unicode
159         < bytes, strings
160         < bufio;
161
162         bufio, path, strconv
163         < STR;
164
165         # OS is basic OS access, including helpers (path/filepath, os/exec, etc).
166         # OS includes string routines, but those must be layered above package os.
167         # OS does not include reflection.
168         io/fs
169         < internal/testlog
170         < internal/poll
171         < os
172         < os/signal;
173
174         io/fs
175         < embed;
176
177         unicode, fmt !< net, os, os/signal;
178
179         os/signal, STR
180         < path/filepath
181         < io/ioutil, os/exec;
182
183         io/ioutil, os/exec, os/signal
184         < OS;
185
186         reflect !< OS;
187
188         OS
189         < golang.org/x/sys/cpu;
190
191         os < internal/godebug;
192
193         # FMT is OS (which includes string routines) plus reflect and fmt.
194         # It does not include package log, which should be avoided in core packages.
195         strconv, unicode
196         < reflect;
197
198         os, reflect
199         < internal/fmtsort
200         < fmt;
201
202         OS, fmt
203         < FMT;
204
205         log !< FMT;
206
207         OS, FMT
208         < internal/execabs;
209
210         OS, internal/execabs
211         < internal/goroot;
212
213         # Misc packages needing only FMT.
214         FMT
215         < html,
216           mime/quotedprintable,
217           net/internal/socktest,
218           net/url,
219           runtime/trace,
220           text/scanner,
221           text/tabwriter;
222
223         # encodings
224         # core ones do not use fmt.
225         io, strconv
226         < encoding;
227
228         encoding, reflect
229         < encoding/binary
230         < encoding/base32, encoding/base64;
231
232         FMT, encoding < flag;
233
234         fmt !< encoding/base32, encoding/base64;
235
236         FMT, encoding/base32, encoding/base64
237         < encoding/ascii85, encoding/csv, encoding/gob, encoding/hex,
238           encoding/json, encoding/pem, encoding/xml, mime;
239
240         # hashes
241         io
242         < hash
243         < hash/adler32, hash/crc32, hash/crc64, hash/fnv, hash/maphash;
244
245         # math/big
246         FMT, encoding/binary, math/rand
247         < math/big;
248
249         # compression
250         FMT, encoding/binary, hash/adler32, hash/crc32
251         < compress/bzip2, compress/flate, compress/lzw
252         < archive/zip, compress/gzip, compress/zlib;
253
254         # templates
255         FMT
256         < text/template/parse;
257
258         net/url, text/template/parse
259         < text/template
260         < internal/lazytemplate;
261
262         encoding/json, html, text/template
263         < html/template;
264
265         # regexp
266         FMT
267         < regexp/syntax
268         < regexp
269         < internal/lazyregexp;
270
271         # suffix array
272         encoding/binary, regexp
273         < index/suffixarray;
274
275         # executable parsing
276         FMT, encoding/binary, compress/zlib
277         < runtime/debug
278         < debug/dwarf
279         < debug/elf, debug/gosym, debug/macho, debug/pe, debug/plan9obj, internal/xcoff
280         < debug/buildinfo
281         < DEBUG;
282
283         # go parser and friends.
284         FMT
285         < go/token
286         < go/scanner
287         < go/ast
288         < go/internal/typeparams
289         < go/parser;
290
291         FMT
292         < go/build/constraint;
293
294         go/build/constraint, go/parser, text/tabwriter
295         < go/printer
296         < go/format;
297
298         go/parser, internal/lazyregexp, text/template
299         < go/doc;
300
301         math/big, go/token
302         < go/constant;
303
304         container/heap, go/constant, go/parser, regexp
305         < go/types;
306
307         FMT, internal/goexperiment
308         < internal/buildcfg;
309
310         go/build/constraint, go/doc, go/parser, internal/buildcfg, internal/goroot, internal/goversion
311         < go/build;
312
313         # databases
314         FMT
315         < database/sql/internal
316         < database/sql/driver
317         < database/sql;
318
319         # images
320         FMT, compress/lzw, compress/zlib
321         < image/color
322         < image, image/color/palette
323         < image/internal/imageutil
324         < image/draw
325         < image/gif, image/jpeg, image/png;
326
327         # cgo, delayed as long as possible.
328         # If you add a dependency on CGO, you must add the package
329         # to cgoPackages in cmd/dist/test.go as well.
330         RUNTIME
331         < C
332         < runtime/cgo
333         < CGO
334         < runtime/race, runtime/msan, runtime/asan;
335
336         # Bulk of the standard library must not use cgo.
337         # The prohibition stops at net and os/user.
338         C !< fmt, go/types;
339
340         CGO, OS
341         < plugin;
342
343         CGO, FMT
344         < os/user
345         < archive/tar;
346
347         sync
348         < internal/singleflight;
349
350         os
351         < golang.org/x/net/dns/dnsmessage,
352           golang.org/x/net/lif,
353           golang.org/x/net/route;
354
355         os, runtime, strconv, sync, unsafe,
356         internal/godebug
357         < internal/intern;
358
359         internal/bytealg, internal/intern, internal/itoa, math/bits, sort, strconv
360         < net/netip;
361
362         # net is unavoidable when doing any networking,
363         # so large dependencies must be kept out.
364         # This is a long-looking list but most of these
365         # are small with few dependencies.
366         CGO,
367         golang.org/x/net/dns/dnsmessage,
368         golang.org/x/net/lif,
369         golang.org/x/net/route,
370         internal/godebug,
371         internal/nettrace,
372         internal/poll,
373         internal/singleflight,
374         internal/race,
375         net/netip,
376         os
377         < net;
378
379         fmt, unicode !< net;
380         math/rand !< net; # net uses runtime instead
381
382         # NET is net plus net-helper packages.
383         FMT, net
384         < net/textproto;
385
386         mime, net/textproto, net/url
387         < NET;
388
389         # logging - most packages should not import; http and up is allowed
390         FMT
391         < log;
392
393         log !< crypto/tls, database/sql, go/importer, testing;
394
395         FMT, log, net
396         < log/syslog;
397
398         NET, log
399         < net/mail;
400
401         NONE < crypto/internal/boring/sig;
402         sync/atomic < crypto/internal/boring/fipstls;
403
404         encoding/binary, golang.org/x/sys/cpu, hash,
405         FMT, math/big, embed,
406         CGO, crypto/internal/boring/sig, crypto/internal/boring/fipstls
407         < crypto
408         < crypto/subtle
409         < crypto/internal/subtle
410         < crypto/elliptic/internal/fiat
411         < crypto/elliptic/internal/nistec
412         < crypto/ed25519/internal/edwards25519/field, golang.org/x/crypto/curve25519/internal/field
413         < crypto/ed25519/internal/edwards25519
414         < crypto/cipher
415         < encoding/asn1
416         < crypto/internal/boring
417         < crypto/aes, crypto/des, crypto/hmac, crypto/md5, crypto/rc4,
418           crypto/sha1, crypto/sha256, crypto/sha512
419         < crypto/rand
420         < crypto/internal/randutil
421         < crypto/ed25519
422         < golang.org/x/crypto/cryptobyte/asn1
423         < golang.org/x/crypto/cryptobyte
424         < golang.org/x/crypto/curve25519
425         < crypto/dsa, crypto/elliptic, crypto/rsa
426         < crypto/ecdsa
427         < CRYPTO-BORING;
428
429         net !< CRYPTO-BORING;
430
431         # TLS, Prince of Dependencies.
432         CRYPTO-BORING, NET, container/list, encoding/hex, encoding/pem
433         < golang.org/x/crypto/internal/subtle
434         < golang.org/x/crypto/chacha20
435         < golang.org/x/crypto/internal/poly1305
436         < golang.org/x/crypto/chacha20poly1305
437         < golang.org/x/crypto/hkdf
438         < crypto/x509/internal/macos
439         < crypto/x509/pkix
440         < crypto/x509
441         < crypto/tls;
442
443         crypto/internal/boring/sig, crypto/internal/boring/fipstls
444         < crypto/tls/fipsonly;
445
446         crypto/internal/boring
447         < crypto/boring;
448
449         # crypto-aware packages
450
451         CRYPTO-BORING, DEBUG, go/build, go/types, text/scanner
452         < internal/pkgbits
453         < go/internal/gcimporter, go/internal/gccgoimporter, go/internal/srcimporter
454         < go/importer;
455
456         NET, crypto/rand, mime/quotedprintable
457         < mime/multipart;
458
459         crypto/tls
460         < net/smtp;
461
462         # HTTP, King of Dependencies.
463
464         FMT
465         < golang.org/x/net/http2/hpack
466         < net/http/internal, net/http/internal/ascii, net/http/internal/testcert;
467
468         FMT, NET, container/list, encoding/binary, log
469         < golang.org/x/text/transform
470         < golang.org/x/text/unicode/norm
471         < golang.org/x/text/unicode/bidi
472         < golang.org/x/text/secure/bidirule
473         < golang.org/x/net/idna
474         < golang.org/x/net/http/httpguts, golang.org/x/net/http/httpproxy;
475
476         NET, crypto/tls
477         < net/http/httptrace;
478
479         compress/gzip,
480         golang.org/x/net/http/httpguts,
481         golang.org/x/net/http/httpproxy,
482         golang.org/x/net/http2/hpack,
483         net/http/internal,
484         net/http/internal/ascii,
485         net/http/internal/testcert,
486         net/http/httptrace,
487         mime/multipart,
488         log
489         < net/http;
490
491         # HTTP-aware packages
492
493         encoding/json, net/http
494         < expvar;
495
496         net/http, net/http/internal/ascii
497         < net/http/cookiejar, net/http/httputil;
498
499         net/http, flag
500         < net/http/httptest;
501
502         net/http, regexp
503         < net/http/cgi
504         < net/http/fcgi;
505
506         # Profiling
507         FMT, compress/gzip, encoding/binary, text/tabwriter
508         < runtime/pprof;
509
510         OS, compress/gzip, regexp
511         < internal/profile;
512
513         html, internal/profile, net/http, runtime/pprof, runtime/trace
514         < net/http/pprof;
515
516         # RPC
517         encoding/gob, encoding/json, go/token, html/template, net/http
518         < net/rpc
519         < net/rpc/jsonrpc;
520
521         # System Information
522         internal/cpu, sync
523         < internal/sysinfo;
524
525         # Test-only
526         log
527         < testing/iotest
528         < testing/fstest;
529
530         FMT, flag, math/rand
531         < testing/quick;
532
533         FMT, DEBUG, flag, runtime/trace, internal/sysinfo, math/rand
534         < testing;
535
536         FMT, crypto/sha256, encoding/json, go/ast, go/parser, go/token,
537         internal/godebug, math/rand, encoding/hex, crypto/sha256
538         < internal/fuzz;
539
540         internal/fuzz, internal/testlog, runtime/pprof, regexp
541         < testing/internal/testdeps;
542
543         OS, flag, testing, internal/cfg
544         < internal/testenv;
545
546         OS, encoding/base64
547         < internal/obscuretestdata;
548
549         CGO, OS, fmt
550         < os/signal/internal/pty;
551
552         NET, testing, math/rand
553         < golang.org/x/net/nettest;
554
555         syscall
556         < os/exec/internal/fdtest;
557
558         FMT, container/heap, math/rand
559         < internal/trace;
560
561         FMT
562         < internal/diff, internal/txtar;
563 `
564
565 // listStdPkgs returns the same list of packages as "go list std".
566 func listStdPkgs(goroot string) ([]string, error) {
567         // Based on cmd/go's matchPackages function.
568         var pkgs []string
569
570         src := filepath.Join(goroot, "src") + string(filepath.Separator)
571         walkFn := func(path string, d fs.DirEntry, err error) error {
572                 if err != nil || !d.IsDir() || path == src {
573                         return nil
574                 }
575
576                 base := filepath.Base(path)
577                 if strings.HasPrefix(base, ".") || strings.HasPrefix(base, "_") || base == "testdata" {
578                         return filepath.SkipDir
579                 }
580
581                 name := filepath.ToSlash(path[len(src):])
582                 if name == "builtin" || name == "cmd" {
583                         return filepath.SkipDir
584                 }
585
586                 pkgs = append(pkgs, strings.TrimPrefix(name, "vendor/"))
587                 return nil
588         }
589         if err := filepath.WalkDir(src, walkFn); err != nil {
590                 return nil, err
591         }
592         return pkgs, nil
593 }
594
595 func TestDependencies(t *testing.T) {
596         if !testenv.HasSrc() {
597                 // Tests run in a limited file system and we do not
598                 // provide access to every source file.
599                 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
600         }
601
602         ctxt := Default
603         all, err := listStdPkgs(ctxt.GOROOT)
604         if err != nil {
605                 t.Fatal(err)
606         }
607         sort.Strings(all)
608
609         sawImport := map[string]map[string]bool{} // from package => to package => true
610         policy := depsPolicy(t)
611
612         for _, pkg := range all {
613                 imports, err := findImports(pkg)
614                 if err != nil {
615                         t.Error(err)
616                         continue
617                 }
618                 if sawImport[pkg] == nil {
619                         sawImport[pkg] = map[string]bool{}
620                 }
621                 ok := policy[pkg]
622                 var bad []string
623                 for _, imp := range imports {
624                         sawImport[pkg][imp] = true
625                         if !ok[imp] {
626                                 bad = append(bad, imp)
627                         }
628                 }
629                 if bad != nil {
630                         t.Errorf("unexpected dependency: %s imports %v", pkg, bad)
631                 }
632         }
633 }
634
635 var buildIgnore = []byte("\n//go:build ignore")
636
637 func findImports(pkg string) ([]string, error) {
638         vpkg := pkg
639         if strings.HasPrefix(pkg, "golang.org") {
640                 vpkg = "vendor/" + pkg
641         }
642         dir := filepath.Join(Default.GOROOT, "src", vpkg)
643         files, err := os.ReadDir(dir)
644         if err != nil {
645                 return nil, err
646         }
647         var imports []string
648         var haveImport = map[string]bool{}
649         fset := token.NewFileSet()
650         for _, file := range files {
651                 name := file.Name()
652                 if name == "slice_go14.go" || name == "slice_go18.go" {
653                         // These files are for compiler bootstrap with older versions of Go and not built in the standard build.
654                         continue
655                 }
656                 if !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") {
657                         continue
658                 }
659                 info := fileInfo{
660                         name: filepath.Join(dir, name),
661                         fset: fset,
662                 }
663                 f, err := os.Open(info.name)
664                 if err != nil {
665                         return nil, err
666                 }
667                 err = readGoInfo(f, &info)
668                 f.Close()
669                 if err != nil {
670                         return nil, fmt.Errorf("reading %v: %v", name, err)
671                 }
672                 if info.parsed.Name.Name == "main" {
673                         continue
674                 }
675                 if bytes.Contains(info.header, buildIgnore) {
676                         continue
677                 }
678                 for _, imp := range info.imports {
679                         path := imp.path
680                         if !haveImport[path] {
681                                 haveImport[path] = true
682                                 imports = append(imports, path)
683                         }
684                 }
685         }
686         sort.Strings(imports)
687         return imports, nil
688 }
689
690 // depsPolicy returns a map m such that m[p][d] == true when p can import d.
691 func depsPolicy(t *testing.T) map[string]map[string]bool {
692         allowed := map[string]map[string]bool{"NONE": {}}
693         disallowed := [][2][]string{}
694
695         parseDepsRules(t, func(deps []string, op string, users []string) {
696                 if op == "!<" {
697                         disallowed = append(disallowed, [2][]string{deps, users})
698                         return
699                 }
700                 for _, u := range users {
701                         if allowed[u] != nil {
702                                 t.Errorf("multiple deps lists for %s", u)
703                         }
704                         allowed[u] = make(map[string]bool)
705                         for _, d := range deps {
706                                 if allowed[d] == nil {
707                                         t.Errorf("use of %s before its deps list", d)
708                                 }
709                                 allowed[u][d] = true
710                         }
711                 }
712         })
713
714         // Check for missing deps info.
715         for _, deps := range allowed {
716                 for d := range deps {
717                         if allowed[d] == nil {
718                                 t.Errorf("missing deps list for %s", d)
719                         }
720                 }
721         }
722
723         // Complete transitive allowed deps.
724         for k := range allowed {
725                 for i := range allowed {
726                         for j := range allowed {
727                                 if i != k && k != j && allowed[i][k] && allowed[k][j] {
728                                         if i == j {
729                                                 // Can only happen along with a "use of X before deps" error above,
730                                                 // but this error is more specific - it makes clear that reordering the
731                                                 // rules will not be enough to fix the problem.
732                                                 t.Errorf("deps policy cycle: %s < %s < %s", j, k, i)
733                                         }
734                                         allowed[i][j] = true
735                                 }
736                         }
737                 }
738         }
739
740         // Check negative assertions against completed allowed deps.
741         for _, bad := range disallowed {
742                 deps, users := bad[0], bad[1]
743                 for _, d := range deps {
744                         for _, u := range users {
745                                 if allowed[u][d] {
746                                         t.Errorf("deps policy incorrect: assertion failed: %s !< %s", d, u)
747                                 }
748                         }
749                 }
750         }
751
752         if t.Failed() {
753                 t.FailNow()
754         }
755
756         return allowed
757 }
758
759 // parseDepsRules parses depsRules, calling save(deps, op, users)
760 // for each deps < users or deps !< users rule
761 // (op is "<" or "!<").
762 func parseDepsRules(t *testing.T, save func(deps []string, op string, users []string)) {
763         p := &depsParser{t: t, lineno: 1, text: depsRules}
764
765         var prev []string
766         var op string
767         for {
768                 list, tok := p.nextList()
769                 if tok == "" {
770                         if prev == nil {
771                                 break
772                         }
773                         p.syntaxError("unexpected EOF")
774                 }
775                 if prev != nil {
776                         save(prev, op, list)
777                 }
778                 prev = list
779                 if tok == ";" {
780                         prev = nil
781                         op = ""
782                         continue
783                 }
784                 if tok != "<" && tok != "!<" {
785                         p.syntaxError("missing <")
786                 }
787                 op = tok
788         }
789 }
790
791 // A depsParser parses the depsRules syntax described above.
792 type depsParser struct {
793         t        *testing.T
794         lineno   int
795         lastWord string
796         text     string
797 }
798
799 // syntaxError reports a parsing error.
800 func (p *depsParser) syntaxError(msg string) {
801         p.t.Fatalf("deps:%d: syntax error: %s near %s", p.lineno, msg, p.lastWord)
802 }
803
804 // nextList parses and returns a comma-separated list of names.
805 func (p *depsParser) nextList() (list []string, token string) {
806         for {
807                 tok := p.nextToken()
808                 switch tok {
809                 case "":
810                         if len(list) == 0 {
811                                 return nil, ""
812                         }
813                         fallthrough
814                 case ",", "<", "!<", ";":
815                         p.syntaxError("bad list syntax")
816                 }
817                 list = append(list, tok)
818
819                 tok = p.nextToken()
820                 if tok != "," {
821                         return list, tok
822                 }
823         }
824 }
825
826 // nextToken returns the next token in the deps rules,
827 // one of ";" "," "<" "!<" or a name.
828 func (p *depsParser) nextToken() string {
829         for {
830                 if p.text == "" {
831                         return ""
832                 }
833                 switch p.text[0] {
834                 case ';', ',', '<':
835                         t := p.text[:1]
836                         p.text = p.text[1:]
837                         return t
838
839                 case '!':
840                         if len(p.text) < 2 || p.text[1] != '<' {
841                                 p.syntaxError("unexpected token !")
842                         }
843                         p.text = p.text[2:]
844                         return "!<"
845
846                 case '#':
847                         i := strings.Index(p.text, "\n")
848                         if i < 0 {
849                                 i = len(p.text)
850                         }
851                         p.text = p.text[i:]
852                         continue
853
854                 case '\n':
855                         p.lineno++
856                         fallthrough
857                 case ' ', '\t':
858                         p.text = p.text[1:]
859                         continue
860
861                 default:
862                         i := strings.IndexAny(p.text, "!;,<#\n \t")
863                         if i < 0 {
864                                 i = len(p.text)
865                         }
866                         t := p.text[:i]
867                         p.text = p.text[i:]
868                         p.lastWord = t
869                         return t
870                 }
871         }
872 }
873
874 // TestStdlibLowercase tests that all standard library package names are
875 // lowercase. See Issue 40065.
876 func TestStdlibLowercase(t *testing.T) {
877         if !testenv.HasSrc() {
878                 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
879         }
880
881         ctxt := Default
882         all, err := listStdPkgs(ctxt.GOROOT)
883         if err != nil {
884                 t.Fatal(err)
885         }
886
887         for _, pkgname := range all {
888                 if strings.ToLower(pkgname) != pkgname {
889                         t.Errorf("package %q should not use upper-case path", pkgname)
890                 }
891         }
892 }
893
894 // TestFindImports tests that findImports works.  See #43249.
895 func TestFindImports(t *testing.T) {
896         imports, err := findImports("go/build")
897         if err != nil {
898                 t.Fatal(err)
899         }
900         t.Logf("go/build imports %q", imports)
901         want := []string{"bytes", "os", "path/filepath", "strings"}
902 wantLoop:
903         for _, w := range want {
904                 for _, imp := range imports {
905                         if imp == w {
906                                 continue wantLoop
907                         }
908                 }
909                 t.Errorf("expected to find %q in import list", w)
910         }
911 }