mirror of
https://github.com/golang/go
synced 2024-11-18 18:44:42 -07:00
imports: make tests use a helper, read from disk more
Replace the repeated Process/err check/output block with a test helper eliminating a lot of copy/pasting. Change many tests to get their input from disk rather than the argument to Process; this is mostly me being selfish: go/packages doesn't support creating new packages with overlays and that breaks a lot of these tests. Change-Id: Ib1b7908c3d86d17323a94f1ee4ce074e264327b4 Reviewed-on: https://go-review.googlesource.com/c/141680 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Heschi Kreinick <heschi@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
321fe744f3
commit
bf693efdb3
@ -1247,37 +1247,29 @@ var _, _ = rand.Read, rand.NewZipf
|
|||||||
func TestSimpleCases(t *testing.T) {
|
func TestSimpleCases(t *testing.T) {
|
||||||
defer func(lp string) { LocalPrefix = lp }(LocalPrefix)
|
defer func(lp string) { LocalPrefix = lp }(LocalPrefix)
|
||||||
LocalPrefix = "local,github.com/local"
|
LocalPrefix = "local,github.com/local"
|
||||||
|
for _, tt := range tests {
|
||||||
testConfig{
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
// Skeleton non-stdlib packages for use during testing.
|
options := &Options{
|
||||||
// Each includes one arbitrary symbol, e.g. the first declaration in the first file.
|
TabWidth: 8,
|
||||||
// Try not to add more without a good reason.
|
TabIndent: true,
|
||||||
gopathFiles: map[string]string{
|
Comments: true,
|
||||||
"appengine/x.go": "package appengine\nfunc Main(){}\n",
|
Fragment: true,
|
||||||
"appengine/datastore/x.go": "package datastore\nvar ErrInvalidEntityType error\n",
|
FormatOnly: tt.formatOnly,
|
||||||
"rsc.io/p/x.go": "package p\nfunc P(){}\n",
|
}
|
||||||
"code.google.com/p/snappy-go/snappy/x.go": "package snappy\nvar ErrCorrupt error\n",
|
testConfig{
|
||||||
},
|
// Skeleton non-stdlib packages for use during testing.
|
||||||
}.test(t, func(t *goimportTest) {
|
// Each includes one arbitrary symbol, e.g. the first declaration in the first file.
|
||||||
for _, tt := range tests {
|
// Try not to add more without a good reason.
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
gopathFiles: map[string]string{
|
||||||
options := &Options{
|
"appengine/x.go": "package appengine\nfunc Main(){}\n",
|
||||||
TabWidth: 8,
|
"appengine/datastore/x.go": "package datastore\nvar ErrInvalidEntityType error\n",
|
||||||
TabIndent: true,
|
"rsc.io/p/x.go": "package p\nfunc P(){}\n",
|
||||||
Comments: true,
|
"code.google.com/p/snappy-go/snappy/x.go": "package snappy\nvar ErrCorrupt error\n",
|
||||||
Fragment: true,
|
"x/x.go": tt.in,
|
||||||
FormatOnly: tt.formatOnly,
|
},
|
||||||
}
|
}.processTest(t, "x/x.go", nil, options, tt.out)
|
||||||
buf, err := Process(tt.name+".go", []byte(tt.in), options)
|
})
|
||||||
if err != nil {
|
}
|
||||||
t.Fatalf("Process(...) = %v", err)
|
|
||||||
}
|
|
||||||
if got := string(buf); got != tt.out {
|
|
||||||
t.Errorf("output differs:\nGOT:\n%s\nWANT:\n%s\n", got, tt.out)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1321,26 +1313,17 @@ func bar() {
|
|||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
options := &Options{
|
||||||
|
TabWidth: 8,
|
||||||
|
TabIndent: true,
|
||||||
|
Comments: true,
|
||||||
|
Fragment: true,
|
||||||
|
}
|
||||||
testConfig{
|
testConfig{
|
||||||
gopathFiles: map[string]string{
|
gopathFiles: map[string]string{
|
||||||
"x.go": tt.in,
|
"x.go": tt.in,
|
||||||
},
|
},
|
||||||
}.test(t, func(t *goimportTest) {
|
}.processTest(t, "x.go", nil, options, tt.out)
|
||||||
options := &Options{
|
|
||||||
TabWidth: 8,
|
|
||||||
TabIndent: true,
|
|
||||||
Comments: true,
|
|
||||||
Fragment: true,
|
|
||||||
}
|
|
||||||
buf, err := Process(filepath.Join(t.gopath, "/src/x.go"), nil, options)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("Process(...) = %v", tt.name, err)
|
|
||||||
|
|
||||||
}
|
|
||||||
if got := string(buf); got != tt.out {
|
|
||||||
t.Errorf("output differs:\nGOT:\n%s\nWANT:\n%s\n", tt.name, got, tt.out)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1354,23 +1337,14 @@ func TestImportSymlinks(t *testing.T) {
|
|||||||
t.Skipf("skipping test on %q as there are no symlinks", runtime.GOOS)
|
t.Skipf("skipping test on %q as there are no symlinks", runtime.GOOS)
|
||||||
}
|
}
|
||||||
|
|
||||||
testConfig{
|
const input = `package p
|
||||||
gopathFiles: map[string]string{
|
|
||||||
"../target/f.go": "package mypkg\nvar Foo = 123\n",
|
|
||||||
"x/mypkg": "LINK:../../target", // valid symlink
|
|
||||||
"x/apkg": "LINK:..", // symlink loop
|
|
||||||
},
|
|
||||||
}.test(t, func(t *goimportTest) {
|
|
||||||
build.Default.GOPATH = t.gopath
|
|
||||||
|
|
||||||
input := `package p
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
_ = fmt.Print
|
_ = fmt.Print
|
||||||
_ = mypkg.Foo
|
_ = mypkg.Foo
|
||||||
)
|
)
|
||||||
`
|
`
|
||||||
output := `package p
|
const want = `package p
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -1382,14 +1356,15 @@ var (
|
|||||||
_ = mypkg.Foo
|
_ = mypkg.Foo
|
||||||
)
|
)
|
||||||
`
|
`
|
||||||
buf, err := Process(t.gopath+"/src/myotherpkg/toformat.go", []byte(input), &Options{})
|
|
||||||
if err != nil {
|
testConfig{
|
||||||
t.Fatal(err)
|
gopathFiles: map[string]string{
|
||||||
}
|
"../target/f.go": "package mypkg\nvar Foo = 123\n",
|
||||||
if got := string(buf); got != output {
|
"x/mypkg": "LINK:../../target", // valid symlink
|
||||||
t.Fatalf("results differ\nGOT:\n%s\nWANT:\n%s\n", got, output)
|
"x/apkg": "LINK:..", // symlink loop
|
||||||
}
|
"myotherpackage/toformat.go": input,
|
||||||
})
|
},
|
||||||
|
}.processTest(t, "myotherpackage/toformat.go", nil, nil, want)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestImportSymlinksWithIgnore(t *testing.T) {
|
func TestImportSymlinksWithIgnore(t *testing.T) {
|
||||||
@ -1398,22 +1373,14 @@ func TestImportSymlinksWithIgnore(t *testing.T) {
|
|||||||
t.Skipf("skipping test on %q as there are no symlinks", runtime.GOOS)
|
t.Skipf("skipping test on %q as there are no symlinks", runtime.GOOS)
|
||||||
}
|
}
|
||||||
|
|
||||||
testConfig{
|
const input = `package p
|
||||||
gopathFiles: map[string]string{
|
|
||||||
"../target/f.go": "package mypkg\nvar Foo = 123\n",
|
|
||||||
"x/mypkg": "LINK:../../target", // valid symlink
|
|
||||||
"x/apkg": "LINK:..", // symlink loop
|
|
||||||
".goimportsignore": "x/mypkg\n",
|
|
||||||
},
|
|
||||||
}.test(t, func(t *goimportTest) {
|
|
||||||
input := `package p
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
_ = fmt.Print
|
_ = fmt.Print
|
||||||
_ = mypkg.Foo
|
_ = mypkg.Foo
|
||||||
)
|
)
|
||||||
`
|
`
|
||||||
output := `package p
|
const want = `package p
|
||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
@ -1422,20 +1389,21 @@ var (
|
|||||||
_ = mypkg.Foo
|
_ = mypkg.Foo
|
||||||
)
|
)
|
||||||
`
|
`
|
||||||
buf, err := Process(t.gopath+"/src/myotherpkg/toformat.go", []byte(input), &Options{})
|
|
||||||
if err != nil {
|
testConfig{
|
||||||
t.Fatal(err)
|
gopathFiles: map[string]string{
|
||||||
}
|
"../target/f.go": "package mypkg\nvar Foo = 123\n",
|
||||||
if got := string(buf); got != output {
|
"x/mypkg": "LINK:../../target", // valid symlink
|
||||||
t.Fatalf("results differ\nGOT:\n%s\nWANT:\n%s\n", got, output)
|
"x/apkg": "LINK:..", // symlink loop
|
||||||
}
|
"myotherpkg/toformat.go": input,
|
||||||
})
|
".goimportsignore": "x/mypkg\n",
|
||||||
|
},
|
||||||
|
}.processTest(t, "myotherpkg/toformat.go", nil, nil, want)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test for x/y/v2 convention for package y.
|
// Test for x/y/v2 convention for package y.
|
||||||
func TestModuleVersion(t *testing.T) {
|
func TestModuleVersion(t *testing.T) {
|
||||||
testConfig{}.test(t, func(t *goimportTest) {
|
const input = `package p
|
||||||
input := `package p
|
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -1448,14 +1416,12 @@ var (
|
|||||||
_ = foo.Foo
|
_ = foo.Foo
|
||||||
)
|
)
|
||||||
`
|
`
|
||||||
buf, err := Process(filepath.Join(t.gopath, "src/mypkg.com/outpkg/toformat.go"), []byte(input), &Options{})
|
|
||||||
if err != nil {
|
testConfig{
|
||||||
t.Fatal(err)
|
gopathFiles: map[string]string{
|
||||||
}
|
"mypkg.com/outpkg/toformat.go": input,
|
||||||
if got := string(buf); got != input {
|
},
|
||||||
t.Fatalf("results differ\nGOT:\n%s\nWANT:\n%s\n", got, 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
|
||||||
@ -1463,12 +1429,7 @@ var (
|
|||||||
// "mypkg.com/mypkg.v1" would be removed if goimports wasn't able to detect
|
// "mypkg.com/mypkg.v1" would be removed if goimports wasn't able to detect
|
||||||
// that the package name is "mypkg".
|
// that the package name is "mypkg".
|
||||||
func TestVendorPackage(t *testing.T) {
|
func TestVendorPackage(t *testing.T) {
|
||||||
testConfig{
|
const input = `package p
|
||||||
gopathFiles: map[string]string{
|
|
||||||
"mypkg.com/outpkg/vendor/mypkg.com/mypkg.v1/f.go": "package mypkg\nvar Foo = 123\n",
|
|
||||||
},
|
|
||||||
}.test(t, func(t *goimportTest) {
|
|
||||||
input := `package p
|
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -1481,32 +1442,12 @@ var (
|
|||||||
_ = mypkg.Foo
|
_ = mypkg.Foo
|
||||||
)
|
)
|
||||||
`
|
`
|
||||||
buf, err := Process(filepath.Join(t.gopath, "src/mypkg.com/outpkg/toformat.go"), []byte(input), &Options{})
|
testConfig{
|
||||||
if err != nil {
|
gopathFiles: map[string]string{
|
||||||
t.Fatal(err)
|
"mypkg.com/outpkg/vendor/mypkg.com/mypkg.v1/f.go": "package mypkg\nvar Foo = 123\n",
|
||||||
}
|
"mypkg.com/outpkg/toformat.go": input,
|
||||||
if got := string(buf); got != input {
|
},
|
||||||
t.Fatalf("results differ\nGOT:\n%s\nWANT:\n%s\n", got, input)
|
}.processTest(t, "mypkg.com/outpkg/toformat.go", nil, nil, input)
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func withEmptyGoPath(fn func()) {
|
|
||||||
scanOnce = sync.Once{}
|
|
||||||
|
|
||||||
oldGOPATH := build.Default.GOPATH
|
|
||||||
oldGOROOT := build.Default.GOROOT
|
|
||||||
oldCompiler := build.Default.Compiler
|
|
||||||
build.Default.GOPATH = ""
|
|
||||||
build.Default.Compiler = "gc"
|
|
||||||
|
|
||||||
defer func() {
|
|
||||||
build.Default.GOPATH = oldGOPATH
|
|
||||||
build.Default.GOROOT = oldGOROOT
|
|
||||||
build.Default.Compiler = oldCompiler
|
|
||||||
}()
|
|
||||||
|
|
||||||
fn()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInternal(t *testing.T) {
|
func TestInternal(t *testing.T) {
|
||||||
@ -1521,31 +1462,21 @@ import "foo/internal/race"
|
|||||||
var _ = race.Acquire
|
var _ = race.Acquire
|
||||||
`
|
`
|
||||||
|
|
||||||
tc := testConfig{
|
// Packages under the same directory should be able to use internal packages.
|
||||||
|
testConfig{
|
||||||
gopathFiles: map[string]string{
|
gopathFiles: map[string]string{
|
||||||
"foo/internal/race/x.go": "package race\n func Acquire(){}\n",
|
"foo/internal/race/x.go": "package race\n func Acquire(){}\n",
|
||||||
|
"foo/bar/x.go": input,
|
||||||
},
|
},
|
||||||
}
|
}.processTest(t, "foo/bar/x.go", nil, nil, importAdded)
|
||||||
// Packages under the same directory should be able to use internal packages.
|
|
||||||
tc.test(t, func(t *goimportTest) {
|
|
||||||
buf, err := Process(filepath.Join(t.gopath, "src/foo/bar/x.go"), []byte(input), &Options{})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if got := string(buf); got != importAdded {
|
|
||||||
t.Errorf("results differ\nGOT:\n%s\nWANT:\n%s\n", got, importAdded)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
// Packages outside the same directory should not.
|
// Packages outside the same directory should not.
|
||||||
tc.test(t, func(t *goimportTest) {
|
testConfig{
|
||||||
buf, err := Process(filepath.Join(t.gopath, "src/bar/x.go"), []byte(input), &Options{})
|
gopathFiles: map[string]string{
|
||||||
if err != nil {
|
"foo/internal/race/x.go": "package race\n func Acquire(){}\n",
|
||||||
t.Fatal(err)
|
"bar/x.go": input,
|
||||||
}
|
},
|
||||||
if got := string(buf); got != input {
|
}.processTest(t, "bar/x.go", nil, nil, input)
|
||||||
t.Errorf("results differ\nGOT:\n%s\nWANT:\n%s\n", got, input)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProcessVendor(t *testing.T) {
|
func TestProcessVendor(t *testing.T) {
|
||||||
@ -1562,16 +1493,9 @@ var _ = hpack.HuffmanDecode
|
|||||||
testConfig{
|
testConfig{
|
||||||
gopathFiles: map[string]string{
|
gopathFiles: map[string]string{
|
||||||
"vendor/golang.org/x/net/http2/hpack/huffman.go": "package hpack\nfunc HuffmanDecode() { }\n",
|
"vendor/golang.org/x/net/http2/hpack/huffman.go": "package hpack\nfunc HuffmanDecode() { }\n",
|
||||||
|
"bar/x.go": input,
|
||||||
},
|
},
|
||||||
}.test(t, func(t *goimportTest) {
|
}.processTest(t, "bar/x.go", nil, nil, want)
|
||||||
buf, err := Process(filepath.Join(t.gopath, "src/bar/x.go"), []byte(input), &Options{})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if got := string(buf); got != want {
|
|
||||||
t.Errorf("results differ\nGOT:\n%s\nWANT:\n%s\n", got, want)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFindStdlib(t *testing.T) {
|
func TestFindStdlib(t *testing.T) {
|
||||||
@ -1617,6 +1541,7 @@ type testConfig struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func mustTempDir(t *testing.T, prefix string) string {
|
func mustTempDir(t *testing.T, prefix string) string {
|
||||||
|
t.Helper()
|
||||||
dir, err := ioutil.TempDir("", prefix)
|
dir, err := ioutil.TempDir("", prefix)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@ -1644,6 +1569,8 @@ func mapToDir(destDir string, files map[string]string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c testConfig) test(t *testing.T, fn func(*goimportTest)) {
|
func (c testConfig) test(t *testing.T, fn func(*goimportTest)) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
goroot := c.goroot
|
goroot := c.goroot
|
||||||
gopath := c.gopath
|
gopath := c.gopath
|
||||||
|
|
||||||
@ -1663,19 +1590,39 @@ func (c testConfig) test(t *testing.T, fn func(*goimportTest)) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
withEmptyGoPath(func() {
|
scanOnce = sync.Once{}
|
||||||
if goroot != "" {
|
|
||||||
build.Default.GOROOT = goroot
|
|
||||||
}
|
|
||||||
build.Default.GOPATH = gopath
|
|
||||||
|
|
||||||
it := &goimportTest{
|
oldGOPATH := build.Default.GOPATH
|
||||||
T: t,
|
oldGOROOT := build.Default.GOROOT
|
||||||
goroot: build.Default.GOROOT,
|
oldCompiler := build.Default.Compiler
|
||||||
gopath: gopath,
|
build.Default.GOPATH = ""
|
||||||
ctx: &build.Default,
|
build.Default.Compiler = "gc"
|
||||||
}
|
|
||||||
fn(it)
|
defer func() {
|
||||||
|
build.Default.GOPATH = oldGOPATH
|
||||||
|
build.Default.GOROOT = oldGOROOT
|
||||||
|
build.Default.Compiler = oldCompiler
|
||||||
|
}()
|
||||||
|
|
||||||
|
if goroot != "" {
|
||||||
|
build.Default.GOROOT = goroot
|
||||||
|
}
|
||||||
|
build.Default.GOPATH = gopath
|
||||||
|
|
||||||
|
it := &goimportTest{
|
||||||
|
T: t,
|
||||||
|
goroot: build.Default.GOROOT,
|
||||||
|
gopath: gopath,
|
||||||
|
ctx: &build.Default,
|
||||||
|
}
|
||||||
|
fn(it)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c testConfig) processTest(t *testing.T, file string, contents []byte, opts *Options, want string) {
|
||||||
|
t.Helper()
|
||||||
|
c.test(t, func(t *goimportTest) {
|
||||||
|
t.Helper()
|
||||||
|
t.process(file, contents, opts, want)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1686,30 +1633,37 @@ type goimportTest struct {
|
|||||||
gopath string
|
gopath string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *goimportTest) process(file string, contents []byte, opts *Options, want string) {
|
||||||
|
t.Helper()
|
||||||
|
buf, err := Process(filepath.Join(t.gopath, "src", file), contents, opts)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if string(buf) != want {
|
||||||
|
t.Errorf("Got:\n%s\nWant:\n%s", buf, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Tests that added imports are renamed when the import path's base doesn't
|
// Tests that added imports are renamed when the import path's base doesn't
|
||||||
// match its package name. For example, we want to generate:
|
// match its package name. For example, we want to generate:
|
||||||
//
|
//
|
||||||
// import cloudbilling "google.golang.org/api/cloudbilling/v1"
|
// import cloudbilling "google.golang.org/api/cloudbilling/v1"
|
||||||
func TestRenameWhenPackageNameMismatch(t *testing.T) {
|
func TestRenameWhenPackageNameMismatch(t *testing.T) {
|
||||||
testConfig{
|
const input = `package main
|
||||||
gopathFiles: map[string]string{
|
const Y = bar.X`
|
||||||
"foo/bar/v1/x.go": "package bar \n const X = 1",
|
|
||||||
},
|
const want = `package main
|
||||||
}.test(t, func(t *goimportTest) {
|
|
||||||
buf, err := Process(t.gopath+"/src/test/t.go", []byte("package main \n const Y = bar.X"), &Options{})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
const want = `package main
|
|
||||||
|
|
||||||
import bar "foo/bar/v1"
|
import bar "foo/bar/v1"
|
||||||
|
|
||||||
const Y = bar.X
|
const Y = bar.X
|
||||||
`
|
`
|
||||||
if string(buf) != want {
|
testConfig{
|
||||||
t.Errorf("Got:\n%s\nWant:\n%s", buf, want)
|
gopathFiles: map[string]string{
|
||||||
}
|
"foo/bar/v1/x.go": "package bar \n const X = 1",
|
||||||
})
|
"test/t.go": input,
|
||||||
|
},
|
||||||
|
}.processTest(t, "test/t.go", nil, nil, want)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests that the LocalPrefix option causes imports
|
// Tests that the LocalPrefix option causes imports
|
||||||
@ -1796,35 +1750,31 @@ const _ = runtime.GOOS
|
|||||||
tt.config.test(t, func(t *goimportTest) {
|
tt.config.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
|
||||||
buf, err := Process(t.gopath+"/src/test/t.go", []byte(tt.src), &Options{})
|
t.process("test/t.go", []byte(tt.src), nil, tt.want)
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if string(buf) != tt.want {
|
|
||||||
t.Errorf("Got:\n%s\nWant:\n%s", buf, tt.want)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests that "package documentation" files are ignored.
|
// Tests that "package documentation" files are ignored.
|
||||||
func TestIgnoreDocumentationPackage(t *testing.T) {
|
func TestIgnoreDocumentationPackage(t *testing.T) {
|
||||||
|
const input = `package x
|
||||||
|
|
||||||
|
const Y = foo.X
|
||||||
|
`
|
||||||
|
const want = `package x
|
||||||
|
|
||||||
|
import "foo"
|
||||||
|
|
||||||
|
const Y = foo.X
|
||||||
|
`
|
||||||
|
|
||||||
testConfig{
|
testConfig{
|
||||||
gopathFiles: map[string]string{
|
gopathFiles: map[string]string{
|
||||||
"foo/foo.go": "package foo\nconst X = 1\n",
|
"foo/foo.go": "package foo\nconst X = 1\n",
|
||||||
"foo/doc.go": "package documentation \n // just to confuse things\n",
|
"foo/doc.go": "package documentation \n // just to confuse things\n",
|
||||||
|
"x/x.go": input,
|
||||||
},
|
},
|
||||||
}.test(t, func(t *goimportTest) {
|
}.processTest(t, "x/x.go", nil, nil, want)
|
||||||
const in = "package x\n\nconst Y = foo.X\n"
|
|
||||||
const want = "package x\n\nimport \"foo\"\n\nconst Y = foo.X\n"
|
|
||||||
buf, err := Process(t.gopath+"/src/x/x.go", []byte(in), nil)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if string(buf) != want {
|
|
||||||
t.Errorf("wrong output.\ngot:\n%q\nwant:\n%q\n", in, want)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests importPathToNameGoPathParse and in particular that it stops
|
// Tests importPathToNameGoPathParse and in particular that it stops
|
||||||
@ -1852,47 +1802,62 @@ func TestImportPathToNameGoPathParse(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestIgnoreConfiguration(t *testing.T) {
|
func TestIgnoreConfiguration(t *testing.T) {
|
||||||
|
const input = `package x
|
||||||
|
|
||||||
|
const _ = pkg.X
|
||||||
|
`
|
||||||
|
const want = `package x
|
||||||
|
|
||||||
|
import "otherwise-longer-so-worse.example.net/foo/pkg"
|
||||||
|
|
||||||
|
const _ = pkg.X
|
||||||
|
`
|
||||||
|
|
||||||
testConfig{
|
testConfig{
|
||||||
gopathFiles: map[string]string{
|
gopathFiles: map[string]string{
|
||||||
".goimportsignore": "# comment line\n\n example.net", // tests comment, blank line, whitespace trimming
|
".goimportsignore": "# comment line\n\n example.net", // tests comment, blank line, whitespace trimming
|
||||||
"example.net/pkg/pkg.go": "package pkg\nconst X = 1",
|
"example.net/pkg/pkg.go": "package pkg\nconst X = 1",
|
||||||
"otherwise-longer-so-worse.example.net/foo/pkg/pkg.go": "package pkg\nconst X = 1",
|
"otherwise-longer-so-worse.example.net/foo/pkg/pkg.go": "package pkg\nconst X = 1",
|
||||||
|
"x/x.go": input,
|
||||||
},
|
},
|
||||||
}.test(t, func(t *goimportTest) {
|
}.processTest(t, "x/x.go", nil, nil, want)
|
||||||
const in = "package x\n\nconst _ = pkg.X\n"
|
|
||||||
const want = "package x\n\nimport \"otherwise-longer-so-worse.example.net/foo/pkg\"\n\nconst _ = pkg.X\n"
|
|
||||||
buf, err := Process(t.gopath+"/src/x/x.go", []byte(in), nil)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if string(buf) != want {
|
|
||||||
t.Errorf("wrong output.\ngot:\n%q\nwant:\n%q\n", buf, want)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Skip "node_modules" directory.
|
// Skip "node_modules" directory.
|
||||||
func TestSkipNodeModules(t *testing.T) {
|
func TestSkipNodeModules(t *testing.T) {
|
||||||
|
const input = `package x
|
||||||
|
|
||||||
|
const _ = pkg.X
|
||||||
|
`
|
||||||
|
const want = `package x
|
||||||
|
|
||||||
|
import "otherwise-longer.net/not_modules/pkg"
|
||||||
|
|
||||||
|
const _ = pkg.X
|
||||||
|
`
|
||||||
|
|
||||||
testConfig{
|
testConfig{
|
||||||
gopathFiles: map[string]string{
|
gopathFiles: map[string]string{
|
||||||
"example.net/node_modules/pkg/a.go": "package pkg\nconst X = 1",
|
"example.net/node_modules/pkg/a.go": "package pkg\nconst X = 1",
|
||||||
"otherwise-longer.net/not_modules/pkg/a.go": "package pkg\nconst X = 1",
|
"otherwise-longer.net/not_modules/pkg/a.go": "package pkg\nconst X = 1",
|
||||||
|
"x/x.go": input,
|
||||||
},
|
},
|
||||||
}.test(t, func(t *goimportTest) {
|
}.processTest(t, "x/x.go", nil, nil, want)
|
||||||
const in = "package x\n\nconst _ = pkg.X\n"
|
|
||||||
const want = "package x\n\nimport \"otherwise-longer.net/not_modules/pkg\"\n\nconst _ = pkg.X\n"
|
|
||||||
buf, err := Process(t.gopath+"/src/x/x.go", []byte(in), nil)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if string(buf) != want {
|
|
||||||
t.Errorf("wrong output.\ngot:\n%q\nwant:\n%q\n", buf, want)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// golang.org/issue/16458 -- if GOROOT is a prefix of GOPATH, GOPATH is ignored.
|
// golang.org/issue/16458 -- if GOROOT is a prefix of GOPATH, GOPATH is ignored.
|
||||||
func TestGoRootPrefixOfGoPath(t *testing.T) {
|
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")
|
dir := mustTempDir(t, "importstest")
|
||||||
defer os.RemoveAll(dir)
|
defer os.RemoveAll(dir)
|
||||||
testConfig{
|
testConfig{
|
||||||
@ -1900,19 +1865,9 @@ func TestGoRootPrefixOfGoPath(t *testing.T) {
|
|||||||
gopath: filepath.Join(dir, "gopath"),
|
gopath: filepath.Join(dir, "gopath"),
|
||||||
gopathFiles: map[string]string{
|
gopathFiles: map[string]string{
|
||||||
"example.com/foo/pkg.go": "package foo\nconst X = 1",
|
"example.com/foo/pkg.go": "package foo\nconst X = 1",
|
||||||
|
"x/x.go": input,
|
||||||
},
|
},
|
||||||
}.test(t, func(t *goimportTest) {
|
}.processTest(t, "x/x.go", nil, nil, want)
|
||||||
const in = "package x\n\nconst _ = foo.X\n"
|
|
||||||
const want = "package x\n\nimport \"example.com/foo\"\n\nconst _ = foo.X\n"
|
|
||||||
buf, err := Process(t.gopath+"/src/x/x.go", []byte(in), nil)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if string(buf) != want {
|
|
||||||
t.Errorf("wrong output.\ngot:\n%q\nwant:\n%q\n", buf, 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
|
||||||
@ -1939,18 +1894,10 @@ var time Time
|
|||||||
|
|
||||||
testConfig{
|
testConfig{
|
||||||
gopathFiles: map[string]string{
|
gopathFiles: map[string]string{
|
||||||
|
"pkg/uses.go": usesGlobal,
|
||||||
"pkg/global.go": declaresGlobal,
|
"pkg/global.go": declaresGlobal,
|
||||||
},
|
},
|
||||||
}.test(t, func(t *goimportTest) {
|
}.processTest(t, "pkg/uses.go", nil, nil, usesGlobal)
|
||||||
buf, err := Process(
|
|
||||||
filepath.Join(t.gopath, "src/pkg/uses.go"), []byte(usesGlobal), nil)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if string(buf) != usesGlobal {
|
|
||||||
t.Errorf("wrong output.\ngot:\n%q\nwant:\n%q\n", buf, 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
|
||||||
@ -1994,23 +1941,12 @@ func LogSomethingElse() {
|
|||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
const pkg = "siblingimporttest"
|
|
||||||
const siblingFile = pkg + "/needs_import.go"
|
|
||||||
testConfig{
|
testConfig{
|
||||||
gopathFiles: map[string]string{
|
gopathFiles: map[string]string{
|
||||||
siblingFile: need,
|
"p/needs_import.go": need,
|
||||||
pkg + "/provides_import.go": provide,
|
"p/provides_import.go": provide,
|
||||||
},
|
},
|
||||||
}.test(t, func(t *goimportTest) {
|
}.processTest(t, "p/needs_import.go", nil, nil, want)
|
||||||
buf, err := Process(
|
|
||||||
t.gopath+"/src/"+siblingFile, []byte(need), nil)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if string(buf) != want {
|
|
||||||
t.Errorf("wrong output.\ngot:\n%q\nwant:\n%q\n", buf, want)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPkgIsCandidate(t *testing.T) {
|
func TestPkgIsCandidate(t *testing.T) {
|
||||||
@ -2169,28 +2105,24 @@ func TestProcessStdin(t *testing.T) {
|
|||||||
// should be the closest match.
|
// should be the closest match.
|
||||||
// https://golang.org/issues/17557
|
// https://golang.org/issues/17557
|
||||||
func TestLocalPackagePromotion(t *testing.T) {
|
func TestLocalPackagePromotion(t *testing.T) {
|
||||||
testConfig{
|
const input = `package main
|
||||||
gopathFiles: map[string]string{
|
var c = &config.SystemConfig{}
|
||||||
"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
|
const want = `package main
|
||||||
"mycompany.net/tool/config/config.go": "package config\n type SystemConfig struct {}", // Local package should be promoted over shorter package
|
|
||||||
},
|
|
||||||
}.test(t, func(t *goimportTest) {
|
|
||||||
const in = "package main\n var c = &config.SystemConfig{}"
|
|
||||||
const want = `package main
|
|
||||||
|
|
||||||
import "mycompany.net/tool/config"
|
import "mycompany.net/tool/config"
|
||||||
|
|
||||||
var c = &config.SystemConfig{}
|
var c = &config.SystemConfig{}
|
||||||
`
|
`
|
||||||
got, err := Process(filepath.Join(t.gopath, "src", "mycompany.net/tool/main.go"), []byte(in), nil)
|
|
||||||
if err != nil {
|
testConfig{
|
||||||
t.Fatal(err)
|
gopathFiles: map[string]string{
|
||||||
}
|
"config.net/config/config.go": "package config\n type SystemConfig struct {}", // Will match but should not be first choice
|
||||||
if string(got) != want {
|
"mycompany.net/config/config.go": "package config\n type SystemConfig struct {}", // Will match but should not be first choice
|
||||||
t.Errorf("Process = %q; want %q", got, want)
|
"mycompany.net/tool/config/config.go": "package config\n type SystemConfig struct {}", // Local package should be promoted over shorter package
|
||||||
}
|
"mycompany.net/tool/main.go": input,
|
||||||
})
|
},
|
||||||
|
}.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
|
||||||
@ -2199,49 +2131,37 @@ var c = &config.SystemConfig{}
|
|||||||
// standard library.
|
// standard library.
|
||||||
// https://golang.org/issues/17557
|
// https://golang.org/issues/17557
|
||||||
func TestFindImportInLocalGoFiles(t *testing.T) {
|
func TestFindImportInLocalGoFiles(t *testing.T) {
|
||||||
testConfig{
|
const input = `package main
|
||||||
gopathFiles: map[string]string{
|
var _ = &bytes.Buffer{}`
|
||||||
"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
|
const want = `package main
|
||||||
"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
|
|
||||||
},
|
|
||||||
}.test(t, func(t *goimportTest) {
|
|
||||||
const in = "package main\n var _ = &bytes.Buffer{}"
|
|
||||||
const want = `package main
|
|
||||||
|
|
||||||
import "bytes.net/bytes"
|
import "bytes.net/bytes"
|
||||||
|
|
||||||
var _ = &bytes.Buffer{}
|
var _ = &bytes.Buffer{}
|
||||||
`
|
`
|
||||||
got, err := Process(filepath.Join(t.gopath, "src", "mycompany.net/tool/main.go"), []byte(in), nil)
|
testConfig{
|
||||||
if err != nil {
|
gopathFiles: map[string]string{
|
||||||
t.Fatal(err)
|
"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
|
||||||
if string(got) != want {
|
"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
|
||||||
t.Errorf("Process = got %q; want %q", got, want)
|
"mycompany.net/tool/main.go": input,
|
||||||
}
|
},
|
||||||
})
|
}.processTest(t, "mycompany.net/tool/main.go", nil, nil, want)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestImportNoGoFiles(t *testing.T) {
|
func TestImportNoGoFiles(t *testing.T) {
|
||||||
testConfig{
|
const input = `package main
|
||||||
gopathFiles: map[string]string{},
|
var _ = &bytes.Buffer{}`
|
||||||
}.test(t, func(t *goimportTest) {
|
|
||||||
const in = "package main\n var _ = &bytes.Buffer{}"
|
const want = `package main
|
||||||
const want = `package main
|
|
||||||
|
|
||||||
import "bytes"
|
import "bytes"
|
||||||
|
|
||||||
var _ = &bytes.Buffer{}
|
var _ = &bytes.Buffer{}
|
||||||
`
|
`
|
||||||
got, err := Process(filepath.Join(t.gopath, "src", "mycompany.net/tool/main.go"), []byte(in), nil)
|
|
||||||
if err != nil {
|
testConfig{}.processTest(t, "mycompany.net/tool/main.go", []byte(input), nil, want)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if string(got) != want {
|
|
||||||
t.Errorf("Process = got %q; want %q", got, want)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensures a token as large as 500000 bytes can be handled
|
// Ensures a token as large as 500000 bytes can be handled
|
||||||
@ -2249,7 +2169,7 @@ var _ = &bytes.Buffer{}
|
|||||||
func TestProcessLargeToken(t *testing.T) {
|
func TestProcessLargeToken(t *testing.T) {
|
||||||
largeString := strings.Repeat("x", 500000)
|
largeString := strings.Repeat("x", 500000)
|
||||||
|
|
||||||
in := `package testimports
|
input := `package testimports
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -2262,12 +2182,6 @@ const x = mypkg.Sprintf("%s", "my package")
|
|||||||
// end
|
// end
|
||||||
`
|
`
|
||||||
|
|
||||||
out, err := Process("foo", []byte(in), nil)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("Process returned error.\n got:\n%v\nwant:\nnil", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
want := `package testimports
|
want := `package testimports
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -2281,7 +2195,8 @@ const x = mypkg.Sprintf("%s", "my package")
|
|||||||
|
|
||||||
// end
|
// end
|
||||||
`
|
`
|
||||||
if got := string(out); got != want {
|
|
||||||
t.Errorf("Process returned unexpected result.\ngot:\n%.100v\nwant:\n%.100v", got, want)
|
testConfig{
|
||||||
}
|
gopathFiles: map[string]string{"foo.go": input},
|
||||||
|
}.processTest(t, "foo.go", nil, nil, want)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user