mirror of
https://github.com/golang/go
synced 2024-11-18 16:14:46 -07:00
go/packages: fix ad-hoc package tests
Make sure that the user's environment doesn't affect the adhoc package tests. Noticed that one test failed with GO111MODULE=on, so added a hack to try and support that case. Not sure if that's the right approach. Fixes golang/go#33374 Change-Id: I373cd57b9982cd46193c61d08e262fd50b5b7e18 Reviewed-on: https://go-review.googlesource.com/c/tools/+/195319 Run-TryBot: Rebecca Stambler <rstambler@golang.org> Reviewed-by: Michael Matloob <matloob@golang.org>
This commit is contained in:
parent
920acffc3e
commit
341939e086
@ -354,6 +354,16 @@ func adHocPackage(cfg *Config, driver driver, pattern, query string) (*driverRes
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// If we get nothing back from `go list`, try to make this file into its own ad-hoc package.
|
||||
if len(dirResponse.Packages) == 0 && err == nil {
|
||||
dirResponse.Packages = append(dirResponse.Packages, &Package{
|
||||
ID: "command-line-arguments",
|
||||
PkgPath: query,
|
||||
GoFiles: []string{query},
|
||||
CompiledGoFiles: []string{query},
|
||||
})
|
||||
dirResponse.Roots = append(dirResponse.Roots, "command-line-arguments")
|
||||
}
|
||||
// Special case to handle issue #33482:
|
||||
// If this is a file= query for ad-hoc packages where the file only exists on an overlay,
|
||||
// and exists outside of a module, add the file in for the package.
|
||||
|
@ -1125,9 +1125,6 @@ func testcontainsInOverlays(t *testing.T, exporter packagestest.Exporter) {
|
||||
}
|
||||
|
||||
func TestAdHocPackagesBadImport(t *testing.T) {
|
||||
// TODO: Enable this test when github.com/golang/go/issues/33374 is resolved.
|
||||
t.Skip()
|
||||
|
||||
// This test doesn't use packagestest because we are testing ad-hoc packages,
|
||||
// which are outside of $GOPATH and outside of a module.
|
||||
tmp, err := ioutil.TempDir("", "a")
|
||||
@ -1145,27 +1142,33 @@ const A = 1
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
config := &packages.Config{
|
||||
Dir: tmp,
|
||||
Mode: packages.LoadAllSyntax,
|
||||
}
|
||||
initial, err := packages.Load(config, fmt.Sprintf("file=%s", filename))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
// Check value of a.A.
|
||||
a := initial[0]
|
||||
if a.Errors != nil {
|
||||
t.Fatalf("a: got errors %+v, want no error", err)
|
||||
}
|
||||
aA := constant(a, "A")
|
||||
if aA == nil {
|
||||
t.Errorf("a.A: got nil")
|
||||
return
|
||||
}
|
||||
got := aA.Val().String()
|
||||
if want := "1"; got != want {
|
||||
t.Errorf("a.A: got %s, want %s", got, want)
|
||||
// Make sure that the user's value of GO111MODULE does not affect test results.
|
||||
for _, go111module := range []string{"off", "auto", "on"} {
|
||||
config := &packages.Config{
|
||||
Env: append(os.Environ(), "GOPACKAGESDRIVER=off", fmt.Sprintf("GO111MODULE=%s", go111module)),
|
||||
Dir: tmp,
|
||||
Mode: packages.LoadAllSyntax,
|
||||
Logf: t.Logf,
|
||||
}
|
||||
initial, err := packages.Load(config, fmt.Sprintf("file=%s", filename))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if len(initial) == 0 {
|
||||
t.Fatalf("no packages for %s with GO111MODULE=%s", filename, go111module)
|
||||
}
|
||||
// Check value of a.A.
|
||||
a := initial[0]
|
||||
// There's an error because there's a bad import.
|
||||
aA := constant(a, "A")
|
||||
if aA == nil {
|
||||
t.Errorf("a.A: got nil")
|
||||
return
|
||||
}
|
||||
got := aA.Val().String()
|
||||
if want := "1"; got != want {
|
||||
t.Errorf("a.A: got %s, want %s", got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1184,35 +1187,39 @@ func TestAdHocOverlays(t *testing.T) {
|
||||
content := []byte(`package a
|
||||
const A = 1
|
||||
`)
|
||||
config := &packages.Config{
|
||||
Dir: tmp,
|
||||
Env: append(os.Environ(), "GOPACKAGESDRIVER=off"),
|
||||
Mode: packages.LoadAllSyntax,
|
||||
Overlay: map[string][]byte{
|
||||
filename: content,
|
||||
},
|
||||
Logf: t.Logf,
|
||||
}
|
||||
initial, err := packages.Load(config, fmt.Sprintf("file=%s", filename))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(initial) == 0 {
|
||||
t.Fatalf("no packages for %s", filename)
|
||||
}
|
||||
// Check value of a.A.
|
||||
a := initial[0]
|
||||
if a.Errors != nil {
|
||||
t.Fatalf("a: got errors %+v, want no error", err)
|
||||
}
|
||||
aA := constant(a, "A")
|
||||
if aA == nil {
|
||||
t.Errorf("a.A: got nil")
|
||||
return
|
||||
}
|
||||
got := aA.Val().String()
|
||||
if want := "1"; got != want {
|
||||
t.Errorf("a.A: got %s, want %s", got, want)
|
||||
|
||||
// Make sure that the user's value of GO111MODULE does not affect test results.
|
||||
for _, go111module := range []string{"off", "auto", "on"} {
|
||||
config := &packages.Config{
|
||||
Dir: tmp,
|
||||
Env: append(os.Environ(), "GOPACKAGESDRIVER=off", fmt.Sprintf("GO111MODULE=%s", go111module)),
|
||||
Mode: packages.LoadAllSyntax,
|
||||
Overlay: map[string][]byte{
|
||||
filename: content,
|
||||
},
|
||||
Logf: t.Logf,
|
||||
}
|
||||
initial, err := packages.Load(config, fmt.Sprintf("file=%s", filename))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(initial) == 0 {
|
||||
t.Fatalf("no packages for %s with GO111MODULE=%s", filename, go111module)
|
||||
}
|
||||
// Check value of a.A.
|
||||
a := initial[0]
|
||||
if a.Errors != nil {
|
||||
t.Fatalf("a: got errors %+v, want no error", err)
|
||||
}
|
||||
aA := constant(a, "A")
|
||||
if aA == nil {
|
||||
t.Errorf("a.A: got nil")
|
||||
return
|
||||
}
|
||||
got := aA.Val().String()
|
||||
if want := "1"; got != want {
|
||||
t.Errorf("a.A: got %s, want %s", got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user