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