Debian Codegraph @

go/buildutil/allpackages_test.go

raw symbols SCIP
1 // Copyright 2014 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 // Incomplete source tree on Android.
6
7 //go:build !android
8
9 package buildutil_test
10
11 import (
12 "go/build"
13 "runtime"
14 "sort"
15 "strings"
16 "testing"
17
18 "golang.org/x/tools/go/buildutil"
19 "golang.org/x/tools/internal/packagestest"
20 )
21
22 func TestAllPackages(t *testing.T) {
23 if runtime.Compiler == "gccgo" {
24 t.Skip("gccgo has no standard packages")
25 }
26
27 exported := packagestest.Export(t, packagestest.GOPATH, []packagestest.Module{
28 {Name: "golang.org/x/tools/go/buildutil", Files: packagestest.MustCopyFileTree(".")}})
29 defer exported.Cleanup()
30
31 var gopath string
32 for _, env := range exported.Config.Env {
33 if !strings.HasPrefix(env, "GOPATH=") {
34 continue
35 }
36 gopath = strings.TrimPrefix(env, "GOPATH=")
37 }
38 if gopath == "" {
39 t.Fatal("Failed to fish GOPATH out of env: ", exported.Config.Env)
40 }
41
42 var buildContext = build.Default
43 buildContext.GOPATH = gopath
44 all := buildutil.AllPackages(&buildContext)
45
46 set := make(map[string]bool)
47 for _, pkg := range all {
48 set[pkg] = true
49 }
50
51 const wantAtLeast = 250
52 if len(all) < wantAtLeast {
53 t.Errorf("Found only %d packages, want at least %d", len(all), wantAtLeast)
54 }
55
56 for _, want := range []string{"fmt", "crypto/sha256", "golang.org/x/tools/go/buildutil"} {
57 if !set[want] {
58 t.Errorf("Package %q not found; got %s", want, all)
59 }
60 }
61 }
62
63 func TestExpandPatterns(t *testing.T) {
64 tree := make(map[string]map[string]string)
65 for _, pkg := range []string{
66 "encoding",
67 "encoding/xml",
68 "encoding/hex",
69 "encoding/json",
70 "fmt",
71 } {
72 tree[pkg] = make(map[string]string)
73 }
74 ctxt := buildutil.FakeContext(tree)
75
76 for _, test := range []struct {
77 patterns string
78 want string
79 }{
80 {"", ""},
81 {"fmt", "fmt"},
82 {"nosuchpkg", "nosuchpkg"},
83 {"nosuchdir/...", ""},
84 {"...", "encoding encoding/hex encoding/json encoding/xml fmt"},
85 {"encoding/... -encoding/xml", "encoding encoding/hex encoding/json"},
86 {"... -encoding/...", "fmt"},
87 {"encoding", "encoding"},
88 {"encoding/", "encoding"},
89 } {
90 var pkgs []string
91 for pkg := range buildutil.ExpandPatterns(ctxt, strings.Fields(test.patterns)) {
92 pkgs = append(pkgs, pkg)
93 }
94 sort.Strings(pkgs)
95 got := strings.Join(pkgs, " ")
96 if got != test.want {
97 t.Errorf("ExpandPatterns(%s) = %s, want %s",
98 test.patterns, got, test.want)
99 }
100 }
101 }