Merge "Convert hiddenapi tests to use test fixtures" am: af0379ca9c

Original change: https://android-review.googlesource.com/c/platform/build/soong/+/1634629

Change-Id: Iae0fd40f741b106f65c0ebe4eed8c4cb543e745b
This commit is contained in:
Paul Duffin
2021-03-16 15:45:03 +00:00
committed by Automerger Merge Worker
2 changed files with 78 additions and 106 deletions

View File

@@ -31,6 +31,8 @@ func RegisterHiddenApiSingletonComponents(ctx android.RegistrationContext) {
ctx.RegisterModuleType("hiddenapi_flags", hiddenAPIFlagsFactory) ctx.RegisterModuleType("hiddenapi_flags", hiddenAPIFlagsFactory)
} }
var PrepareForTestWithHiddenApiBuildComponents = android.FixtureRegisterWithContext(RegisterHiddenApiSingletonComponents)
type hiddenAPISingletonPathsStruct struct { type hiddenAPISingletonPathsStruct struct {
// The path to the CSV file that contains the flags that will be encoded into the dex boot jars. // The path to the CSV file that contains the flags that will be encoded into the dex boot jars.
// //

View File

@@ -16,68 +16,48 @@ package java
import ( import (
"fmt" "fmt"
"strings" "path/filepath"
"testing" "testing"
"android/soong/android" "android/soong/android"
"github.com/google/blueprint/proptools" "github.com/google/blueprint/proptools"
) )
func testConfigWithBootJars(bp string, bootJars []string, prebuiltHiddenApiDir *string) android.Config { func fixtureSetBootJarsProductVariable(bootJars ...string) android.FixturePreparer {
config := testConfig(nil, bp, nil) return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
config.TestProductVariables.BootJars = android.CreateTestConfiguredJarList(bootJars) variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
config.TestProductVariables.PrebuiltHiddenApiDir = prebuiltHiddenApiDir })
return config
} }
func testContextWithHiddenAPI(config android.Config) *android.TestContext { func fixtureSetPrebuiltHiddenApiDirProductVariable(prebuiltHiddenApiDir *string) android.FixturePreparer {
ctx := testContext(config) return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
RegisterHiddenApiSingletonComponents(ctx) variables.PrebuiltHiddenApiDir = prebuiltHiddenApiDir
return ctx })
} }
func testHiddenAPIWithConfig(t *testing.T, config android.Config) *android.TestContext { var hiddenApiFixtureFactory = javaFixtureFactory.Extend(PrepareForTestWithHiddenApiBuildComponents)
t.Helper()
ctx := testContextWithHiddenAPI(config)
run(t, ctx, config)
return ctx
}
func testHiddenAPIBootJars(t *testing.T, bp string, bootJars []string, prebuiltHiddenApiDir *string) (*android.TestContext, android.Config) {
config := testConfigWithBootJars(bp, bootJars, prebuiltHiddenApiDir)
return testHiddenAPIWithConfig(t, config), config
}
func testHiddenAPIUnbundled(t *testing.T, unbundled bool) (*android.TestContext, android.Config) {
config := testConfig(nil, ``, nil)
config.TestProductVariables.Always_use_prebuilt_sdks = proptools.BoolPtr(unbundled)
return testHiddenAPIWithConfig(t, config), config
}
func TestHiddenAPISingleton(t *testing.T) { func TestHiddenAPISingleton(t *testing.T) {
ctx, _ := testHiddenAPIBootJars(t, ` result := hiddenApiFixtureFactory.Extend(
fixtureSetBootJarsProductVariable("platform:foo"),
).RunTestWithBp(t, `
java_library { java_library {
name: "foo", name: "foo",
srcs: ["a.java"], srcs: ["a.java"],
compile_dex: true, compile_dex: true,
} }
`, []string{"platform:foo"}, nil) `)
hiddenAPI := ctx.SingletonForTests("hiddenapi") hiddenAPI := result.SingletonForTests("hiddenapi")
hiddenapiRule := hiddenAPI.Rule("hiddenapi") hiddenapiRule := hiddenAPI.Rule("hiddenapi")
want := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar" want := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar"
if !strings.Contains(hiddenapiRule.RuleParams.Command, want) { android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, want)
t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", want, hiddenapiRule.RuleParams.Command)
}
} }
func TestHiddenAPIIndexSingleton(t *testing.T) { func TestHiddenAPIIndexSingleton(t *testing.T) {
ctx, _ := testHiddenAPIBootJars(t, ` result := hiddenApiFixtureFactory.Extend(
fixtureSetBootJarsProductVariable("platform:foo", "platform:bar"),
).RunTestWithBp(t, `
java_library { java_library {
name: "foo", name: "foo",
srcs: ["a.java"], srcs: ["a.java"],
@@ -106,9 +86,9 @@ func TestHiddenAPIIndexSingleton(t *testing.T) {
srcs: ["a.java"], srcs: ["a.java"],
compile_dex: true, compile_dex: true,
} }
`, []string{"platform:foo", "platform:bar"}, nil) `)
hiddenAPIIndex := ctx.SingletonForTests("hiddenapi_index") hiddenAPIIndex := result.SingletonForTests("hiddenapi_index")
indexRule := hiddenAPIIndex.Rule("singleton-merged-hiddenapi-index") indexRule := hiddenAPIIndex.Rule("singleton-merged-hiddenapi-index")
CheckHiddenAPIRuleInputs(t, ` CheckHiddenAPIRuleInputs(t, `
.intermediates/bar/android_common/hiddenapi/index.csv .intermediates/bar/android_common/hiddenapi/index.csv
@@ -118,7 +98,7 @@ func TestHiddenAPIIndexSingleton(t *testing.T) {
// Make sure that the foo-hiddenapi-annotations.jar is included in the inputs to the rules that // Make sure that the foo-hiddenapi-annotations.jar is included in the inputs to the rules that
// creates the index.csv file. // creates the index.csv file.
foo := ctx.ModuleForTests("foo", "android_common") foo := result.ModuleForTests("foo", "android_common")
indexParams := foo.Output("hiddenapi/index.csv") indexParams := foo.Output("hiddenapi/index.csv")
CheckHiddenAPIRuleInputs(t, ` CheckHiddenAPIRuleInputs(t, `
.intermediates/foo-hiddenapi-annotations/android_common/javac/foo-hiddenapi-annotations.jar .intermediates/foo-hiddenapi-annotations/android_common/javac/foo-hiddenapi-annotations.jar
@@ -127,7 +107,15 @@ func TestHiddenAPIIndexSingleton(t *testing.T) {
} }
func TestHiddenAPISingletonWithSourceAndPrebuiltPreferredButNoDex(t *testing.T) { func TestHiddenAPISingletonWithSourceAndPrebuiltPreferredButNoDex(t *testing.T) {
config := testConfigWithBootJars(` expectedErrorMessage :=
"hiddenapi has determined that the source module \"foo\" should be ignored as it has been" +
" replaced by the prebuilt module \"prebuilt_foo\" but unfortunately it does not provide a" +
" suitable boot dex jar"
hiddenApiFixtureFactory.Extend(
fixtureSetBootJarsProductVariable("platform:foo"),
).ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(expectedErrorMessage)).
RunTestWithBp(t, `
java_library { java_library {
name: "foo", name: "foo",
srcs: ["a.java"], srcs: ["a.java"],
@@ -139,35 +127,30 @@ func TestHiddenAPISingletonWithSourceAndPrebuiltPreferredButNoDex(t *testing.T)
jars: ["a.jar"], jars: ["a.jar"],
prefer: true, prefer: true,
} }
`, []string{"platform:foo"}, nil) `)
ctx := testContextWithHiddenAPI(config)
runWithErrors(t, ctx, config,
"hiddenapi has determined that the source module \"foo\" should be ignored as it has been"+
" replaced by the prebuilt module \"prebuilt_foo\" but unfortunately it does not provide a"+
" suitable boot dex jar")
} }
func TestHiddenAPISingletonWithPrebuilt(t *testing.T) { func TestHiddenAPISingletonWithPrebuilt(t *testing.T) {
ctx, _ := testHiddenAPIBootJars(t, ` result := hiddenApiFixtureFactory.Extend(
fixtureSetBootJarsProductVariable("platform:foo"),
).RunTestWithBp(t, `
java_import { java_import {
name: "foo", name: "foo",
jars: ["a.jar"], jars: ["a.jar"],
compile_dex: true, compile_dex: true,
} }
`, []string{"platform:foo"}, nil) `)
hiddenAPI := ctx.SingletonForTests("hiddenapi") hiddenAPI := result.SingletonForTests("hiddenapi")
hiddenapiRule := hiddenAPI.Rule("hiddenapi") hiddenapiRule := hiddenAPI.Rule("hiddenapi")
want := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar" want := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar"
if !strings.Contains(hiddenapiRule.RuleParams.Command, want) { android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, want)
t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", want, hiddenapiRule.RuleParams.Command)
}
} }
func TestHiddenAPISingletonWithPrebuiltUseSource(t *testing.T) { func TestHiddenAPISingletonWithPrebuiltUseSource(t *testing.T) {
ctx, _ := testHiddenAPIBootJars(t, ` result := hiddenApiFixtureFactory.Extend(
fixtureSetBootJarsProductVariable("platform:foo"),
).RunTestWithBp(t, `
java_library { java_library {
name: "foo", name: "foo",
srcs: ["a.java"], srcs: ["a.java"],
@@ -180,23 +163,21 @@ func TestHiddenAPISingletonWithPrebuiltUseSource(t *testing.T) {
compile_dex: true, compile_dex: true,
prefer: false, prefer: false,
} }
`, []string{"platform:foo"}, nil) `)
hiddenAPI := ctx.SingletonForTests("hiddenapi") hiddenAPI := result.SingletonForTests("hiddenapi")
hiddenapiRule := hiddenAPI.Rule("hiddenapi") hiddenapiRule := hiddenAPI.Rule("hiddenapi")
fromSourceJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar" fromSourceJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar"
if !strings.Contains(hiddenapiRule.RuleParams.Command, fromSourceJarArg) { android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, fromSourceJarArg)
t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", fromSourceJarArg, hiddenapiRule.RuleParams.Command)
}
prebuiltJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/dex/foo.jar" prebuiltJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/dex/foo.jar"
if strings.Contains(hiddenapiRule.RuleParams.Command, prebuiltJarArg) { android.AssertStringDoesNotContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, prebuiltJarArg)
t.Errorf("Did not expect %s in hiddenapi command, but it was present: %s", prebuiltJarArg, hiddenapiRule.RuleParams.Command)
}
} }
func TestHiddenAPISingletonWithPrebuiltOverrideSource(t *testing.T) { func TestHiddenAPISingletonWithPrebuiltOverrideSource(t *testing.T) {
ctx, _ := testHiddenAPIBootJars(t, ` result := hiddenApiFixtureFactory.Extend(
fixtureSetBootJarsProductVariable("platform:foo"),
).RunTestWithBp(t, `
java_library { java_library {
name: "foo", name: "foo",
srcs: ["a.java"], srcs: ["a.java"],
@@ -209,19 +190,15 @@ func TestHiddenAPISingletonWithPrebuiltOverrideSource(t *testing.T) {
compile_dex: true, compile_dex: true,
prefer: true, prefer: true,
} }
`, []string{"platform:foo"}, nil) `)
hiddenAPI := ctx.SingletonForTests("hiddenapi") hiddenAPI := result.SingletonForTests("hiddenapi")
hiddenapiRule := hiddenAPI.Rule("hiddenapi") hiddenapiRule := hiddenAPI.Rule("hiddenapi")
prebuiltJarArg := "--boot-dex=" + buildDir + "/.intermediates/prebuilt_foo/android_common/dex/foo.jar" prebuiltJarArg := "--boot-dex=" + buildDir + "/.intermediates/prebuilt_foo/android_common/dex/foo.jar"
if !strings.Contains(hiddenapiRule.RuleParams.Command, prebuiltJarArg) { android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, prebuiltJarArg)
t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", prebuiltJarArg, hiddenapiRule.RuleParams.Command)
}
fromSourceJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar" fromSourceJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar"
if strings.Contains(hiddenapiRule.RuleParams.Command, fromSourceJarArg) { android.AssertStringDoesNotContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, fromSourceJarArg)
t.Errorf("Did not expect %s in hiddenapi command, but it was present: %s", fromSourceJarArg, hiddenapiRule.RuleParams.Command)
}
} }
func TestHiddenAPISingletonSdks(t *testing.T) { func TestHiddenAPISingletonSdks(t *testing.T) {
@@ -251,29 +228,25 @@ func TestHiddenAPISingletonSdks(t *testing.T) {
} }
for _, tc := range testCases { for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
ctx, _ := testHiddenAPIUnbundled(t, tc.unbundledBuild) result := hiddenApiFixtureFactory.Extend(
android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
variables.Always_use_prebuilt_sdks = proptools.BoolPtr(tc.unbundledBuild)
}),
).RunTest(t)
hiddenAPI := ctx.SingletonForTests("hiddenapi") hiddenAPI := result.SingletonForTests("hiddenapi")
hiddenapiRule := hiddenAPI.Rule("hiddenapi") hiddenapiRule := hiddenAPI.Rule("hiddenapi")
wantPublicStubs := "--public-stub-classpath=" + generateSdkDexPath(tc.publicStub, tc.unbundledBuild) wantPublicStubs := "--public-stub-classpath=" + generateSdkDexPath(tc.publicStub, tc.unbundledBuild)
if !strings.Contains(hiddenapiRule.RuleParams.Command, wantPublicStubs) { android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantPublicStubs)
t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", wantPublicStubs, hiddenapiRule.RuleParams.Command)
}
wantSystemStubs := "--system-stub-classpath=" + generateSdkDexPath(tc.systemStub, tc.unbundledBuild) wantSystemStubs := "--system-stub-classpath=" + generateSdkDexPath(tc.systemStub, tc.unbundledBuild)
if !strings.Contains(hiddenapiRule.RuleParams.Command, wantSystemStubs) { android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantSystemStubs)
t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", wantSystemStubs, hiddenapiRule.RuleParams.Command)
}
wantTestStubs := "--test-stub-classpath=" + generateSdkDexPath(tc.testStub, tc.unbundledBuild) wantTestStubs := "--test-stub-classpath=" + generateSdkDexPath(tc.testStub, tc.unbundledBuild)
if !strings.Contains(hiddenapiRule.RuleParams.Command, wantTestStubs) { android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantTestStubs)
t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", wantTestStubs, hiddenapiRule.RuleParams.Command)
}
wantCorePlatformStubs := "--core-platform-stub-classpath=" + generateDexPath(tc.corePlatformStub) wantCorePlatformStubs := "--core-platform-stub-classpath=" + generateDexPath(defaultJavaDir, tc.corePlatformStub)
if !strings.Contains(hiddenapiRule.RuleParams.Command, wantCorePlatformStubs) { android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantCorePlatformStubs)
t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", wantCorePlatformStubs, hiddenapiRule.RuleParams.Command)
}
}) })
} }
} }
@@ -282,15 +255,15 @@ func generateDexedPath(subDir, dex, module string) string {
return fmt.Sprintf("%s/.intermediates/%s/android_common/%s/%s.jar", buildDir, subDir, dex, module) return fmt.Sprintf("%s/.intermediates/%s/android_common/%s/%s.jar", buildDir, subDir, dex, module)
} }
func generateDexPath(module string) string { func generateDexPath(moduleDir string, module string) string {
return generateDexedPath(module, "dex", module) return generateDexedPath(filepath.Join(moduleDir, module), "dex", module)
} }
func generateSdkDexPath(module string, unbundled bool) string { func generateSdkDexPath(module string, unbundled bool) string {
if unbundled { if unbundled {
return generateDexedPath("prebuilts/sdk/"+module, "dex", module) return generateDexedPath("prebuilts/sdk/"+module, "dex", module)
} }
return generateDexPath(module) return generateDexPath(defaultJavaDir, module)
} }
func TestHiddenAPISingletonWithPrebuiltCsvFile(t *testing.T) { func TestHiddenAPISingletonWithPrebuiltCsvFile(t *testing.T) {
@@ -304,36 +277,33 @@ func TestHiddenAPISingletonWithPrebuiltCsvFile(t *testing.T) {
// Where to find the prebuilt hiddenapi files: // Where to find the prebuilt hiddenapi files:
prebuiltHiddenApiDir := "path/to/prebuilt/hiddenapi" prebuiltHiddenApiDir := "path/to/prebuilt/hiddenapi"
ctx, _ := testHiddenAPIBootJars(t, ` result := hiddenApiFixtureFactory.Extend(
fixtureSetBootJarsProductVariable("platform:foo"),
fixtureSetPrebuiltHiddenApiDirProductVariable(&prebuiltHiddenApiDir),
).RunTestWithBp(t, `
java_import { java_import {
name: "foo", name: "foo",
jars: ["a.jar"], jars: ["a.jar"],
compile_dex: true, compile_dex: true,
} }
`, []string{"platform:foo"}, &prebuiltHiddenApiDir) `)
expectedCpInput := prebuiltHiddenApiDir + "/hiddenapi-flags.csv" expectedCpInput := prebuiltHiddenApiDir + "/hiddenapi-flags.csv"
expectedCpOutput := buildDir + "/hiddenapi/hiddenapi-flags.csv" expectedCpOutput := buildDir + "/hiddenapi/hiddenapi-flags.csv"
expectedFlagsCsv := buildDir + "/hiddenapi/hiddenapi-flags.csv" expectedFlagsCsv := buildDir + "/hiddenapi/hiddenapi-flags.csv"
foo := ctx.ModuleForTests("foo", "android_common") foo := result.ModuleForTests("foo", "android_common")
hiddenAPI := ctx.SingletonForTests("hiddenapi") hiddenAPI := result.SingletonForTests("hiddenapi")
cpRule := hiddenAPI.Rule("Cp") cpRule := hiddenAPI.Rule("Cp")
actualCpInput := cpRule.BuildParams.Input actualCpInput := cpRule.BuildParams.Input
actualCpOutput := cpRule.BuildParams.Output actualCpOutput := cpRule.BuildParams.Output
encodeDexRule := foo.Rule("hiddenAPIEncodeDex") encodeDexRule := foo.Rule("hiddenAPIEncodeDex")
actualFlagsCsv := encodeDexRule.BuildParams.Args["flagsCsv"] actualFlagsCsv := encodeDexRule.BuildParams.Args["flagsCsv"]
if actualCpInput.String() != expectedCpInput { android.AssertStringEquals(t, "hiddenapi cp rule input", expectedCpInput, actualCpInput.String())
t.Errorf("Prebuilt hiddenapi cp rule input mismatch, actual: %s, expected: %s", actualCpInput, expectedCpInput)
}
if actualCpOutput.String() != expectedCpOutput { android.AssertStringEquals(t, "hiddenapi cp rule output", expectedCpOutput, actualCpOutput.String())
t.Errorf("Prebuilt hiddenapi cp rule output mismatch, actual: %s, expected: %s", actualCpOutput, expectedCpOutput)
}
if actualFlagsCsv != expectedFlagsCsv { android.AssertStringEquals(t, "hiddenapi encode dex rule flags csv", expectedFlagsCsv, actualFlagsCsv)
t.Errorf("Prebuilt hiddenapi encode dex rule flags csv mismatch, actual: %s, expected: %s", actualFlagsCsv, expectedFlagsCsv)
}
} }