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