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.
18 "golang.org/x/tools/go/buildutil"
19 "golang.org/x/tools/go/packages/packagestest"
22 func TestAllPackages(t *testing.T) {
23 if runtime.Compiler == "gccgo" {
24 t.Skip("gccgo has no standard packages")
27 exported := packagestest.Export(t, packagestest.GOPATH, []packagestest.Module{
28 {Name: "golang.org/x/tools/go/buildutil", Files: packagestest.MustCopyFileTree(".")}})
29 defer exported.Cleanup()
32 for _, env := range exported.Config.Env {
33 if !strings.HasPrefix(env, "GOPATH=") {
36 gopath = strings.TrimPrefix(env, "GOPATH=")
39 t.Fatal("Failed to fish GOPATH out of env: ", exported.Config.Env)
42 var buildContext = build.Default
43 buildContext.GOPATH = gopath
44 all := buildutil.AllPackages(&buildContext)
46 set := make(map[string]bool)
47 for _, pkg := range all {
51 const wantAtLeast = 250
52 if len(all) < wantAtLeast {
53 t.Errorf("Found only %d packages, want at least %d", len(all), wantAtLeast)
56 for _, want := range []string{"fmt", "crypto/sha256", "golang.org/x/tools/go/buildutil"} {
58 t.Errorf("Package %q not found; got %s", want, all)
63 func TestExpandPatterns(t *testing.T) {
64 tree := make(map[string]map[string]string)
65 for _, pkg := range []string{
72 tree[pkg] = make(map[string]string)
74 ctxt := buildutil.FakeContext(tree)
76 for _, test := range []struct {
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"},
91 for pkg := range buildutil.ExpandPatterns(ctxt, strings.Fields(test.patterns)) {
92 pkgs = append(pkgs, pkg)
95 got := strings.Join(pkgs, " ")
97 t.Errorf("ExpandPatterns(%s) = %s, want %s",
98 test.patterns, got, test.want)