Colin Cross | 0e99175 | 2019-06-10 15:41:28 -0700 | [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 ( |
| 18 | "testing" |
Colin Cross | 0e99175 | 2019-06-10 15:41:28 -0700 | [diff] [blame] | 19 | ) |
| 20 | |
| 21 | type defaultsTestProperties struct { |
Paul Duffin | 7999627 | 2022-05-04 11:39:52 +0000 | [diff] [blame] | 22 | Foo []string |
| 23 | Bar []string |
| 24 | Nested struct { |
| 25 | Fizz *bool |
| 26 | } |
| 27 | Other struct { |
| 28 | Buzz *string |
| 29 | } |
Colin Cross | 0e99175 | 2019-06-10 15:41:28 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | type defaultsTestModule struct { |
| 33 | ModuleBase |
| 34 | DefaultableModuleBase |
| 35 | properties defaultsTestProperties |
| 36 | } |
| 37 | |
| 38 | func (d *defaultsTestModule) GenerateAndroidBuildActions(ctx ModuleContext) { |
| 39 | ctx.Build(pctx, BuildParams{ |
| 40 | Rule: Touch, |
| 41 | Output: PathForModuleOut(ctx, "out"), |
| 42 | }) |
| 43 | } |
| 44 | |
| 45 | func defaultsTestModuleFactory() Module { |
| 46 | module := &defaultsTestModule{} |
| 47 | module.AddProperties(&module.properties) |
Colin Cross | 0e99175 | 2019-06-10 15:41:28 -0700 | [diff] [blame] | 48 | InitAndroidModule(module) |
Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 49 | InitDefaultableModule(module) |
Colin Cross | 0e99175 | 2019-06-10 15:41:28 -0700 | [diff] [blame] | 50 | return module |
| 51 | } |
| 52 | |
| 53 | type defaultsTestDefaults struct { |
| 54 | ModuleBase |
| 55 | DefaultsModuleBase |
| 56 | } |
| 57 | |
| 58 | func defaultsTestDefaultsFactory() Module { |
| 59 | defaults := &defaultsTestDefaults{} |
| 60 | defaults.AddProperties(&defaultsTestProperties{}) |
| 61 | InitDefaultsModule(defaults) |
| 62 | return defaults |
| 63 | } |
| 64 | |
Paul Duffin | 7c166b4 | 2021-03-16 19:31:17 +0000 | [diff] [blame] | 65 | var prepareForDefaultsTest = GroupFixturePreparers( |
| 66 | PrepareForTestWithDefaults, |
| 67 | FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 68 | ctx.RegisterModuleType("test", defaultsTestModuleFactory) |
| 69 | ctx.RegisterModuleType("defaults", defaultsTestDefaultsFactory) |
| 70 | }), |
| 71 | ) |
| 72 | |
Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 73 | func TestDefaults(t *testing.T) { |
| 74 | bp := ` |
| 75 | defaults { |
| 76 | name: "transitive", |
| 77 | foo: ["transitive"], |
| 78 | } |
| 79 | |
| 80 | defaults { |
| 81 | name: "defaults", |
| 82 | defaults: ["transitive"], |
| 83 | foo: ["defaults"], |
| 84 | } |
| 85 | |
| 86 | test { |
| 87 | name: "foo", |
| 88 | defaults: ["defaults"], |
| 89 | foo: ["module"], |
| 90 | } |
| 91 | ` |
| 92 | |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 93 | result := GroupFixturePreparers( |
Paul Duffin | 7c166b4 | 2021-03-16 19:31:17 +0000 | [diff] [blame] | 94 | prepareForDefaultsTest, |
| 95 | FixtureWithRootAndroidBp(bp), |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 96 | ).RunTest(t) |
Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 97 | |
Paul Duffin | 7c166b4 | 2021-03-16 19:31:17 +0000 | [diff] [blame] | 98 | foo := result.Module("foo", "").(*defaultsTestModule) |
Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 99 | |
Paul Duffin | 7c166b4 | 2021-03-16 19:31:17 +0000 | [diff] [blame] | 100 | AssertDeepEquals(t, "foo", []string{"transitive", "defaults", "module"}, foo.properties.Foo) |
Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Colin Cross | 0e99175 | 2019-06-10 15:41:28 -0700 | [diff] [blame] | 103 | func TestDefaultsAllowMissingDependencies(t *testing.T) { |
Colin Cross | 0e99175 | 2019-06-10 15:41:28 -0700 | [diff] [blame] | 104 | bp := ` |
| 105 | defaults { |
| 106 | name: "defaults", |
| 107 | defaults: ["missing"], |
| 108 | foo: ["defaults"], |
| 109 | } |
| 110 | |
| 111 | test { |
| 112 | name: "missing_defaults", |
| 113 | defaults: ["missing"], |
| 114 | foo: ["module"], |
| 115 | } |
| 116 | |
| 117 | test { |
| 118 | name: "missing_transitive_defaults", |
| 119 | defaults: ["defaults"], |
| 120 | foo: ["module"], |
| 121 | } |
| 122 | ` |
| 123 | |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 124 | result := GroupFixturePreparers( |
Paul Duffin | 7c166b4 | 2021-03-16 19:31:17 +0000 | [diff] [blame] | 125 | prepareForDefaultsTest, |
| 126 | PrepareForTestWithAllowMissingDependencies, |
| 127 | FixtureWithRootAndroidBp(bp), |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 128 | ).RunTest(t) |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 129 | |
Paul Duffin | 7c166b4 | 2021-03-16 19:31:17 +0000 | [diff] [blame] | 130 | missingDefaults := result.ModuleForTests("missing_defaults", "").Output("out") |
| 131 | missingTransitiveDefaults := result.ModuleForTests("missing_transitive_defaults", "").Output("out") |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 132 | |
Paul Duffin | 7c166b4 | 2021-03-16 19:31:17 +0000 | [diff] [blame] | 133 | AssertSame(t, "missing_defaults rule", ErrorRule, missingDefaults.Rule) |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 134 | |
Paul Duffin | 7c166b4 | 2021-03-16 19:31:17 +0000 | [diff] [blame] | 135 | AssertStringEquals(t, "missing_defaults", "module missing_defaults missing dependencies: missing\n", missingDefaults.Args["error"]) |
Colin Cross | 0e99175 | 2019-06-10 15:41:28 -0700 | [diff] [blame] | 136 | |
| 137 | // TODO: missing transitive defaults is currently not handled |
| 138 | _ = missingTransitiveDefaults |
| 139 | } |
Paul Duffin | 7999627 | 2022-05-04 11:39:52 +0000 | [diff] [blame] | 140 | |
| 141 | func TestProtectedProperties_ProtectedPropertyNotSet(t *testing.T) { |
| 142 | bp := ` |
| 143 | defaults { |
| 144 | name: "transitive", |
| 145 | protected_properties: ["foo"], |
| 146 | } |
| 147 | ` |
| 148 | |
| 149 | GroupFixturePreparers( |
| 150 | prepareForDefaultsTest, |
| 151 | FixtureWithRootAndroidBp(bp), |
| 152 | ).ExtendWithErrorHandler(FixtureExpectsAtLeastOneErrorMatchingPattern( |
| 153 | "module \"transitive\": foo: is not set; protected properties must be explicitly set")). |
| 154 | RunTest(t) |
| 155 | } |
| 156 | |
| 157 | func TestProtectedProperties_ProtectedPropertyNotLeaf(t *testing.T) { |
| 158 | bp := ` |
| 159 | defaults { |
| 160 | name: "transitive", |
| 161 | protected_properties: ["nested"], |
| 162 | nested: { |
| 163 | fizz: true, |
| 164 | }, |
| 165 | } |
| 166 | ` |
| 167 | |
| 168 | GroupFixturePreparers( |
| 169 | prepareForDefaultsTest, |
| 170 | FixtureWithRootAndroidBp(bp), |
| 171 | ).ExtendWithErrorHandler(FixtureExpectsAtLeastOneErrorMatchingPattern( |
| 172 | `\Qmodule "transitive": nested: property is not supported by this module type "defaults"\E`)). |
| 173 | RunTest(t) |
| 174 | } |
| 175 | |
| 176 | // TestProtectedProperties_ApplyDefaults makes sure that the protected_properties property has |
| 177 | // defaults applied. |
| 178 | func TestProtectedProperties_HasDefaultsApplied(t *testing.T) { |
| 179 | |
| 180 | bp := ` |
| 181 | defaults { |
| 182 | name: "transitive", |
| 183 | protected_properties: ["foo"], |
| 184 | foo: ["transitive"], |
| 185 | } |
| 186 | |
| 187 | defaults { |
| 188 | name: "defaults", |
| 189 | defaults: ["transitive"], |
| 190 | protected_properties: ["bar"], |
| 191 | bar: ["defaults"], |
| 192 | } |
| 193 | ` |
| 194 | |
| 195 | result := GroupFixturePreparers( |
| 196 | prepareForDefaultsTest, |
| 197 | FixtureWithRootAndroidBp(bp), |
| 198 | ).RunTest(t) |
| 199 | |
| 200 | defaults := result.Module("defaults", "").(DefaultsModule) |
| 201 | AssertDeepEquals(t, "defaults protected properties", []string{"foo", "bar"}, defaults.protectedProperties()) |
| 202 | } |
| 203 | |
| 204 | // TestProtectedProperties_ProtectAllProperties makes sure that protected_properties: ["*"] protects |
| 205 | // all properties. |
| 206 | func TestProtectedProperties_ProtectAllProperties(t *testing.T) { |
| 207 | |
| 208 | bp := ` |
| 209 | defaults { |
| 210 | name: "transitive", |
| 211 | protected_properties: ["other.buzz"], |
| 212 | other: { |
| 213 | buzz: "transitive", |
| 214 | }, |
| 215 | } |
| 216 | |
| 217 | defaults { |
| 218 | name: "defaults", |
| 219 | defaults: ["transitive"], |
| 220 | visibility: ["//visibility:private"], |
| 221 | protected_properties: ["*"], |
| 222 | foo: ["other"], |
| 223 | bar: ["defaults"], |
| 224 | nested: { |
| 225 | fizz: true, |
| 226 | } |
| 227 | } |
| 228 | ` |
| 229 | |
| 230 | result := GroupFixturePreparers( |
| 231 | prepareForDefaultsTest, |
| 232 | FixtureWithRootAndroidBp(bp), |
| 233 | ).RunTest(t) |
| 234 | |
| 235 | defaults := result.Module("defaults", "").(DefaultsModule) |
| 236 | AssertDeepEquals(t, "defaults protected properties", []string{"other.buzz", "bar", "foo", "nested.fizz"}, |
| 237 | defaults.protectedProperties()) |
| 238 | } |
| 239 | |
| 240 | func TestProtectedProperties_DetectedOverride(t *testing.T) { |
| 241 | bp := ` |
| 242 | defaults { |
| 243 | name: "defaults", |
| 244 | protected_properties: ["foo", "nested.fizz"], |
| 245 | foo: ["defaults"], |
| 246 | nested: { |
| 247 | fizz: true, |
| 248 | }, |
| 249 | } |
| 250 | |
| 251 | test { |
| 252 | name: "foo", |
| 253 | defaults: ["defaults"], |
| 254 | foo: ["module"], |
| 255 | nested: { |
| 256 | fizz: false, |
| 257 | }, |
| 258 | } |
| 259 | ` |
| 260 | |
| 261 | GroupFixturePreparers( |
| 262 | prepareForDefaultsTest, |
| 263 | FixtureWithRootAndroidBp(bp), |
| 264 | ).ExtendWithErrorHandler(FixtureExpectsAllErrorsToMatchAPattern( |
| 265 | []string{ |
| 266 | `\Qmodule "foo": attempts to append ["module"] to protected property "foo"'s value of ["defaults"] defined in module "defaults"\E`, |
| 267 | `\Qmodule "foo": attempts to override protected property "nested.fizz" defined in module "defaults" with a different value (override true with false) so removing the property may necessitate other changes.\E`, |
| 268 | })).RunTest(t) |
| 269 | } |
| 270 | |
| 271 | func TestProtectedProperties_DefaultsConflict(t *testing.T) { |
| 272 | bp := ` |
| 273 | defaults { |
| 274 | name: "defaults1", |
| 275 | protected_properties: ["other.buzz"], |
| 276 | other: { |
| 277 | buzz: "value", |
| 278 | }, |
| 279 | } |
| 280 | |
| 281 | defaults { |
| 282 | name: "defaults2", |
| 283 | protected_properties: ["other.buzz"], |
| 284 | other: { |
| 285 | buzz: "another", |
| 286 | }, |
| 287 | } |
| 288 | |
| 289 | test { |
| 290 | name: "foo", |
| 291 | defaults: ["defaults1", "defaults2"], |
| 292 | } |
| 293 | ` |
| 294 | |
| 295 | GroupFixturePreparers( |
| 296 | prepareForDefaultsTest, |
| 297 | FixtureWithRootAndroidBp(bp), |
| 298 | ).ExtendWithErrorHandler(FixtureExpectsAtLeastOneErrorMatchingPattern( |
| 299 | `\Qmodule "foo": has conflicting default values for protected property "other.buzz": |
| 300 | defaults module "defaults1" provides value "value" |
| 301 | defaults module "defaults2" provides value "another"\E`, |
| 302 | )).RunTest(t) |
| 303 | } |