GoPLS Viewer

Home|gopls/go/buildutil/util_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
5package buildutil_test
6
7import (
8    "go/build"
9    "io/ioutil"
10    "os"
11    "path/filepath"
12    "runtime"
13    "strings"
14    "testing"
15
16    "golang.org/x/tools/go/buildutil"
17    "golang.org/x/tools/go/packages/packagestest"
18)
19
20func TestContainingPackage(t *testing.T) {
21    if runtime.Compiler == "gccgo" {
22        t.Skip("gccgo has no GOROOT")
23    }
24
25    exported := packagestest.Export(tpackagestest.GOPATH, []packagestest.Module{
26        {Name"golang.org/x/tools/go/buildutil"Filespackagestest.MustCopyFileTree(".")}})
27    defer exported.Cleanup()
28
29    goroot := runtime.GOROOT()
30    var gopath string
31    for _env := range exported.Config.Env {
32        if !strings.HasPrefix(env"GOPATH=") {
33            continue
34        }
35        gopath = strings.TrimPrefix(env"GOPATH=")
36    }
37    if gopath == "" {
38        t.Fatal("Failed to fish GOPATH out of env: "exported.Config.Env)
39    }
40    buildutildir := filepath.Join(gopath"golang.org""x""tools""go""buildutil")
41
42    type Test struct {
43        gopathfilenamewantPkg string
44    }
45
46    tests := []Test{
47        {gopathgoroot + "/src/fmt/print.go""fmt"},
48        {gopathgoroot + "/src/encoding/json/foo.go""encoding/json"},
49        {gopathgoroot + "/src/encoding/missing/foo.go""(not found)"},
50        {gopathgopath + "/src/golang.org/x/tools/go/buildutil/util_test.go",
51            "golang.org/x/tools/go/buildutil"},
52    }
53
54    if runtime.GOOS != "windows" && runtime.GOOS != "plan9" {
55        // Make a symlink to gopath for test
56        tmperr := ioutil.TempDir(os.TempDir(), "go")
57        if err != nil {
58            t.Errorf("Unable to create a temporary directory in %s"os.TempDir())
59        }
60
61        defer os.RemoveAll(tmp)
62
63        // symlink between $GOPATH/src and /tmp/go/src
64        // in order to test all possible symlink cases
65        if err := os.Symlink(gopath+"/src"tmp+"/src"); err != nil {
66            t.Fatal(err)
67        }
68        tests = append(tests, []Test{
69            {gopathtmp + "/src/golang.org/x/tools/go/buildutil/util_test.go""golang.org/x/tools/go/buildutil"},
70            {tmpgopath + "/src/golang.org/x/tools/go/buildutil/util_test.go""golang.org/x/tools/go/buildutil"},
71            {tmptmp + "/src/golang.org/x/tools/go/buildutil/util_test.go""golang.org/x/tools/go/buildutil"},
72        }...)
73    }
74
75    for _test := range tests {
76        var got string
77        var buildContext = build.Default
78        buildContext.GOPATH = test.gopath
79        bperr := buildutil.ContainingPackage(&buildContextbuildutildirtest.filename)
80        if err != nil {
81            got = "(not found)"
82        } else {
83            got = bp.ImportPath
84        }
85        if got != test.wantPkg {
86            t.Errorf("ContainingPackage(%q) = %s, want %s"test.filenamegottest.wantPkg)
87        }
88    }
89
90}
91
MembersX
TestContainingPackage.Test
TestContainingPackage.RangeStmt_2238.BlockStmt.got
TestContainingPackage.RangeStmt_718.env
TestContainingPackage.Test.filename
TestContainingPackage.Test.wantPkg
TestContainingPackage.RangeStmt_2238.test
TestContainingPackage.RangeStmt_2238.BlockStmt.buildContext
TestContainingPackage.RangeStmt_2238.BlockStmt.err
TestContainingPackage.goroot
TestContainingPackage.Test.gopath
TestContainingPackage.tests
TestContainingPackage.BlockStmt.err
TestContainingPackage
TestContainingPackage.exported
TestContainingPackage.gopath
TestContainingPackage.buildutildir
TestContainingPackage.BlockStmt.tmp
TestContainingPackage.RangeStmt_2238.BlockStmt.bp
TestContainingPackage.t
Members
X