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 | 69a680f | 2024-01-29 13:18:43 -0800 | [diff] [blame] | 19 | |
| 20 | "github.com/google/blueprint" |
Colin Cross | 0e99175 | 2019-06-10 15:41:28 -0700 | [diff] [blame] | 21 | ) |
| 22 | |
| 23 | type defaultsTestProperties struct { |
Colin Cross | 69a680f | 2024-01-29 13:18:43 -0800 | [diff] [blame] | 24 | Foo []string |
| 25 | Path_prop []string `android:"path"` |
Colin Cross | 0e99175 | 2019-06-10 15:41:28 -0700 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | type defaultsTestModule struct { |
| 29 | ModuleBase |
| 30 | DefaultableModuleBase |
| 31 | properties defaultsTestProperties |
| 32 | } |
| 33 | |
| 34 | func (d *defaultsTestModule) GenerateAndroidBuildActions(ctx ModuleContext) { |
| 35 | ctx.Build(pctx, BuildParams{ |
| 36 | Rule: Touch, |
| 37 | Output: PathForModuleOut(ctx, "out"), |
| 38 | }) |
| 39 | } |
| 40 | |
| 41 | func defaultsTestModuleFactory() Module { |
| 42 | module := &defaultsTestModule{} |
| 43 | module.AddProperties(&module.properties) |
Colin Cross | 0e99175 | 2019-06-10 15:41:28 -0700 | [diff] [blame] | 44 | InitAndroidModule(module) |
Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 45 | InitDefaultableModule(module) |
Colin Cross | 0e99175 | 2019-06-10 15:41:28 -0700 | [diff] [blame] | 46 | return module |
| 47 | } |
| 48 | |
| 49 | type defaultsTestDefaults struct { |
| 50 | ModuleBase |
| 51 | DefaultsModuleBase |
| 52 | } |
| 53 | |
| 54 | func defaultsTestDefaultsFactory() Module { |
| 55 | defaults := &defaultsTestDefaults{} |
| 56 | defaults.AddProperties(&defaultsTestProperties{}) |
| 57 | InitDefaultsModule(defaults) |
| 58 | return defaults |
| 59 | } |
| 60 | |
Paul Duffin | 7c166b4 | 2021-03-16 19:31:17 +0000 | [diff] [blame] | 61 | var prepareForDefaultsTest = GroupFixturePreparers( |
| 62 | PrepareForTestWithDefaults, |
| 63 | FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 64 | ctx.RegisterModuleType("test", defaultsTestModuleFactory) |
| 65 | ctx.RegisterModuleType("defaults", defaultsTestDefaultsFactory) |
| 66 | }), |
| 67 | ) |
| 68 | |
Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 69 | func TestDefaults(t *testing.T) { |
| 70 | bp := ` |
| 71 | defaults { |
| 72 | name: "transitive", |
| 73 | foo: ["transitive"], |
| 74 | } |
| 75 | |
| 76 | defaults { |
| 77 | name: "defaults", |
| 78 | defaults: ["transitive"], |
| 79 | foo: ["defaults"], |
| 80 | } |
| 81 | |
| 82 | test { |
| 83 | name: "foo", |
| 84 | defaults: ["defaults"], |
| 85 | foo: ["module"], |
| 86 | } |
| 87 | ` |
| 88 | |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 89 | result := GroupFixturePreparers( |
Paul Duffin | 7c166b4 | 2021-03-16 19:31:17 +0000 | [diff] [blame] | 90 | prepareForDefaultsTest, |
| 91 | FixtureWithRootAndroidBp(bp), |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 92 | ).RunTest(t) |
Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 93 | |
Paul Duffin | 7c166b4 | 2021-03-16 19:31:17 +0000 | [diff] [blame] | 94 | foo := result.Module("foo", "").(*defaultsTestModule) |
Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 95 | |
Paul Duffin | 7c166b4 | 2021-03-16 19:31:17 +0000 | [diff] [blame] | 96 | AssertDeepEquals(t, "foo", []string{"transitive", "defaults", "module"}, foo.properties.Foo) |
Colin Cross | eabaedd | 2020-02-06 17:01:55 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Colin Cross | 0e99175 | 2019-06-10 15:41:28 -0700 | [diff] [blame] | 99 | func TestDefaultsAllowMissingDependencies(t *testing.T) { |
Colin Cross | 0e99175 | 2019-06-10 15:41:28 -0700 | [diff] [blame] | 100 | bp := ` |
| 101 | defaults { |
| 102 | name: "defaults", |
| 103 | defaults: ["missing"], |
| 104 | foo: ["defaults"], |
| 105 | } |
| 106 | |
| 107 | test { |
| 108 | name: "missing_defaults", |
| 109 | defaults: ["missing"], |
| 110 | foo: ["module"], |
| 111 | } |
| 112 | |
| 113 | test { |
| 114 | name: "missing_transitive_defaults", |
| 115 | defaults: ["defaults"], |
| 116 | foo: ["module"], |
| 117 | } |
| 118 | ` |
| 119 | |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 120 | result := GroupFixturePreparers( |
Paul Duffin | 7c166b4 | 2021-03-16 19:31:17 +0000 | [diff] [blame] | 121 | prepareForDefaultsTest, |
| 122 | PrepareForTestWithAllowMissingDependencies, |
| 123 | FixtureWithRootAndroidBp(bp), |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 124 | ).RunTest(t) |
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 | missingDefaults := result.ModuleForTests("missing_defaults", "").Output("out") |
| 127 | missingTransitiveDefaults := result.ModuleForTests("missing_transitive_defaults", "").Output("out") |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 128 | |
Paul Duffin | 7c166b4 | 2021-03-16 19:31:17 +0000 | [diff] [blame] | 129 | AssertSame(t, "missing_defaults rule", ErrorRule, missingDefaults.Rule) |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 130 | |
Paul Duffin | 7c166b4 | 2021-03-16 19:31:17 +0000 | [diff] [blame] | 131 | 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] | 132 | |
| 133 | // TODO: missing transitive defaults is currently not handled |
| 134 | _ = missingTransitiveDefaults |
| 135 | } |
Colin Cross | 69a680f | 2024-01-29 13:18:43 -0800 | [diff] [blame] | 136 | |
| 137 | func TestDefaultsPathProperties(t *testing.T) { |
| 138 | bp := ` |
| 139 | defaults { |
| 140 | name: "defaults", |
| 141 | path_prop: [":gen"], |
| 142 | } |
| 143 | |
| 144 | test { |
| 145 | name: "foo", |
| 146 | defaults: ["defaults"], |
| 147 | } |
| 148 | |
| 149 | test { |
| 150 | name: "gen", |
| 151 | } |
| 152 | ` |
| 153 | |
| 154 | result := GroupFixturePreparers( |
| 155 | prepareForDefaultsTest, |
| 156 | FixtureWithRootAndroidBp(bp), |
| 157 | ).RunTest(t) |
| 158 | |
| 159 | collectDeps := func(m Module) []string { |
| 160 | var deps []string |
| 161 | result.VisitDirectDeps(m, func(dep blueprint.Module) { |
| 162 | deps = append(deps, result.ModuleName(dep)) |
| 163 | }) |
| 164 | return deps |
| 165 | } |
| 166 | |
| 167 | foo := result.Module("foo", "") |
| 168 | defaults := result.Module("defaults", "") |
| 169 | |
| 170 | AssertStringListContains(t, "foo should depend on gen", collectDeps(foo), "gen") |
| 171 | AssertStringListDoesNotContain(t, "defaults should not depend on gen", collectDeps(defaults), "gen") |
| 172 | } |