Source file
src/go/build/deps_test.go
1
2
3
4
5
6
7
8 package build
9
10 import (
11 "bytes"
12 "fmt"
13 "go/token"
14 "internal/dag"
15 "internal/testenv"
16 "io/fs"
17 "os"
18 "path/filepath"
19 "slices"
20 "strings"
21 "testing"
22 )
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 var depsRules = `
39 # No dependencies allowed for any of these packages.
40 NONE
41 < unsafe
42 < cmp,
43 container/list,
44 container/ring,
45 internal/byteorder,
46 internal/cfg,
47 internal/coverage,
48 internal/coverage/rtcov,
49 internal/coverage/uleb128,
50 internal/coverage/calloc,
51 internal/cpu,
52 internal/goarch,
53 internal/godebugs,
54 internal/goexperiment,
55 internal/goos,
56 internal/goversion,
57 internal/nettrace,
58 internal/platform,
59 internal/profilerecord,
60 internal/syslist,
61 internal/trace/traceviewer/format,
62 log/internal,
63 math/bits,
64 structs,
65 unicode,
66 unicode/utf8,
67 unicode/utf16;
68
69 internal/goarch < internal/abi;
70 internal/byteorder, internal/goarch < internal/chacha8rand;
71
72 # RUNTIME is the core runtime group of packages, all of them very light-weight.
73 internal/abi,
74 internal/chacha8rand,
75 internal/coverage/rtcov,
76 internal/cpu,
77 internal/goarch,
78 internal/godebugs,
79 internal/goexperiment,
80 internal/goos,
81 internal/profilerecord,
82 math/bits,
83 structs
84 < internal/bytealg
85 < internal/stringslite
86 < internal/itoa
87 < internal/unsafeheader
88 < internal/race
89 < internal/msan
90 < internal/asan
91 < internal/runtime/sys
92 < internal/runtime/syscall
93 < internal/runtime/atomic
94 < internal/runtime/exithook
95 < internal/runtime/math
96 < internal/runtime/maps
97 < runtime
98 < sync/atomic
99 < internal/sync
100 < weak
101 < sync
102 < internal/bisect
103 < internal/godebug
104 < internal/reflectlite
105 < errors
106 < internal/oserror;
107
108 cmp, runtime, math/bits
109 < iter
110 < maps, slices;
111
112 internal/oserror, maps, slices
113 < RUNTIME;
114
115 RUNTIME
116 < sort
117 < container/heap
118 < unique;
119
120 RUNTIME
121 < io;
122
123 RUNTIME
124 < arena;
125
126 syscall !< io;
127 reflect !< sort;
128
129 RUNTIME, unicode/utf8
130 < path;
131
132 unicode !< path;
133
134 RUNTIME
135 < internal/synctest
136 < testing/synctest;
137
138 # SYSCALL is RUNTIME plus the packages necessary for basic system calls.
139 RUNTIME, unicode/utf8, unicode/utf16, internal/synctest
140 < internal/syscall/windows/sysdll, syscall/js
141 < syscall
142 < internal/syscall/unix, internal/syscall/windows, internal/syscall/windows/registry
143 < internal/syscall/execenv
144 < SYSCALL;
145
146 # TIME is SYSCALL plus the core packages about time, including context.
147 SYSCALL
148 < time/tzdata
149 < time
150 < context
151 < TIME;
152
153 TIME, io, path, slices
154 < io/fs;
155
156 # MATH is RUNTIME plus the basic math packages.
157 RUNTIME
158 < math
159 < MATH;
160
161 unicode !< math;
162
163 MATH
164 < math/cmplx;
165
166 MATH
167 < math/rand, math/rand/v2;
168
169 MATH
170 < runtime/metrics;
171
172 MATH, unicode/utf8
173 < strconv;
174
175 unicode !< strconv;
176
177 # STR is basic string and buffer manipulation.
178 RUNTIME, io, unicode/utf8, unicode/utf16, unicode
179 < bytes, strings
180 < bufio;
181
182 bufio, path, strconv
183 < STR;
184
185 # OS is basic OS access, including helpers (path/filepath, os/exec, etc).
186 # OS includes string routines, but those must be layered above package os.
187 # OS does not include reflection.
188 io/fs
189 < internal/testlog
190 < internal/poll
191 < internal/filepathlite
192 < os
193 < os/signal;
194
195 io/fs
196 < embed;
197
198 unicode, fmt !< net, os, os/signal;
199
200 os/signal, internal/filepathlite, STR
201 < path/filepath
202 < io/ioutil;
203
204 path/filepath, internal/godebug < os/exec;
205
206 io/ioutil, os/exec, os/signal
207 < OS;
208
209 reflect !< OS;
210
211 OS
212 < golang.org/x/sys/cpu;
213
214 # FMT is OS (which includes string routines) plus reflect and fmt.
215 # It does not include package log, which should be avoided in core packages.
216 arena, strconv, unicode
217 < reflect;
218
219 os, reflect
220 < internal/fmtsort
221 < fmt;
222
223 OS, fmt
224 < FMT;
225
226 log !< FMT;
227
228 # Misc packages needing only FMT.
229 FMT
230 < html,
231 internal/dag,
232 internal/goroot,
233 internal/types/errors,
234 mime/quotedprintable,
235 net/internal/socktest,
236 net/url,
237 runtime/trace,
238 text/scanner,
239 text/tabwriter;
240
241 io, reflect
242 < internal/saferio;
243
244 # encodings
245 # core ones do not use fmt.
246 io, strconv, slices
247 < encoding;
248
249 encoding, reflect
250 < encoding/binary
251 < encoding/base32, encoding/base64;
252
253 FMT, encoding < flag;
254
255 fmt !< encoding/base32, encoding/base64;
256
257 FMT, encoding/base32, encoding/base64, internal/saferio
258 < encoding/ascii85, encoding/csv, encoding/gob, encoding/hex,
259 encoding/json, encoding/pem, encoding/xml, mime;
260
261 # hashes
262 io
263 < hash
264 < hash/adler32, hash/crc32, hash/crc64, hash/fnv;
265
266 # math/big
267 FMT, math/rand
268 < math/big;
269
270 # compression
271 FMT, encoding/binary, hash/adler32, hash/crc32, sort
272 < compress/bzip2, compress/flate, compress/lzw, internal/zstd
273 < archive/zip, compress/gzip, compress/zlib;
274
275 # templates
276 FMT
277 < text/template/parse;
278
279 net/url, text/template/parse
280 < text/template
281 < internal/lazytemplate;
282
283 # regexp
284 FMT, sort
285 < regexp/syntax
286 < regexp
287 < internal/lazyregexp;
288
289 encoding/json, html, text/template, regexp
290 < html/template;
291
292 # suffix array
293 encoding/binary, regexp
294 < index/suffixarray;
295
296 # executable parsing
297 FMT, encoding/binary, compress/zlib, internal/saferio, internal/zstd, sort
298 < runtime/debug
299 < debug/dwarf
300 < debug/elf, debug/gosym, debug/macho, debug/pe, debug/plan9obj, internal/xcoff
301 < debug/buildinfo
302 < DEBUG;
303
304 # go parser and friends.
305 FMT, sort
306 < internal/gover
307 < go/version
308 < go/token
309 < go/scanner
310 < go/ast
311 < go/internal/typeparams;
312
313 FMT
314 < go/build/constraint;
315
316 FMT, sort
317 < go/doc/comment;
318
319 go/internal/typeparams, go/build/constraint
320 < go/parser;
321
322 go/doc/comment, go/parser, text/tabwriter
323 < go/printer
324 < go/format;
325
326 math/big, go/token
327 < go/constant;
328
329 FMT, internal/goexperiment
330 < internal/buildcfg;
331
332 container/heap, go/constant, go/parser, internal/buildcfg, internal/goversion, internal/types/errors
333 < go/types;
334
335 # The vast majority of standard library packages should not be resorting to regexp.
336 # go/types is a good chokepoint. It shouldn't use regexp, nor should anything
337 # that is low-enough level to be used by go/types.
338 regexp !< go/types;
339
340 go/doc/comment, go/parser, internal/lazyregexp, text/template
341 < go/doc;
342
343 go/build/constraint, go/doc, go/parser, internal/buildcfg, internal/goroot, internal/goversion, internal/platform, internal/syslist
344 < go/build;
345
346 # databases
347 FMT
348 < database/sql/internal
349 < database/sql/driver;
350
351 database/sql/driver, math/rand/v2 < database/sql;
352
353 # images
354 FMT, compress/lzw, compress/zlib
355 < image/color
356 < image, image/color/palette
357 < image/internal/imageutil
358 < image/draw
359 < image/gif, image/jpeg, image/png;
360
361 # cgo, delayed as long as possible.
362 # If you add a dependency on CGO, you must add the package
363 # to cgoPackages in cmd/dist/test.go as well.
364 RUNTIME
365 < C
366 < runtime/cgo
367 < CGO
368 < runtime/msan, runtime/asan;
369
370 # runtime/race
371 NONE < runtime/race/internal/amd64v1;
372 NONE < runtime/race/internal/amd64v3;
373 CGO, runtime/race/internal/amd64v1, runtime/race/internal/amd64v3 < runtime/race;
374
375 # Bulk of the standard library must not use cgo.
376 # The prohibition stops at net and os/user.
377 C !< fmt, go/types, CRYPTO-MATH, log/slog;
378
379 CGO, OS
380 < plugin;
381
382 CGO, FMT
383 < os/user
384 < archive/tar;
385
386 sync
387 < internal/singleflight;
388
389 os
390 < golang.org/x/net/dns/dnsmessage,
391 golang.org/x/net/lif,
392 golang.org/x/net/route;
393
394 internal/bytealg, internal/itoa, math/bits, slices, strconv, unique
395 < net/netip;
396
397 # net is unavoidable when doing any networking,
398 # so large dependencies must be kept out.
399 # This is a long-looking list but most of these
400 # are small with few dependencies.
401 CGO,
402 golang.org/x/net/dns/dnsmessage,
403 golang.org/x/net/lif,
404 golang.org/x/net/route,
405 internal/godebug,
406 internal/nettrace,
407 internal/poll,
408 internal/singleflight,
409 net/netip,
410 os,
411 sort
412 < net;
413
414 fmt, unicode !< net;
415 math/rand !< net; # net uses runtime instead
416
417 # NET is net plus net-helper packages.
418 FMT, net
419 < net/textproto;
420
421 mime, net/textproto, net/url
422 < NET;
423
424 # logging - most packages should not import; http and up is allowed
425 FMT, log/internal
426 < log;
427
428 log, log/slog !< crypto/tls, database/sql, go/importer, testing;
429
430 FMT, log, net
431 < log/syslog;
432
433 RUNTIME
434 < log/slog/internal, log/slog/internal/buffer;
435
436 FMT,
437 encoding, encoding/json,
438 log, log/internal,
439 log/slog/internal, log/slog/internal/buffer,
440 slices
441 < log/slog
442 < log/slog/internal/slogtest, log/slog/internal/benchmarks;
443
444 NET, log
445 < net/mail;
446
447 # FIPS is the FIPS 140 module.
448 # It must not depend on external crypto packages.
449 # See also fips140deps.AllowedInternalPackages.
450
451 io, math/rand/v2 < crypto/internal/randutil;
452
453 STR < crypto/internal/impl;
454
455 OS < crypto/internal/sysrand
456 < crypto/internal/entropy;
457
458 internal/byteorder < crypto/internal/fips140deps/byteorder;
459 internal/cpu, internal/goarch < crypto/internal/fips140deps/cpu;
460 internal/godebug < crypto/internal/fips140deps/godebug;
461
462 STR, crypto/internal/impl,
463 crypto/internal/entropy,
464 crypto/internal/randutil,
465 crypto/internal/fips140deps/byteorder,
466 crypto/internal/fips140deps/cpu,
467 crypto/internal/fips140deps/godebug
468 < crypto/internal/fips140
469 < crypto/internal/fips140/alias
470 < crypto/internal/fips140/subtle
471 < crypto/internal/fips140/sha256
472 < crypto/internal/fips140/sha512
473 < crypto/internal/fips140/sha3
474 < crypto/internal/fips140/hmac
475 < crypto/internal/fips140/check
476 < crypto/internal/fips140/pbkdf2
477 < crypto/internal/fips140/aes
478 < crypto/internal/fips140/drbg
479 < crypto/internal/fips140/aes/gcm
480 < crypto/internal/fips140/hkdf
481 < crypto/internal/fips140/mlkem
482 < crypto/internal/fips140/ssh
483 < crypto/internal/fips140/tls12
484 < crypto/internal/fips140/tls13
485 < crypto/internal/fips140/bigmod
486 < crypto/internal/fips140/nistec/fiat
487 < crypto/internal/fips140/nistec
488 < crypto/internal/fips140/ecdh
489 < crypto/internal/fips140/ecdsa
490 < crypto/internal/fips140/edwards25519/field
491 < crypto/internal/fips140/edwards25519
492 < crypto/internal/fips140/ed25519
493 < crypto/internal/fips140/rsa
494 < FIPS;
495
496 FIPS, internal/godebug < crypto/fips140;
497
498 crypto, hash !< FIPS;
499
500 # CRYPTO is core crypto algorithms - no cgo, fmt, net.
501 # Mostly wrappers around the FIPS module.
502
503 NONE < crypto/internal/boring/sig, crypto/internal/boring/syso;
504 sync/atomic < crypto/internal/boring/bcache;
505
506 FIPS, internal/godebug, hash, embed,
507 crypto/internal/boring/sig,
508 crypto/internal/boring/syso,
509 crypto/internal/boring/bcache
510 < crypto/internal/fips140only
511 < crypto
512 < crypto/subtle
513 < crypto/sha3
514 < crypto/internal/fips140hash
515 < crypto/cipher
516 < crypto/internal/boring
517 < crypto/boring
518 < crypto/aes,
519 crypto/des,
520 crypto/rc4,
521 crypto/md5,
522 crypto/sha1,
523 crypto/sha256,
524 crypto/sha512,
525 crypto/hmac,
526 crypto/hkdf,
527 crypto/pbkdf2,
528 crypto/ecdh,
529 crypto/mlkem
530 < CRYPTO;
531
532 CGO, fmt, net !< CRYPTO;
533
534 # CRYPTO-MATH is crypto that exposes math/big APIs - no cgo, net; fmt now ok.
535
536 CRYPTO, FMT, math/big
537 < crypto/internal/boring/bbig
538 < crypto/rand
539 < crypto/ed25519 # depends on crypto/rand.Reader
540 < encoding/asn1
541 < golang.org/x/crypto/cryptobyte/asn1
542 < golang.org/x/crypto/cryptobyte
543 < crypto/dsa, crypto/elliptic, crypto/rsa
544 < crypto/ecdsa
545 < CRYPTO-MATH;
546
547 CGO, net !< CRYPTO-MATH;
548
549 # TLS, Prince of Dependencies.
550
551 FIPS, sync/atomic < crypto/tls/internal/fips140tls;
552
553 crypto/internal/boring/sig, crypto/tls/internal/fips140tls < crypto/tls/fipsonly;
554
555 CRYPTO, golang.org/x/sys/cpu, encoding/binary, reflect
556 < golang.org/x/crypto/internal/alias
557 < golang.org/x/crypto/internal/subtle
558 < golang.org/x/crypto/chacha20
559 < golang.org/x/crypto/internal/poly1305
560 < golang.org/x/crypto/chacha20poly1305;
561
562 CRYPTO-MATH, NET, container/list, encoding/hex, encoding/pem,
563 golang.org/x/crypto/chacha20poly1305, crypto/tls/internal/fips140tls
564 < crypto/internal/hpke
565 < crypto/x509/internal/macos
566 < crypto/x509/pkix
567 < crypto/x509
568 < crypto/tls;
569
570 # crypto-aware packages
571
572 DEBUG, go/build, go/types, text/scanner, crypto/sha256
573 < internal/pkgbits, internal/exportdata
574 < go/internal/gcimporter, go/internal/gccgoimporter, go/internal/srcimporter
575 < go/importer;
576
577 NET, crypto/rand, mime/quotedprintable
578 < mime/multipart;
579
580 crypto/tls
581 < net/smtp;
582
583 crypto/rand
584 < hash/maphash; # for purego implementation
585
586 # HTTP, King of Dependencies.
587
588 FMT
589 < golang.org/x/net/http2/hpack
590 < net/http/internal, net/http/internal/ascii, net/http/internal/testcert;
591
592 FMT, NET, container/list, encoding/binary, log
593 < golang.org/x/text/transform
594 < golang.org/x/text/unicode/norm
595 < golang.org/x/text/unicode/bidi
596 < golang.org/x/text/secure/bidirule
597 < golang.org/x/net/idna
598 < golang.org/x/net/http/httpguts, golang.org/x/net/http/httpproxy;
599
600 NET, crypto/tls
601 < net/http/httptrace;
602
603 compress/gzip,
604 golang.org/x/net/http/httpguts,
605 golang.org/x/net/http/httpproxy,
606 golang.org/x/net/http2/hpack,
607 net/http/internal,
608 net/http/internal/ascii,
609 net/http/internal/testcert,
610 net/http/httptrace,
611 mime/multipart,
612 log
613 < net/http;
614
615 # HTTP-aware packages
616
617 encoding/json, net/http
618 < expvar;
619
620 net/http, net/http/internal/ascii
621 < net/http/cookiejar, net/http/httputil;
622
623 net/http, flag
624 < net/http/httptest;
625
626 net/http, regexp
627 < net/http/cgi
628 < net/http/fcgi;
629
630 # Profiling
631 FMT, compress/gzip, encoding/binary, sort, text/tabwriter
632 < runtime/pprof;
633
634 OS, compress/gzip, internal/lazyregexp
635 < internal/profile;
636
637 html, internal/profile, net/http, runtime/pprof, runtime/trace
638 < net/http/pprof;
639
640 # RPC
641 encoding/gob, encoding/json, go/token, html/template, net/http
642 < net/rpc
643 < net/rpc/jsonrpc;
644
645 # System Information
646 bufio, bytes, internal/cpu, io, os, strings, sync
647 < internal/sysinfo;
648
649 # Test-only
650 log
651 < testing/iotest
652 < testing/fstest;
653
654 FMT, flag, math/rand
655 < testing/quick;
656
657 FMT, DEBUG, flag, runtime/trace, internal/sysinfo, math/rand
658 < testing;
659
660 log/slog, testing
661 < testing/slogtest;
662
663 FMT, crypto/sha256, encoding/json, go/ast, go/parser, go/token,
664 internal/godebug, math/rand, encoding/hex
665 < internal/fuzz;
666
667 OS, flag, testing, internal/cfg, internal/platform, internal/goroot
668 < internal/testenv;
669
670 OS, encoding/base64
671 < internal/obscuretestdata;
672
673 CGO, OS, fmt
674 < internal/testpty;
675
676 NET, testing, math/rand
677 < golang.org/x/net/nettest;
678
679 syscall
680 < os/exec/internal/fdtest;
681
682 FMT, sort
683 < internal/diff;
684
685 FMT
686 < internal/txtar;
687
688 CRYPTO-MATH, testing, internal/testenv, encoding/json
689 < crypto/internal/cryptotest;
690
691 CGO, FMT
692 < crypto/internal/sysrand/internal/seccomp;
693
694 FIPS
695 < crypto/internal/fips140/check/checktest;
696
697 # v2 execution trace parser.
698 FMT
699 < internal/trace/event;
700
701 internal/trace/event
702 < internal/trace/event/go122;
703
704 FMT, io, internal/trace/event/go122
705 < internal/trace/version;
706
707 FMT, encoding/binary, internal/trace/version
708 < internal/trace/raw;
709
710 FMT, internal/trace/event, internal/trace/version, io, sort, encoding/binary
711 < internal/trace/internal/oldtrace;
712
713 FMT, encoding/binary, internal/trace/version, internal/trace/internal/oldtrace, container/heap, math/rand
714 < internal/trace;
715
716 regexp, internal/trace, internal/trace/raw, internal/txtar
717 < internal/trace/testtrace;
718
719 regexp, internal/txtar, internal/trace, internal/trace/raw
720 < internal/trace/internal/testgen/go122;
721
722 # cmd/trace dependencies.
723 FMT,
724 embed,
725 encoding/json,
726 html/template,
727 internal/profile,
728 internal/trace,
729 internal/trace/traceviewer/format,
730 net/http
731 < internal/trace/traceviewer;
732
733 # Coverage.
734 FMT, hash/fnv, encoding/binary, regexp, sort, text/tabwriter,
735 internal/coverage, internal/coverage/uleb128
736 < internal/coverage/cmerge,
737 internal/coverage/pods,
738 internal/coverage/slicereader,
739 internal/coverage/slicewriter;
740
741 internal/coverage/slicereader, internal/coverage/slicewriter
742 < internal/coverage/stringtab
743 < internal/coverage/decodecounter, internal/coverage/decodemeta,
744 internal/coverage/encodecounter, internal/coverage/encodemeta;
745
746 internal/coverage/cmerge
747 < internal/coverage/cformat;
748
749 internal/coverage, crypto/sha256, FMT
750 < cmd/internal/cov/covcmd;
751
752 encoding/json,
753 runtime/debug,
754 internal/coverage/calloc,
755 internal/coverage/cformat,
756 internal/coverage/decodecounter, internal/coverage/decodemeta,
757 internal/coverage/encodecounter, internal/coverage/encodemeta,
758 internal/coverage/pods
759 < internal/coverage/cfile
760 < runtime/coverage;
761
762 internal/coverage/cfile, internal/fuzz, internal/testlog, runtime/pprof, regexp
763 < testing/internal/testdeps;
764
765 # Test-only packages can have anything they want
766 CGO, internal/syscall/unix < net/internal/cgotest;
767
768
769 `
770
771
772 func listStdPkgs(goroot string) ([]string, error) {
773
774 var pkgs []string
775
776 src := filepath.Join(goroot, "src") + string(filepath.Separator)
777 walkFn := func(path string, d fs.DirEntry, err error) error {
778 if err != nil || !d.IsDir() || path == src {
779 return nil
780 }
781
782 base := filepath.Base(path)
783 if strings.HasPrefix(base, ".") || strings.HasPrefix(base, "_") || base == "testdata" {
784 return filepath.SkipDir
785 }
786
787 name := filepath.ToSlash(path[len(src):])
788 if name == "builtin" || name == "cmd" {
789 return filepath.SkipDir
790 }
791
792 pkgs = append(pkgs, strings.TrimPrefix(name, "vendor/"))
793 return nil
794 }
795 if err := filepath.WalkDir(src, walkFn); err != nil {
796 return nil, err
797 }
798 return pkgs, nil
799 }
800
801 func TestDependencies(t *testing.T) {
802 testenv.MustHaveSource(t)
803
804 ctxt := Default
805 all, err := listStdPkgs(ctxt.GOROOT)
806 if err != nil {
807 t.Fatal(err)
808 }
809 slices.Sort(all)
810
811 sawImport := map[string]map[string]bool{}
812 policy := depsPolicy(t)
813
814 for _, pkg := range all {
815 imports, err := findImports(pkg)
816 if err != nil {
817 t.Error(err)
818 continue
819 }
820 if sawImport[pkg] == nil {
821 sawImport[pkg] = map[string]bool{}
822 }
823 var bad []string
824 for _, imp := range imports {
825 sawImport[pkg][imp] = true
826 if !policy.HasEdge(pkg, imp) {
827 bad = append(bad, imp)
828 }
829 }
830 if bad != nil {
831 t.Errorf("unexpected dependency: %s imports %v", pkg, bad)
832 }
833 }
834 }
835
836 var buildIgnore = []byte("\n//go:build ignore")
837
838 func findImports(pkg string) ([]string, error) {
839 vpkg := pkg
840 if strings.HasPrefix(pkg, "golang.org") {
841 vpkg = "vendor/" + pkg
842 }
843 dir := filepath.Join(Default.GOROOT, "src", vpkg)
844 files, err := os.ReadDir(dir)
845 if err != nil {
846 return nil, err
847 }
848 var imports []string
849 var haveImport = map[string]bool{}
850 if pkg == "crypto/internal/boring" {
851 haveImport["C"] = true
852 }
853 fset := token.NewFileSet()
854 for _, file := range files {
855 name := file.Name()
856 if name == "slice_go14.go" || name == "slice_go18.go" {
857
858 continue
859 }
860 if !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") {
861 continue
862 }
863 info := fileInfo{
864 name: filepath.Join(dir, name),
865 fset: fset,
866 }
867 f, err := os.Open(info.name)
868 if err != nil {
869 return nil, err
870 }
871 err = readGoInfo(f, &info)
872 f.Close()
873 if err != nil {
874 return nil, fmt.Errorf("reading %v: %v", name, err)
875 }
876 if info.parsed.Name.Name == "main" {
877 continue
878 }
879 if bytes.Contains(info.header, buildIgnore) {
880 continue
881 }
882 for _, imp := range info.imports {
883 path := imp.path
884 if !haveImport[path] {
885 haveImport[path] = true
886 imports = append(imports, path)
887 }
888 }
889 }
890 slices.Sort(imports)
891 return imports, nil
892 }
893
894
895 func depsPolicy(t *testing.T) *dag.Graph {
896 g, err := dag.Parse(depsRules)
897 if err != nil {
898 t.Fatal(err)
899 }
900 return g
901 }
902
903
904
905 func TestStdlibLowercase(t *testing.T) {
906 testenv.MustHaveSource(t)
907
908 ctxt := Default
909 all, err := listStdPkgs(ctxt.GOROOT)
910 if err != nil {
911 t.Fatal(err)
912 }
913
914 for _, pkgname := range all {
915 if strings.ToLower(pkgname) != pkgname {
916 t.Errorf("package %q should not use upper-case path", pkgname)
917 }
918 }
919 }
920
921
922 func TestFindImports(t *testing.T) {
923 imports, err := findImports("go/build")
924 if err != nil {
925 t.Fatal(err)
926 }
927 t.Logf("go/build imports %q", imports)
928 want := []string{"bytes", "os", "path/filepath", "strings"}
929 wantLoop:
930 for _, w := range want {
931 for _, imp := range imports {
932 if imp == w {
933 continue wantLoop
934 }
935 }
936 t.Errorf("expected to find %q in import list", w)
937 }
938 }
939
View as plain text