From 9e0c3f9993cb9545f6bd5fe22e8046564f771176 Mon Sep 17 00:00:00 2001 From: Paul Duffin Date: Tue, 30 Mar 2021 22:45:21 +0100 Subject: [PATCH] Convert remaining rust tests to use test fixtures Removes now unused CreateTestContext and stops exporting the RegisterRequiredBuildComponentsForTest() method as it is only used locally. Bug: 181070625 Test: m nothing Change-Id: Ia2fd3e090f975ee4c807da2c6c162ef3cf7ac446 --- rust/clippy_test.go | 37 ++++++++++--------------------------- rust/compiler_test.go | 41 +++++++++++------------------------------ rust/testing.go | 16 ++-------------- 3 files changed, 23 insertions(+), 71 deletions(-) diff --git a/rust/clippy_test.go b/rust/clippy_test.go index e90564f63..bd3bfb151 100644 --- a/rust/clippy_test.go +++ b/rust/clippy_test.go @@ -18,7 +18,6 @@ import ( "testing" "android/soong/android" - "android/soong/cc" ) func TestClippy(t *testing.T) { @@ -45,15 +44,6 @@ func TestClippy(t *testing.T) { clippy_lints: "none", }` - bp = bp + GatherRequiredDepsForTest() - bp = bp + cc.GatherRequiredDepsForTest(android.NoOsType) - - fs := map[string][]byte{ - // Reuse the same blueprint file for subdirectories. - "external/Android.bp": []byte(bp), - "hardware/Android.bp": []byte(bp), - } - var clippyLintTests = []struct { modulePath string fooFlags string @@ -66,29 +56,22 @@ func TestClippy(t *testing.T) { for _, tc := range clippyLintTests { t.Run("path="+tc.modulePath, func(t *testing.T) { - config := android.TestArchConfig(t.TempDir(), nil, bp, fs) - ctx := CreateTestContext(config) - ctx.Register() - _, errs := ctx.ParseFileList(".", []string{tc.modulePath + "Android.bp"}) - android.FailIfErrored(t, errs) - _, errs = ctx.PrepareBuildActions(config) - android.FailIfErrored(t, errs) + result := android.GroupFixturePreparers( + prepareForRustTest, + // Test with the blueprint file in different directories. + android.FixtureAddTextFile(tc.modulePath+"Android.bp", bp), + ).RunTest(t) - r := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib").MaybeRule("clippy") - if r.Args["clippyFlags"] != tc.fooFlags { - t.Errorf("Incorrect flags for libfoo: %q, want %q", r.Args["clippyFlags"], tc.fooFlags) - } + r := result.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib").MaybeRule("clippy") + android.AssertStringEquals(t, "libfoo flags", tc.fooFlags, r.Args["clippyFlags"]) - r = ctx.ModuleForTests("libbar", "android_arm64_armv8-a_dylib").MaybeRule("clippy") - if r.Args["clippyFlags"] != "${config.ClippyDefaultLints}" { - t.Errorf("Incorrect flags for libbar: %q, want %q", r.Args["clippyFlags"], "${config.ClippyDefaultLints}") - } + r = result.ModuleForTests("libbar", "android_arm64_armv8-a_dylib").MaybeRule("clippy") + android.AssertStringEquals(t, "libbar flags", "${config.ClippyDefaultLints}", r.Args["clippyFlags"]) - r = ctx.ModuleForTests("libfoobar", "android_arm64_armv8-a_dylib").MaybeRule("clippy") + r = result.ModuleForTests("libfoobar", "android_arm64_armv8-a_dylib").MaybeRule("clippy") if r.Rule != nil { t.Errorf("libfoobar is setup to use clippy when explicitly disabled: clippyFlags=%q", r.Args["clippyFlags"]) } - }) } } diff --git a/rust/compiler_test.go b/rust/compiler_test.go index 3ed086f20..c75276239 100644 --- a/rust/compiler_test.go +++ b/rust/compiler_test.go @@ -19,7 +19,6 @@ import ( "testing" "android/soong/android" - "android/soong/cc" ) // Test that feature flags are being correctly generated. @@ -132,15 +131,6 @@ func TestLints(t *testing.T) { lints: "none", }` - bp = bp + GatherRequiredDepsForTest() - bp = bp + cc.GatherRequiredDepsForTest(android.NoOsType) - - fs := map[string][]byte{ - // Reuse the same blueprint file for subdirectories. - "external/Android.bp": []byte(bp), - "hardware/Android.bp": []byte(bp), - } - var lintTests = []struct { modulePath string fooFlags string @@ -153,29 +143,20 @@ func TestLints(t *testing.T) { for _, tc := range lintTests { t.Run("path="+tc.modulePath, func(t *testing.T) { - config := android.TestArchConfig(t.TempDir(), nil, bp, fs) - ctx := CreateTestContext(config) - ctx.Register() - _, errs := ctx.ParseFileList(".", []string{tc.modulePath + "Android.bp"}) - android.FailIfErrored(t, errs) - _, errs = ctx.PrepareBuildActions(config) - android.FailIfErrored(t, errs) + result := android.GroupFixturePreparers( + prepareForRustTest, + // Test with the blueprint file in different directories. + android.FixtureAddTextFile(tc.modulePath+"Android.bp", bp), + ).RunTest(t) - r := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib").MaybeRule("rustc") - if !strings.Contains(r.Args["rustcFlags"], tc.fooFlags) { - t.Errorf("Incorrect flags for libfoo: %q, want %q", r.Args["rustcFlags"], tc.fooFlags) - } + r := result.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib").MaybeRule("rustc") + android.AssertStringDoesContain(t, "libfoo flags", r.Args["rustcFlags"], tc.fooFlags) - r = ctx.ModuleForTests("libbar", "android_arm64_armv8-a_dylib").MaybeRule("rustc") - if !strings.Contains(r.Args["rustcFlags"], "${config.RustDefaultLints}") { - t.Errorf("Incorrect flags for libbar: %q, want %q", r.Args["rustcFlags"], "${config.RustDefaultLints}") - } - - r = ctx.ModuleForTests("libfoobar", "android_arm64_armv8-a_dylib").MaybeRule("rustc") - if !strings.Contains(r.Args["rustcFlags"], "${config.RustAllowAllLints}") { - t.Errorf("Incorrect flags for libfoobar: %q, want %q", r.Args["rustcFlags"], "${config.RustAllowAllLints}") - } + r = result.ModuleForTests("libbar", "android_arm64_armv8-a_dylib").MaybeRule("rustc") + android.AssertStringDoesContain(t, "libbar flags", r.Args["rustcFlags"], "${config.RustDefaultLints}") + r = result.ModuleForTests("libfoobar", "android_arm64_armv8-a_dylib").MaybeRule("rustc") + android.AssertStringDoesContain(t, "libfoobar flags", r.Args["rustcFlags"], "${config.RustAllowAllLints}") }) } } diff --git a/rust/testing.go b/rust/testing.go index 5be71c90e..75adcfce9 100644 --- a/rust/testing.go +++ b/rust/testing.go @@ -17,13 +17,12 @@ package rust import ( "android/soong/android" "android/soong/cc" - "android/soong/genrule" ) // Preparer that will define all cc module types and a limited set of mutators and singletons that // make those module types usable. var PrepareForTestWithRustBuildComponents = android.GroupFixturePreparers( - android.FixtureRegisterWithContext(RegisterRequiredBuildComponentsForTest), + android.FixtureRegisterWithContext(registerRequiredBuildComponentsForTest), ) // The directory in which rust test default modules will be defined. @@ -197,7 +196,7 @@ func GatherRequiredDepsForTest() string { return bp } -func RegisterRequiredBuildComponentsForTest(ctx android.RegistrationContext) { +func registerRequiredBuildComponentsForTest(ctx android.RegistrationContext) { ctx.RegisterModuleType("rust_binary", RustBinaryFactory) ctx.RegisterModuleType("rust_binary_host", RustBinaryHostFactory) ctx.RegisterModuleType("rust_bindgen", RustBindgenFactory) @@ -231,14 +230,3 @@ func RegisterRequiredBuildComponentsForTest(ctx android.RegistrationContext) { }) ctx.RegisterSingletonType("rust_project_generator", rustProjectGeneratorSingleton) } - -func CreateTestContext(config android.Config) *android.TestContext { - ctx := android.NewTestArchContext(config) - android.RegisterPrebuiltMutators(ctx) - ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators) - genrule.RegisterGenruleBuildComponents(ctx) - cc.RegisterRequiredBuildComponentsForTest(ctx) - RegisterRequiredBuildComponentsForTest(ctx) - - return ctx -}