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