From cf99646d84d2e5f25f0853f1f3b3233efb3ceff2 Mon Sep 17 00:00:00 2001 From: Rebecca Stambler Date: Fri, 10 Aug 2018 15:20:38 -0400 Subject: [PATCH] imports: stub out dirPackageInfo function in tests TestFixImports did not have to explicitly add "strings" to the simplePkgs map - this only happened because sibling files were searched and the strings import was found through them. If we stub out the dirPackageInfo function, we have to explicitly add the strings import. Change-Id: I149e522fad37039fc790e6a855834d86ff050674 Reviewed-on: https://go-review.googlesource.com/129036 Run-TryBot: Rebecca Stambler TryBot-Result: Gobot Gobot Reviewed-by: Alan Donovan --- imports/fix_test.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/imports/fix_test.go b/imports/fix_test.go index 0d413e43..e5b30740 100644 --- a/imports/fix_test.go +++ b/imports/fix_test.go @@ -8,6 +8,7 @@ import ( "bytes" "context" "flag" + "fmt" "go/build" "io/ioutil" "os" @@ -1066,16 +1067,22 @@ func TestFixImports(t *testing.T) { "regexp": "regexp", "snappy": "code.google.com/p/snappy-go/snappy", "str": "strings", + "strings": "strings", "user": "appengine/user", "zip": "archive/zip", } - old := findImport + oldFindImport := findImport + oldDirPackageInfo := dirPackageInfo defer func() { - findImport = old + findImport = oldFindImport + dirPackageInfo = oldDirPackageInfo }() findImport = func(_ context.Context, pkgName string, symbols map[string]bool, filename string) (string, bool, error) { return simplePkgs[pkgName], pkgName == "str", nil } + dirPackageInfo = func(_, _, _ string) (*packageInfo, error) { + return nil, fmt.Errorf("no directory package info in tests") + } options := &Options{ TabWidth: 8, @@ -1832,8 +1839,8 @@ func TestImportPathToNameGoPathParse(t *testing.T) { func TestIgnoreConfiguration(t *testing.T) { testConfig{ gopathFiles: map[string]string{ - ".goimportsignore": "# comment line\n\n example.net", // tests comment, blank line, whitespace trimming - "example.net/pkg/pkg.go": "package pkg\nconst X = 1", + ".goimportsignore": "# comment line\n\n example.net", // tests comment, blank line, whitespace trimming + "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", }, }.test(t, func(t *goimportTest) {