1
0
mirror of https://github.com/golang/go synced 2024-11-18 11:04:42 -07:00

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 <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
This commit is contained in:
Rebecca Stambler 2018-08-10 15:20:38 -04:00
parent 7883bde2a5
commit cf99646d84

View File

@ -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) {