From c3c5d5e351b8ed863b59cf6cf68322b16061c1b8 Mon Sep 17 00:00:00 2001 From: Paul Duffin Date: Fri, 29 Nov 2019 20:45:22 +0000 Subject: [PATCH] Improve testing of sdk snapshot generation Adds TestHelper to provide general test helper functionality for use by any test. Adds testSdkResult, composed with TestHelper that encapsulates the result of processing and sdk {..} definition and provides specialized support for testing the build rules. Dedups the analysis of the sdk build rules, and improves it to extract more information, and in different forms. That is represented by the snapshotBuildInfo struct. Adds a CheckSnapshot() method which checks the snapshot for a specified sdk version. It takes a list of functions that can each perform a check on a specific facet of the supplied snapshotBuildInfo. Methods are provided for tests to use to check the following facets: * Generated Android.bp contents. * Copy rules * Merge zip inputs This approach makes it possible for each test to customize what is being checked without either duplicating functionality, causing a proliferation of specialized forms of the CheckSnapshot method for different types of tests or adding arguments for every possible check that any test would need which would lead to lots of churn to existing tests when new arguments are added. The main testing improvement is for CheckSnapshot() to actually try and load the Android.bp that is generated. In order to do that it was necessary to create a mock filesystem populated with information from the build rules, i.e. the destination files from every Cp command as well as the destination directory from every repackage zip command. That helps detect a number of sources of errors: * Failing to copy a file/directory that is mentioned in the generated Android.bp file. * Invalid properties. * Invalid format of the .bp file. * Integrity issues within the .bp file. Bug: 143678475 Test: m conscrypt-module-sdk Change-Id: I4d3fe18f86698186d18e7e8b32d2e319183f7f0c --- sdk/sdk_test.go | 184 ++++++++++++------------------------- sdk/testing.go | 239 +++++++++++++++++++++++++++++++++++++++++++++--- sdk/update.go | 1 - 3 files changed, 283 insertions(+), 141 deletions(-) diff --git a/sdk/sdk_test.go b/sdk/sdk_test.go index 9fea26049..0b8dfc9e5 100644 --- a/sdk/sdk_test.go +++ b/sdk/sdk_test.go @@ -15,10 +15,8 @@ package sdk import ( - "path/filepath" "testing" - "android/soong/android" "android/soong/cc" ) @@ -28,7 +26,7 @@ func TestMain(m *testing.M) { } func TestBasicSdkWithJava(t *testing.T) { - ctx, _ := testSdk(t, ` + result := testSdk(t, ` sdk { name: "mysdk", java_libs: ["myjavalib"], @@ -89,11 +87,11 @@ func TestBasicSdkWithJava(t *testing.T) { } `) - sdkMemberV1 := ctx.ModuleForTests("sdkmember_mysdk_1", "android_common_myapex").Rule("combineJar").Output - sdkMemberV2 := ctx.ModuleForTests("sdkmember_mysdk_2", "android_common_myapex2").Rule("combineJar").Output + sdkMemberV1 := result.ModuleForTests("sdkmember_mysdk_1", "android_common_myapex").Rule("combineJar").Output + sdkMemberV2 := result.ModuleForTests("sdkmember_mysdk_2", "android_common_myapex2").Rule("combineJar").Output - javalibForMyApex := ctx.ModuleForTests("myjavalib", "android_common_myapex") - javalibForMyApex2 := ctx.ModuleForTests("myjavalib", "android_common_myapex2") + javalibForMyApex := result.ModuleForTests("myjavalib", "android_common_myapex") + javalibForMyApex2 := result.ModuleForTests("myjavalib", "android_common_myapex2") // Depending on the uses_sdks value, different libs are linked ensureListContains(t, pathsToStrings(javalibForMyApex.Rule("javac").Implicits), sdkMemberV1.String()) @@ -101,7 +99,7 @@ func TestBasicSdkWithJava(t *testing.T) { } func TestBasicSdkWithCc(t *testing.T) { - ctx, _ := testSdk(t, ` + result := testSdk(t, ` sdk { name: "mysdk", native_shared_libs: ["sdkmember"], @@ -166,11 +164,11 @@ func TestBasicSdkWithCc(t *testing.T) { } `) - sdkMemberV1 := ctx.ModuleForTests("sdkmember_mysdk_1", "android_arm64_armv8-a_core_shared_myapex").Rule("toc").Output - sdkMemberV2 := ctx.ModuleForTests("sdkmember_mysdk_2", "android_arm64_armv8-a_core_shared_myapex2").Rule("toc").Output + sdkMemberV1 := result.ModuleForTests("sdkmember_mysdk_1", "android_arm64_armv8-a_core_shared_myapex").Rule("toc").Output + sdkMemberV2 := result.ModuleForTests("sdkmember_mysdk_2", "android_arm64_armv8-a_core_shared_myapex2").Rule("toc").Output - cpplibForMyApex := ctx.ModuleForTests("mycpplib", "android_arm64_armv8-a_core_shared_myapex") - cpplibForMyApex2 := ctx.ModuleForTests("mycpplib", "android_arm64_armv8-a_core_shared_myapex2") + cpplibForMyApex := result.ModuleForTests("mycpplib", "android_arm64_armv8-a_core_shared_myapex") + cpplibForMyApex2 := result.ModuleForTests("mycpplib", "android_arm64_armv8-a_core_shared_myapex2") // Depending on the uses_sdks value, different libs are linked ensureListContains(t, pathsToStrings(cpplibForMyApex.Rule("ld").Implicits), sdkMemberV1.String()) @@ -268,7 +266,7 @@ func TestDepNotInRequiredSdks(t *testing.T) { } func TestSdkIsCompileMultilibBoth(t *testing.T) { - ctx, _ := testSdk(t, ` + result := testSdk(t, ` sdk { name: "mysdk", native_shared_libs: ["sdkmember"], @@ -282,11 +280,11 @@ func TestSdkIsCompileMultilibBoth(t *testing.T) { } `) - armOutput := ctx.ModuleForTests("sdkmember", "android_arm_armv7-a-neon_core_shared").Module().(*cc.Module).OutputFile() - arm64Output := ctx.ModuleForTests("sdkmember", "android_arm64_armv8-a_core_shared").Module().(*cc.Module).OutputFile() + armOutput := result.Module("sdkmember", "android_arm_armv7-a-neon_core_shared").(*cc.Module).OutputFile() + arm64Output := result.Module("sdkmember", "android_arm64_armv8-a_core_shared").(*cc.Module).OutputFile() var inputs []string - buildParams := ctx.ModuleForTests("mysdk", "android_common").Module().BuildParamsForTests() + buildParams := result.Module("mysdk", "android_common").BuildParamsForTests() for _, bp := range buildParams { if bp.Input != nil { inputs = append(inputs, bp.Input.String()) @@ -299,7 +297,7 @@ func TestSdkIsCompileMultilibBoth(t *testing.T) { } func TestSnapshot(t *testing.T) { - ctx, config := testSdk(t, ` + result := testSdk(t, ` sdk { name: "mysdk", java_libs: ["myjavalib"], @@ -341,9 +339,9 @@ func TestSnapshot(t *testing.T) { } `) - sdk := ctx.ModuleForTests("mysdk", "android_common").Module().(*sdk) - - checkSnapshotAndroidBpContents(t, sdk, `// This is auto-generated. DO NOT EDIT. + result.CheckSnapshot("mysdk", "android_common", + checkAndroidBpContents(` +// This is auto-generated. DO NOT EDIT. java_import { name: "mysdk_myjavalib@current", @@ -421,65 +419,30 @@ sdk_snapshot { stubs_sources: ["mysdk_myjavaapistubs@current"], native_shared_libs: ["mysdk_mynativelib@current"], } - -`) - - var copySrcs []string - var copyDests []string - buildParams := sdk.BuildParamsForTests() - var mergeZipInputs []string - var intermediateZip string - var outputZip string - for _, bp := range buildParams { - ruleString := bp.Rule.String() - if ruleString == android.Cp.String() { - copySrcs = append(copySrcs, bp.Input.String()) - copyDests = append(copyDests, bp.Output.Rel()) // rooted at the snapshot root - } else if ruleString == zipFiles.String() { - intermediateZip = bp.Output.String() - } else if ruleString == mergeZips.String() { - input := bp.Input.String() - if intermediateZip != input { - t.Errorf("Intermediate zip %s is not an input to merge_zips, %s is used instead", intermediateZip, input) - } - mergeZipInputs = bp.Inputs.Strings() - outputZip = bp.Output.String() - } - } - - buildDir := config.BuildDir() - ensureListContains(t, copySrcs, "aidl/foo/bar/Test.aidl") - ensureListContains(t, copySrcs, "include/Test.h") - ensureListContains(t, copySrcs, filepath.Join(buildDir, ".intermediates/mynativelib/android_arm64_armv8-a_core_shared/gen/aidl/aidl/foo/bar/BnTest.h")) - ensureListContains(t, copySrcs, filepath.Join(buildDir, ".intermediates/mynativelib/android_arm64_armv8-a_core_shared/gen/aidl/aidl/foo/bar/BpTest.h")) - ensureListContains(t, copySrcs, filepath.Join(buildDir, ".intermediates/mynativelib/android_arm64_armv8-a_core_shared/gen/aidl/aidl/foo/bar/Test.h")) - ensureListContains(t, copySrcs, filepath.Join(buildDir, ".intermediates/myjavalib/android_common/turbine-combined/myjavalib.jar")) - ensureListContains(t, copySrcs, filepath.Join(buildDir, ".intermediates/mynativelib/android_arm64_armv8-a_core_shared/mynativelib.so")) - - ensureListContains(t, copyDests, "aidl/aidl/foo/bar/Test.aidl") - ensureListContains(t, copyDests, "arm64/include/include/Test.h") - ensureListContains(t, copyDests, "arm64/include_gen/mynativelib/aidl/foo/bar/BnTest.h") - ensureListContains(t, copyDests, "arm64/include_gen/mynativelib/aidl/foo/bar/BpTest.h") - ensureListContains(t, copyDests, "arm64/include_gen/mynativelib/aidl/foo/bar/Test.h") - ensureListContains(t, copyDests, "java/myjavalib.jar") - ensureListContains(t, copyDests, "arm64/lib/mynativelib.so") - - expectedOutputZip := filepath.Join(buildDir, ".intermediates/mysdk/android_common/mysdk-current.zip") - expectedRepackagedZip := filepath.Join(buildDir, ".intermediates/mysdk/android_common/tmp/java/myjavaapistubs_stubs_sources.zip") - - // Ensure that the droidstubs .srcjar as repackaged into a temporary zip file - // and then merged together with the intermediate snapshot zip. - ensureListContains(t, mergeZipInputs, expectedRepackagedZip) - if outputZip != expectedOutputZip { - t.Errorf("Expected snapshot output to be %q but was %q", expectedOutputZip, outputZip) - } +`), + checkAllCopyRules(` +.intermediates/myjavalib/android_common/turbine-combined/myjavalib.jar -> java/myjavalib.jar +aidl/foo/bar/Test.aidl -> aidl/aidl/foo/bar/Test.aidl +.intermediates/mynativelib/android_arm64_armv8-a_core_shared/mynativelib.so -> arm64/lib/mynativelib.so +include/Test.h -> arm64/include/include/Test.h +.intermediates/mynativelib/android_arm64_armv8-a_core_shared/gen/aidl/aidl/foo/bar/Test.h -> arm64/include_gen/mynativelib/aidl/foo/bar/Test.h +.intermediates/mynativelib/android_arm64_armv8-a_core_shared/gen/aidl/aidl/foo/bar/BnTest.h -> arm64/include_gen/mynativelib/aidl/foo/bar/BnTest.h +.intermediates/mynativelib/android_arm64_armv8-a_core_shared/gen/aidl/aidl/foo/bar/BpTest.h -> arm64/include_gen/mynativelib/aidl/foo/bar/BpTest.h +.intermediates/mynativelib/android_arm_armv7-a-neon_core_shared/mynativelib.so -> arm/lib/mynativelib.so +include/Test.h -> arm/include/include/Test.h +.intermediates/mynativelib/android_arm_armv7-a-neon_core_shared/gen/aidl/aidl/foo/bar/Test.h -> arm/include_gen/mynativelib/aidl/foo/bar/Test.h +.intermediates/mynativelib/android_arm_armv7-a-neon_core_shared/gen/aidl/aidl/foo/bar/BnTest.h -> arm/include_gen/mynativelib/aidl/foo/bar/BnTest.h +.intermediates/mynativelib/android_arm_armv7-a-neon_core_shared/gen/aidl/aidl/foo/bar/BpTest.h -> arm/include_gen/mynativelib/aidl/foo/bar/BpTest.h +`), + checkMergeZip(".intermediates/mysdk/android_common/tmp/java/myjavaapistubs_stubs_sources.zip"), + ) } func TestHostSnapshot(t *testing.T) { // b/145598135 - Generating host snapshots for anything other than linux is not supported. SkipIfNotLinux(t) - ctx, config := testSdk(t, ` + result := testSdk(t, ` sdk { name: "mysdk", device_supported: false, @@ -528,9 +491,9 @@ func TestHostSnapshot(t *testing.T) { } `) - sdk := ctx.ModuleForTests("mysdk", "linux_glibc_common").Module().(*sdk) - - checkSnapshotAndroidBpContents(t, sdk, `// This is auto-generated. DO NOT EDIT. + result.CheckSnapshot("mysdk", "linux_glibc_common", + checkAndroidBpContents(` +// This is auto-generated. DO NOT EDIT. java_import { name: "mysdk_myjavalib@current", @@ -622,56 +585,21 @@ sdk_snapshot { stubs_sources: ["mysdk_myjavaapistubs@current"], native_shared_libs: ["mysdk_mynativelib@current"], } - -`) - - var copySrcs []string - var copyDests []string - buildParams := sdk.BuildParamsForTests() - var mergeZipInputs []string - var intermediateZip string - var outputZip string - for _, bp := range buildParams { - ruleString := bp.Rule.String() - if ruleString == android.Cp.String() { - copySrcs = append(copySrcs, bp.Input.String()) - copyDests = append(copyDests, bp.Output.Rel()) // rooted at the snapshot root - } else if ruleString == zipFiles.String() { - intermediateZip = bp.Output.String() - } else if ruleString == mergeZips.String() { - input := bp.Input.String() - if intermediateZip != input { - t.Errorf("Intermediate zip %s is not an input to merge_zips, %s is used instead", intermediateZip, input) - } - mergeZipInputs = bp.Inputs.Strings() - outputZip = bp.Output.String() - } - } - - buildDir := config.BuildDir() - ensureListContains(t, copySrcs, "aidl/foo/bar/Test.aidl") - ensureListContains(t, copySrcs, "include/Test.h") - ensureListContains(t, copySrcs, filepath.Join(buildDir, ".intermediates/mynativelib/linux_glibc_x86_64_shared/gen/aidl/aidl/foo/bar/BnTest.h")) - ensureListContains(t, copySrcs, filepath.Join(buildDir, ".intermediates/mynativelib/linux_glibc_x86_64_shared/gen/aidl/aidl/foo/bar/BpTest.h")) - ensureListContains(t, copySrcs, filepath.Join(buildDir, ".intermediates/mynativelib/linux_glibc_x86_64_shared/gen/aidl/aidl/foo/bar/Test.h")) - ensureListContains(t, copySrcs, filepath.Join(buildDir, ".intermediates/myjavalib/linux_glibc_common/javac/myjavalib.jar")) - ensureListContains(t, copySrcs, filepath.Join(buildDir, ".intermediates/mynativelib/linux_glibc_x86_64_shared/mynativelib.so")) - - ensureListContains(t, copyDests, "aidl/aidl/foo/bar/Test.aidl") - ensureListContains(t, copyDests, "x86_64/include/include/Test.h") - ensureListContains(t, copyDests, "x86_64/include_gen/mynativelib/aidl/foo/bar/BnTest.h") - ensureListContains(t, copyDests, "x86_64/include_gen/mynativelib/aidl/foo/bar/BpTest.h") - ensureListContains(t, copyDests, "x86_64/include_gen/mynativelib/aidl/foo/bar/Test.h") - ensureListContains(t, copyDests, "java/myjavalib.jar") - ensureListContains(t, copyDests, "x86_64/lib/mynativelib.so") - - expectedOutputZip := filepath.Join(buildDir, ".intermediates/mysdk/linux_glibc_common/mysdk-current.zip") - expectedRepackagedZip := filepath.Join(buildDir, ".intermediates/mysdk/linux_glibc_common/tmp/java/myjavaapistubs_stubs_sources.zip") - - // Ensure that the droidstubs .srcjar as repackaged into a temporary zip file - // and then merged together with the intermediate snapshot zip. - ensureListContains(t, mergeZipInputs, expectedRepackagedZip) - if outputZip != expectedOutputZip { - t.Errorf("Expected snapshot output to be %q but was %q", expectedOutputZip, outputZip) - } +`), + checkAllCopyRules(` +.intermediates/myjavalib/linux_glibc_common/javac/myjavalib.jar -> java/myjavalib.jar +aidl/foo/bar/Test.aidl -> aidl/aidl/foo/bar/Test.aidl +.intermediates/mynativelib/linux_glibc_x86_64_shared/mynativelib.so -> x86_64/lib/mynativelib.so +include/Test.h -> x86_64/include/include/Test.h +.intermediates/mynativelib/linux_glibc_x86_64_shared/gen/aidl/aidl/foo/bar/Test.h -> x86_64/include_gen/mynativelib/aidl/foo/bar/Test.h +.intermediates/mynativelib/linux_glibc_x86_64_shared/gen/aidl/aidl/foo/bar/BnTest.h -> x86_64/include_gen/mynativelib/aidl/foo/bar/BnTest.h +.intermediates/mynativelib/linux_glibc_x86_64_shared/gen/aidl/aidl/foo/bar/BpTest.h -> x86_64/include_gen/mynativelib/aidl/foo/bar/BpTest.h +.intermediates/mynativelib/linux_glibc_x86_shared/mynativelib.so -> x86/lib/mynativelib.so +include/Test.h -> x86/include/include/Test.h +.intermediates/mynativelib/linux_glibc_x86_shared/gen/aidl/aidl/foo/bar/Test.h -> x86/include_gen/mynativelib/aidl/foo/bar/Test.h +.intermediates/mynativelib/linux_glibc_x86_shared/gen/aidl/aidl/foo/bar/BnTest.h -> x86/include_gen/mynativelib/aidl/foo/bar/BnTest.h +.intermediates/mynativelib/linux_glibc_x86_shared/gen/aidl/aidl/foo/bar/BpTest.h -> x86/include_gen/mynativelib/aidl/foo/bar/BpTest.h +`), + checkMergeZip(".intermediates/mysdk/linux_glibc_common/tmp/java/myjavaapistubs_stubs_sources.zip"), + ) } diff --git a/sdk/testing.go b/sdk/testing.go index 29815e0df..ec451edc0 100644 --- a/sdk/testing.go +++ b/sdk/testing.go @@ -15,8 +15,10 @@ package sdk import ( + "fmt" "io/ioutil" "os" + "path/filepath" "strings" "testing" @@ -26,7 +28,7 @@ import ( "android/soong/java" ) -func testSdkContext(bp string) (*android.TestContext, android.Config) { +func testSdkContext(bp string, fs map[string][]byte) (*android.TestContext, android.Config) { config := android.TestArchConfig(buildDir, nil) ctx := android.NewTestArchContext() @@ -90,7 +92,7 @@ func testSdkContext(bp string) (*android.TestContext, android.Config) { } ` + cc.GatherRequiredDepsForTest(android.Android) - ctx.MockFileSystem(map[string][]byte{ + mockFS := map[string][]byte{ "Android.bp": []byte(bp), "build/make/target/product/security": nil, "apex_manifest.json": nil, @@ -107,23 +109,39 @@ func testSdkContext(bp string) (*android.TestContext, android.Config) { "libfoo.so": nil, "stubs-sources/foo/bar/Foo.java": nil, "foo/bar/Foo.java": nil, - }) + } + + for k, v := range fs { + mockFS[k] = v + } + + ctx.MockFileSystem(mockFS) return ctx, config } -func testSdk(t *testing.T, bp string) (*android.TestContext, android.Config) { - ctx, config := testSdkContext(bp) +func testSdk(t *testing.T, bp string) *testSdkResult { + t.Helper() + return testSdkWithFs(t, bp, nil) +} + +func testSdkWithFs(t *testing.T, bp string, fs map[string][]byte) *testSdkResult { + t.Helper() + ctx, config := testSdkContext(bp, fs) _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) android.FailIfErrored(t, errs) _, errs = ctx.PrepareBuildActions(config) android.FailIfErrored(t, errs) - return ctx, config + return &testSdkResult{ + TestHelper: TestHelper{t: t}, + ctx: ctx, + config: config, + } } func testSdkError(t *testing.T, pattern, bp string) { t.Helper() - ctx, config := testSdkContext(bp) + ctx, config := testSdkContext(bp, nil) _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) if len(errs) > 0 { android.FailIfNoMatchingErrors(t, pattern, errs) @@ -153,14 +171,211 @@ func pathsToStrings(paths android.Paths) []string { return ret } -func checkSnapshotAndroidBpContents(t *testing.T, s *sdk, expectedContents string) { - t.Helper() - androidBpContents := strings.NewReplacer("\\n", "\n").Replace(s.GetAndroidBpContentsForTests()) - if androidBpContents != expectedContents { - t.Errorf("Android.bp contents do not match, expected %s, actual %s", expectedContents, androidBpContents) +// Provides general test support. +type TestHelper struct { + t *testing.T +} + +func (h *TestHelper) AssertStringEquals(message string, expected string, actual string) { + h.t.Helper() + if actual != expected { + h.t.Errorf("%s: expected %s, actual %s", message, expected, actual) } } +func (h *TestHelper) AssertTrimmedStringEquals(message string, expected string, actual string) { + h.t.Helper() + h.AssertStringEquals(message, strings.TrimSpace(expected), strings.TrimSpace(actual)) +} + +// Encapsulates result of processing an SDK definition. Provides support for +// checking the state of the build structures. +type testSdkResult struct { + TestHelper + ctx *android.TestContext + config android.Config +} + +// Analyse the sdk build rules to extract information about what it is doing. + +// e.g. find the src/dest pairs from each cp command, the various zip files +// generated, etc. +func (r *testSdkResult) getSdkSnapshotBuildInfo(sdk *sdk) *snapshotBuildInfo { + androidBpContents := strings.NewReplacer("\\n", "\n").Replace(sdk.GetAndroidBpContentsForTests()) + + info := &snapshotBuildInfo{ + r: r, + androidBpContents: androidBpContents, + } + + buildParams := sdk.BuildParamsForTests() + copyRules := &strings.Builder{} + for _, bp := range buildParams { + switch bp.Rule.String() { + case android.Cp.String(): + // Get source relative to build directory. + src := r.pathRelativeToBuildDir(bp.Input) + // Get destination relative to the snapshot root + dest := bp.Output.Rel() + _, _ = fmt.Fprintf(copyRules, "%s -> %s\n", src, dest) + info.snapshotContents = append(info.snapshotContents, dest) + + case repackageZip.String(): + // Add the destdir to the snapshot contents as that is effectively where + // the content of the repackaged zip is copied. + dest := bp.Args["destdir"] + info.snapshotContents = append(info.snapshotContents, dest) + + case zipFiles.String(): + // This could be an intermediate zip file and not the actual output zip. + // In that case this will be overridden when the rule to merge the zips + // is processed. + info.outputZip = r.pathRelativeToBuildDir(bp.Output) + + case mergeZips.String(): + // Copy the current outputZip to the intermediateZip. + info.intermediateZip = info.outputZip + mergeInput := r.pathRelativeToBuildDir(bp.Input) + if info.intermediateZip != mergeInput { + r.t.Errorf("Expected intermediate zip %s to be an input to merge zips but found %s instead", + info.intermediateZip, mergeInput) + } + + // Override output zip (which was actually the intermediate zip file) with the actual + // output zip. + info.outputZip = r.pathRelativeToBuildDir(bp.Output) + + // Save the zips to be merged into the intermediate zip. + info.mergeZips = r.pathsRelativeToBuildDir(bp.Inputs) + } + } + + info.copyRules = copyRules.String() + + return info +} + +func (r *testSdkResult) Module(name string, variant string) android.Module { + return r.ctx.ModuleForTests(name, variant).Module() +} + +func (r *testSdkResult) ModuleForTests(name string, variant string) android.TestingModule { + return r.ctx.ModuleForTests(name, variant) +} + +func (r *testSdkResult) pathRelativeToBuildDir(path android.Path) string { + buildDir := filepath.Clean(r.config.BuildDir()) + "/" + return strings.TrimPrefix(filepath.Clean(path.String()), buildDir) +} + +func (r *testSdkResult) pathsRelativeToBuildDir(paths android.Paths) []string { + var result []string + for _, path := range paths { + result = append(result, r.pathRelativeToBuildDir(path)) + } + return result +} + +// Check the snapshot build rules. +// +// Takes a list of functions which check different facets of the snapshot build rules. +// Allows each test to customize what is checked without duplicating lots of code +// or proliferating check methods of different flavors. +func (r *testSdkResult) CheckSnapshot(name string, variant string, checkers ...snapshotBuildInfoChecker) { + r.t.Helper() + + sdk := r.Module(name, variant).(*sdk) + + snapshotBuildInfo := r.getSdkSnapshotBuildInfo(sdk) + + // Check state of the snapshot build. + for _, checker := range checkers { + checker(snapshotBuildInfo) + } + + // Make sure that the generated zip file is in the correct place. + actual := snapshotBuildInfo.outputZip + r.AssertStringEquals("Snapshot zip file in wrong place", + fmt.Sprintf(".intermediates/%s/%s/%s-current.zip", name, variant, name), actual) + + // Populate a mock filesystem with the files that would have been copied by + // the rules. + fs := make(map[string][]byte) + for _, dest := range snapshotBuildInfo.snapshotContents { + fs[dest] = nil + } + + // Process the generated bp file to make sure it is valid. + testSdkWithFs(r.t, snapshotBuildInfo.androidBpContents, fs) +} + +type snapshotBuildInfoChecker func(info *snapshotBuildInfo) + +// Check that the snapshot's generated Android.bp is correct. +// +// Both the expected and actual string are both trimmed before comparing. +func checkAndroidBpContents(expected string) snapshotBuildInfoChecker { + return func(info *snapshotBuildInfo) { + info.r.t.Helper() + info.r.AssertTrimmedStringEquals("Android.bp contents do not match", expected, info.androidBpContents) + } +} + +// Check that the snapshot's copy rules are correct. +// +// The copy rules are formatted as -> , one per line and then compared +// to the supplied expected string. Both the expected and actual string are trimmed +// before comparing. +func checkAllCopyRules(expected string) snapshotBuildInfoChecker { + return func(info *snapshotBuildInfo) { + info.r.t.Helper() + info.r.AssertTrimmedStringEquals("Incorrect copy rules", expected, info.copyRules) + } +} + +// Check that the specified path is in the list of zips to merge with the intermediate zip. +func checkMergeZip(expected string) snapshotBuildInfoChecker { + return func(info *snapshotBuildInfo) { + info.r.t.Helper() + if info.intermediateZip == "" { + info.r.t.Errorf("No intermediate zip file was created") + } + ensureListContains(info.r.t, info.mergeZips, expected) + } +} + +// Encapsulates information about the snapshot build structure in order to insulate tests from +// knowing too much about internal structures. +// +// All source/input paths are relative either the build directory. All dest/output paths are +// relative to the snapshot root directory. +type snapshotBuildInfo struct { + r *testSdkResult + + // The contents of the generated Android.bp file + androidBpContents string + + // The paths, relative to the snapshot root, of all files and directories copied into the + // snapshot. + snapshotContents []string + + // A formatted representation of the src/dest pairs, one pair per line, of the format + // src -> dest + copyRules string + + // The path to the intermediate zip, which is a zip created from the source files copied + // into the snapshot directory and which will be merged with other zips to form the final output. + // Is am empty string if there is no intermediate zip because there are no zips to merge in. + intermediateZip string + + // The paths to the zips to merge into the output zip, does not include the intermediate + // zip. + mergeZips []string + + // The final output zip. + outputZip string +} + var buildDir string func setUp() { diff --git a/sdk/update.go b/sdk/update.go index 602e0a498..7731fbb3e 100644 --- a/sdk/update.go +++ b/sdk/update.go @@ -276,7 +276,6 @@ func generateBpContents(contents *generatedContents, bpFile *bpFile) { outputPropertySet(contents, &bpModule.bpPropertySet) contents.Printfln("}") } - contents.Printfln("") } func outputPropertySet(contents *generatedContents, set *bpPropertySet) {