diff --git a/java/java_test.go b/java/java_test.go index 911265532..e8c4a0f50 100644 --- a/java/java_test.go +++ b/java/java_test.go @@ -48,6 +48,26 @@ func tearDown() { os.RemoveAll(buildDir) } +// Factory to use to create fixtures for tests in this package. +var javaFixtureFactory = android.NewFixtureFactory( + &buildDir, + genrule.PrepareForTestWithGenRuleBuildComponents, + // Get the CC build components but not default modules. + cc.PrepareForTestWithCcBuildComponents, + // Include all the default java modules. + PrepareForTestWithJavaDefaultModules, + android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) { + ctx.RegisterModuleType("java_plugin", PluginFactory) + ctx.RegisterModuleType("python_binary_host", python.PythonBinaryHostFactory) + + ctx.PreDepsMutators(python.RegisterPythonPreDepsMutators) + ctx.RegisterPreSingletonType("overlay", OverlaySingletonFactory) + ctx.RegisterPreSingletonType("sdk_versions", sdkPreSingletonFactory) + }), + javaMockFS().AddToFixture(), + dexpreopt.PrepareForTestWithDexpreopt, +) + func TestMain(m *testing.M) { run := func() int { setUp() @@ -59,10 +79,20 @@ func TestMain(m *testing.M) { os.Exit(run()) } +// testConfig is a legacy way of creating a test Config for testing java modules. +// +// See testJava for an explanation as to how to stop using this deprecated method. +// +// deprecated func testConfig(env map[string]string, bp string, fs map[string][]byte) android.Config { return TestConfig(buildDir, env, bp, fs) } +// testContext is a legacy way of creating a TestContext for testing java modules. +// +// See testJava for an explanation as to how to stop using this deprecated method. +// +// deprecated func testContext(config android.Config) *android.TestContext { ctx := android.NewTestArchContext(config) @@ -92,6 +122,11 @@ func testContext(config android.Config) *android.TestContext { return ctx } +// run is a legacy way of running tests of java modules. +// +// See testJava for an explanation as to how to stop using this deprecated method. +// +// deprecated func run(t *testing.T, ctx *android.TestContext, config android.Config) { t.Helper() @@ -105,23 +140,38 @@ func run(t *testing.T, ctx *android.TestContext, config android.Config) { android.FailIfErrored(t, errs) } +// testJavaError is a legacy way of running tests of java modules that expect errors. +// +// See testJava for an explanation as to how to stop using this deprecated method. +// +// deprecated func testJavaError(t *testing.T, pattern string, bp string) (*android.TestContext, android.Config) { t.Helper() return testJavaErrorWithConfig(t, pattern, testConfig(nil, bp, nil)) } +// testJavaErrorWithConfig is a legacy way of running tests of java modules that expect errors. +// +// See testJava for an explanation as to how to stop using this deprecated method. +// +// deprecated func testJavaErrorWithConfig(t *testing.T, pattern string, config android.Config) (*android.TestContext, android.Config) { t.Helper() - ctx := testContext(config) - + // This must be done on the supplied config and not as part of the fixture because any changes to + // the fixture's config will be ignored when RunTestWithConfig replaces it. pathCtx := android.PathContextForTesting(config) dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx)) - - runWithErrors(t, ctx, config, pattern) - - return ctx, config + result := javaFixtureFactory. + ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)). + RunTestWithConfig(t, config) + return result.TestContext, result.Config } +// runWithErrors is a legacy way of running tests of java modules that expect errors. +// +// See testJava for an explanation as to how to stop using this deprecated method. +// +// deprecated func runWithErrors(t *testing.T, ctx *android.TestContext, config android.Config, pattern string) { ctx.Register() _, errs := ctx.ParseBlueprintsFiles("Android.bp") @@ -139,22 +189,43 @@ func runWithErrors(t *testing.T, ctx *android.TestContext, config android.Config return } -func testJavaWithFS(t *testing.T, bp string, fs map[string][]byte) (*android.TestContext, android.Config) { +// testJavaWithFS runs tests using the javaFixtureFactory +// +// See testJava for an explanation as to how to stop using this deprecated method. +// +// deprecated +func testJavaWithFS(t *testing.T, bp string, fs android.MockFS) (*android.TestContext, android.Config) { t.Helper() - return testJavaWithConfig(t, testConfig(nil, bp, fs)) + result := javaFixtureFactory.Extend(fs.AddToFixture()).RunTestWithBp(t, bp) + return result.TestContext, result.Config } +// testJava runs tests using the javaFixtureFactory +// +// Do not add any new usages of this, instead use the javaFixtureFactory directly as it makes it +// much easier to customize the test behavior. +// +// If it is necessary to customize the behavior of an existing test that uses this then please first +// convert the test to using javaFixtureFactory first and then in a following change add the +// appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify +// that it did not change the test behavior unexpectedly. +// +// deprecated func testJava(t *testing.T, bp string) (*android.TestContext, android.Config) { t.Helper() - return testJavaWithFS(t, bp, nil) + result := javaFixtureFactory.RunTestWithBp(t, bp) + return result.TestContext, result.Config } +// testJavaWithConfig runs tests using the javaFixtureFactory +// +// See testJava for an explanation as to how to stop using this deprecated method. +// +// deprecated func testJavaWithConfig(t *testing.T, config android.Config) (*android.TestContext, android.Config) { t.Helper() - ctx := testContext(config) - run(t, ctx, config) - - return ctx, config + result := javaFixtureFactory.RunTestWithConfig(t, config) + return result.TestContext, result.Config } func moduleToPath(name string) string { @@ -168,6 +239,12 @@ func moduleToPath(name string) string { } } +// defaultModuleToPath constructs a path to the turbine generate jar for a default test module that +// is defined in PrepareForIntegrationTestWithJava +func defaultModuleToPath(name string) string { + return filepath.Join(buildDir, ".intermediates", defaultJavaDir, name, "android_common", "turbine-combined", name+".jar") +} + func TestJavaLinkType(t *testing.T) { testJava(t, ` java_library { @@ -2352,7 +2429,7 @@ func TestPatchModule(t *testing.T) { expected := "java.base=.:" + buildDir checkPatchModuleFlag(t, ctx, "bar", expected) expected = "java.base=" + strings.Join([]string{ - ".", buildDir, "dir", "dir2", "nested", moduleToPath("ext"), moduleToPath("framework")}, ":") + ".", buildDir, "dir", "dir2", "nested", defaultModuleToPath("ext"), defaultModuleToPath("framework")}, ":") checkPatchModuleFlag(t, ctx, "baz", expected) }) } diff --git a/java/testing.go b/java/testing.go index bfa1e6b2a..4e1997e45 100644 --- a/java/testing.go +++ b/java/testing.go @@ -29,10 +29,40 @@ import ( "github.com/google/blueprint" ) -func TestConfig(buildDir string, env map[string]string, bp string, fs map[string][]byte) android.Config { - bp += GatherRequiredDepsForTest() +const defaultJavaDir = "default/java" - mockFS := map[string][]byte{ +// Test fixture preparer that will register most java build components. +// +// Singletons and mutators should only be added here if they are needed for a majority of java +// module types, otherwise they should be added under a separate preparer to allow them to be +// selected only when needed to reduce test execution time. +// +// Module types do not have much of an overhead unless they are used so this should include as many +// module types as possible. The exceptions are those module types that require mutators and/or +// singletons in order to function in which case they should be kept together in a separate +// preparer. +var PrepareForTestWithJavaBuildComponents = android.FixtureRegisterWithContext(RegisterRequiredBuildComponentsForTest) + +// Test fixture preparer that will define default java modules, e.g. standard prebuilt modules. +var PrepareForTestWithJavaDefaultModules = android.GroupFixturePreparers( + // Make sure that mutators and module types, e.g. prebuilt mutators available. + android.PrepareForTestWithAndroidBuildComponents, + // Make sure that all the module types used in the defaults are registered. + PrepareForTestWithJavaBuildComponents, + // The java default module definitions. + android.FixtureAddTextFile(defaultJavaDir+"/Android.bp", GatherRequiredDepsForTest()), +) + +// Prepare a fixture to use all java module types, mutators and singletons fully. +// +// This should only be used by tests that want to run with as much of the build enabled as possible. +var PrepareForIntegrationTestWithJava = android.GroupFixturePreparers( + cc.PrepareForIntegrationTestWithCc, + PrepareForTestWithJavaDefaultModules, +) + +func javaMockFS() android.MockFS { + mockFS := android.MockFS{ "api/current.txt": nil, "api/removed.txt": nil, "api/system-current.txt": nil, @@ -64,6 +94,14 @@ func TestConfig(buildDir string, env map[string]string, bp string, fs map[string mockFS[k] = v } + return mockFS +} + +func TestConfig(buildDir string, env map[string]string, bp string, fs map[string][]byte) android.Config { + bp += GatherRequiredDepsForTest() + + mockFS := javaMockFS() + cc.GatherRequiredFilesForTest(mockFS) for k, v := range fs {