Merge "Allow createGlobalSoongConfig() to be used from tests"

This commit is contained in:
Paul Duffin 2021-01-22 17:42:00 +00:00 committed by Gerrit Code Review
commit 8479ea296d
4 changed files with 15 additions and 34 deletions

View File

@ -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)

View File

@ -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"),
}
}

View File

@ -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")

View File

@ -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 {