github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/tools/go/buildutil/util_test.go (about) 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 // +build !android 8 9 package buildutil_test 10 11 import ( 12 "go/build" 13 "os" 14 "path/filepath" 15 "runtime" 16 "testing" 17 18 "golang.org/x/tools/go/buildutil" 19 ) 20 21 func TestContainingPackage(t *testing.T) { 22 // unvirtualized: 23 goroot := runtime.GOROOT() 24 gopath := filepath.SplitList(os.Getenv("GOPATH"))[0] 25 26 tests := [][2]string{ 27 {goroot + "/src/fmt/print.go", "fmt"}, 28 {goroot + "/src/encoding/json/foo.go", "encoding/json"}, 29 {goroot + "/src/encoding/missing/foo.go", "(not found)"}, 30 {gopath + "/src/golang.org/x/tools/go/buildutil/util_test.go", 31 "golang.org/x/tools/go/buildutil"}, 32 } 33 // TODO(adonovan): simplify after Go 1.6. 34 if buildutil.AllowVendor != 0 { 35 tests = append(tests, [2]string{ 36 gopath + "/src/vendor/golang.org/x/net/http2/hpack/hpack.go", 37 "vendor/golang.org/x/net/http2/hpack", 38 }) 39 } 40 for _, test := range tests { 41 file, want := test[0], test[1] 42 bp, err := buildutil.ContainingPackage(&build.Default, ".", file) 43 got := bp.ImportPath 44 if err != nil { 45 got = "(not found)" 46 } 47 if got != want { 48 t.Errorf("ContainingPackage(%q) = %s, want %s", file, got, want) 49 } 50 } 51 52 // TODO(adonovan): test on virtualized GOPATH too. 53 } 54 55 func TestStripVendor(t *testing.T) { 56 for _, test := range []struct { 57 path, want string 58 }{ 59 {"", ""}, 60 {"a", "a"}, 61 {"a/b", "a/b"}, 62 {"a/vendor/b", "b"}, 63 {"a/b/vendor/c/d", "c/d"}, 64 {"vendor/a/b", "a/b"}, 65 {"a/vendor", "a/vendor"}, 66 } { 67 if got := buildutil.StripVendor(test.path); got != test.want { 68 t.Errorf("StripVendor(%q) = %q, want %q", 69 test.path, got, test.want) 70 } 71 } 72 }