]> Cypherpunks.ru repositories - gostls13.git/blob - src/go/build/deps_test.go
[dev.boringcrypto] crypto/..., go/build: align deps test with standard rules
[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, CRYPTO-MATH;
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, crypto/internal/boring/syso;
401         sync/atomic < crypto/internal/boring/fipstls;
402         crypto/internal/boring/sig, crypto/internal/boring/fipstls < crypto/tls/fipsonly;
403
404         # CRYPTO is core crypto algorithms - no cgo, fmt, net.
405         # Unfortunately, stuck with reflect via encoding/binary.
406         crypto/internal/boring/sig,
407         crypto/internal/boring/syso,
408         encoding/binary,
409         golang.org/x/sys/cpu,
410         hash
411         < crypto
412         < crypto/subtle
413         < crypto/internal/subtle
414         < crypto/elliptic/internal/fiat
415         < crypto/elliptic/internal/nistec
416         < crypto/ed25519/internal/edwards25519/field, golang.org/x/crypto/curve25519/internal/field
417         < crypto/ed25519/internal/edwards25519
418         < crypto/cipher
419         < crypto/internal/boring
420         < crypto/boring
421         < crypto/aes, crypto/des, crypto/hmac, crypto/md5, crypto/rc4,
422           crypto/sha1, crypto/sha256, crypto/sha512
423         < CRYPTO;
424
425         CGO, fmt, net !< CRYPTO;
426
427         # CRYPTO-MATH is core bignum-based crypto - no cgo, net; fmt now ok.
428         CRYPTO, FMT, math/big, embed
429         < crypto/internal/boring/bbig
430         < crypto/internal/randutil
431         < crypto/rand
432         < crypto/ed25519
433         < encoding/asn1
434         < golang.org/x/crypto/cryptobyte/asn1
435         < golang.org/x/crypto/cryptobyte
436         < golang.org/x/crypto/curve25519
437         < crypto/dsa, crypto/elliptic, crypto/rsa
438         < crypto/ecdsa
439         < CRYPTO-MATH;
440
441         CGO, net !< CRYPTO-MATH;
442
443         # TLS, Prince of Dependencies.
444         CRYPTO-MATH, NET, container/list, encoding/hex, encoding/pem
445         < golang.org/x/crypto/internal/subtle
446         < golang.org/x/crypto/chacha20
447         < golang.org/x/crypto/internal/poly1305
448         < golang.org/x/crypto/chacha20poly1305
449         < golang.org/x/crypto/hkdf
450         < crypto/x509/internal/macos
451         < crypto/x509/pkix;
452
453         crypto/internal/boring/fipstls, crypto/x509/pkix
454         < crypto/x509
455         < crypto/tls;
456
457         # crypto-aware packages
458
459         DEBUG, go/build, go/types, text/scanner, crypto/md5
460         < internal/pkgbits
461         < go/internal/gcimporter, go/internal/gccgoimporter, go/internal/srcimporter
462         < go/importer;
463
464         NET, crypto/rand, mime/quotedprintable
465         < mime/multipart;
466
467         crypto/tls
468         < net/smtp;
469
470         # HTTP, King of Dependencies.
471
472         FMT
473         < golang.org/x/net/http2/hpack
474         < net/http/internal, net/http/internal/ascii, net/http/internal/testcert;
475
476         FMT, NET, container/list, encoding/binary, log
477         < golang.org/x/text/transform
478         < golang.org/x/text/unicode/norm
479         < golang.org/x/text/unicode/bidi
480         < golang.org/x/text/secure/bidirule
481         < golang.org/x/net/idna
482         < golang.org/x/net/http/httpguts, golang.org/x/net/http/httpproxy;
483
484         NET, crypto/tls
485         < net/http/httptrace;
486
487         compress/gzip,
488         golang.org/x/net/http/httpguts,
489         golang.org/x/net/http/httpproxy,
490         golang.org/x/net/http2/hpack,
491         net/http/internal,
492         net/http/internal/ascii,
493         net/http/internal/testcert,
494         net/http/httptrace,
495         mime/multipart,
496         log
497         < net/http;
498
499         # HTTP-aware packages
500
501         encoding/json, net/http
502         < expvar;
503
504         net/http, net/http/internal/ascii
505         < net/http/cookiejar, net/http/httputil;
506
507         net/http, flag
508         < net/http/httptest;
509
510         net/http, regexp
511         < net/http/cgi
512         < net/http/fcgi;
513
514         # Profiling
515         FMT, compress/gzip, encoding/binary, text/tabwriter
516         < runtime/pprof;
517
518         OS, compress/gzip, regexp
519         < internal/profile;
520
521         html, internal/profile, net/http, runtime/pprof, runtime/trace
522         < net/http/pprof;
523
524         # RPC
525         encoding/gob, encoding/json, go/token, html/template, net/http
526         < net/rpc
527         < net/rpc/jsonrpc;
528
529         # System Information
530         internal/cpu, sync
531         < internal/sysinfo;
532
533         # Test-only
534         log
535         < testing/iotest
536         < testing/fstest;
537
538         FMT, flag, math/rand
539         < testing/quick;
540
541         FMT, DEBUG, flag, runtime/trace, internal/sysinfo, math/rand
542         < testing;
543
544         FMT, crypto/sha256, encoding/json, go/ast, go/parser, go/token,
545         internal/godebug, math/rand, encoding/hex, crypto/sha256
546         < internal/fuzz;
547
548         internal/fuzz, internal/testlog, runtime/pprof, regexp
549         < testing/internal/testdeps;
550
551         OS, flag, testing, internal/cfg
552         < internal/testenv;
553
554         OS, encoding/base64
555         < internal/obscuretestdata;
556
557         CGO, OS, fmt
558         < os/signal/internal/pty;
559
560         NET, testing, math/rand
561         < golang.org/x/net/nettest;
562
563         syscall
564         < os/exec/internal/fdtest;
565
566         FMT, container/heap, math/rand
567         < internal/trace;
568
569         FMT
570         < internal/diff, internal/txtar;
571 `
572
573 // listStdPkgs returns the same list of packages as "go list std".
574 func listStdPkgs(goroot string) ([]string, error) {
575         // Based on cmd/go's matchPackages function.
576         var pkgs []string
577
578         src := filepath.Join(goroot, "src") + string(filepath.Separator)
579         walkFn := func(path string, d fs.DirEntry, err error) error {
580                 if err != nil || !d.IsDir() || path == src {
581                         return nil
582                 }
583
584                 base := filepath.Base(path)
585                 if strings.HasPrefix(base, ".") || strings.HasPrefix(base, "_") || base == "testdata" {
586                         return filepath.SkipDir
587                 }
588
589                 name := filepath.ToSlash(path[len(src):])
590                 if name == "builtin" || name == "cmd" {
591                         return filepath.SkipDir
592                 }
593
594                 pkgs = append(pkgs, strings.TrimPrefix(name, "vendor/"))
595                 return nil
596         }
597         if err := filepath.WalkDir(src, walkFn); err != nil {
598                 return nil, err
599         }
600         return pkgs, nil
601 }
602
603 func TestDependencies(t *testing.T) {
604         if !testenv.HasSrc() {
605                 // Tests run in a limited file system and we do not
606                 // provide access to every source file.
607                 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
608         }
609
610         ctxt := Default
611         all, err := listStdPkgs(ctxt.GOROOT)
612         if err != nil {
613                 t.Fatal(err)
614         }
615         sort.Strings(all)
616
617         sawImport := map[string]map[string]bool{} // from package => to package => true
618         policy := depsPolicy(t)
619
620         for _, pkg := range all {
621                 imports, err := findImports(pkg)
622                 if err != nil {
623                         t.Error(err)
624                         continue
625                 }
626                 if sawImport[pkg] == nil {
627                         sawImport[pkg] = map[string]bool{}
628                 }
629                 ok := policy[pkg]
630                 var bad []string
631                 for _, imp := range imports {
632                         sawImport[pkg][imp] = true
633                         if !ok[imp] {
634                                 bad = append(bad, imp)
635                         }
636                 }
637                 if bad != nil {
638                         t.Errorf("unexpected dependency: %s imports %v", pkg, bad)
639                 }
640         }
641 }
642
643 var buildIgnore = []byte("\n//go:build ignore")
644
645 func findImports(pkg string) ([]string, error) {
646         vpkg := pkg
647         if strings.HasPrefix(pkg, "golang.org") {
648                 vpkg = "vendor/" + pkg
649         }
650         dir := filepath.Join(Default.GOROOT, "src", vpkg)
651         files, err := os.ReadDir(dir)
652         if err != nil {
653                 return nil, err
654         }
655         var imports []string
656         var haveImport = map[string]bool{}
657         if pkg == "crypto/internal/boring" {
658                 haveImport["C"] = true // kludge: prevent C from appearing in crypto/internal/boring imports
659         }
660         fset := token.NewFileSet()
661         for _, file := range files {
662                 name := file.Name()
663                 if name == "slice_go14.go" || name == "slice_go18.go" {
664                         // These files are for compiler bootstrap with older versions of Go and not built in the standard build.
665                         continue
666                 }
667                 if !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") {
668                         continue
669                 }
670                 info := fileInfo{
671                         name: filepath.Join(dir, name),
672                         fset: fset,
673                 }
674                 f, err := os.Open(info.name)
675                 if err != nil {
676                         return nil, err
677                 }
678                 err = readGoInfo(f, &info)
679                 f.Close()
680                 if err != nil {
681                         return nil, fmt.Errorf("reading %v: %v", name, err)
682                 }
683                 if info.parsed.Name.Name == "main" {
684                         continue
685                 }
686                 if bytes.Contains(info.header, buildIgnore) {
687                         continue
688                 }
689                 for _, imp := range info.imports {
690                         path := imp.path
691                         if !haveImport[path] {
692                                 haveImport[path] = true
693                                 imports = append(imports, path)
694                         }
695                 }
696         }
697         sort.Strings(imports)
698         return imports, nil
699 }
700
701 // depsPolicy returns a map m such that m[p][d] == true when p can import d.
702 func depsPolicy(t *testing.T) map[string]map[string]bool {
703         allowed := map[string]map[string]bool{"NONE": {}}
704         disallowed := [][2][]string{}
705
706         parseDepsRules(t, func(deps []string, op string, users []string) {
707                 if op == "!<" {
708                         disallowed = append(disallowed, [2][]string{deps, users})
709                         return
710                 }
711                 for _, u := range users {
712                         if allowed[u] != nil {
713                                 t.Errorf("multiple deps lists for %s", u)
714                         }
715                         allowed[u] = make(map[string]bool)
716                         for _, d := range deps {
717                                 if allowed[d] == nil {
718                                         t.Errorf("use of %s before its deps list", d)
719                                 }
720                                 allowed[u][d] = true
721                         }
722                 }
723         })
724
725         // Check for missing deps info.
726         for _, deps := range allowed {
727                 for d := range deps {
728                         if allowed[d] == nil {
729                                 t.Errorf("missing deps list for %s", d)
730                         }
731                 }
732         }
733
734         // Complete transitive allowed deps.
735         for k := range allowed {
736                 for i := range allowed {
737                         for j := range allowed {
738                                 if i != k && k != j && allowed[i][k] && allowed[k][j] {
739                                         if i == j {
740                                                 // Can only happen along with a "use of X before deps" error above,
741                                                 // but this error is more specific - it makes clear that reordering the
742                                                 // rules will not be enough to fix the problem.
743                                                 t.Errorf("deps policy cycle: %s < %s < %s", j, k, i)
744                                         }
745                                         allowed[i][j] = true
746                                 }
747                         }
748                 }
749         }
750
751         // Check negative assertions against completed allowed deps.
752         for _, bad := range disallowed {
753                 deps, users := bad[0], bad[1]
754                 for _, d := range deps {
755                         for _, u := range users {
756                                 if allowed[u][d] {
757                                         t.Errorf("deps policy incorrect: assertion failed: %s !< %s", d, u)
758                                 }
759                         }
760                 }
761         }
762
763         if t.Failed() {
764                 t.FailNow()
765         }
766
767         return allowed
768 }
769
770 // parseDepsRules parses depsRules, calling save(deps, op, users)
771 // for each deps < users or deps !< users rule
772 // (op is "<" or "!<").
773 func parseDepsRules(t *testing.T, save func(deps []string, op string, users []string)) {
774         p := &depsParser{t: t, lineno: 1, text: depsRules}
775
776         var prev []string
777         var op string
778         for {
779                 list, tok := p.nextList()
780                 if tok == "" {
781                         if prev == nil {
782                                 break
783                         }
784                         p.syntaxError("unexpected EOF")
785                 }
786                 if prev != nil {
787                         save(prev, op, list)
788                 }
789                 prev = list
790                 if tok == ";" {
791                         prev = nil
792                         op = ""
793                         continue
794                 }
795                 if tok != "<" && tok != "!<" {
796                         p.syntaxError("missing <")
797                 }
798                 op = tok
799         }
800 }
801
802 // A depsParser parses the depsRules syntax described above.
803 type depsParser struct {
804         t        *testing.T
805         lineno   int
806         lastWord string
807         text     string
808 }
809
810 // syntaxError reports a parsing error.
811 func (p *depsParser) syntaxError(msg string) {
812         p.t.Fatalf("deps:%d: syntax error: %s near %s", p.lineno, msg, p.lastWord)
813 }
814
815 // nextList parses and returns a comma-separated list of names.
816 func (p *depsParser) nextList() (list []string, token string) {
817         for {
818                 tok := p.nextToken()
819                 switch tok {
820                 case "":
821                         if len(list) == 0 {
822                                 return nil, ""
823                         }
824                         fallthrough
825                 case ",", "<", "!<", ";":
826                         p.syntaxError("bad list syntax")
827                 }
828                 list = append(list, tok)
829
830                 tok = p.nextToken()
831                 if tok != "," {
832                         return list, tok
833                 }
834         }
835 }
836
837 // nextToken returns the next token in the deps rules,
838 // one of ";" "," "<" "!<" or a name.
839 func (p *depsParser) nextToken() string {
840         for {
841                 if p.text == "" {
842                         return ""
843                 }
844                 switch p.text[0] {
845                 case ';', ',', '<':
846                         t := p.text[:1]
847                         p.text = p.text[1:]
848                         return t
849
850                 case '!':
851                         if len(p.text) < 2 || p.text[1] != '<' {
852                                 p.syntaxError("unexpected token !")
853                         }
854                         p.text = p.text[2:]
855                         return "!<"
856
857                 case '#':
858                         i := strings.Index(p.text, "\n")
859                         if i < 0 {
860                                 i = len(p.text)
861                         }
862                         p.text = p.text[i:]
863                         continue
864
865                 case '\n':
866                         p.lineno++
867                         fallthrough
868                 case ' ', '\t':
869                         p.text = p.text[1:]
870                         continue
871
872                 default:
873                         i := strings.IndexAny(p.text, "!;,<#\n \t")
874                         if i < 0 {
875                                 i = len(p.text)
876                         }
877                         t := p.text[:i]
878                         p.text = p.text[i:]
879                         p.lastWord = t
880                         return t
881                 }
882         }
883 }
884
885 // TestStdlibLowercase tests that all standard library package names are
886 // lowercase. See Issue 40065.
887 func TestStdlibLowercase(t *testing.T) {
888         if !testenv.HasSrc() {
889                 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
890         }
891
892         ctxt := Default
893         all, err := listStdPkgs(ctxt.GOROOT)
894         if err != nil {
895                 t.Fatal(err)
896         }
897
898         for _, pkgname := range all {
899                 if strings.ToLower(pkgname) != pkgname {
900                         t.Errorf("package %q should not use upper-case path", pkgname)
901                 }
902         }
903 }
904
905 // TestFindImports tests that findImports works.  See #43249.
906 func TestFindImports(t *testing.T) {
907         imports, err := findImports("go/build")
908         if err != nil {
909                 t.Fatal(err)
910         }
911         t.Logf("go/build imports %q", imports)
912         want := []string{"bytes", "os", "path/filepath", "strings"}
913 wantLoop:
914         for _, w := range want {
915                 for _, imp := range imports {
916                         if imp == w {
917                                 continue wantLoop
918                         }
919                 }
920                 t.Errorf("expected to find %q in import list", w)
921         }
922 }