Loading android/fixture.go +29 −0 Original line number Diff line number Diff line Loading @@ -524,6 +524,18 @@ func (h *TestHelper) AssertStringEquals(message string, expected string, actual } } // AssertErrorMessageEquals checks if the error is not nil and has the expected message. If it does // not then this reports an error prefixed with the supplied message and including a reason for why // it failed. func (h *TestHelper) AssertErrorMessageEquals(message string, expected string, actual error) { h.Helper() if actual == nil { h.Errorf("Expected error but was nil") } else if actual.Error() != expected { h.Errorf("%s: expected %s, actual %s", message, expected, actual.Error()) } } // AssertTrimmedStringEquals checks if the expected and actual values are the same after trimming // leading and trailing spaces from them both. If they are not then it reports an error prefixed // with the supplied message and including a reason for why it failed. Loading Loading @@ -578,6 +590,23 @@ func (h *TestHelper) AssertDeepEquals(message string, expected interface{}, actu } } // AssertPanic checks that the supplied function panics as expected. func (h *TestHelper) AssertPanic(message string, funcThatShouldPanic func()) { h.Helper() panicked := false func() { defer func() { if x := recover(); x != nil { panicked = true } }() funcThatShouldPanic() }() if !panicked { h.Error(message) } } // Struct to allow TestResult to embed a *TestContext and allow call forwarding to its methods. type testContext struct { *TestContext Loading sdk/bp_test.go +7 −7 Original line number Diff line number Diff line Loading @@ -54,7 +54,7 @@ func propertyStructFixture() interface{} { return str } func checkPropertySetFixture(h *TestHelper, val interface{}, hasTags bool) { func checkPropertySetFixture(h android.TestHelper, val interface{}, hasTags bool) { set := val.(*bpPropertySet) h.AssertDeepEquals("wrong x value", "taxi", set.getValue("x")) h.AssertDeepEquals("wrong y value", 1729, set.getValue("y")) Loading @@ -73,7 +73,7 @@ func checkPropertySetFixture(h *TestHelper, val interface{}, hasTags bool) { } func TestAddPropertySimple(t *testing.T) { h := &TestHelper{t} h := android.TestHelper{t} set := newPropertySet() for name, val := range map[string]interface{}{ "x": "taxi", Loading @@ -92,7 +92,7 @@ func TestAddPropertySimple(t *testing.T) { } func TestAddPropertySubset(t *testing.T) { h := &TestHelper{t} h := android.TestHelper{t} getFixtureMap := map[string]func() interface{}{ "property set": propertySetFixture, "property struct": propertyStructFixture, Loading Loading @@ -139,7 +139,7 @@ func TestAddPropertySubset(t *testing.T) { } func TestAddPropertySetNew(t *testing.T) { h := &TestHelper{t} h := android.TestHelper{t} set := newPropertySet() subset := set.AddPropertySet("sub") subset.AddProperty("new", "d^^b") Loading @@ -147,7 +147,7 @@ func TestAddPropertySetNew(t *testing.T) { } func TestAddPropertySetExisting(t *testing.T) { h := &TestHelper{t} h := android.TestHelper{t} set := propertySetFixture().(*bpPropertySet) subset := set.AddPropertySet("sub") subset.AddProperty("new", "d^^b") Loading Loading @@ -181,7 +181,7 @@ func (t removeFredTransformation) transformPropertySetAfterContents(name string, func TestTransformRemoveProperty(t *testing.T) { helper := &TestHelper{t} helper := android.TestHelper{t} set := newPropertySet() set.AddProperty("name", "name") Loading @@ -196,7 +196,7 @@ func TestTransformRemoveProperty(t *testing.T) { func TestTransformRemovePropertySet(t *testing.T) { helper := &TestHelper{t} helper := android.TestHelper{t} set := newPropertySet() set.AddProperty("name", "name") Loading sdk/sdk_test.go +2 −2 Original line number Diff line number Diff line Loading @@ -387,7 +387,7 @@ func TestCommonValueOptimization(t *testing.T) { extractor := newCommonValueExtractor(common) h := TestHelper{t} h := android.TestHelper{t} err := extractor.extractCommonProperties(common, structs) h.AssertDeepEquals("unexpected error", nil, err) Loading Loading @@ -462,7 +462,7 @@ func TestCommonValueOptimization_InvalidArchSpecificVariants(t *testing.T) { extractor := newCommonValueExtractor(common) h := TestHelper{t} h := android.TestHelper{t} err := extractor.extractCommonProperties(common, structs) h.AssertErrorMessageEquals("unexpected error", `field "S_Common" is not tagged as "arch_variant" but has arch specific properties: Loading sdk/testing.go +12 −66 Original line number Diff line number Diff line Loading @@ -19,7 +19,6 @@ import ( "io/ioutil" "os" "path/filepath" "reflect" "strings" "testing" Loading Loading @@ -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, } Loading Loading @@ -181,63 +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() expected = strings.TrimSpace(expected) actual = strings.TrimSpace(actual) if actual != expected { h.t.Errorf("%s: expected:\n%s\nactual:\n%s\n", message, expected, 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 } Loading Loading @@ -291,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) } Loading Loading @@ -324,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 Loading Loading @@ -354,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) Loading @@ -364,7 +310,7 @@ 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) } } Loading @@ -376,7 +322,7 @@ func checkAndroidBpContents(expected string) snapshotBuildInfoChecker { // Both the expected and actual string are both trimmed before comparing. func checkUnversionedAndroidBpContents(expected string) snapshotBuildInfoChecker { return func(info *snapshotBuildInfo) { info.r.t.Helper() info.r.Helper() info.r.AssertTrimmedStringEquals("unversioned Android.bp contents do not match", expected, info.androidUnversionedBpContents) } } Loading @@ -391,7 +337,7 @@ func checkUnversionedAndroidBpContents(expected string) snapshotBuildInfoChecker // Both the expected and actual string are both trimmed before comparing. func checkVersionedAndroidBpContents(expected string) snapshotBuildInfoChecker { return func(info *snapshotBuildInfo) { info.r.t.Helper() info.r.Helper() info.r.AssertTrimmedStringEquals("versioned Android.bp contents do not match", expected, info.androidVersionedBpContents) } } Loading @@ -403,14 +349,14 @@ func checkVersionedAndroidBpContents(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) } } Loading @@ -418,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) Loading Loading
android/fixture.go +29 −0 Original line number Diff line number Diff line Loading @@ -524,6 +524,18 @@ func (h *TestHelper) AssertStringEquals(message string, expected string, actual } } // AssertErrorMessageEquals checks if the error is not nil and has the expected message. If it does // not then this reports an error prefixed with the supplied message and including a reason for why // it failed. func (h *TestHelper) AssertErrorMessageEquals(message string, expected string, actual error) { h.Helper() if actual == nil { h.Errorf("Expected error but was nil") } else if actual.Error() != expected { h.Errorf("%s: expected %s, actual %s", message, expected, actual.Error()) } } // AssertTrimmedStringEquals checks if the expected and actual values are the same after trimming // leading and trailing spaces from them both. If they are not then it reports an error prefixed // with the supplied message and including a reason for why it failed. Loading Loading @@ -578,6 +590,23 @@ func (h *TestHelper) AssertDeepEquals(message string, expected interface{}, actu } } // AssertPanic checks that the supplied function panics as expected. func (h *TestHelper) AssertPanic(message string, funcThatShouldPanic func()) { h.Helper() panicked := false func() { defer func() { if x := recover(); x != nil { panicked = true } }() funcThatShouldPanic() }() if !panicked { h.Error(message) } } // Struct to allow TestResult to embed a *TestContext and allow call forwarding to its methods. type testContext struct { *TestContext Loading
sdk/bp_test.go +7 −7 Original line number Diff line number Diff line Loading @@ -54,7 +54,7 @@ func propertyStructFixture() interface{} { return str } func checkPropertySetFixture(h *TestHelper, val interface{}, hasTags bool) { func checkPropertySetFixture(h android.TestHelper, val interface{}, hasTags bool) { set := val.(*bpPropertySet) h.AssertDeepEquals("wrong x value", "taxi", set.getValue("x")) h.AssertDeepEquals("wrong y value", 1729, set.getValue("y")) Loading @@ -73,7 +73,7 @@ func checkPropertySetFixture(h *TestHelper, val interface{}, hasTags bool) { } func TestAddPropertySimple(t *testing.T) { h := &TestHelper{t} h := android.TestHelper{t} set := newPropertySet() for name, val := range map[string]interface{}{ "x": "taxi", Loading @@ -92,7 +92,7 @@ func TestAddPropertySimple(t *testing.T) { } func TestAddPropertySubset(t *testing.T) { h := &TestHelper{t} h := android.TestHelper{t} getFixtureMap := map[string]func() interface{}{ "property set": propertySetFixture, "property struct": propertyStructFixture, Loading Loading @@ -139,7 +139,7 @@ func TestAddPropertySubset(t *testing.T) { } func TestAddPropertySetNew(t *testing.T) { h := &TestHelper{t} h := android.TestHelper{t} set := newPropertySet() subset := set.AddPropertySet("sub") subset.AddProperty("new", "d^^b") Loading @@ -147,7 +147,7 @@ func TestAddPropertySetNew(t *testing.T) { } func TestAddPropertySetExisting(t *testing.T) { h := &TestHelper{t} h := android.TestHelper{t} set := propertySetFixture().(*bpPropertySet) subset := set.AddPropertySet("sub") subset.AddProperty("new", "d^^b") Loading Loading @@ -181,7 +181,7 @@ func (t removeFredTransformation) transformPropertySetAfterContents(name string, func TestTransformRemoveProperty(t *testing.T) { helper := &TestHelper{t} helper := android.TestHelper{t} set := newPropertySet() set.AddProperty("name", "name") Loading @@ -196,7 +196,7 @@ func TestTransformRemoveProperty(t *testing.T) { func TestTransformRemovePropertySet(t *testing.T) { helper := &TestHelper{t} helper := android.TestHelper{t} set := newPropertySet() set.AddProperty("name", "name") Loading
sdk/sdk_test.go +2 −2 Original line number Diff line number Diff line Loading @@ -387,7 +387,7 @@ func TestCommonValueOptimization(t *testing.T) { extractor := newCommonValueExtractor(common) h := TestHelper{t} h := android.TestHelper{t} err := extractor.extractCommonProperties(common, structs) h.AssertDeepEquals("unexpected error", nil, err) Loading Loading @@ -462,7 +462,7 @@ func TestCommonValueOptimization_InvalidArchSpecificVariants(t *testing.T) { extractor := newCommonValueExtractor(common) h := TestHelper{t} h := android.TestHelper{t} err := extractor.extractCommonProperties(common, structs) h.AssertErrorMessageEquals("unexpected error", `field "S_Common" is not tagged as "arch_variant" but has arch specific properties: Loading
sdk/testing.go +12 −66 Original line number Diff line number Diff line Loading @@ -19,7 +19,6 @@ import ( "io/ioutil" "os" "path/filepath" "reflect" "strings" "testing" Loading Loading @@ -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, } Loading Loading @@ -181,63 +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() expected = strings.TrimSpace(expected) actual = strings.TrimSpace(actual) if actual != expected { h.t.Errorf("%s: expected:\n%s\nactual:\n%s\n", message, expected, 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 } Loading Loading @@ -291,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) } Loading Loading @@ -324,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 Loading Loading @@ -354,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) Loading @@ -364,7 +310,7 @@ 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) } } Loading @@ -376,7 +322,7 @@ func checkAndroidBpContents(expected string) snapshotBuildInfoChecker { // Both the expected and actual string are both trimmed before comparing. func checkUnversionedAndroidBpContents(expected string) snapshotBuildInfoChecker { return func(info *snapshotBuildInfo) { info.r.t.Helper() info.r.Helper() info.r.AssertTrimmedStringEquals("unversioned Android.bp contents do not match", expected, info.androidUnversionedBpContents) } } Loading @@ -391,7 +337,7 @@ func checkUnversionedAndroidBpContents(expected string) snapshotBuildInfoChecker // Both the expected and actual string are both trimmed before comparing. func checkVersionedAndroidBpContents(expected string) snapshotBuildInfoChecker { return func(info *snapshotBuildInfo) { info.r.t.Helper() info.r.Helper() info.r.AssertTrimmedStringEquals("versioned Android.bp contents do not match", expected, info.androidVersionedBpContents) } } Loading @@ -403,14 +349,14 @@ func checkVersionedAndroidBpContents(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) } } Loading @@ -418,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) Loading