| 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 | package buildutil_testscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/buildutil_test`/ |
| 6 | |
| 7 | import ( |
| 8 | "go/buildscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/" |
| 9 | "osscip-go gomod github.com/golang/go/src go1.25.0 os/" |
| 10 | "path/filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/" |
| 11 | "runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/" |
| 12 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 13 | "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/" |
| 14 | |
| 15 | "golang.org/x/tools/go/buildutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/buildutil`/" |
| 16 | "golang.org/x/tools/internal/packagestestscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/" |
| 17 | ) |
| 18 | |
| 19 | func TestContainingPackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/buildutil_test`/TestContainingPackage().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 20 | if runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/.Compilerscip-go gomod github.com/golang/go/src go1.25.0 runtime/Compiler. == "gccgo" { |
| 21 | t.Skipscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Skip().("gccgo has no GOROOT") |
| 22 | } |
| 23 | |
| 24 | exported := packagestestscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/.Exportscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Export().(t, packagestestscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/.GOPATHscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/GOPATH., []packagestestscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/.Modulescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Module#{ |
| 25 | {Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Module#Name.: "golang.org/x/tools/go/buildutil", Filesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Module#Files.: packagestestscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/.MustCopyFileTreescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/MustCopyFileTree().(".")}}) |
| 26 | defer exported.Cleanupscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#Cleanup().() |
| 27 | |
| 28 | goroot := runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/.GOROOTscip-go gomod github.com/golang/go/src go1.25.0 runtime/GOROOT().() |
| 29 | var gopath string |
| 30 | for _, env := range exported.Configscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#Config..Envscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#Env. { |
| 31 | if !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasPrefix().(env, "GOPATH=") { |
| 32 | continue |
| 33 | } |
| 34 | gopath = stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/TrimPrefix().(env, "GOPATH=") |
| 35 | } |
| 36 | if gopath == "" { |
| 37 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().("Failed to fish GOPATH out of env: ", exported.Configscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#Config..Envscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#Env.) |
| 38 | } |
| 39 | buildutildir := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(gopath, "golang.org", "x", "tools", "go", "buildutil") |
| 40 | |
| 41 | type Test struct { |
| 42 | gopath, filename, wantPkg string |
| 43 | } |
| 44 | |
| 45 | tests := []Test{ |
| 46 | {gopath, goroot + "/src/fmt/print.go", "fmt"}, |
| 47 | {gopath, goroot + "/src/encoding/json/foo.go", "encoding/json"}, |
| 48 | {gopath, goroot + "/src/encoding/missing/foo.go", "(not found)"}, |
| 49 | {gopath, gopath + "/src/golang.org/x/tools/go/buildutil/util_test.go", |
| 50 | "golang.org/x/tools/go/buildutil"}, |
| 51 | } |
| 52 | |
| 53 | if runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/.GOOSscip-go gomod github.com/golang/go/src go1.25.0 runtime/GOOS. != "windows" && runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/.GOOSscip-go gomod github.com/golang/go/src go1.25.0 runtime/GOOS. != "plan9" { |
| 54 | // Make a symlink to gopath for test |
| 55 | tmp, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.MkdirTempscip-go gomod github.com/golang/go/src go1.25.0 os/MkdirTemp().(osscip-go gomod github.com/golang/go/src go1.25.0 os/.TempDirscip-go gomod github.com/golang/go/src go1.25.0 os/TempDir().(), "go") |
| 56 | if err != nil { |
| 57 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Unable to create a temporary directory in %s", osscip-go gomod github.com/golang/go/src go1.25.0 os/.TempDirscip-go gomod github.com/golang/go/src go1.25.0 os/TempDir().()) |
| 58 | } |
| 59 | |
| 60 | defer osscip-go gomod github.com/golang/go/src go1.25.0 os/.RemoveAllscip-go gomod github.com/golang/go/src go1.25.0 os/RemoveAll().(tmp) |
| 61 | |
| 62 | // symlink between $GOPATH/src and /tmp/go/src |
| 63 | // in order to test all possible symlink cases |
| 64 | if err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Symlinkscip-go gomod github.com/golang/go/src go1.25.0 os/Symlink().(gopath+"/src", tmp+"/src"); err != nil { |
| 65 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 66 | } |
| 67 | tests = append(tests, []Test{ |
| 68 | {gopath, tmp + "/src/golang.org/x/tools/go/buildutil/util_test.go", "golang.org/x/tools/go/buildutil"}, |
| 69 | {tmp, gopath + "/src/golang.org/x/tools/go/buildutil/util_test.go", "golang.org/x/tools/go/buildutil"}, |
| 70 | {tmp, tmp + "/src/golang.org/x/tools/go/buildutil/util_test.go", "golang.org/x/tools/go/buildutil"}, |
| 71 | }...) |
| 72 | } |
| 73 | |
| 74 | for _, test := range tests { |
| 75 | var got string |
| 76 | var buildContext = buildscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/.Defaultscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Default. |
| 77 | buildContext.GOPATHscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Context#GOPATH. = test.gopath |
| 78 | bp, err := buildutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/buildutil`/.ContainingPackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/buildutil`/ContainingPackage().(&buildContext, buildutildir, test.filename) |
| 79 | if err != nil { |
| 80 | got = "(not found)" |
| 81 | } else { |
| 82 | got = bp.ImportPathscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Package#ImportPath. |
| 83 | } |
| 84 | if got != test.wantPkg { |
| 85 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("ContainingPackage(%q) = %s, want %s", test.filename, got, test.wantPkg) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | } |