GoPLS Viewer

Home|gopls/go/buildutil/allpackages_test.go
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
10package buildutil_test
11
12import (
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
23func TestAllPackages(t *testing.T) {
24    if runtime.Compiler == "gccgo" {
25        t.Skip("gccgo has no standard packages")
26    }
27
28    exported := packagestest.Export(tpackagestest.GOPATH, []packagestest.Module{
29        {Name"golang.org/x/tools/go/buildutil"Filespackagestest.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"wantall)
60        }
61    }
62}
63
64func 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(ctxtstrings.Fields(test.patterns)) {
93            pkgs = append(pkgspkg)
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.patternsgottest.want)
100        }
101    }
102}
103
MembersX
TestExpandPatterns.RangeStmt_1765.BlockStmt.RangeStmt_2178.pkg
runtime
packagestest
TestAllPackages.t
TestAllPackages.gopath
TestAllPackages.buildContext
TestExpandPatterns.tree
TestExpandPatterns.RangeStmt_1765.test
TestAllPackages.exported
TestAllPackages.RangeStmt_746.env
TestAllPackages.RangeStmt_1128.pkg
TestExpandPatterns.RangeStmt_1572.pkg
TestExpandPatterns.RangeStmt_1765.BlockStmt.pkgs
buildutil
TestAllPackages.all
TestAllPackages.set
TestAllPackages.wantAtLeast
TestExpandPatterns
TestExpandPatterns.t
TestExpandPatterns.ctxt
testing
TestAllPackages
TestAllPackages.RangeStmt_1313.want
TestExpandPatterns.RangeStmt_1765.BlockStmt.got
Members
X