From 6ccbc913e4f905c1d7ce70f78e098ecc5cd253c0 Mon Sep 17 00:00:00 2001 From: Colin Cross Date: Tue, 10 Oct 2017 23:07:38 -0700 Subject: [PATCH] Let tests override environment Pass an environment to TestConfig that will be used for all Config.Getenv calls. Test: none Change-Id: I683cd9c9e0db61c9bfd2adb27fca78f558f225c4 --- android/config.go | 19 ++++++++++--------- android/paths_test.go | 2 +- android/prebuilt_test.go | 2 +- cc/test_data_test.go | 2 +- java/java_test.go | 2 +- python/python_test.go | 2 +- 6 files changed, 15 insertions(+), 14 deletions(-) diff --git a/android/config.go b/android/config.go index a70fa768c..90616528f 100644 --- a/android/config.go +++ b/android/config.go @@ -78,6 +78,7 @@ type config struct { srcDir string // the path of the root source directory buildDir string // the path of the build output directory + env map[string]string envLock sync.Mutex envDeps map[string]string envFrozen bool @@ -168,15 +169,15 @@ func saveToConfigFile(config jsonConfigurable, filename string) error { } // TestConfig returns a Config object suitable for using for tests -func TestConfig(buildDir string) Config { +func TestConfig(buildDir string, env map[string]string) Config { config := &config{ ProductVariables: productVariables{ DeviceName: stringPtr("test_device"), }, - buildDir: buildDir, - captureBuild: true, - ignoreEnvironment: true, + buildDir: buildDir, + captureBuild: true, + env: env, } config.deviceConfig = &deviceConfig{ config: config, @@ -186,8 +187,8 @@ func TestConfig(buildDir string) Config { } // TestConfig returns a Config object suitable for using for tests that need to run the arch mutator -func TestArchConfig(buildDir string) Config { - testConfig := TestConfig(buildDir) +func TestArchConfig(buildDir string, env map[string]string) Config { + testConfig := TestConfig(buildDir, env) config := testConfig.config config.Targets = map[OsClass][]Target{ @@ -212,6 +213,8 @@ func NewConfig(srcDir, buildDir string) (Config, error) { ConfigFileName: filepath.Join(buildDir, configFileName), ProductVariablesFileName: filepath.Join(buildDir, productVariablesFileName), + env: originalEnv, + srcDir: srcDir, buildDir: buildDir, } @@ -335,9 +338,7 @@ func (c *config) Getenv(key string) string { if c.envFrozen { panic("Cannot access new environment variables after envdeps are frozen") } - if !c.ignoreEnvironment { - val, _ = originalEnv[key] - } + val, _ = c.env[key] c.envDeps[key] = val } return val diff --git a/android/paths_test.go b/android/paths_test.go index 3986b71cf..248f4d43a 100644 --- a/android/paths_test.go +++ b/android/paths_test.go @@ -209,7 +209,7 @@ func (m moduleInstallPathContextImpl) InstallInSanitizerDir() bool { } func TestPathForModuleInstall(t *testing.T) { - testConfig := TestConfig("") + testConfig := TestConfig("", nil) hostTarget := Target{Os: Linux} deviceTarget := Target{Os: Android} diff --git a/android/prebuilt_test.go b/android/prebuilt_test.go index 9a1de9c75..93f580535 100644 --- a/android/prebuilt_test.go +++ b/android/prebuilt_test.go @@ -118,7 +118,7 @@ func TestPrebuilts(t *testing.T) { } defer os.RemoveAll(buildDir) - config := TestConfig(buildDir) + config := TestConfig(buildDir, nil) for _, test := range prebuiltsTests { t.Run(test.name, func(t *testing.T) { diff --git a/cc/test_data_test.go b/cc/test_data_test.go index 962bde5bc..434edcdf6 100644 --- a/cc/test_data_test.go +++ b/cc/test_data_test.go @@ -117,7 +117,7 @@ func TestDataTests(t *testing.T) { } defer os.RemoveAll(buildDir) - config := android.TestConfig(buildDir) + config := android.TestConfig(buildDir, nil) for _, test := range testDataTests { t.Run(test.name, func(t *testing.T) { diff --git a/java/java_test.go b/java/java_test.go index 40343408d..a86973dd0 100644 --- a/java/java_test.go +++ b/java/java_test.go @@ -52,7 +52,7 @@ func TestMain(m *testing.M) { } func testJava(t *testing.T, bp string) *android.TestContext { - config := android.TestArchConfig(buildDir) + config := android.TestArchConfig(buildDir, nil) ctx := android.NewTestArchContext() ctx.RegisterModuleType("android_app", android.ModuleFactoryAdaptor(AndroidAppFactory)) diff --git a/python/python_test.go b/python/python_test.go index 873730265..176c6eca0 100644 --- a/python/python_test.go +++ b/python/python_test.go @@ -450,7 +450,7 @@ func setupBuildEnv(t *testing.T) (config android.Config, buildDir string) { t.Fatal(err) } - config = android.TestConfig(buildDir) + config = android.TestConfig(buildDir, nil) return }