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
This commit is contained in:
Paul Duffin 2021-03-08 11:28:18 +00:00
parent 8bd286590b
commit 1172fed8eb
2 changed files with 32 additions and 52 deletions

View File

@ -54,6 +54,8 @@ func RegisterPrebuiltEtcBuildComponents(ctx android.RegistrationContext) {
ctx.RegisterModuleType("prebuilt_dsp", PrebuiltDSPFactory) ctx.RegisterModuleType("prebuilt_dsp", PrebuiltDSPFactory)
} }
var PrepareForTestWithPrebuiltEtc = android.FixtureRegisterWithContext(RegisterPrebuiltEtcBuildComponents)
type prebuiltEtcProperties struct { type prebuiltEtcProperties struct {
// Source file of this prebuilt. Can reference a genrule type module with the ":module" syntax. // Source file of this prebuilt. Can reference a genrule type module with the ":module" syntax.
Src *string `android:"path,arch_variant"` Src *string `android:"path,arch_variant"`

View File

@ -49,58 +49,34 @@ func TestMain(m *testing.M) {
os.Exit(run()) os.Exit(run())
} }
func testPrebuiltEtcContext(t *testing.T, bp string) (*android.TestContext, android.Config) { var prebuiltEtcFixtureFactory = android.NewFixtureFactory(
fs := map[string][]byte{ &buildDir,
android.PrepareForTestWithArchMutator,
PrepareForTestWithPrebuiltEtc,
android.FixtureMergeMockFs(android.MockFS{
"foo.conf": nil, "foo.conf": nil,
"bar.conf": nil, "bar.conf": nil,
"baz.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) { func testPrebuiltEtc(t *testing.T, bp string) (*android.TestContext, android.Config) {
t.Helper() t.Helper()
result := prebuiltEtcFixtureFactory.RunTestWithBp(t, bp)
ctx, config := testPrebuiltEtcContext(t, bp) return result.TestContext, result.Config
_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
android.FailIfErrored(t, errs)
_, errs = ctx.PrepareBuildActions(config)
android.FailIfErrored(t, errs)
return ctx, 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) { func TestPrebuiltEtcVariants(t *testing.T) {
ctx, _ := testPrebuiltEtc(t, ` ctx, _ := testPrebuiltEtc(t, `
prebuilt_etc { prebuilt_etc {
@ -227,14 +203,16 @@ func TestPrebuiltEtcRelativeInstallPathInstallDirPath(t *testing.T) {
} }
func TestPrebuiltEtcCannotSetRelativeInstallPathAndSubDir(t *testing.T) { func TestPrebuiltEtcCannotSetRelativeInstallPathAndSubDir(t *testing.T) {
testPrebuiltEtcError(t, "relative_install_path is set. Cannot set sub_dir", ` prebuiltEtcFixtureFactory.
prebuilt_etc { ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern("relative_install_path is set. Cannot set sub_dir")).
name: "foo.conf", RunTestWithBp(t, `
src: "foo.conf", prebuilt_etc {
sub_dir: "bar", name: "foo.conf",
relative_install_path: "bar", src: "foo.conf",
} sub_dir: "bar",
`) relative_install_path: "bar",
}
`)
} }
func TestPrebuiltEtcHost(t *testing.T) { func TestPrebuiltEtcHost(t *testing.T) {