Support test fixtures in java package
Restructures the java package test setup code to create FixturePreparer
instances for setting up a test fixture and converts some tests to
use it.
The goal with this change is not to switch all the java tests over to
directly using the new model but instead to ensure that the majority of
the java tests run with the new model, to allow existing tests to
easily switch to the new model when needed and to allow dependent
packages to be switched to the new model.
Bug: 181070625
Test: m nothing
Change-Id: I1c9d96ddbc973aaf9733dcd7fa0479f79b0f471f
diff --git a/java/java_test.go b/java/java_test.go
index 9112655..e8c4a0f 100644
--- a/java/java_test.go
+++ b/java/java_test.go
@@ -48,6 +48,26 @@
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 @@
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 @@
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 @@
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 @@
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 @@
}
}
+// 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 @@
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)
})
}