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