mirror of
https://github.com/golang/go.git
synced 2025-05-05 15:43:04 +00:00
imports: port tests to packagestest
I tried to introduce new modules where it made sense. I deleted the weird goroot test because I removed the optimization it was testing in an earlier CL. Change-Id: I219ddaa4f462a4aeb640f62215d16f246511a5fe Reviewed-on: https://go-review.googlesource.com/c/144497 Run-TryBot: Heschi Kreinick <heschi@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
parent
a2dc47679d
commit
e74f1bd585
@ -27,7 +27,7 @@ import (
|
|||||||
// └── golang.org
|
// └── golang.org
|
||||||
// └── repob
|
// └── repob
|
||||||
// └── b
|
// └── b
|
||||||
// └── b.go
|
// └── b.go
|
||||||
// GOPATH would be set to
|
// GOPATH would be set to
|
||||||
// /sometemporarydirectory/repoa;/sometemporarydirectory/repob
|
// /sometemporarydirectory/repoa;/sometemporarydirectory/repob
|
||||||
// and the working directory would be
|
// and the working directory would be
|
||||||
|
@ -932,15 +932,3 @@ func findImportStdlib(shortPkg string, symbols map[string]bool) (importPath stri
|
|||||||
}
|
}
|
||||||
return importPath, importPath != ""
|
return importPath, importPath != ""
|
||||||
}
|
}
|
||||||
|
|
||||||
// fileInDir reports whether the provided file path looks like
|
|
||||||
// it's in dir. (without hitting the filesystem)
|
|
||||||
func fileInDir(file, dir string) bool {
|
|
||||||
rest := strings.TrimPrefix(file, dir)
|
|
||||||
if len(rest) == len(file) {
|
|
||||||
// dir is not a prefix of file.
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
// Check for boundary: either nothing (file == dir), or a slash.
|
|
||||||
return len(rest) == 0 || rest[0] == '/' || rest[0] == '\\'
|
|
||||||
}
|
|
||||||
|
@ -7,13 +7,13 @@ package imports
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"go/build"
|
"go/build"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"golang.org/x/tools/go/packages/packagestest"
|
||||||
)
|
)
|
||||||
|
|
||||||
var tests = []struct {
|
var tests = []struct {
|
||||||
@ -1122,17 +1122,30 @@ func TestSimpleCases(t *testing.T) {
|
|||||||
// Skeleton non-stdlib packages for use during testing.
|
// Skeleton non-stdlib packages for use during testing.
|
||||||
// Each includes one arbitrary symbol, e.g. the first declaration in the first file.
|
// Each includes one arbitrary symbol, e.g. the first declaration in the first file.
|
||||||
// Try not to add more without a good reason.
|
// Try not to add more without a good reason.
|
||||||
gopathFiles: map[string]string{
|
modules: []packagestest.Module{
|
||||||
"appengine/x.go": "package appengine\nfunc Main(){}\n",
|
{
|
||||||
"appengine/datastore/x.go": "package datastore\nvar ErrInvalidEntityType error\n",
|
Name: "appengine",
|
||||||
"rsc.io/p/x.go": "package p\nfunc P(){}\n",
|
Files: fm{
|
||||||
"code.google.com/p/snappy-go/snappy/x.go": "package snappy\nvar ErrCorrupt error\n",
|
"x.go": "package appengine\nfunc Main(){}\n",
|
||||||
"x/x.go": tt.in,
|
"datastore/x.go": "package datastore\nvar ErrInvalidEntityType error\n",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "rsc.io",
|
||||||
|
Files: fm{"p/x.go": "package p\nfunc P(){}\n"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "code.google.com/p/snappy-go",
|
||||||
|
Files: fm{"snappy/x.go": "package snappy\nvar ErrCorrupt error\n"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "golang.org/fake",
|
||||||
|
Files: fm{"x.go": tt.in},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}.processTest(t, "x/x.go", nil, options, tt.out)
|
}.processTest(t, "golang.org/fake", "x.go", nil, options, tt.out)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReadFromFilesystem(t *testing.T) {
|
func TestReadFromFilesystem(t *testing.T) {
|
||||||
@ -1182,10 +1195,11 @@ func bar() {
|
|||||||
Fragment: true,
|
Fragment: true,
|
||||||
}
|
}
|
||||||
testConfig{
|
testConfig{
|
||||||
gopathFiles: map[string]string{
|
module: packagestest.Module{
|
||||||
"x.go": tt.in,
|
Name: "golang.org/fake",
|
||||||
|
Files: fm{"x.go": tt.in},
|
||||||
},
|
},
|
||||||
}.processTest(t, "x.go", nil, options, tt.out)
|
}.processTest(t, "golang.org/fake", "x.go", nil, options, tt.out)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1210,7 +1224,8 @@ var (
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"x/mypkg"
|
|
||||||
|
"golang.org/fake/x/y/mypkg"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -1220,13 +1235,16 @@ var (
|
|||||||
`
|
`
|
||||||
|
|
||||||
testConfig{
|
testConfig{
|
||||||
gopathFiles: map[string]string{
|
module: packagestest.Module{
|
||||||
"../target/f.go": "package mypkg\nvar Foo = 123\n",
|
Name: "golang.org/fake",
|
||||||
"x/mypkg": "LINK:../../target", // valid symlink
|
Files: fm{
|
||||||
"x/apkg": "LINK:..", // symlink loop
|
"target/f.go": "package mypkg\nvar Foo = 123\n",
|
||||||
"myotherpackage/toformat.go": input,
|
"x/y/mypkg": packagestest.Symlink("../../target"), // valid symlink
|
||||||
|
"x/y/apkg": packagestest.Symlink(".."), // symlink loop
|
||||||
|
"myotherpackage/toformat.go": input,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}.processTest(t, "myotherpackage/toformat.go", nil, nil, want)
|
}.processTest(t, "golang.org/fake", "myotherpackage/toformat.go", nil, nil, want)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestImportSymlinksWithIgnore(t *testing.T) {
|
func TestImportSymlinksWithIgnore(t *testing.T) {
|
||||||
@ -1253,14 +1271,17 @@ var (
|
|||||||
`
|
`
|
||||||
|
|
||||||
testConfig{
|
testConfig{
|
||||||
gopathFiles: map[string]string{
|
module: packagestest.Module{
|
||||||
"../target/f.go": "package mypkg\nvar Foo = 123\n",
|
Name: "golang.org/fake",
|
||||||
"x/mypkg": "LINK:../../target", // valid symlink
|
Files: fm{
|
||||||
"x/apkg": "LINK:..", // symlink loop
|
"target/f.go": "package mypkg\nvar Foo = 123\n",
|
||||||
"myotherpkg/toformat.go": input,
|
"x/y/mypkg": packagestest.Symlink("../../target"), // valid symlink
|
||||||
".goimportsignore": "x/mypkg\n",
|
"x/y/apkg": packagestest.Symlink(".."), // symlink loop
|
||||||
|
"myotherpkg/toformat.go": input,
|
||||||
|
"../../.goimportsignore": "golang.org/fake/x/y/mypkg\n",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}.processTest(t, "myotherpkg/toformat.go", nil, nil, want)
|
}.processTest(t, "golang.org/fake", "myotherpkg/toformat.go", nil, nil, want)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test for x/y/v2 convention for package y.
|
// Test for x/y/v2 convention for package y.
|
||||||
@ -1280,10 +1301,11 @@ var (
|
|||||||
`
|
`
|
||||||
|
|
||||||
testConfig{
|
testConfig{
|
||||||
gopathFiles: map[string]string{
|
module: packagestest.Module{
|
||||||
"mypkg.com/outpkg/toformat.go": input,
|
Name: "mypkg.com/outpkg",
|
||||||
|
Files: fm{"toformat.go": input},
|
||||||
},
|
},
|
||||||
}.processTest(t, "mypkg.com/outpkg/toformat.go", nil, nil, input)
|
}.processTest(t, "mypkg.com/outpkg", "toformat.go", nil, nil, input)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test for correctly identifying the name of a vendored package when it
|
// Test for correctly identifying the name of a vendored package when it
|
||||||
@ -1305,11 +1327,14 @@ var (
|
|||||||
)
|
)
|
||||||
`
|
`
|
||||||
testConfig{
|
testConfig{
|
||||||
gopathFiles: map[string]string{
|
module: packagestest.Module{
|
||||||
"mypkg.com/outpkg/vendor/mypkg.com/mypkg.v1/f.go": "package mypkg\nvar Foo = 123\n",
|
Name: "mypkg.com/outpkg",
|
||||||
"mypkg.com/outpkg/toformat.go": input,
|
Files: fm{
|
||||||
|
"vendor/mypkg.com/mypkg.v1/f.go": "package mypkg\nvar Foo = 123\n",
|
||||||
|
"toformat.go": input,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}.processTest(t, "mypkg.com/outpkg/toformat.go", nil, nil, input)
|
}.processTest(t, "mypkg.com/outpkg", "toformat.go", nil, nil, input)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInternal(t *testing.T) {
|
func TestInternal(t *testing.T) {
|
||||||
@ -1319,26 +1344,35 @@ var _ = race.Acquire
|
|||||||
`
|
`
|
||||||
const importAdded = `package bar
|
const importAdded = `package bar
|
||||||
|
|
||||||
import "foo/internal/race"
|
import "foo.com/internal/race"
|
||||||
|
|
||||||
var _ = race.Acquire
|
var _ = race.Acquire
|
||||||
`
|
`
|
||||||
|
|
||||||
// Packages under the same directory should be able to use internal packages.
|
// Packages under the same directory should be able to use internal packages.
|
||||||
testConfig{
|
testConfig{
|
||||||
gopathFiles: map[string]string{
|
module: packagestest.Module{
|
||||||
"foo/internal/race/x.go": "package race\n func Acquire(){}\n",
|
Name: "foo.com",
|
||||||
"foo/bar/x.go": input,
|
Files: fm{
|
||||||
|
"internal/race/x.go": "package race\n func Acquire(){}\n",
|
||||||
|
"bar/x.go": input,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}.processTest(t, "foo/bar/x.go", nil, nil, importAdded)
|
}.processTest(t, "foo.com", "bar/x.go", nil, nil, importAdded)
|
||||||
|
|
||||||
// Packages outside the same directory should not.
|
// Packages outside the same directory should not.
|
||||||
testConfig{
|
testConfig{
|
||||||
gopathFiles: map[string]string{
|
modules: []packagestest.Module{
|
||||||
"foo/internal/race/x.go": "package race\n func Acquire(){}\n",
|
{
|
||||||
"bar/x.go": input,
|
Name: "foo.com",
|
||||||
|
Files: fm{"internal/race/x.go": "package race\n func Acquire(){}\n"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "bar.com",
|
||||||
|
Files: fm{"x.go": input},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}.processTest(t, "bar/x.go", nil, nil, input)
|
}.processTest(t, "bar.com", "x.go", nil, nil, input)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProcessVendor(t *testing.T) {
|
func TestProcessVendor(t *testing.T) {
|
||||||
@ -1353,11 +1387,14 @@ import "golang.org/x/net/http2/hpack"
|
|||||||
var _ = hpack.HuffmanDecode
|
var _ = hpack.HuffmanDecode
|
||||||
`
|
`
|
||||||
testConfig{
|
testConfig{
|
||||||
gopathFiles: map[string]string{
|
module: packagestest.Module{
|
||||||
"vendor/golang.org/x/net/http2/hpack/huffman.go": "package hpack\nfunc HuffmanDecode() { }\n",
|
Name: "foo.com",
|
||||||
"bar/x.go": input,
|
Files: fm{
|
||||||
|
"vendor/golang.org/x/net/http2/hpack/huffman.go": "package hpack\nfunc HuffmanDecode() { }\n",
|
||||||
|
"bar/x.go": input,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}.processTest(t, "bar/x.go", nil, nil, want)
|
}.processTest(t, "foo.com", "bar/x.go", nil, nil, want)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFindStdlib(t *testing.T) {
|
func TestFindStdlib(t *testing.T) {
|
||||||
@ -1388,70 +1425,34 @@ func TestFindStdlib(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type testConfig struct {
|
type testConfig struct {
|
||||||
// goroot and gopath optionally specifies the path on disk
|
module packagestest.Module
|
||||||
// to use for the GOROOT and GOPATH. If empty, a temp directory
|
modules []packagestest.Module
|
||||||
// is made if needed.
|
|
||||||
goroot, gopath string
|
|
||||||
|
|
||||||
// gorootFiles optionally specifies the complete contents of GOROOT to use,
|
|
||||||
// If nil, the normal current $GOROOT is used.
|
|
||||||
gorootFiles map[string]string // paths relative to $GOROOT/src to contents
|
|
||||||
|
|
||||||
// gopathFiles is like gorootFiles, but for $GOPATH.
|
|
||||||
// If nil, there is no GOPATH, though.
|
|
||||||
gopathFiles map[string]string // paths relative to $GOPATH/src to contents
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func mustTempDir(t *testing.T, prefix string) string {
|
// fm is the type for a packagestest.Module's Files, abbreviated for shorter lines.
|
||||||
t.Helper()
|
type fm map[string]interface{}
|
||||||
dir, err := ioutil.TempDir("", prefix)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
return dir
|
|
||||||
}
|
|
||||||
|
|
||||||
func mapToDir(destDir string, files map[string]string) error {
|
|
||||||
for path, contents := range files {
|
|
||||||
file := filepath.Join(destDir, "src", path)
|
|
||||||
if err := os.MkdirAll(filepath.Dir(file), 0755); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
var err error
|
|
||||||
if strings.HasPrefix(contents, "LINK:") {
|
|
||||||
err = os.Symlink(strings.TrimPrefix(contents, "LINK:"), file)
|
|
||||||
} else {
|
|
||||||
err = ioutil.WriteFile(file, []byte(contents), 0644)
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c testConfig) test(t *testing.T, fn func(*goimportTest)) {
|
func (c testConfig) test(t *testing.T, fn func(*goimportTest)) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
goroot := c.goroot
|
var exported *packagestest.Exported
|
||||||
gopath := c.gopath
|
if c.module.Name != "" {
|
||||||
|
c.modules = []packagestest.Module{c.module}
|
||||||
if c.gorootFiles != nil && goroot == "" {
|
|
||||||
goroot = mustTempDir(t, "goroot-")
|
|
||||||
defer os.RemoveAll(goroot)
|
|
||||||
}
|
|
||||||
if err := mapToDir(goroot, c.gorootFiles); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.gopathFiles != nil && gopath == "" {
|
exported = packagestest.Export(t, packagestest.GOPATH, c.modules)
|
||||||
gopath = mustTempDir(t, "gopath-")
|
defer exported.Cleanup()
|
||||||
defer os.RemoveAll(gopath)
|
|
||||||
}
|
env := make(map[string]string)
|
||||||
if err := mapToDir(gopath, c.gopathFiles); err != nil {
|
for _, kv := range exported.Config.Env {
|
||||||
t.Fatal(err)
|
split := strings.Split(kv, "=")
|
||||||
|
k, v := split[0], split[1]
|
||||||
|
env[k] = v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
goroot := env["GOROOT"]
|
||||||
|
gopath := env["GOPATH"]
|
||||||
|
|
||||||
scanOnce = sync.Once{}
|
scanOnce = sync.Once{}
|
||||||
|
|
||||||
oldGOPATH := build.Default.GOPATH
|
oldGOPATH := build.Default.GOPATH
|
||||||
@ -1466,40 +1467,47 @@ func (c testConfig) test(t *testing.T, fn func(*goimportTest)) {
|
|||||||
build.Default.Compiler = oldCompiler
|
build.Default.Compiler = oldCompiler
|
||||||
}()
|
}()
|
||||||
|
|
||||||
if goroot != "" {
|
build.Default.GOROOT = goroot
|
||||||
build.Default.GOROOT = goroot
|
|
||||||
}
|
|
||||||
build.Default.GOPATH = gopath
|
build.Default.GOPATH = gopath
|
||||||
|
|
||||||
it := &goimportTest{
|
it := &goimportTest{
|
||||||
T: t,
|
T: t,
|
||||||
goroot: build.Default.GOROOT,
|
goroot: build.Default.GOROOT,
|
||||||
gopath: gopath,
|
gopath: gopath,
|
||||||
ctx: &build.Default,
|
ctx: &build.Default,
|
||||||
|
exported: exported,
|
||||||
}
|
}
|
||||||
fn(it)
|
fn(it)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c testConfig) processTest(t *testing.T, file string, contents []byte, opts *Options, want string) {
|
func (c testConfig) processTest(t *testing.T, module, file string, contents []byte, opts *Options, want string) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
c.test(t, func(t *goimportTest) {
|
c.test(t, func(t *goimportTest) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
t.process(file, contents, opts, want)
|
f := t.exported.File(module, file)
|
||||||
|
if f == "" {
|
||||||
|
t.Fatalf("%v not found in exported files (typo in filename?)", file)
|
||||||
|
}
|
||||||
|
t.process(f, contents, opts, want)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
type goimportTest struct {
|
type goimportTest struct {
|
||||||
*testing.T
|
*testing.T
|
||||||
ctx *build.Context
|
ctx *build.Context
|
||||||
goroot string
|
goroot string
|
||||||
gopath string
|
gopath string
|
||||||
|
exported *packagestest.Exported
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *goimportTest) process(file string, contents []byte, opts *Options, want string) {
|
func (t *goimportTest) process(file string, contents []byte, opts *Options, want string) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
buf, err := Process(filepath.Join(t.gopath, "src", file), contents, opts)
|
if !filepath.IsAbs(file) {
|
||||||
|
file = filepath.Join(t.gopath, "src", file)
|
||||||
|
}
|
||||||
|
buf, err := Process(file, contents, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatalf("Process() = %v", err)
|
||||||
}
|
}
|
||||||
if string(buf) != want {
|
if string(buf) != want {
|
||||||
t.Errorf("Got:\n%s\nWant:\n%s", buf, want)
|
t.Errorf("Got:\n%s\nWant:\n%s", buf, want)
|
||||||
@ -1516,41 +1524,47 @@ func TestRenameWhenPackageNameMismatch(t *testing.T) {
|
|||||||
|
|
||||||
const want = `package main
|
const want = `package main
|
||||||
|
|
||||||
import bar "foo/bar/v1"
|
import bar "foo.com/foo/bar/v1"
|
||||||
|
|
||||||
const Y = bar.X
|
const Y = bar.X
|
||||||
`
|
`
|
||||||
testConfig{
|
testConfig{
|
||||||
gopathFiles: map[string]string{
|
module: packagestest.Module{
|
||||||
"foo/bar/v1/x.go": "package bar \n const X = 1",
|
Name: "foo.com",
|
||||||
"test/t.go": input,
|
Files: fm{
|
||||||
|
"foo/bar/v1/x.go": "package bar \n const X = 1",
|
||||||
|
"test/t.go": input,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}.processTest(t, "test/t.go", nil, nil, want)
|
}.processTest(t, "foo.com", "test/t.go", nil, nil, want)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests that the LocalPrefix option causes imports
|
// Tests that the LocalPrefix option causes imports
|
||||||
// to be added into a later group (num=3).
|
// to be added into a later group (num=3).
|
||||||
func TestLocalPrefix(t *testing.T) {
|
func TestLocalPrefix(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
config testConfig
|
modules []packagestest.Module
|
||||||
localPrefix string
|
localPrefix string
|
||||||
src string
|
src string
|
||||||
want string
|
want string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
config: testConfig{
|
modules: []packagestest.Module{
|
||||||
gopathFiles: map[string]string{
|
{
|
||||||
"foo/bar/bar.go": "package bar \n const X = 1",
|
Name: "foo.com",
|
||||||
|
Files: fm{
|
||||||
|
"bar/bar.go": "package bar \n const X = 1",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
localPrefix: "foo/",
|
localPrefix: "foo.com/",
|
||||||
src: "package main \n const Y = bar.X \n const _ = runtime.GOOS",
|
src: "package main \n const Y = bar.X \n const _ = runtime.GOOS",
|
||||||
want: `package main
|
want: `package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"runtime"
|
"runtime"
|
||||||
|
|
||||||
"foo/bar"
|
"foo.com/bar"
|
||||||
)
|
)
|
||||||
|
|
||||||
const Y = bar.X
|
const Y = bar.X
|
||||||
@ -1558,21 +1572,24 @@ const _ = runtime.GOOS
|
|||||||
`,
|
`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
config: testConfig{
|
modules: []packagestest.Module{
|
||||||
gopathFiles: map[string]string{
|
{
|
||||||
"foo/foo.go": "package foo \n const X = 1",
|
Name: "foo.com",
|
||||||
"foo/bar/bar.go": "package bar \n const X = 1",
|
Files: fm{
|
||||||
|
"foo/foo.go": "package foo \n const X = 1",
|
||||||
|
"foo/bar/bar.go": "package bar \n const X = 1",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
localPrefix: "foo/",
|
localPrefix: "foo.com/foo",
|
||||||
src: "package main \n const Y = bar.X \n const Z = foo.X \n const _ = runtime.GOOS",
|
src: "package main \n const Y = bar.X \n const Z = foo.X \n const _ = runtime.GOOS",
|
||||||
want: `package main
|
want: `package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"runtime"
|
"runtime"
|
||||||
|
|
||||||
"foo"
|
"foo.com/foo"
|
||||||
"foo/bar"
|
"foo.com/foo/bar"
|
||||||
)
|
)
|
||||||
|
|
||||||
const Y = bar.X
|
const Y = bar.X
|
||||||
@ -1581,14 +1598,21 @@ const _ = runtime.GOOS
|
|||||||
`,
|
`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
config: testConfig{
|
modules: []packagestest.Module{
|
||||||
gopathFiles: map[string]string{
|
{
|
||||||
"example.org/pkg/pkg.go": "package pkg \n const A = 1",
|
Name: "example.org/pkg",
|
||||||
"foo/bar/bar.go": "package bar \n const B = 1",
|
Files: fm{"pkg.go": "package pkg \n const A = 1"},
|
||||||
"code.org/r/p/expproj/expproj.go": "package expproj \n const C = 1",
|
},
|
||||||
|
{
|
||||||
|
Name: "foo.com",
|
||||||
|
Files: fm{"bar/bar.go": "package bar \n const B = 1"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "code.org/r/p",
|
||||||
|
Files: fm{"expproj/expproj.go": "package expproj \n const C = 1"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
localPrefix: "example.org/pkg,foo/,code.org",
|
localPrefix: "example.org/pkg,foo.com/,code.org",
|
||||||
src: "package main \n const X = pkg.A \n const Y = bar.B \n const Z = expproj.C \n const _ = runtime.GOOS",
|
src: "package main \n const X = pkg.A \n const Y = bar.B \n const Z = expproj.C \n const _ = runtime.GOOS",
|
||||||
want: `package main
|
want: `package main
|
||||||
|
|
||||||
@ -1597,7 +1621,7 @@ import (
|
|||||||
|
|
||||||
"code.org/r/p/expproj"
|
"code.org/r/p/expproj"
|
||||||
"example.org/pkg"
|
"example.org/pkg"
|
||||||
"foo/bar"
|
"foo.com/bar"
|
||||||
)
|
)
|
||||||
|
|
||||||
const X = pkg.A
|
const X = pkg.A
|
||||||
@ -1609,7 +1633,9 @@ const _ = runtime.GOOS
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
tt.config.test(t, func(t *goimportTest) {
|
testConfig{
|
||||||
|
modules: tt.modules,
|
||||||
|
}.test(t, func(t *goimportTest) {
|
||||||
defer func(s string) { LocalPrefix = s }(LocalPrefix)
|
defer func(s string) { LocalPrefix = s }(LocalPrefix)
|
||||||
LocalPrefix = tt.localPrefix
|
LocalPrefix = tt.localPrefix
|
||||||
t.process("test/t.go", []byte(tt.src), nil, tt.want)
|
t.process("test/t.go", []byte(tt.src), nil, tt.want)
|
||||||
@ -1625,18 +1651,21 @@ const Y = foo.X
|
|||||||
`
|
`
|
||||||
const want = `package x
|
const want = `package x
|
||||||
|
|
||||||
import "foo"
|
import "foo.com/foo"
|
||||||
|
|
||||||
const Y = foo.X
|
const Y = foo.X
|
||||||
`
|
`
|
||||||
|
|
||||||
testConfig{
|
testConfig{
|
||||||
gopathFiles: map[string]string{
|
module: packagestest.Module{
|
||||||
"foo/foo.go": "package foo\nconst X = 1\n",
|
Name: "foo.com",
|
||||||
"foo/doc.go": "package documentation \n // just to confuse things\n",
|
Files: fm{
|
||||||
"x/x.go": input,
|
"foo/foo.go": "package foo\nconst X = 1\n",
|
||||||
|
"foo/doc.go": "package documentation \n // just to confuse things\n",
|
||||||
|
"x/x.go": input,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}.processTest(t, "x/x.go", nil, nil, want)
|
}.processTest(t, "foo.com", "x/x.go", nil, nil, want)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests importPathToNameGoPathParse and in particular that it stops
|
// Tests importPathToNameGoPathParse and in particular that it stops
|
||||||
@ -1645,11 +1674,14 @@ const Y = foo.X
|
|||||||
// never make it that far).
|
// never make it that far).
|
||||||
func TestImportPathToNameGoPathParse(t *testing.T) {
|
func TestImportPathToNameGoPathParse(t *testing.T) {
|
||||||
testConfig{
|
testConfig{
|
||||||
gopathFiles: map[string]string{
|
module: packagestest.Module{
|
||||||
"example.net/pkg/doc.go": "package documentation\n", // ignored
|
Name: "example.net/pkg",
|
||||||
"example.net/pkg/gen.go": "package main\n", // also ignored
|
Files: fm{
|
||||||
"example.net/pkg/pkg.go": "package the_pkg_name_to_find\n and this syntax error is ignored because of parser.PackageClauseOnly",
|
"doc.go": "package documentation\n", // ignored
|
||||||
"example.net/pkg/z.go": "package inconsistent\n", // inconsistent but ignored
|
"gen.go": "package main\n", // also ignored
|
||||||
|
"pkg.go": "package the_pkg_name_to_find\n and this syntax error is ignored because of parser.PackageClauseOnly",
|
||||||
|
"z.go": "package inconsistent\n", // inconsistent but ignored
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}.test(t, func(t *goimportTest) {
|
}.test(t, func(t *goimportTest) {
|
||||||
got, err := importPathToNameGoPathParse("example.net/pkg", filepath.Join(t.gopath, "src", "other.net"))
|
got, err := importPathToNameGoPathParse("example.net/pkg", filepath.Join(t.gopath, "src", "other.net"))
|
||||||
@ -1670,19 +1702,22 @@ const _ = pkg.X
|
|||||||
`
|
`
|
||||||
const want = `package x
|
const want = `package x
|
||||||
|
|
||||||
import "otherwise-longer-so-worse.example.net/foo/pkg"
|
import "foo.com/otherwise-longer-so-worse-example/foo/pkg"
|
||||||
|
|
||||||
const _ = pkg.X
|
const _ = pkg.X
|
||||||
`
|
`
|
||||||
|
|
||||||
testConfig{
|
testConfig{
|
||||||
gopathFiles: map[string]string{
|
module: packagestest.Module{
|
||||||
".goimportsignore": "# comment line\n\n example.net", // tests comment, blank line, whitespace trimming
|
Name: "foo.com",
|
||||||
"example.net/pkg/pkg.go": "package pkg\nconst X = 1",
|
Files: fm{
|
||||||
"otherwise-longer-so-worse.example.net/foo/pkg/pkg.go": "package pkg\nconst X = 1",
|
"../.goimportsignore": "# comment line\n\n foo.com/example", // tests comment, blank line, whitespace trimming
|
||||||
"x/x.go": input,
|
"example/pkg/pkg.go": "package pkg\nconst X = 1",
|
||||||
|
"otherwise-longer-so-worse-example/foo/pkg/pkg.go": "package pkg\nconst X = 1",
|
||||||
|
"x/x.go": input,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}.processTest(t, "x/x.go", nil, nil, want)
|
}.processTest(t, "foo.com", "x/x.go", nil, nil, want)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Skip "node_modules" directory.
|
// Skip "node_modules" directory.
|
||||||
@ -1693,43 +1728,21 @@ const _ = pkg.X
|
|||||||
`
|
`
|
||||||
const want = `package x
|
const want = `package x
|
||||||
|
|
||||||
import "otherwise-longer.net/not_modules/pkg"
|
import "foo.com/otherwise-longer/not_modules/pkg"
|
||||||
|
|
||||||
const _ = pkg.X
|
const _ = pkg.X
|
||||||
`
|
`
|
||||||
|
|
||||||
testConfig{
|
testConfig{
|
||||||
gopathFiles: map[string]string{
|
module: packagestest.Module{
|
||||||
"example.net/node_modules/pkg/a.go": "package pkg\nconst X = 1",
|
Name: "foo.com",
|
||||||
"otherwise-longer.net/not_modules/pkg/a.go": "package pkg\nconst X = 1",
|
Files: fm{
|
||||||
"x/x.go": input,
|
"example/node_modules/pkg/a.go": "package pkg\nconst X = 1",
|
||||||
|
"otherwise-longer/not_modules/pkg/a.go": "package pkg\nconst X = 1",
|
||||||
|
"x/x.go": input,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}.processTest(t, "x/x.go", nil, nil, want)
|
}.processTest(t, "foo.com", "x/x.go", nil, nil, want)
|
||||||
}
|
|
||||||
|
|
||||||
// golang.org/issue/16458 -- if GOROOT is a prefix of GOPATH, GOPATH is ignored.
|
|
||||||
func TestGoRootPrefixOfGoPath(t *testing.T) {
|
|
||||||
const input = `package x
|
|
||||||
|
|
||||||
const _ = foo.X
|
|
||||||
`
|
|
||||||
const want = `package x
|
|
||||||
|
|
||||||
import "example.com/foo"
|
|
||||||
|
|
||||||
const _ = foo.X
|
|
||||||
`
|
|
||||||
|
|
||||||
dir := mustTempDir(t, "importstest")
|
|
||||||
defer os.RemoveAll(dir)
|
|
||||||
testConfig{
|
|
||||||
goroot: filepath.Join(dir, "go"),
|
|
||||||
gopath: filepath.Join(dir, "gopath"),
|
|
||||||
gopathFiles: map[string]string{
|
|
||||||
"example.com/foo/pkg.go": "package foo\nconst X = 1",
|
|
||||||
"x/x.go": input,
|
|
||||||
},
|
|
||||||
}.processTest(t, "x/x.go", nil, nil, want)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests that package global variables with the same name and function name as
|
// Tests that package global variables with the same name and function name as
|
||||||
@ -1755,11 +1768,14 @@ var time Time
|
|||||||
`
|
`
|
||||||
|
|
||||||
testConfig{
|
testConfig{
|
||||||
gopathFiles: map[string]string{
|
module: packagestest.Module{
|
||||||
"pkg/uses.go": usesGlobal,
|
Name: "foo.com",
|
||||||
"pkg/global.go": declaresGlobal,
|
Files: fm{
|
||||||
|
"pkg/uses.go": usesGlobal,
|
||||||
|
"pkg/global.go": declaresGlobal,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}.processTest(t, "pkg/uses.go", nil, nil, usesGlobal)
|
}.processTest(t, "foo.com", "pkg/uses.go", nil, nil, usesGlobal)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests that sibling files - other files in the same package - can provide an
|
// Tests that sibling files - other files in the same package - can provide an
|
||||||
@ -1804,11 +1820,14 @@ func LogSomethingElse() {
|
|||||||
`
|
`
|
||||||
|
|
||||||
testConfig{
|
testConfig{
|
||||||
gopathFiles: map[string]string{
|
module: packagestest.Module{
|
||||||
"p/needs_import.go": need,
|
Name: "foo.com",
|
||||||
"p/provides_import.go": provide,
|
Files: fm{
|
||||||
|
"p/needs_import.go": need,
|
||||||
|
"p/provides_import.go": provide,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}.processTest(t, "p/needs_import.go", nil, nil, want)
|
}.processTest(t, "foo.com", "p/needs_import.go", nil, nil, want)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPkgIsCandidate(t *testing.T) {
|
func TestPkgIsCandidate(t *testing.T) {
|
||||||
@ -1978,13 +1997,24 @@ var c = &config.SystemConfig{}
|
|||||||
`
|
`
|
||||||
|
|
||||||
testConfig{
|
testConfig{
|
||||||
gopathFiles: map[string]string{
|
modules: []packagestest.Module{
|
||||||
"config.net/config/config.go": "package config\n type SystemConfig struct {}", // Will match but should not be first choice
|
{
|
||||||
"mycompany.net/config/config.go": "package config\n type SystemConfig struct {}", // Will match but should not be first choice
|
Name: "config.net/config",
|
||||||
"mycompany.net/tool/config/config.go": "package config\n type SystemConfig struct {}", // Local package should be promoted over shorter package
|
Files: fm{"config.go": "package config\n type SystemConfig struct {}"}, // Will match but should not be first choice
|
||||||
"mycompany.net/tool/main.go": input,
|
},
|
||||||
|
{
|
||||||
|
Name: "mycompany.net/config",
|
||||||
|
Files: fm{"config.go": "package config\n type SystemConfig struct {}"}, // Will match but should not be first choice
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "mycompany.net/tool",
|
||||||
|
Files: fm{
|
||||||
|
"config/config.go": "package config\n type SystemConfig struct {}", // Local package should be promoted over shorter package
|
||||||
|
"main.go": input,
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}.processTest(t, "mycompany.net/tool/main.go", nil, nil, want)
|
}.processTest(t, "mycompany.net/tool", "main.go", nil, nil, want)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests FindImportInLocalGoFiles looks at the import lines for other Go files in the
|
// Tests FindImportInLocalGoFiles looks at the import lines for other Go files in the
|
||||||
@ -2003,13 +2033,21 @@ import "bytes.net/bytes"
|
|||||||
var _ = &bytes.Buffer{}
|
var _ = &bytes.Buffer{}
|
||||||
`
|
`
|
||||||
testConfig{
|
testConfig{
|
||||||
gopathFiles: map[string]string{
|
modules: []packagestest.Module{
|
||||||
"bytes.net/bytes/bytes.go": "package bytes\n type Buffer struct {}", // Should be selected over standard library
|
{
|
||||||
"mycompany.net/tool/io.go": "package main\n import \"bytes.net/bytes\"\n var _ = &bytes.Buffer{}", // Contains package import that will cause stdlib to be ignored
|
Name: "bytes.net/bytes",
|
||||||
"mycompany.net/tool/err.go": "package main\n import \"bogus.net/bytes\"\n var _ = &bytes.Buffer{}", // Contains import which is not resolved, so it is ignored
|
Files: fm{"bytes.go": "package bytes\n type Buffer struct {}"}, // Should be selected over standard library
|
||||||
"mycompany.net/tool/main.go": input,
|
},
|
||||||
|
{
|
||||||
|
Name: "mycompany.net/tool",
|
||||||
|
Files: fm{
|
||||||
|
"io.go": "package main\n import \"bytes.net/bytes\"\n var _ = &bytes.Buffer{}", // Contains package import that will cause stdlib to be ignored
|
||||||
|
"err.go": "package main\n import \"bogus.net/bytes\"\n var _ = &bytes.Buffer{}", // Contains import which is not resolved, so it is ignored
|
||||||
|
"main.go": input,
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}.processTest(t, "mycompany.net/tool/main.go", nil, nil, want)
|
}.processTest(t, "mycompany.net/tool", "main.go", nil, nil, want)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestImportNoGoFiles(t *testing.T) {
|
func TestImportNoGoFiles(t *testing.T) {
|
||||||
@ -2023,7 +2061,13 @@ import "bytes"
|
|||||||
var _ = &bytes.Buffer{}
|
var _ = &bytes.Buffer{}
|
||||||
`
|
`
|
||||||
|
|
||||||
testConfig{}.processTest(t, "mycompany.net/tool/main.go", []byte(input), nil, want)
|
buf, err := Process("mycompany.net/tool/main.go", []byte(input), nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Process() = %v", err)
|
||||||
|
}
|
||||||
|
if string(buf) != want {
|
||||||
|
t.Errorf("Got:\n%s\nWant:\n%s", buf, want)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensures a token as large as 500000 bytes can be handled
|
// Ensures a token as large as 500000 bytes can be handled
|
||||||
@ -2059,6 +2103,9 @@ const x = mypkg.Sprintf("%s", "my package")
|
|||||||
`
|
`
|
||||||
|
|
||||||
testConfig{
|
testConfig{
|
||||||
gopathFiles: map[string]string{"foo.go": input},
|
module: packagestest.Module{
|
||||||
}.processTest(t, "foo.go", nil, nil, want)
|
Name: "foo.com",
|
||||||
|
Files: fm{"foo.go": input},
|
||||||
|
},
|
||||||
|
}.processTest(t, "foo.com", "foo.go", nil, nil, want)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user