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