diff options
Diffstat (limited to 'android/soong_config_modules_test.go')
-rw-r--r-- | android/soong_config_modules_test.go | 249 |
1 files changed, 214 insertions, 35 deletions
diff --git a/android/soong_config_modules_test.go b/android/soong_config_modules_test.go index f905b1ab2..8f252d944 100644 --- a/android/soong_config_modules_test.go +++ b/android/soong_config_modules_test.go @@ -15,12 +15,27 @@ package android import ( - "reflect" "testing" ) +type soongConfigTestDefaultsModuleProperties struct { +} + +type soongConfigTestDefaultsModule struct { + ModuleBase + DefaultsModuleBase +} + +func soongConfigTestDefaultsModuleFactory() Module { + m := &soongConfigTestDefaultsModule{} + m.AddProperties(&soongConfigTestModuleProperties{}) + InitDefaultsModule(m) + return m +} + type soongConfigTestModule struct { ModuleBase + DefaultableModuleBase props soongConfigTestModuleProperties } @@ -32,6 +47,7 @@ func soongConfigTestModuleFactory() Module { m := &soongConfigTestModule{} m.AddProperties(&m.props) InitAndroidModule(m) + InitDefaultableModule(m) return m } @@ -40,18 +56,23 @@ func (t soongConfigTestModule) GenerateAndroidBuildActions(ModuleContext) {} func TestSoongConfigModule(t *testing.T) { configBp := ` soong_config_module_type { - name: "acme_test_defaults", - module_type: "test_defaults", + name: "acme_test", + module_type: "test", config_namespace: "acme", - variables: ["board", "feature1", "FEATURE3"], - bool_variables: ["feature2"], - value_variables: ["size"], - properties: ["cflags", "srcs"], + variables: ["board", "feature1", "FEATURE3", "unused_string_var"], + bool_variables: ["feature2", "unused_feature"], + value_variables: ["size", "unused_size"], + properties: ["cflags", "srcs", "defaults"], } soong_config_string_variable { name: "board", - values: ["soc_a", "soc_b"], + values: ["soc_a", "soc_b", "soc_c", "soc_d"], + } + + soong_config_string_variable { + name: "unused_string_var", + values: ["a", "b"], } soong_config_bool_variable { @@ -66,14 +87,20 @@ func TestSoongConfigModule(t *testing.T) { importBp := ` soong_config_module_type_import { from: "SoongConfig.bp", - module_types: ["acme_test_defaults"], + module_types: ["acme_test"], } ` bp := ` - acme_test_defaults { + test_defaults { + name: "foo_defaults", + cflags: ["DEFAULT"], + } + + acme_test { name: "foo", cflags: ["-DGENERIC"], + defaults: ["foo_defaults"], soong_config_variables: { board: { soc_a: { @@ -82,6 +109,60 @@ func TestSoongConfigModule(t *testing.T) { soc_b: { cflags: ["-DSOC_B"], }, + soc_c: {}, + conditions_default: { + cflags: ["-DSOC_CONDITIONS_DEFAULT"], + }, + }, + size: { + cflags: ["-DSIZE=%s"], + conditions_default: { + cflags: ["-DSIZE=CONDITIONS_DEFAULT"], + }, + }, + feature1: { + conditions_default: { + cflags: ["-DF1_CONDITIONS_DEFAULT"], + }, + cflags: ["-DFEATURE1"], + }, + feature2: { + cflags: ["-DFEATURE2"], + conditions_default: { + cflags: ["-DF2_CONDITIONS_DEFAULT"], + }, + }, + FEATURE3: { + cflags: ["-DFEATURE3"], + }, + }, + } + + test_defaults { + name: "foo_defaults_a", + cflags: ["DEFAULT_A"], + } + + test_defaults { + name: "foo_defaults_b", + cflags: ["DEFAULT_B"], + } + + acme_test { + name: "foo_with_defaults", + cflags: ["-DGENERIC"], + defaults: ["foo_defaults"], + soong_config_variables: { + board: { + soc_a: { + cflags: ["-DSOC_A"], + defaults: ["foo_defaults_a"], + }, + soc_b: { + cflags: ["-DSOC_B"], + defaults: ["foo_defaults_b"], + }, + soc_c: {}, }, size: { cflags: ["-DSIZE=%s"], @@ -99,35 +180,125 @@ func TestSoongConfigModule(t *testing.T) { } ` - run := func(t *testing.T, bp string, fs map[string][]byte) { - config := TestConfig(buildDir, nil, bp, fs) + fixtureForVendorVars := func(vars map[string]map[string]string) FixturePreparer { + return FixtureModifyProductVariables(func(variables FixtureProductVariables) { + variables.VendorVars = vars + }) + } + + run := func(t *testing.T, bp string, fs MockFS) { + testCases := []struct { + name string + preparer FixturePreparer + fooExpectedFlags []string + fooDefaultsExpectedFlags []string + }{ + { + name: "withValues", + preparer: fixtureForVendorVars(map[string]map[string]string{ + "acme": { + "board": "soc_a", + "size": "42", + "feature1": "true", + "feature2": "false", + // FEATURE3 unset + "unused_feature": "true", // unused + "unused_size": "1", // unused + "unused_string_var": "a", // unused + }, + }), + fooExpectedFlags: []string{ + "DEFAULT", + "-DGENERIC", + "-DF2_CONDITIONS_DEFAULT", + "-DSIZE=42", + "-DSOC_A", + "-DFEATURE1", + }, + fooDefaultsExpectedFlags: []string{ + "DEFAULT_A", + "DEFAULT", + "-DGENERIC", + "-DSIZE=42", + "-DSOC_A", + "-DFEATURE1", + }, + }, + { + name: "empty_prop_for_string_var", + preparer: fixtureForVendorVars(map[string]map[string]string{ + "acme": {"board": "soc_c"}}), + fooExpectedFlags: []string{ + "DEFAULT", + "-DGENERIC", + "-DF2_CONDITIONS_DEFAULT", + "-DSIZE=CONDITIONS_DEFAULT", + "-DF1_CONDITIONS_DEFAULT", + }, + fooDefaultsExpectedFlags: []string{ + "DEFAULT", + "-DGENERIC", + }, + }, + { + name: "unused_string_var", + preparer: fixtureForVendorVars(map[string]map[string]string{ + "acme": {"board": "soc_d"}}), + fooExpectedFlags: []string{ + "DEFAULT", + "-DGENERIC", + "-DF2_CONDITIONS_DEFAULT", + "-DSIZE=CONDITIONS_DEFAULT", + "-DSOC_CONDITIONS_DEFAULT", // foo does not contain a prop "soc_d", so we use the default + "-DF1_CONDITIONS_DEFAULT", + }, + fooDefaultsExpectedFlags: []string{ + "DEFAULT", + "-DGENERIC", + }, + }, - config.TestProductVariables.VendorVars = map[string]map[string]string{ - "acme": map[string]string{ - "board": "soc_a", - "size": "42", - "feature1": "true", - "feature2": "false", - // FEATURE3 unset + { + name: "conditions_default", + preparer: fixtureForVendorVars(map[string]map[string]string{}), + fooExpectedFlags: []string{ + "DEFAULT", + "-DGENERIC", + "-DF2_CONDITIONS_DEFAULT", + "-DSIZE=CONDITIONS_DEFAULT", + "-DSOC_CONDITIONS_DEFAULT", + "-DF1_CONDITIONS_DEFAULT", + }, + fooDefaultsExpectedFlags: []string{ + "DEFAULT", + "-DGENERIC", + }, }, } - ctx := NewTestContext() - ctx.RegisterModuleType("soong_config_module_type_import", soongConfigModuleTypeImportFactory) - ctx.RegisterModuleType("soong_config_module_type", soongConfigModuleTypeFactory) - ctx.RegisterModuleType("soong_config_string_variable", soongConfigStringVariableDummyFactory) - ctx.RegisterModuleType("soong_config_bool_variable", soongConfigBoolVariableDummyFactory) - ctx.RegisterModuleType("test_defaults", soongConfigTestModuleFactory) - ctx.Register(config) - - _, errs := ctx.ParseBlueprintsFiles("Android.bp") - FailIfErrored(t, errs) - _, errs = ctx.PrepareBuildActions(config) - FailIfErrored(t, errs) - - foo := ctx.ModuleForTests("foo", "").Module().(*soongConfigTestModule) - if g, w := foo.props.Cflags, []string{"-DGENERIC", "-DSIZE=42", "-DSOC_A", "-DFEATURE1"}; !reflect.DeepEqual(g, w) { - t.Errorf("wanted foo cflags %q, got %q", w, g) + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + result := GroupFixturePreparers( + tc.preparer, + PrepareForTestWithDefaults, + FixtureRegisterWithContext(func(ctx RegistrationContext) { + ctx.RegisterModuleType("soong_config_module_type_import", soongConfigModuleTypeImportFactory) + ctx.RegisterModuleType("soong_config_module_type", soongConfigModuleTypeFactory) + ctx.RegisterModuleType("soong_config_string_variable", soongConfigStringVariableDummyFactory) + ctx.RegisterModuleType("soong_config_bool_variable", soongConfigBoolVariableDummyFactory) + ctx.RegisterModuleType("test_defaults", soongConfigTestDefaultsModuleFactory) + ctx.RegisterModuleType("test", soongConfigTestModuleFactory) + }), + fs.AddToFixture(), + FixtureWithRootAndroidBp(bp), + ).RunTest(t) + + foo := result.ModuleForTests("foo", "").Module().(*soongConfigTestModule) + AssertDeepEquals(t, "foo cflags", tc.fooExpectedFlags, foo.props.Cflags) + + fooDefaults := result.ModuleForTests("foo_with_defaults", "").Module().(*soongConfigTestModule) + AssertDeepEquals(t, "foo_with_defaults cflags", tc.fooDefaultsExpectedFlags, fooDefaults.props.Cflags) + }) } } @@ -141,3 +312,11 @@ func TestSoongConfigModule(t *testing.T) { }) }) } + +func testConfigWithVendorVars(buildDir, bp string, fs map[string][]byte, vendorVars map[string]map[string]string) Config { + config := TestConfig(buildDir, nil, bp, fs) + + config.TestProductVariables.VendorVars = vendorVars + + return config +} |