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