From 1172fed8eb534ffb7fe1f28fff96a6156f4794cb Mon Sep 17 00:00:00 2001 From: Paul Duffin Date: Mon, 8 Mar 2021 11:28:18 +0000 Subject: [PATCH] Support test fixtures in etc package Restructures the etc package test setup code to create FixturePreparer instances for setting up a test fixture. Bug: 181070625 Test: m nothing Change-Id: I6f269c9cb3f4ab2756beddd13a719f9b222f7156 --- etc/prebuilt_etc.go | 2 + etc/prebuilt_etc_test.go | 82 +++++++++++++++------------------------- 2 files changed, 32 insertions(+), 52 deletions(-) diff --git a/etc/prebuilt_etc.go b/etc/prebuilt_etc.go index b07ad9115..6291325f4 100644 --- a/etc/prebuilt_etc.go +++ b/etc/prebuilt_etc.go @@ -54,6 +54,8 @@ func RegisterPrebuiltEtcBuildComponents(ctx android.RegistrationContext) { ctx.RegisterModuleType("prebuilt_dsp", PrebuiltDSPFactory) } +var PrepareForTestWithPrebuiltEtc = android.FixtureRegisterWithContext(RegisterPrebuiltEtcBuildComponents) + type prebuiltEtcProperties struct { // Source file of this prebuilt. Can reference a genrule type module with the ":module" syntax. Src *string `android:"path,arch_variant"` diff --git a/etc/prebuilt_etc_test.go b/etc/prebuilt_etc_test.go index 585760d47..87b3adf3e 100644 --- a/etc/prebuilt_etc_test.go +++ b/etc/prebuilt_etc_test.go @@ -49,58 +49,34 @@ func TestMain(m *testing.M) { os.Exit(run()) } -func testPrebuiltEtcContext(t *testing.T, bp string) (*android.TestContext, android.Config) { - fs := map[string][]byte{ +var prebuiltEtcFixtureFactory = android.NewFixtureFactory( + &buildDir, + android.PrepareForTestWithArchMutator, + PrepareForTestWithPrebuiltEtc, + android.FixtureMergeMockFs(android.MockFS{ "foo.conf": nil, "bar.conf": nil, "baz.conf": nil, - } - - config := android.TestArchConfig(buildDir, nil, bp, fs) - - ctx := android.NewTestArchContext(config) - ctx.RegisterModuleType("prebuilt_etc", PrebuiltEtcFactory) - ctx.RegisterModuleType("prebuilt_etc_host", PrebuiltEtcHostFactory) - ctx.RegisterModuleType("prebuilt_usr_share", PrebuiltUserShareFactory) - ctx.RegisterModuleType("prebuilt_usr_share_host", PrebuiltUserShareHostFactory) - ctx.RegisterModuleType("prebuilt_font", PrebuiltFontFactory) - ctx.RegisterModuleType("prebuilt_firmware", PrebuiltFirmwareFactory) - ctx.RegisterModuleType("prebuilt_dsp", PrebuiltDSPFactory) - ctx.Register() - - return ctx, config -} + }), +) +// testPrebuiltEtc runs tests using the prebuiltEtcFixtureFactory +// +// Do not add any new usages of this, instead use the prebuiltEtcFixtureFactory 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 prebuiltEtcFixtureFactory 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 testPrebuiltEtc(t *testing.T, bp string) (*android.TestContext, android.Config) { t.Helper() - - ctx, config := testPrebuiltEtcContext(t, bp) - _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) - android.FailIfErrored(t, errs) - _, errs = ctx.PrepareBuildActions(config) - android.FailIfErrored(t, errs) - - return ctx, config + result := prebuiltEtcFixtureFactory.RunTestWithBp(t, bp) + return result.TestContext, result.Config } -func testPrebuiltEtcError(t *testing.T, pattern, bp string) { - t.Helper() - - ctx, config := testPrebuiltEtcContext(t, bp) - _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) - if len(errs) > 0 { - android.FailIfNoMatchingErrors(t, pattern, errs) - return - } - - _, errs = ctx.PrepareBuildActions(config) - if len(errs) > 0 { - android.FailIfNoMatchingErrors(t, pattern, errs) - return - } - - t.Fatalf("missing expected error %q (0 errors are returned)", pattern) -} func TestPrebuiltEtcVariants(t *testing.T) { ctx, _ := testPrebuiltEtc(t, ` prebuilt_etc { @@ -227,14 +203,16 @@ func TestPrebuiltEtcRelativeInstallPathInstallDirPath(t *testing.T) { } func TestPrebuiltEtcCannotSetRelativeInstallPathAndSubDir(t *testing.T) { - testPrebuiltEtcError(t, "relative_install_path is set. Cannot set sub_dir", ` - prebuilt_etc { - name: "foo.conf", - src: "foo.conf", - sub_dir: "bar", - relative_install_path: "bar", - } - `) + prebuiltEtcFixtureFactory. + ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern("relative_install_path is set. Cannot set sub_dir")). + RunTestWithBp(t, ` + prebuilt_etc { + name: "foo.conf", + src: "foo.conf", + sub_dir: "bar", + relative_install_path: "bar", + } + `) } func TestPrebuiltEtcHost(t *testing.T) {