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