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.
5 // Incomplete source tree on Android.
10 package buildutil_test
19 "golang.org/x/tools/go/buildutil"
20 "golang.org/x/tools/go/packages/packagestest"
23 func TestAllPackages(t *testing.T) {
24 if runtime.Compiler == "gccgo" {
25 t.Skip("gccgo has no standard packages")
28 exported := packagestest.Export(t, packagestest.GOPATH, []packagestest.Module{
29 {Name: "golang.org/x/tools/go/buildutil", Files: packagestest.MustCopyFileTree(".")}})
30 defer exported.Cleanup()
33 for _, env := range exported.Config.Env {
34 if !strings.HasPrefix(env, "GOPATH=") {
37 gopath = strings.TrimPrefix(env, "GOPATH=")
40 t.Fatal("Failed to fish GOPATH out of env: ", exported.Config.Env)
43 var buildContext = build.Default
44 buildContext.GOPATH = gopath
45 all := buildutil.AllPackages(&buildContext)
47 set := make(map[string]bool)
48 for _, pkg := range all {
52 const wantAtLeast = 250
53 if len(all) < wantAtLeast {
54 t.Errorf("Found only %d packages, want at least %d", len(all), wantAtLeast)
57 for _, want := range []string{"fmt", "crypto/sha256", "golang.org/x/tools/go/buildutil"} {
59 t.Errorf("Package %q not found; got %s", want, all)
64 func TestExpandPatterns(t *testing.T) {
65 tree := make(map[string]map[string]string)
66 for _, pkg := range []string{
73 tree[pkg] = make(map[string]string)
75 ctxt := buildutil.FakeContext(tree)
77 for _, test := range []struct {
83 {"nosuchpkg", "nosuchpkg"},
84 {"nosuchdir/...", ""},
85 {"...", "encoding encoding/hex encoding/json encoding/xml fmt"},
86 {"encoding/... -encoding/xml", "encoding encoding/hex encoding/json"},
87 {"... -encoding/...", "fmt"},
88 {"encoding", "encoding"},
89 {"encoding/", "encoding"},
92 for pkg := range buildutil.ExpandPatterns(ctxt, strings.Fields(test.patterns)) {
93 pkgs = append(pkgs, pkg)
96 got := strings.Join(pkgs, " ")
98 t.Errorf("ExpandPatterns(%s) = %s, want %s",
99 test.patterns, got, test.want)