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 { |
| 22 | Foo []string |
| 23 | } |
| 24 | |
| 25 | type defaultsTestModule struct { |
| 26 | ModuleBase |
| 27 | DefaultableModuleBase |
| 28 | properties defaultsTestProperties |
| 29 | } |
| 30 | |
| 31 | func (d *defaultsTestModule) GenerateAndroidBuildActions(ctx ModuleContext) { |
| 32 | ctx.Build(pctx, BuildParams{ |
| 33 | Rule: Touch, |
| 34 | Output: PathForModuleOut(ctx, "out"), |
| 35 | }) |
| 36 | } |
| 37 | |
| 38 | func defaultsTestModuleFactory() Module { |
| 39 | module := &defaultsTestModule{} |
| 40 | module.AddProperties(&module.properties) |
Colin Cross | 0e99175 | 2019-06-10 15:41:28 -0700 | [diff] [blame] | 41 | InitAndroidModule(module) |
Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 42 | InitDefaultableModule(module) |
Colin Cross | 0e99175 | 2019-06-10 15:41:28 -0700 | [diff] [blame] | 43 | return module |
| 44 | } |
| 45 | |
| 46 | type defaultsTestDefaults struct { |
| 47 | ModuleBase |
| 48 | DefaultsModuleBase |
| 49 | } |
| 50 | |
| 51 | func defaultsTestDefaultsFactory() Module { |
| 52 | defaults := &defaultsTestDefaults{} |
| 53 | defaults.AddProperties(&defaultsTestProperties{}) |
| 54 | InitDefaultsModule(defaults) |
| 55 | return defaults |
| 56 | } |
| 57 | |
Paul Duffin | 7c166b4 | 2021-03-16 19:31:17 +0000 | [diff] [blame] | 58 | var prepareForDefaultsTest = GroupFixturePreparers( |
| 59 | PrepareForTestWithDefaults, |
| 60 | FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 61 | ctx.RegisterModuleType("test", defaultsTestModuleFactory) |
| 62 | ctx.RegisterModuleType("defaults", defaultsTestDefaultsFactory) |
| 63 | }), |
| 64 | ) |
| 65 | |
Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 66 | func TestDefaults(t *testing.T) { |
| 67 | bp := ` |
| 68 | defaults { |
| 69 | name: "transitive", |
| 70 | foo: ["transitive"], |
| 71 | } |
| 72 | |
| 73 | defaults { |
| 74 | name: "defaults", |
| 75 | defaults: ["transitive"], |
| 76 | foo: ["defaults"], |
| 77 | } |
| 78 | |
| 79 | test { |
| 80 | name: "foo", |
| 81 | defaults: ["defaults"], |
| 82 | foo: ["module"], |
| 83 | } |
| 84 | ` |
| 85 | |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 86 | result := GroupFixturePreparers( |
Paul Duffin | 7c166b4 | 2021-03-16 19:31:17 +0000 | [diff] [blame] | 87 | prepareForDefaultsTest, |
| 88 | FixtureWithRootAndroidBp(bp), |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 89 | ).RunTest(t) |
Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 90 | |
Paul Duffin | 7c166b4 | 2021-03-16 19:31:17 +0000 | [diff] [blame] | 91 | foo := result.Module("foo", "").(*defaultsTestModule) |
Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 92 | |
Paul Duffin | 7c166b4 | 2021-03-16 19:31:17 +0000 | [diff] [blame] | 93 | AssertDeepEquals(t, "foo", []string{"transitive", "defaults", "module"}, foo.properties.Foo) |
Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Colin Cross | 0e99175 | 2019-06-10 15:41:28 -0700 | [diff] [blame] | 96 | func TestDefaultsAllowMissingDependencies(t *testing.T) { |
Colin Cross | 0e99175 | 2019-06-10 15:41:28 -0700 | [diff] [blame] | 97 | bp := ` |
| 98 | defaults { |
| 99 | name: "defaults", |
| 100 | defaults: ["missing"], |
| 101 | foo: ["defaults"], |
| 102 | } |
| 103 | |
| 104 | test { |
| 105 | name: "missing_defaults", |
| 106 | defaults: ["missing"], |
| 107 | foo: ["module"], |
| 108 | } |
| 109 | |
| 110 | test { |
| 111 | name: "missing_transitive_defaults", |
| 112 | defaults: ["defaults"], |
| 113 | foo: ["module"], |
| 114 | } |
| 115 | ` |
| 116 | |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 117 | result := GroupFixturePreparers( |
Paul Duffin | 7c166b4 | 2021-03-16 19:31:17 +0000 | [diff] [blame] | 118 | prepareForDefaultsTest, |
| 119 | PrepareForTestWithAllowMissingDependencies, |
| 120 | FixtureWithRootAndroidBp(bp), |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 121 | ).RunTest(t) |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 122 | |
Paul Duffin | 7c166b4 | 2021-03-16 19:31:17 +0000 | [diff] [blame] | 123 | missingDefaults := result.ModuleForTests("missing_defaults", "").Output("out") |
| 124 | missingTransitiveDefaults := result.ModuleForTests("missing_transitive_defaults", "").Output("out") |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 125 | |
Paul Duffin | 7c166b4 | 2021-03-16 19:31:17 +0000 | [diff] [blame] | 126 | AssertSame(t, "missing_defaults rule", ErrorRule, missingDefaults.Rule) |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 127 | |
Paul Duffin | 7c166b4 | 2021-03-16 19:31:17 +0000 | [diff] [blame] | 128 | 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] | 129 | |
| 130 | // TODO: missing transitive defaults is currently not handled |
| 131 | _ = missingTransitiveDefaults |
| 132 | } |