Share buildDir for android/soong/android tests

There is no need to create a separate buildDir for each test
file, use TestMain to create a global one for the package.

Test: all soong tests
Change-Id: I435ee7aa88b7e0bb8ccc1ba79f82833a7accf3e9
diff --git a/Android.bp b/Android.bp
index 4db98f8..ec4eb61 100644
--- a/Android.bp
+++ b/Android.bp
@@ -78,6 +78,7 @@
         "android/env.go",
     ],
     testSrcs: [
+        "android/android_test.go",
         "android/arch_test.go",
         "android/config_test.go",
         "android/expand_test.go",
diff --git a/android/android_test.go b/android/android_test.go
new file mode 100644
index 0000000..46b7054
--- /dev/null
+++ b/android/android_test.go
@@ -0,0 +1,46 @@
+// Copyright 2019 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package android
+
+import (
+	"io/ioutil"
+	"os"
+	"testing"
+)
+
+var buildDir string
+
+func setUp() {
+	var err error
+	buildDir, err = ioutil.TempDir("", "soong_android_test")
+	if err != nil {
+		panic(err)
+	}
+}
+
+func tearDown() {
+	os.RemoveAll(buildDir)
+}
+
+func TestMain(m *testing.M) {
+	run := func() int {
+		setUp()
+		defer tearDown()
+
+		return m.Run()
+	}
+
+	os.Exit(run())
+}
diff --git a/android/namespace_test.go b/android/namespace_test.go
index 9a791a5..51a0af2 100644
--- a/android/namespace_test.go
+++ b/android/namespace_test.go
@@ -16,8 +16,6 @@
 
 import (
 	"errors"
-	"io/ioutil"
-	"os"
 	"path/filepath"
 	"reflect"
 	"testing"
@@ -613,12 +611,6 @@
 }
 
 func setupTestFromFiles(bps map[string][]byte) (ctx *TestContext, errs []error) {
-	buildDir, err := ioutil.TempDir("", "soong_namespace_test")
-	if err != nil {
-		return nil, []error{err}
-	}
-	defer os.RemoveAll(buildDir)
-
 	config := TestConfig(buildDir, nil)
 
 	ctx = NewTestContext()
diff --git a/android/neverallow_test.go b/android/neverallow_test.go
index 00c51ea..d0d22d8 100644
--- a/android/neverallow_test.go
+++ b/android/neverallow_test.go
@@ -15,8 +15,6 @@
 package android
 
 import (
-	"io/ioutil"
-	"os"
 	"testing"
 )
 
@@ -151,12 +149,6 @@
 }
 
 func TestNeverallow(t *testing.T) {
-	buildDir, err := ioutil.TempDir("", "soong_neverallow_test")
-	if err != nil {
-		t.Fatal(err)
-	}
-	defer os.RemoveAll(buildDir)
-
 	config := TestConfig(buildDir, nil)
 
 	for _, test := range neverallowTests {
diff --git a/android/paths_test.go b/android/paths_test.go
index e0c2f6c..7bcfe41 100644
--- a/android/paths_test.go
+++ b/android/paths_test.go
@@ -17,8 +17,6 @@
 import (
 	"errors"
 	"fmt"
-	"io/ioutil"
-	"os"
 	"reflect"
 	"strings"
 	"testing"
@@ -880,12 +878,6 @@
 }
 
 func TestPathsForModuleSrc(t *testing.T) {
-	buildDir, err := ioutil.TempDir("", "soong_paths_for_module_src_test")
-	if err != nil {
-		t.Fatal(err)
-	}
-	defer os.RemoveAll(buildDir)
-
 	tests := []pathForModuleSrcTestCase{
 		{
 			name: "path",
@@ -966,12 +958,6 @@
 }
 
 func TestPathForModuleSrc(t *testing.T) {
-	buildDir, err := ioutil.TempDir("", "soong_path_for_module_src_test")
-	if err != nil {
-		t.Fatal(err)
-	}
-	defer os.RemoveAll(buildDir)
-
 	tests := []pathForModuleSrcTestCase{
 		{
 			name: "path",
@@ -1039,12 +1025,6 @@
 }
 
 func TestPathsForModuleSrc_AllowMissingDependencies(t *testing.T) {
-	buildDir, err := ioutil.TempDir("", "soong_paths_for_module_src_allow_missing_dependencies_test")
-	if err != nil {
-		t.Fatal(err)
-	}
-	defer os.RemoveAll(buildDir)
-
 	config := TestConfig(buildDir, nil)
 	config.TestProductVariables.Allow_missing_dependencies = proptools.BoolPtr(true)
 
diff --git a/android/prebuilt_etc_test.go b/android/prebuilt_etc_test.go
index d977c30..0a2c7a4 100644
--- a/android/prebuilt_etc_test.go
+++ b/android/prebuilt_etc_test.go
@@ -15,16 +15,13 @@
 package android
 
 import (
-	"io/ioutil"
-	"os"
 	"path/filepath"
 	"reflect"
 	"testing"
 )
 
 func testPrebuiltEtc(t *testing.T, bp string) (*TestContext, Config) {
-	config, buildDir := setUp(t)
-	defer tearDown(buildDir)
+	config := TestArchConfig(buildDir, nil)
 	ctx := NewTestArchContext()
 	ctx.RegisterModuleType("prebuilt_etc", ModuleFactoryAdaptor(PrebuiltEtcFactory))
 	ctx.RegisterModuleType("prebuilt_etc_host", ModuleFactoryAdaptor(PrebuiltEtcHostFactory))
@@ -51,20 +48,6 @@
 	return ctx, config
 }
 
-func setUp(t *testing.T) (config Config, buildDir string) {
-	buildDir, err := ioutil.TempDir("", "soong_prebuilt_etc_test")
-	if err != nil {
-		t.Fatal(err)
-	}
-
-	config = TestArchConfig(buildDir, nil)
-	return
-}
-
-func tearDown(buildDir string) {
-	os.RemoveAll(buildDir)
-}
-
 func TestPrebuiltEtcVariants(t *testing.T) {
 	ctx, _ := testPrebuiltEtc(t, `
 		prebuilt_etc {
diff --git a/android/prebuilt_test.go b/android/prebuilt_test.go
index a5b85c8..0a18e2c 100644
--- a/android/prebuilt_test.go
+++ b/android/prebuilt_test.go
@@ -16,8 +16,6 @@
 
 import (
 	"fmt"
-	"io/ioutil"
-	"os"
 	"testing"
 
 	"github.com/google/blueprint"
@@ -127,12 +125,6 @@
 }
 
 func TestPrebuilts(t *testing.T) {
-	buildDir, err := ioutil.TempDir("", "soong_prebuilt_test")
-	if err != nil {
-		t.Fatal(err)
-	}
-	defer os.RemoveAll(buildDir)
-
 	config := TestConfig(buildDir, nil)
 
 	for _, test := range prebuiltsTests {
diff --git a/android/rule_builder_test.go b/android/rule_builder_test.go
index df0f256..cfbc2ab 100644
--- a/android/rule_builder_test.go
+++ b/android/rule_builder_test.go
@@ -16,8 +16,6 @@
 
 import (
 	"fmt"
-	"io/ioutil"
-	"os"
 	"path/filepath"
 	"reflect"
 	"strings"
@@ -418,12 +416,6 @@
 }
 
 func TestRuleBuilder_Build(t *testing.T) {
-	buildDir, err := ioutil.TempDir("", "soong_test_rule_builder")
-	if err != nil {
-		t.Fatal(err)
-	}
-	defer os.RemoveAll(buildDir)
-
 	bp := `
 		rule_builder_test {
 			name: "foo",
diff --git a/android/sh_binary_test.go b/android/sh_binary_test.go
index becb35a..c99e18c 100644
--- a/android/sh_binary_test.go
+++ b/android/sh_binary_test.go
@@ -1,19 +1,11 @@
 package android
 
 import (
-	"io/ioutil"
-	"os"
 	"reflect"
 	"testing"
 )
 
 func testShBinary(t *testing.T, bp string) (*TestContext, Config) {
-	buildDir, err := ioutil.TempDir("", "soong_sh_binary_test")
-	if err != nil {
-		t.Fatal(err)
-	}
-	defer os.RemoveAll(buildDir)
-
 	config := TestArchConfig(buildDir, nil)
 
 	ctx := NewTestArchContext()
diff --git a/android/visibility_test.go b/android/visibility_test.go
index 5ea32a2..1a51495 100644
--- a/android/visibility_test.go
+++ b/android/visibility_test.go
@@ -1,8 +1,6 @@
 package android
 
 import (
-	"io/ioutil"
-	"os"
 	"testing"
 
 	"github.com/google/blueprint"
@@ -663,12 +661,6 @@
 }
 
 func TestVisibility(t *testing.T) {
-	buildDir, err := ioutil.TempDir("", "soong_neverallow_test")
-	if err != nil {
-		t.Fatal(err)
-	}
-	defer os.RemoveAll(buildDir)
-
 	for _, test := range visibilityTests {
 		t.Run(test.name, func(t *testing.T) {
 			_, errs := testVisibility(buildDir, test.fs)
diff --git a/android/vts_config_test.go b/android/vts_config_test.go
index 7d4c9b1..142b2f5 100644
--- a/android/vts_config_test.go
+++ b/android/vts_config_test.go
@@ -15,19 +15,11 @@
 package android
 
 import (
-	"io/ioutil"
-	"os"
 	"testing"
 )
 
 func testVtsConfig(test *testing.T, bpFileContents string) *TestContext {
-	buildDir, err := ioutil.TempDir("", "soong_vts_config_test")
-	if err != nil {
-		test.Fatal(err)
-	}
-
 	config := TestArchConfig(buildDir, nil)
-	defer func() { os.RemoveAll(buildDir) }()
 
 	ctx := NewTestArchContext()
 	ctx.RegisterModuleType("vts_config", ModuleFactoryAdaptor(VtsConfigFactory))