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