diff options
Diffstat (limited to 'sdk/testing.go')
-rw-r--r-- | sdk/testing.go | 123 |
1 files changed, 53 insertions, 70 deletions
diff --git a/sdk/testing.go b/sdk/testing.go index 1ac873b4f..e291bdbdf 100644 --- a/sdk/testing.go +++ b/sdk/testing.go @@ -19,13 +19,13 @@ import ( "io/ioutil" "os" "path/filepath" - "reflect" "strings" "testing" "android/soong/android" "android/soong/apex" "android/soong/cc" + "android/soong/genrule" "android/soong/java" ) @@ -109,6 +109,9 @@ func testSdkContext(bp string, fs map[string][]byte, extraOsTypes []android.OsTy // from java package java.RegisterRequiredBuildComponentsForTest(ctx) + // from genrule package + genrule.RegisterGenruleBuildComponents(ctx) + // from cc package cc.RegisterRequiredBuildComponentsForTest(ctx) @@ -118,12 +121,8 @@ func testSdkContext(bp string, fs map[string][]byte, extraOsTypes []android.OsTy ctx.PostDepsMutators(apex.RegisterPostDepsMutators) // from this package - ctx.RegisterModuleType("sdk", SdkModuleFactory) - ctx.RegisterModuleType("sdk_snapshot", SnapshotModuleFactory) - ctx.RegisterModuleType("module_exports", ModuleExportsFactory) - ctx.RegisterModuleType("module_exports_snapshot", ModuleExportsSnapshotsFactory) - ctx.PreDepsMutators(RegisterPreDepsMutators) - ctx.PostDepsMutators(RegisterPostDepsMutators) + registerModuleExportsBuildComponents(ctx) + registerSdkBuildComponents(ctx) ctx.Register() @@ -137,7 +136,7 @@ func runTests(t *testing.T, ctx *android.TestContext, config android.Config) *te _, errs = ctx.PrepareBuildActions(config) android.FailIfErrored(t, errs) return &testSdkResult{ - TestHelper: TestHelper{t: t}, + TestHelper: android.TestHelper{T: t}, ctx: ctx, config: config, } @@ -181,59 +180,10 @@ func pathsToStrings(paths android.Paths) []string { return ret } -// 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) AssertErrorMessageEquals(message string, expected string, actual error) { - h.t.Helper() - if actual == nil { - h.t.Errorf("Expected error but was nil") - } else if actual.Error() != expected { - h.t.Errorf("%s: expected %s, actual %s", message, expected, actual.Error()) - } -} - -func (h *TestHelper) AssertTrimmedStringEquals(message string, expected string, actual string) { - h.t.Helper() - h.AssertStringEquals(message, strings.TrimSpace(expected), strings.TrimSpace(actual)) -} - -func (h *TestHelper) AssertDeepEquals(message string, expected interface{}, actual interface{}) { - h.t.Helper() - if !reflect.DeepEqual(actual, expected) { - h.t.Errorf("%s: expected %#v, actual %#v", message, expected, actual) - } -} - -func (h *TestHelper) AssertPanic(message string, funcThatShouldPanic func()) { - h.t.Helper() - panicked := false - func() { - defer func() { - if x := recover(); x != nil { - panicked = true - } - }() - funcThatShouldPanic() - }() - if !panicked { - h.t.Error(message) - } -} - // Encapsulates result of processing an SDK definition. Provides support for // checking the state of the build structures. type testSdkResult struct { - TestHelper + android.TestHelper ctx *android.TestContext config android.Config } @@ -243,11 +193,11 @@ type testSdkResult struct { // 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 := sdk.GetAndroidBpContentsForTests() - info := &snapshotBuildInfo{ - r: r, - androidBpContents: androidBpContents, + r: r, + androidBpContents: sdk.GetAndroidBpContentsForTests(), + androidUnversionedBpContents: sdk.GetUnversionedAndroidBpContentsForTests(), + androidVersionedBpContents: sdk.GetVersionedAndroidBpContentsForTests(), } buildParams := sdk.BuildParamsForTests() @@ -287,7 +237,7 @@ func (r *testSdkResult) getSdkSnapshotBuildInfo(sdk *sdk) *snapshotBuildInfo { info.intermediateZip = info.outputZip mergeInput := android.NormalizePathForTesting(bp.Input) if info.intermediateZip != mergeInput { - r.t.Errorf("Expected intermediate zip %s to be an input to merge zips but found %s instead", + r.Errorf("Expected intermediate zip %s to be an input to merge zips but found %s instead", info.intermediateZip, mergeInput) } @@ -320,7 +270,7 @@ func (r *testSdkResult) ModuleForTests(name string, variant string) android.Test // 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, dir string, checkers ...snapshotBuildInfoChecker) { - r.t.Helper() + r.Helper() // The sdk CommonOS variant is always responsible for generating the snapshot. variant := android.CommonOS.Name @@ -350,7 +300,7 @@ func (r *testSdkResult) CheckSnapshot(name string, dir string, checkers ...snaps } // Process the generated bp file to make sure it is valid. - testSdkWithFs(r.t, snapshotBuildInfo.androidBpContents, fs) + testSdkWithFs(r.T, snapshotBuildInfo.androidBpContents, fs) } type snapshotBuildInfoChecker func(info *snapshotBuildInfo) @@ -360,11 +310,38 @@ type snapshotBuildInfoChecker func(info *snapshotBuildInfo) // 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.Helper() info.r.AssertTrimmedStringEquals("Android.bp contents do not match", expected, info.androidBpContents) } } +// Check that the snapshot's unversioned generated Android.bp is correct. +// +// This func should be used to check the general snapshot generation code. +// +// Both the expected and actual string are both trimmed before comparing. +func checkUnversionedAndroidBpContents(expected string) snapshotBuildInfoChecker { + return func(info *snapshotBuildInfo) { + info.r.Helper() + info.r.AssertTrimmedStringEquals("unversioned Android.bp contents do not match", expected, info.androidUnversionedBpContents) + } +} + +// Check that the snapshot's versioned generated Android.bp is correct. +// +// This func should only be used to check the version specific snapshot generation code, +// i.e. the encoding of version into module names and the generation of the _snapshot module. The +// general snapshot generation code should be checked using the checkUnversionedAndroidBpContents() +// func. +// +// Both the expected and actual string are both trimmed before comparing. +func checkVersionedAndroidBpContents(expected string) snapshotBuildInfoChecker { + return func(info *snapshotBuildInfo) { + info.r.Helper() + info.r.AssertTrimmedStringEquals("versioned Android.bp contents do not match", expected, info.androidVersionedBpContents) + } +} + // Check that the snapshot's copy rules are correct. // // The copy rules are formatted as <src> -> <dest>, one per line and then compared @@ -372,14 +349,14 @@ func checkAndroidBpContents(expected string) snapshotBuildInfoChecker { // before comparing. func checkAllCopyRules(expected string) snapshotBuildInfoChecker { return func(info *snapshotBuildInfo) { - info.r.t.Helper() + info.r.Helper() info.r.AssertTrimmedStringEquals("Incorrect copy rules", expected, info.copyRules) } } func checkAllOtherCopyRules(expected string) snapshotBuildInfoChecker { return func(info *snapshotBuildInfo) { - info.r.t.Helper() + info.r.Helper() info.r.AssertTrimmedStringEquals("Incorrect copy rules", expected, info.otherCopyRules) } } @@ -387,9 +364,9 @@ func checkAllOtherCopyRules(expected string) snapshotBuildInfoChecker { // Check that the specified paths match the list of zips to merge with the intermediate zip. func checkMergeZips(expected ...string) snapshotBuildInfoChecker { return func(info *snapshotBuildInfo) { - info.r.t.Helper() + info.r.Helper() if info.intermediateZip == "" { - info.r.t.Errorf("No intermediate zip file was created") + info.r.Errorf("No intermediate zip file was created") } info.r.AssertDeepEquals("mismatching merge zip files", expected, info.mergeZips) @@ -407,6 +384,12 @@ type snapshotBuildInfo struct { // The contents of the generated Android.bp file androidBpContents string + // The contents of the unversioned Android.bp file + androidUnversionedBpContents string + + // The contents of the versioned Android.bp file + androidVersionedBpContents string + // The paths, relative to the snapshot root, of all files and directories copied into the // snapshot. snapshotContents []string |