go/packages: parallelize most tests

This reduces the overall running time on my workstation from ~44s to ~17s.

For golang/go#46764

Change-Id: I94e3c5bf160599687f7aa16513bb7b7e977f14b4
Reviewed-on: https://go-review.googlesource.com/c/tools/+/332350
Trust: Bryan C. Mills <bcmills@google.com>
Run-TryBot: Bryan C. Mills <bcmills@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
diff --git a/go/packages/overlay_test.go b/go/packages/overlay_test.go
index 65c7fc9..baa9315 100644
--- a/go/packages/overlay_test.go
+++ b/go/packages/overlay_test.go
@@ -496,6 +496,7 @@
 }
 
 func TestAdHocOverlays(t *testing.T) {
+	t.Parallel()
 	testenv.NeedsTool(t, "go")
 
 	// This test doesn't use packagestest because we are testing ad-hoc packages,
@@ -551,6 +552,7 @@
 // TestOverlayModFileChanges tests the behavior resulting from having files
 // from multiple modules in overlays.
 func TestOverlayModFileChanges(t *testing.T) {
+	t.Parallel()
 	testenv.NeedsTool(t, "go")
 
 	// Create two unrelated modules in a temporary directory.
@@ -620,6 +622,8 @@
 }
 
 func TestOverlayGOPATHVendoring(t *testing.T) {
+	t.Parallel()
+
 	exported := packagestest.Export(t, packagestest.GOPATH, []packagestest.Module{{
 		Name: "golang.org/fake",
 		Files: map[string]interface{}{
@@ -1042,6 +1046,8 @@
 // This does not use go/packagestest because it needs to write a replace
 // directive with an absolute path in one of the module's go.mod files.
 func TestOverlaysInReplace(t *testing.T) {
+	t.Parallel()
+
 	// Create module b.com in a temporary directory. Do not add any Go files
 	// on disk.
 	tmpPkgs, err := ioutil.TempDir("", "modules")
diff --git a/go/packages/packages_test.go b/go/packages/packages_test.go
index 963c009..afc0a80 100644
--- a/go/packages/packages_test.go
+++ b/go/packages/packages_test.go
@@ -57,6 +57,7 @@
 // testAllOrModules tests f against all packagestest exporters in long mode,
 // but only against the Modules exporter in short mode.
 func testAllOrModules(t *testing.T, f func(*testing.T, packagestest.Exporter)) {
+	t.Parallel()
 	packagestest.TestAll(t, func(t *testing.T, exporter packagestest.Exporter) {
 		t.Helper()
 
@@ -70,6 +71,7 @@
 			t.Fatalf("unexpected exporter %q", exporter.Name())
 		}
 
+		t.Parallel()
 		f(t, exporter)
 	})
 }
@@ -95,6 +97,7 @@
 // The zero-value of Config has LoadFiles mode.
 func TestLoadZeroConfig(t *testing.T) {
 	testenv.NeedsGoPackages(t)
+	t.Parallel()
 
 	initial, err := packages.Load(nil, "hash")
 	if err != nil {
@@ -328,6 +331,8 @@
 }
 
 func TestLoadAbsolutePath(t *testing.T) {
+	t.Parallel()
+
 	exported := packagestest.Export(t, packagestest.GOPATH, []packagestest.Module{{
 		Name: "golang.org/gopatha",
 		Files: map[string]interface{}{
@@ -356,6 +361,8 @@
 }
 
 func TestVendorImports(t *testing.T) {
+	t.Parallel()
+
 	exported := packagestest.Export(t, packagestest.GOPATH, []packagestest.Module{{
 		Name: "golang.org/fake",
 		Files: map[string]interface{}{
@@ -911,6 +918,8 @@
 }
 
 func TestAdHocPackagesBadImport(t *testing.T) {
+	t.Parallel()
+
 	// 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")
@@ -1399,6 +1408,8 @@
 }
 
 func TestRejectInvalidQueries(t *testing.T) {
+	t.Parallel()
+
 	queries := []string{"key=", "key=value"}
 	cfg := &packages.Config{
 		Mode: packages.LoadImports,
@@ -1437,7 +1448,11 @@
 
 }
 
-func TestConfigDefaultEnv(t *testing.T) { testAllOrModules(t, testConfigDefaultEnv) }
+func TestConfigDefaultEnv(t *testing.T) {
+	// packagestest.TestAll instead of testAllOrModulesParallel because this test
+	// can't be parallelized (it modifies the environment).
+	packagestest.TestAll(t, testConfigDefaultEnv)
+}
 func testConfigDefaultEnv(t *testing.T, exporter packagestest.Exporter) {
 	const driverJSON = `{
   "Roots": ["gopackagesdriver"],
@@ -1829,6 +1844,7 @@
 		// See https://golang.org/issue/27100.
 		t.Skip(`skipping on plan9; for some reason "net [syscall.test]" is not loaded`)
 	}
+	t.Parallel()
 	testenv.NeedsGoPackages(t)
 
 	cfg := &packages.Config{
@@ -1887,7 +1903,10 @@
 		packages.NeedName | packages.NeedImports,
 	}
 	for _, mode := range modes {
+		mode := mode
 		t.Run(fmt.Sprint(mode), func(t *testing.T) {
+			t.Parallel()
+
 			exported.Config.Mode = mode
 			pkgs, err := packages.Load(exported.Config, "golang.org/fake/c")
 			if err != nil {
@@ -2658,6 +2677,8 @@
 }
 
 func TestEmptyEnvironment(t *testing.T) {
+	t.Parallel()
+
 	cfg := &packages.Config{
 		Env: []string{"FOO=BAR"},
 	}
diff --git a/go/packages/packagestest/export.go b/go/packages/packagestest/export.go
index 2b93d2c..5dea613 100644
--- a/go/packages/packagestest/export.go
+++ b/go/packages/packagestest/export.go
@@ -159,6 +159,7 @@
 func TestAll(t *testing.T, f func(*testing.T, Exporter)) {
 	t.Helper()
 	for _, e := range All {
+		e := e // in case f calls t.Parallel
 		t.Run(e.Name(), func(t *testing.T) {
 			t.Helper()
 			f(t, e)
@@ -172,6 +173,7 @@
 func BenchmarkAll(b *testing.B, f func(*testing.B, Exporter)) {
 	b.Helper()
 	for _, e := range All {
+		e := e // in case f calls t.Parallel
 		b.Run(e.Name(), func(b *testing.B) {
 			b.Helper()
 			f(b, e)
diff --git a/go/packages/stdlib_test.go b/go/packages/stdlib_test.go
index 254f459..84ea8ad 100644
--- a/go/packages/stdlib_test.go
+++ b/go/packages/stdlib_test.go
@@ -21,7 +21,7 @@
 func TestStdlibMetadata(t *testing.T) {
 	// TODO(adonovan): see if we can get away without this hack.
 	// if runtime.GOOS == "android" {
-	// 	t.Skipf("incomplete std lib on %s", runtime.GOOS)
+	//      t.Skipf("incomplete std lib on %s", runtime.GOOS)
 	// }
 
 	testenv.NeedsGoPackages(t)