mirror of
https://github.com/golang/go
synced 2024-11-18 20:44:45 -07:00
go/packages: fix incorrect x_test graph in fallback
This change fixes a bug that occured in the golist fallback logic when an x_test package imported its own package under test. For a package "a", if "a_test" imported "a", we'd populate "a_test"'s import map with an entry "a [a.test]" pointing to the test variant of the package with id "a [a.test]". This change fixes the key to be "a", the correct import path of the package, not "a [a.test], which is the ID". Change-Id: If798f2675b01aa537c6ccc129dc35d042d967337 Reviewed-on: https://go-review.googlesource.com/133356 Reviewed-by: Ian Cottrell <iancottrell@google.com>
This commit is contained in:
parent
f1c1faf65a
commit
18b2bbde9d
@ -127,9 +127,11 @@ func golistDriverFallback(cfg *Config, words ...string) (*driverResponse, error)
|
|||||||
if isRoot {
|
if isRoot {
|
||||||
response.Roots = append(response.Roots, xtestID)
|
response.Roots = append(response.Roots, xtestID)
|
||||||
}
|
}
|
||||||
for i, imp := range p.XTestImports {
|
// Rewrite import to package under test to refer to test variant.
|
||||||
|
imports := importMap(p.XTestImports)
|
||||||
|
for imp := range imports {
|
||||||
if imp == p.ImportPath {
|
if imp == p.ImportPath {
|
||||||
p.XTestImports[i] = testID
|
imports[imp] = &Package{ID: testID}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -139,7 +141,7 @@ func golistDriverFallback(cfg *Config, words ...string) (*driverResponse, error)
|
|||||||
GoFiles: absJoin(p.Dir, p.XTestGoFiles),
|
GoFiles: absJoin(p.Dir, p.XTestGoFiles),
|
||||||
CompiledGoFiles: absJoin(p.Dir, p.XTestGoFiles),
|
CompiledGoFiles: absJoin(p.Dir, p.XTestGoFiles),
|
||||||
PkgPath: pkgpath,
|
PkgPath: pkgpath,
|
||||||
Imports: importMap(p.XTestImports),
|
Imports: imports,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,51 @@
|
|||||||
|
|
||||||
package packages_test
|
package packages_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"golang.org/x/tools/go/packages"
|
||||||
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
usesOldGolist = true
|
usesOldGolist = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestXTestImports(t *testing.T) {
|
||||||
|
tmp, cleanup := makeTree(t, map[string]string{
|
||||||
|
"src/a/a_test.go": `package a_test; import "a"`,
|
||||||
|
"src/a/a.go": `package a`,
|
||||||
|
})
|
||||||
|
defer cleanup()
|
||||||
|
|
||||||
|
cfg := &packages.Config{
|
||||||
|
Mode: packages.LoadImports,
|
||||||
|
Dir: tmp,
|
||||||
|
Env: append(os.Environ(), "GOPATH="+tmp, "GO111MODULE=off"),
|
||||||
|
Tests: true,
|
||||||
|
}
|
||||||
|
initial, err := packages.Load(cfg, "a")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var gotImports bytes.Buffer
|
||||||
|
for _, pkg := range initial {
|
||||||
|
var imports []string
|
||||||
|
for imp, pkg := range pkg.Imports {
|
||||||
|
imports = append(imports, fmt.Sprintf("%q: %q", imp, pkg.ID))
|
||||||
|
}
|
||||||
|
fmt.Fprintf(&gotImports, "%s {%s}\n", pkg.ID, strings.Join(imports, ", "))
|
||||||
|
}
|
||||||
|
wantImports := `a {}
|
||||||
|
a [a.test] {}
|
||||||
|
a_test [a.test] {"a": "a [a.test]"}
|
||||||
|
`
|
||||||
|
if gotImports.String() != wantImports {
|
||||||
|
t.Fatalf("wrong imports: got <<%s>>, want <<%s>>", gotImports.String(), wantImports)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user