From 88ddfcebc769cb7884c38d144ee893cfb4519053 Mon Sep 17 00:00:00 2001 From: Rebecca Stambler Date: Tue, 6 Aug 2019 17:37:06 -0400 Subject: [PATCH] go/packages: add a test for ad-hoc packages in overlays Updates golang/go#33482 Change-Id: Ib24a0b955694455ddf1fc31011d57a7d797519ae Reviewed-on: https://go-review.googlesource.com/c/tools/+/189217 Run-TryBot: Rebecca Stambler Reviewed-by: Michael Matloob --- go/packages/packages_test.go | 41 ++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/go/packages/packages_test.go b/go/packages/packages_test.go index a736d1e2a5..83852302ae 100644 --- a/go/packages/packages_test.go +++ b/go/packages/packages_test.go @@ -1010,6 +1010,47 @@ func testNewPackagesInOverlay(t *testing.T, exporter packagestest.Exporter) { } } +func TestAdHocOverlays(t *testing.T) { + // Enable this test when https://golang.org/issue/33482 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") + if err != nil { + t.Fatal(err) + } + defer os.Remove(tmp) + + filename := filepath.Join(tmp, "a.go") + content := []byte(`package a +const A = 1 +`) + config := &packages.Config{ + Dir: tmp, + Mode: packages.LoadAllSyntax, + Overlay: map[string][]byte{ + filename: content, + }, + } + initial, err := packages.Load(config, fmt.Sprintf("file=%s", filename)) + if err != nil { + t.Error(err) + } + // Check value of a.A. + a := initial[0] + 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) + } + +} + func TestLoadAllSyntaxImportErrors(t *testing.T) { packagestest.TestAll(t, testLoadAllSyntaxImportErrors) }