From 2a2fb66d6cb99af1a78d410992b28789e8a6b710 Mon Sep 17 00:00:00 2001 From: Paul Duffin Date: Mon, 29 Mar 2021 01:14:55 +0100 Subject: [PATCH] Convert linkerconfig package to use test fixtures Bug: 181070625 Test: m nothing Change-Id: Ic85e353ad8fa2aa324cd75d5bca8a1e31fd977a8 --- linkerconfig/linkerconfig.go | 6 ++- linkerconfig/linkerconfig_test.go | 82 +++++++++---------------------- 2 files changed, 28 insertions(+), 60 deletions(-) diff --git a/linkerconfig/linkerconfig.go b/linkerconfig/linkerconfig.go index ff548e533..da80a4715 100644 --- a/linkerconfig/linkerconfig.go +++ b/linkerconfig/linkerconfig.go @@ -27,7 +27,11 @@ var ( func init() { pctx.HostBinToolVariable("conv_linker_config", "conv_linker_config") - android.RegisterModuleType("linker_config", linkerConfigFactory) + registerLinkerConfigBuildComponent(android.InitRegistrationContext) +} + +func registerLinkerConfigBuildComponent(ctx android.RegistrationContext) { + ctx.RegisterModuleType("linker_config", linkerConfigFactory) } type linkerConfigProperties struct { diff --git a/linkerconfig/linkerconfig_test.go b/linkerconfig/linkerconfig_test.go index 8eed4b5a4..939e4bbb1 100644 --- a/linkerconfig/linkerconfig_test.go +++ b/linkerconfig/linkerconfig_test.go @@ -15,65 +15,29 @@ package linkerconfig import ( - "android/soong/android" - "io/ioutil" "os" "reflect" "testing" + + "android/soong/android" ) -var buildDir string - -func setUp() { - var err error - buildDir, err = ioutil.TempDir("", "soong_etc_test") - if err != nil { - panic(err) - } -} - -func tearDown() { - os.RemoveAll(buildDir) -} - func TestMain(m *testing.M) { - run := func() int { - setUp() - defer tearDown() - - return m.Run() - } - - os.Exit(run()) + os.Exit(m.Run()) } -func testContext(t *testing.T, bp string) *android.TestContext { - t.Helper() - - fs := map[string][]byte{ - "linker.config.json": nil, - } - - config := android.TestArchConfig(buildDir, nil, bp, fs) - - ctx := android.NewTestArchContext(config) - ctx.RegisterModuleType("linker_config", linkerConfigFactory) - ctx.Register() - - _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) - android.FailIfErrored(t, errs) - _, errs = ctx.PrepareBuildActions(config) - android.FailIfErrored(t, errs) - - return ctx -} +var prepareForLinkerConfigTest = android.GroupFixturePreparers( + android.PrepareForTestWithAndroidBuildComponents, + android.FixtureRegisterWithContext(registerLinkerConfigBuildComponent), + android.FixtureAddFile("linker.config.json", nil), +) func TestBaseLinkerConfig(t *testing.T) { - ctx := testContext(t, ` - linker_config { - name: "linker-config-base", - src: "linker.config.json", - } + result := prepareForLinkerConfigTest.RunTestWithBp(t, ` + linker_config { + name: "linker-config-base", + src: "linker.config.json", + } `) expected := map[string][]string{ @@ -82,13 +46,13 @@ func TestBaseLinkerConfig(t *testing.T) { "LOCAL_INSTALLED_MODULE_STEM": {"linker.config.pb"}, } - p := ctx.ModuleForTests("linker-config-base", "android_arm64_armv8-a").Module().(*linkerConfig) + p := result.ModuleForTests("linker-config-base", "android_arm64_armv8-a").Module().(*linkerConfig) if p.outputFilePath.Base() != "linker.config.pb" { t.Errorf("expected linker.config.pb, got %q", p.outputFilePath.Base()) } - entries := android.AndroidMkEntriesForTest(t, ctx, p)[0] + entries := android.AndroidMkEntriesForTest(t, result.TestContext, p)[0] for k, expectedValue := range expected { if value, ok := entries.EntryMap[k]; ok { if !reflect.DeepEqual(value, expectedValue) { @@ -105,18 +69,18 @@ func TestBaseLinkerConfig(t *testing.T) { } func TestUninstallableLinkerConfig(t *testing.T) { - ctx := testContext(t, ` - linker_config { - name: "linker-config-base", - src: "linker.config.json", - installable: false, - } + result := prepareForLinkerConfigTest.RunTestWithBp(t, ` + linker_config { + name: "linker-config-base", + src: "linker.config.json", + installable: false, + } `) expected := []string{"true"} - p := ctx.ModuleForTests("linker-config-base", "android_arm64_armv8-a").Module().(*linkerConfig) - entries := android.AndroidMkEntriesForTest(t, ctx, p)[0] + p := result.ModuleForTests("linker-config-base", "android_arm64_armv8-a").Module().(*linkerConfig) + entries := android.AndroidMkEntriesForTest(t, result.TestContext, p)[0] if value, ok := entries.EntryMap["LOCAL_UNINSTALLABLE_MODULE"]; ok { if !reflect.DeepEqual(value, expected) { t.Errorf("LOCAL_UNINSTALLABLE_MODULE is expected to be true but %s", value)