Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 1 | // Copyright 2019 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package android |
| 16 | |
| 17 | import ( |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 18 | "testing" |
| 19 | ) |
| 20 | |
Liz Kammer | dbd4809 | 2020-09-21 22:24:17 +0000 | [diff] [blame] | 21 | type soongConfigTestDefaultsModuleProperties struct { |
| 22 | } |
| 23 | |
| 24 | type soongConfigTestDefaultsModule struct { |
| 25 | ModuleBase |
| 26 | DefaultsModuleBase |
| 27 | } |
| 28 | |
| 29 | func soongConfigTestDefaultsModuleFactory() Module { |
| 30 | m := &soongConfigTestDefaultsModule{} |
| 31 | m.AddProperties(&soongConfigTestModuleProperties{}) |
| 32 | InitDefaultsModule(m) |
| 33 | return m |
| 34 | } |
| 35 | |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 36 | type soongConfigTestModule struct { |
| 37 | ModuleBase |
Liz Kammer | dbd4809 | 2020-09-21 22:24:17 +0000 | [diff] [blame] | 38 | DefaultableModuleBase |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 39 | props soongConfigTestModuleProperties |
| 40 | } |
| 41 | |
| 42 | type soongConfigTestModuleProperties struct { |
| 43 | Cflags []string |
| 44 | } |
| 45 | |
| 46 | func soongConfigTestModuleFactory() Module { |
| 47 | m := &soongConfigTestModule{} |
| 48 | m.AddProperties(&m.props) |
| 49 | InitAndroidModule(m) |
Liz Kammer | dbd4809 | 2020-09-21 22:24:17 +0000 | [diff] [blame] | 50 | InitDefaultableModule(m) |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 51 | return m |
| 52 | } |
| 53 | |
| 54 | func (t soongConfigTestModule) GenerateAndroidBuildActions(ModuleContext) {} |
| 55 | |
| 56 | func TestSoongConfigModule(t *testing.T) { |
| 57 | configBp := ` |
| 58 | soong_config_module_type { |
Liz Kammer | dbd4809 | 2020-09-21 22:24:17 +0000 | [diff] [blame] | 59 | name: "acme_test", |
| 60 | module_type: "test", |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 61 | config_namespace: "acme", |
Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 62 | variables: ["board", "feature1", "FEATURE3", "unused_string_var"], |
| 63 | bool_variables: ["feature2", "unused_feature"], |
| 64 | value_variables: ["size", "unused_size"], |
Liz Kammer | dbd4809 | 2020-09-21 22:24:17 +0000 | [diff] [blame] | 65 | properties: ["cflags", "srcs", "defaults"], |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | soong_config_string_variable { |
| 69 | name: "board", |
Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 70 | values: ["soc_a", "soc_b", "soc_c", "soc_d"], |
| 71 | } |
| 72 | |
| 73 | soong_config_string_variable { |
| 74 | name: "unused_string_var", |
| 75 | values: ["a", "b"], |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | soong_config_bool_variable { |
| 79 | name: "feature1", |
| 80 | } |
| 81 | |
| 82 | soong_config_bool_variable { |
Colin Cross | 3beeb1e | 2020-02-05 16:27:47 -0800 | [diff] [blame] | 83 | name: "FEATURE3", |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 84 | } |
| 85 | ` |
| 86 | |
| 87 | importBp := ` |
| 88 | soong_config_module_type_import { |
| 89 | from: "SoongConfig.bp", |
Liz Kammer | dbd4809 | 2020-09-21 22:24:17 +0000 | [diff] [blame] | 90 | module_types: ["acme_test"], |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 91 | } |
| 92 | ` |
| 93 | |
| 94 | bp := ` |
Liz Kammer | dbd4809 | 2020-09-21 22:24:17 +0000 | [diff] [blame] | 95 | test_defaults { |
| 96 | name: "foo_defaults", |
| 97 | cflags: ["DEFAULT"], |
| 98 | } |
| 99 | |
| 100 | acme_test { |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 101 | name: "foo", |
| 102 | cflags: ["-DGENERIC"], |
Liz Kammer | dbd4809 | 2020-09-21 22:24:17 +0000 | [diff] [blame] | 103 | defaults: ["foo_defaults"], |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 104 | soong_config_variables: { |
| 105 | board: { |
| 106 | soc_a: { |
| 107 | cflags: ["-DSOC_A"], |
| 108 | }, |
| 109 | soc_b: { |
| 110 | cflags: ["-DSOC_B"], |
| 111 | }, |
Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 112 | soc_c: {}, |
| 113 | conditions_default: { |
| 114 | cflags: ["-DSOC_CONDITIONS_DEFAULT"], |
| 115 | }, |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 116 | }, |
Dan Willemsen | b0935db | 2020-03-23 19:42:18 -0700 | [diff] [blame] | 117 | size: { |
| 118 | cflags: ["-DSIZE=%s"], |
Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 119 | conditions_default: { |
| 120 | cflags: ["-DSIZE=CONDITIONS_DEFAULT"], |
| 121 | }, |
Dan Willemsen | b0935db | 2020-03-23 19:42:18 -0700 | [diff] [blame] | 122 | }, |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 123 | feature1: { |
Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 124 | conditions_default: { |
| 125 | cflags: ["-DF1_CONDITIONS_DEFAULT"], |
| 126 | }, |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 127 | cflags: ["-DFEATURE1"], |
| 128 | }, |
| 129 | feature2: { |
| 130 | cflags: ["-DFEATURE2"], |
Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 131 | conditions_default: { |
| 132 | cflags: ["-DF2_CONDITIONS_DEFAULT"], |
| 133 | }, |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 134 | }, |
Colin Cross | 3beeb1e | 2020-02-05 16:27:47 -0800 | [diff] [blame] | 135 | FEATURE3: { |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 136 | cflags: ["-DFEATURE3"], |
| 137 | }, |
| 138 | }, |
| 139 | } |
Liz Kammer | dbd4809 | 2020-09-21 22:24:17 +0000 | [diff] [blame] | 140 | |
| 141 | test_defaults { |
| 142 | name: "foo_defaults_a", |
| 143 | cflags: ["DEFAULT_A"], |
| 144 | } |
| 145 | |
| 146 | test_defaults { |
| 147 | name: "foo_defaults_b", |
| 148 | cflags: ["DEFAULT_B"], |
| 149 | } |
| 150 | |
| 151 | acme_test { |
| 152 | name: "foo_with_defaults", |
| 153 | cflags: ["-DGENERIC"], |
| 154 | defaults: ["foo_defaults"], |
| 155 | soong_config_variables: { |
| 156 | board: { |
| 157 | soc_a: { |
| 158 | cflags: ["-DSOC_A"], |
| 159 | defaults: ["foo_defaults_a"], |
| 160 | }, |
| 161 | soc_b: { |
| 162 | cflags: ["-DSOC_B"], |
| 163 | defaults: ["foo_defaults_b"], |
| 164 | }, |
Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 165 | soc_c: {}, |
Liz Kammer | dbd4809 | 2020-09-21 22:24:17 +0000 | [diff] [blame] | 166 | }, |
| 167 | size: { |
| 168 | cflags: ["-DSIZE=%s"], |
| 169 | }, |
| 170 | feature1: { |
| 171 | cflags: ["-DFEATURE1"], |
| 172 | }, |
| 173 | feature2: { |
| 174 | cflags: ["-DFEATURE2"], |
| 175 | }, |
| 176 | FEATURE3: { |
| 177 | cflags: ["-DFEATURE3"], |
| 178 | }, |
| 179 | }, |
| 180 | } |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 181 | ` |
| 182 | |
Paul Duffin | 791302b | 2021-03-16 22:45:14 +0000 | [diff] [blame] | 183 | fixtureForVendorVars := func(vars map[string]map[string]string) FixturePreparer { |
| 184 | return FixtureModifyProductVariables(func(variables FixtureProductVariables) { |
| 185 | variables.VendorVars = vars |
| 186 | }) |
| 187 | } |
| 188 | |
| 189 | run := func(t *testing.T, bp string, fs MockFS) { |
Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 190 | testCases := []struct { |
| 191 | name string |
Paul Duffin | 791302b | 2021-03-16 22:45:14 +0000 | [diff] [blame] | 192 | preparer FixturePreparer |
Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 193 | fooExpectedFlags []string |
| 194 | fooDefaultsExpectedFlags []string |
| 195 | }{ |
| 196 | { |
| 197 | name: "withValues", |
Paul Duffin | 791302b | 2021-03-16 22:45:14 +0000 | [diff] [blame] | 198 | preparer: fixtureForVendorVars(map[string]map[string]string{ |
| 199 | "acme": { |
Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 200 | "board": "soc_a", |
| 201 | "size": "42", |
| 202 | "feature1": "true", |
| 203 | "feature2": "false", |
| 204 | // FEATURE3 unset |
| 205 | "unused_feature": "true", // unused |
| 206 | "unused_size": "1", // unused |
| 207 | "unused_string_var": "a", // unused |
| 208 | }, |
| 209 | }), |
| 210 | fooExpectedFlags: []string{ |
| 211 | "DEFAULT", |
| 212 | "-DGENERIC", |
| 213 | "-DF2_CONDITIONS_DEFAULT", |
| 214 | "-DSIZE=42", |
| 215 | "-DSOC_A", |
| 216 | "-DFEATURE1", |
| 217 | }, |
| 218 | fooDefaultsExpectedFlags: []string{ |
| 219 | "DEFAULT_A", |
| 220 | "DEFAULT", |
| 221 | "-DGENERIC", |
| 222 | "-DSIZE=42", |
| 223 | "-DSOC_A", |
| 224 | "-DFEATURE1", |
| 225 | }, |
| 226 | }, |
| 227 | { |
| 228 | name: "empty_prop_for_string_var", |
Paul Duffin | 791302b | 2021-03-16 22:45:14 +0000 | [diff] [blame] | 229 | preparer: fixtureForVendorVars(map[string]map[string]string{ |
| 230 | "acme": {"board": "soc_c"}}), |
Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 231 | fooExpectedFlags: []string{ |
| 232 | "DEFAULT", |
| 233 | "-DGENERIC", |
| 234 | "-DF2_CONDITIONS_DEFAULT", |
| 235 | "-DSIZE=CONDITIONS_DEFAULT", |
| 236 | "-DF1_CONDITIONS_DEFAULT", |
| 237 | }, |
| 238 | fooDefaultsExpectedFlags: []string{ |
| 239 | "DEFAULT", |
| 240 | "-DGENERIC", |
| 241 | }, |
| 242 | }, |
| 243 | { |
| 244 | name: "unused_string_var", |
Paul Duffin | 791302b | 2021-03-16 22:45:14 +0000 | [diff] [blame] | 245 | preparer: fixtureForVendorVars(map[string]map[string]string{ |
| 246 | "acme": {"board": "soc_d"}}), |
Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 247 | fooExpectedFlags: []string{ |
| 248 | "DEFAULT", |
| 249 | "-DGENERIC", |
| 250 | "-DF2_CONDITIONS_DEFAULT", |
| 251 | "-DSIZE=CONDITIONS_DEFAULT", |
| 252 | "-DSOC_CONDITIONS_DEFAULT", // foo does not contain a prop "soc_d", so we use the default |
| 253 | "-DF1_CONDITIONS_DEFAULT", |
| 254 | }, |
| 255 | fooDefaultsExpectedFlags: []string{ |
| 256 | "DEFAULT", |
| 257 | "-DGENERIC", |
| 258 | }, |
| 259 | }, |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 260 | |
Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 261 | { |
Paul Duffin | 791302b | 2021-03-16 22:45:14 +0000 | [diff] [blame] | 262 | name: "conditions_default", |
| 263 | preparer: fixtureForVendorVars(map[string]map[string]string{}), |
Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 264 | fooExpectedFlags: []string{ |
| 265 | "DEFAULT", |
| 266 | "-DGENERIC", |
| 267 | "-DF2_CONDITIONS_DEFAULT", |
| 268 | "-DSIZE=CONDITIONS_DEFAULT", |
| 269 | "-DSOC_CONDITIONS_DEFAULT", |
| 270 | "-DF1_CONDITIONS_DEFAULT", |
| 271 | }, |
| 272 | fooDefaultsExpectedFlags: []string{ |
| 273 | "DEFAULT", |
| 274 | "-DGENERIC", |
| 275 | }, |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 276 | }, |
| 277 | } |
| 278 | |
Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 279 | for _, tc := range testCases { |
Paul Duffin | 791302b | 2021-03-16 22:45:14 +0000 | [diff] [blame] | 280 | t.Run(tc.name, func(t *testing.T) { |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 281 | result := GroupFixturePreparers( |
Paul Duffin | 791302b | 2021-03-16 22:45:14 +0000 | [diff] [blame] | 282 | tc.preparer, |
| 283 | PrepareForTestWithDefaults, |
| 284 | FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 285 | ctx.RegisterModuleType("soong_config_module_type_import", soongConfigModuleTypeImportFactory) |
| 286 | ctx.RegisterModuleType("soong_config_module_type", soongConfigModuleTypeFactory) |
| 287 | ctx.RegisterModuleType("soong_config_string_variable", soongConfigStringVariableDummyFactory) |
| 288 | ctx.RegisterModuleType("soong_config_bool_variable", soongConfigBoolVariableDummyFactory) |
| 289 | ctx.RegisterModuleType("test_defaults", soongConfigTestDefaultsModuleFactory) |
| 290 | ctx.RegisterModuleType("test", soongConfigTestModuleFactory) |
| 291 | }), |
| 292 | fs.AddToFixture(), |
| 293 | FixtureWithRootAndroidBp(bp), |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 294 | ).RunTest(t) |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 295 | |
Paul Duffin | 791302b | 2021-03-16 22:45:14 +0000 | [diff] [blame] | 296 | foo := result.ModuleForTests("foo", "").Module().(*soongConfigTestModule) |
| 297 | AssertDeepEquals(t, "foo cflags", tc.fooExpectedFlags, foo.props.Cflags) |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 298 | |
Paul Duffin | 791302b | 2021-03-16 22:45:14 +0000 | [diff] [blame] | 299 | fooDefaults := result.ModuleForTests("foo_with_defaults", "").Module().(*soongConfigTestModule) |
| 300 | AssertDeepEquals(t, "foo_with_defaults cflags", tc.fooDefaultsExpectedFlags, fooDefaults.props.Cflags) |
| 301 | }) |
Liz Kammer | dbd4809 | 2020-09-21 22:24:17 +0000 | [diff] [blame] | 302 | } |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | t.Run("single file", func(t *testing.T) { |
| 306 | run(t, configBp+bp, nil) |
| 307 | }) |
| 308 | |
| 309 | t.Run("import", func(t *testing.T) { |
| 310 | run(t, importBp+bp, map[string][]byte{ |
| 311 | "SoongConfig.bp": []byte(configBp), |
| 312 | }) |
| 313 | }) |
| 314 | } |
Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 315 | |
| 316 | func testConfigWithVendorVars(buildDir, bp string, fs map[string][]byte, vendorVars map[string]map[string]string) Config { |
| 317 | config := TestConfig(buildDir, nil, bp, fs) |
| 318 | |
| 319 | config.TestProductVariables.VendorVars = vendorVars |
| 320 | |
| 321 | return config |
| 322 | } |