From 9f04524673e84f9be7f06e2e13266d8fda2f151f Mon Sep 17 00:00:00 2001 From: Paul Duffin Date: Thu, 21 Jan 2021 15:05:11 +0000 Subject: [PATCH] Allow createGlobalSoongConfig() to be used from tests Previously, the createGlobalSoongConfig() function was explicitly prevented from being used in tests because it would fail. However, it turns out that is no longer the case and it does now work. That allows the following changes to be made: * Tests no longer need to use GlobalSoongConfigForTests() to prepopulate the cache. * GlobalSoongConfigForTests() is only needed in the dexpreopt package. Bug: 177892522 Test: m nothing Change-Id: Ifcbb1a44254c5d2d10c1d02ab23227488d1d1ed1 --- apex/apex_test.go | 1 - dexpreopt/config.go | 34 +++++++++++----------------------- dexpreopt/dexpreopt_test.go | 6 +++--- java/java_test.go | 8 +------- 4 files changed, 15 insertions(+), 34 deletions(-) diff --git a/apex/apex_test.go b/apex/apex_test.go index 9761e55ec..b9288a6ae 100644 --- a/apex/apex_test.go +++ b/apex/apex_test.go @@ -5957,7 +5957,6 @@ func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, transformDexpreopt ctx.Register() - _ = dexpreopt.GlobalSoongConfigForTests(config) dexpreopt.RegisterToolModulesForTest(ctx) pathCtx := android.PathContextForTesting(config) dexpreoptConfig := dexpreopt.GlobalConfigForTests(pathCtx) diff --git a/dexpreopt/config.go b/dexpreopt/config.go index feddfc0b0..867ece62b 100644 --- a/dexpreopt/config.go +++ b/dexpreopt/config.go @@ -363,13 +363,6 @@ func dex2oatPathFromDep(ctx android.ModuleContext) android.Path { // createGlobalSoongConfig creates a GlobalSoongConfig from the current context. // Should not be used in dexpreopt_gen. func createGlobalSoongConfig(ctx android.ModuleContext) *GlobalSoongConfig { - if ctx.Config().TestProductVariables != nil { - // If we're called in a test there'll be a confusing error from the path - // functions below that gets reported without a stack trace, so let's panic - // properly with a more helpful message. - panic("This should not be called from tests. Please call GlobalSoongConfigForTests somewhere in the test setup.") - } - return &GlobalSoongConfig{ Profman: ctx.Config().HostToolPath(ctx, "profman"), Dex2oat: dex2oatPathFromDep(ctx), @@ -389,8 +382,7 @@ func createGlobalSoongConfig(ctx android.ModuleContext) *GlobalSoongConfig { // being at least one ordinary module with a Dex2oatDepTag dependency. // // TODO(b/147613152): Implement a way to deal with dependencies from singletons, -// and then possibly remove this cache altogether (but the use in -// GlobalSoongConfigForTests also needs to be rethought). +// and then possibly remove this cache altogether. var globalSoongConfigOnceKey = android.NewOnceKey("DexpreoptGlobalSoongConfig") // GetGlobalSoongConfig creates a GlobalSoongConfig the first time it's called, @@ -550,18 +542,14 @@ func GlobalConfigForTests(ctx android.PathContext) *GlobalConfig { } } -func GlobalSoongConfigForTests(config android.Config) *GlobalSoongConfig { - // Install the test GlobalSoongConfig in the Once cache so that later calls to - // Get(Cached)GlobalSoongConfig returns it without trying to create a real one. - return config.Once(globalSoongConfigOnceKey, func() interface{} { - return &GlobalSoongConfig{ - Profman: android.PathForTesting("profman"), - Dex2oat: android.PathForTesting("dex2oat"), - Aapt: android.PathForTesting("aapt"), - SoongZip: android.PathForTesting("soong_zip"), - Zip2zip: android.PathForTesting("zip2zip"), - ManifestCheck: android.PathForTesting("manifest_check"), - ConstructContext: android.PathForTesting("construct_context"), - } - }).(*GlobalSoongConfig) +func globalSoongConfigForTests() *GlobalSoongConfig { + return &GlobalSoongConfig{ + Profman: android.PathForTesting("profman"), + Dex2oat: android.PathForTesting("dex2oat"), + Aapt: android.PathForTesting("aapt"), + SoongZip: android.PathForTesting("soong_zip"), + Zip2zip: android.PathForTesting("zip2zip"), + ManifestCheck: android.PathForTesting("manifest_check"), + ConstructContext: android.PathForTesting("construct_context"), + } } diff --git a/dexpreopt/dexpreopt_test.go b/dexpreopt/dexpreopt_test.go index 59278fdd6..af73d0c69 100644 --- a/dexpreopt/dexpreopt_test.go +++ b/dexpreopt/dexpreopt_test.go @@ -61,7 +61,7 @@ func testModuleConfig(ctx android.PathContext, name, partition string) *ModuleCo func TestDexPreopt(t *testing.T) { config := android.TestConfig("out", nil, "", nil) ctx := android.BuilderContextForTesting(config) - globalSoong := GlobalSoongConfigForTests(config) + globalSoong := globalSoongConfigForTests() global := GlobalConfigForTests(ctx) module := testSystemModuleConfig(ctx, "test") @@ -83,7 +83,7 @@ func TestDexPreopt(t *testing.T) { func TestDexPreoptSystemOther(t *testing.T) { config := android.TestConfig("out", nil, "", nil) ctx := android.BuilderContextForTesting(config) - globalSoong := GlobalSoongConfigForTests(config) + globalSoong := globalSoongConfigForTests() global := GlobalConfigForTests(ctx) systemModule := testSystemModuleConfig(ctx, "Stest") systemProductModule := testSystemProductModuleConfig(ctx, "SPtest") @@ -143,7 +143,7 @@ func TestDexPreoptSystemOther(t *testing.T) { func TestDexPreoptProfile(t *testing.T) { config := android.TestConfig("out", nil, "", nil) ctx := android.BuilderContextForTesting(config) - globalSoong := GlobalSoongConfigForTests(config) + globalSoong := globalSoongConfigForTests() global := GlobalConfigForTests(ctx) module := testSystemModuleConfig(ctx, "test") diff --git a/java/java_test.go b/java/java_test.go index 2a23f371e..7b8984810 100644 --- a/java/java_test.go +++ b/java/java_test.go @@ -61,13 +61,7 @@ func TestMain(m *testing.M) { func testConfig(env map[string]string, bp string, fs map[string][]byte) android.Config { bp += dexpreopt.BpToolModulesForTest() - config := TestConfig(buildDir, env, bp, fs) - - // Set up the global Once cache used for dexpreopt.GlobalSoongConfig, so that - // it doesn't create a real one, which would fail. - _ = dexpreopt.GlobalSoongConfigForTests(config) - - return config + return TestConfig(buildDir, env, bp, fs) } func testContext(config android.Config) *android.TestContext {