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 <rstambler@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
diff --git a/go/packages/packages_test.go b/go/packages/packages_test.go
index a736d1e..8385230 100644
--- a/go/packages/packages_test.go
+++ b/go/packages/packages_test.go
@@ -1010,6 +1010,47 @@
 	}
 }
 
+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)
 }