Let tests override environment

Pass an environment to TestConfig that will be used for
all Config.Getenv calls.

Test: none
Change-Id: I683cd9c9e0db61c9bfd2adb27fca78f558f225c4
This commit is contained in:
Colin Cross
2017-10-10 23:07:38 -07:00
parent 92430106c3
commit 6ccbc913e4
6 changed files with 15 additions and 14 deletions

View File

@@ -78,6 +78,7 @@ type config struct {
srcDir string // the path of the root source directory srcDir string // the path of the root source directory
buildDir string // the path of the build output directory buildDir string // the path of the build output directory
env map[string]string
envLock sync.Mutex envLock sync.Mutex
envDeps map[string]string envDeps map[string]string
envFrozen bool envFrozen bool
@@ -168,15 +169,15 @@ func saveToConfigFile(config jsonConfigurable, filename string) error {
} }
// TestConfig returns a Config object suitable for using for tests // 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{ config := &config{
ProductVariables: productVariables{ ProductVariables: productVariables{
DeviceName: stringPtr("test_device"), DeviceName: stringPtr("test_device"),
}, },
buildDir: buildDir, buildDir: buildDir,
captureBuild: true, captureBuild: true,
ignoreEnvironment: true, env: env,
} }
config.deviceConfig = &deviceConfig{ config.deviceConfig = &deviceConfig{
config: config, 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 // TestConfig returns a Config object suitable for using for tests that need to run the arch mutator
func TestArchConfig(buildDir string) Config { func TestArchConfig(buildDir string, env map[string]string) Config {
testConfig := TestConfig(buildDir) testConfig := TestConfig(buildDir, env)
config := testConfig.config config := testConfig.config
config.Targets = map[OsClass][]Target{ config.Targets = map[OsClass][]Target{
@@ -212,6 +213,8 @@ func NewConfig(srcDir, buildDir string) (Config, error) {
ConfigFileName: filepath.Join(buildDir, configFileName), ConfigFileName: filepath.Join(buildDir, configFileName),
ProductVariablesFileName: filepath.Join(buildDir, productVariablesFileName), ProductVariablesFileName: filepath.Join(buildDir, productVariablesFileName),
env: originalEnv,
srcDir: srcDir, srcDir: srcDir,
buildDir: buildDir, buildDir: buildDir,
} }
@@ -335,9 +338,7 @@ func (c *config) Getenv(key string) string {
if c.envFrozen { if c.envFrozen {
panic("Cannot access new environment variables after envdeps are frozen") panic("Cannot access new environment variables after envdeps are frozen")
} }
if !c.ignoreEnvironment { val, _ = c.env[key]
val, _ = originalEnv[key]
}
c.envDeps[key] = val c.envDeps[key] = val
} }
return val return val

View File

@@ -209,7 +209,7 @@ func (m moduleInstallPathContextImpl) InstallInSanitizerDir() bool {
} }
func TestPathForModuleInstall(t *testing.T) { func TestPathForModuleInstall(t *testing.T) {
testConfig := TestConfig("") testConfig := TestConfig("", nil)
hostTarget := Target{Os: Linux} hostTarget := Target{Os: Linux}
deviceTarget := Target{Os: Android} deviceTarget := Target{Os: Android}

View File

@@ -118,7 +118,7 @@ func TestPrebuilts(t *testing.T) {
} }
defer os.RemoveAll(buildDir) defer os.RemoveAll(buildDir)
config := TestConfig(buildDir) config := TestConfig(buildDir, nil)
for _, test := range prebuiltsTests { for _, test := range prebuiltsTests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {

View File

@@ -117,7 +117,7 @@ func TestDataTests(t *testing.T) {
} }
defer os.RemoveAll(buildDir) defer os.RemoveAll(buildDir)
config := android.TestConfig(buildDir) config := android.TestConfig(buildDir, nil)
for _, test := range testDataTests { for _, test := range testDataTests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {

View File

@@ -52,7 +52,7 @@ func TestMain(m *testing.M) {
} }
func testJava(t *testing.T, bp string) *android.TestContext { func testJava(t *testing.T, bp string) *android.TestContext {
config := android.TestArchConfig(buildDir) config := android.TestArchConfig(buildDir, nil)
ctx := android.NewTestArchContext() ctx := android.NewTestArchContext()
ctx.RegisterModuleType("android_app", android.ModuleFactoryAdaptor(AndroidAppFactory)) ctx.RegisterModuleType("android_app", android.ModuleFactoryAdaptor(AndroidAppFactory))

View File

@@ -450,7 +450,7 @@ func setupBuildEnv(t *testing.T) (config android.Config, buildDir string) {
t.Fatal(err) t.Fatal(err)
} }
config = android.TestConfig(buildDir) config = android.TestConfig(buildDir, nil)
return return
} }