Colin Cross | 1b48842 | 2019-03-04 22:33:56 -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 | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 18 | "testing" |
| 19 | ) |
| 20 | |
| 21 | type pathDepsMutatorTestModule struct { |
| 22 | ModuleBase |
| 23 | props struct { |
| 24 | Foo string `android:"path"` |
Colin Cross | a3a9741 | 2019-03-18 12:24:29 -0700 | [diff] [blame] | 25 | Bar []string `android:"path,arch_variant"` |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 26 | Baz *string `android:"path"` |
| 27 | Qux string |
Jaewoong Jung | 1c1b6e6 | 2021-03-09 15:02:31 -0800 | [diff] [blame] | 28 | V *struct { |
| 29 | W string `android:"path"` |
| 30 | } |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 31 | } |
| 32 | |
Colin Cross | 527f3e5 | 2019-07-15 13:35:21 -0700 | [diff] [blame] | 33 | // A second property struct with a duplicate property name |
| 34 | props2 struct { |
| 35 | Foo string `android:"path"` |
| 36 | } |
| 37 | |
Jiyong Park | 66dd5c0 | 2021-02-24 01:22:57 +0900 | [diff] [blame] | 38 | // nested slices of struct |
| 39 | props3 struct { |
| 40 | X []struct { |
| 41 | Y []struct { |
| 42 | Z []string `android:"path"` |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 47 | sourceDeps []string |
| 48 | } |
| 49 | |
| 50 | func pathDepsMutatorTestModuleFactory() Module { |
| 51 | module := &pathDepsMutatorTestModule{} |
Jiyong Park | 66dd5c0 | 2021-02-24 01:22:57 +0900 | [diff] [blame] | 52 | module.AddProperties(&module.props, &module.props2, &module.props3) |
Colin Cross | a3a9741 | 2019-03-18 12:24:29 -0700 | [diff] [blame] | 53 | InitAndroidArchModule(module, DeviceSupported, MultilibBoth) |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 54 | return module |
| 55 | } |
| 56 | |
| 57 | func (p *pathDepsMutatorTestModule) GenerateAndroidBuildActions(ctx ModuleContext) { |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 58 | ctx.VisitDirectDeps(func(dep Module) { |
| 59 | if _, ok := ctx.OtherModuleDependencyTag(dep).(sourceOrOutputDependencyTag); ok { |
| 60 | p.sourceDeps = append(p.sourceDeps, ctx.OtherModuleName(dep)) |
| 61 | } |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 62 | }) |
Colin Cross | 527f3e5 | 2019-07-15 13:35:21 -0700 | [diff] [blame] | 63 | |
| 64 | if p.props.Foo != "" { |
| 65 | // Make sure there is only one dependency on a module listed in a property present in multiple property structs |
Paul Duffin | d5cf92e | 2021-07-09 17:38:55 +0100 | [diff] [blame] | 66 | m := SrcIsModule(p.props.Foo) |
| 67 | if GetModuleFromPathDep(ctx, m, "") == nil { |
Colin Cross | 527f3e5 | 2019-07-15 13:35:21 -0700 | [diff] [blame] | 68 | ctx.ModuleErrorf("GetDirectDepWithTag failed") |
| 69 | } |
| 70 | } |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | func TestPathDepsMutator(t *testing.T) { |
| 74 | tests := []struct { |
| 75 | name string |
| 76 | bp string |
| 77 | deps []string |
| 78 | }{ |
| 79 | { |
| 80 | name: "all", |
| 81 | bp: ` |
| 82 | test { |
| 83 | name: "foo", |
| 84 | foo: ":a", |
| 85 | bar: [":b"], |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 86 | baz: ":c{.bar}", |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 87 | qux: ":d", |
Jiyong Park | 66dd5c0 | 2021-02-24 01:22:57 +0900 | [diff] [blame] | 88 | x: [ |
| 89 | { |
| 90 | y: [ |
| 91 | { |
| 92 | z: [":x", ":y"], |
| 93 | }, |
| 94 | { |
| 95 | z: [":z"], |
| 96 | }, |
| 97 | ], |
| 98 | }, |
| 99 | ], |
Jaewoong Jung | 1c1b6e6 | 2021-03-09 15:02:31 -0800 | [diff] [blame] | 100 | v: { |
| 101 | w: ":w", |
| 102 | }, |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 103 | }`, |
Jaewoong Jung | 1c1b6e6 | 2021-03-09 15:02:31 -0800 | [diff] [blame] | 104 | deps: []string{"a", "b", "c", "w", "x", "y", "z"}, |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 105 | }, |
Colin Cross | a3a9741 | 2019-03-18 12:24:29 -0700 | [diff] [blame] | 106 | { |
| 107 | name: "arch variant", |
| 108 | bp: ` |
| 109 | test { |
| 110 | name: "foo", |
| 111 | arch: { |
| 112 | arm64: { |
| 113 | bar: [":a"], |
| 114 | }, |
| 115 | arm: { |
| 116 | bar: [":b"], |
| 117 | }, |
| 118 | }, |
| 119 | bar: [":c"], |
| 120 | }`, |
| 121 | deps: []string{"c", "a"}, |
| 122 | }, |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 125 | for _, test := range tests { |
| 126 | t.Run(test.name, func(t *testing.T) { |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 127 | bp := test.bp + ` |
| 128 | filegroup { |
| 129 | name: "a", |
| 130 | } |
| 131 | |
| 132 | filegroup { |
| 133 | name: "b", |
| 134 | } |
| 135 | |
| 136 | filegroup { |
| 137 | name: "c", |
| 138 | } |
| 139 | |
| 140 | filegroup { |
| 141 | name: "d", |
| 142 | } |
Jiyong Park | 66dd5c0 | 2021-02-24 01:22:57 +0900 | [diff] [blame] | 143 | |
| 144 | filegroup { |
Jaewoong Jung | 1c1b6e6 | 2021-03-09 15:02:31 -0800 | [diff] [blame] | 145 | name: "w", |
| 146 | } |
| 147 | |
| 148 | filegroup { |
Jiyong Park | 66dd5c0 | 2021-02-24 01:22:57 +0900 | [diff] [blame] | 149 | name: "x", |
| 150 | } |
| 151 | |
| 152 | filegroup { |
| 153 | name: "y", |
| 154 | } |
| 155 | |
| 156 | filegroup { |
| 157 | name: "z", |
| 158 | } |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 159 | ` |
| 160 | |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 161 | result := GroupFixturePreparers( |
Paul Duffin | d518b7e | 2021-03-17 00:09:35 +0000 | [diff] [blame] | 162 | PrepareForTestWithArchMutator, |
| 163 | PrepareForTestWithFilegroup, |
| 164 | FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 165 | ctx.RegisterModuleType("test", pathDepsMutatorTestModuleFactory) |
| 166 | }), |
| 167 | FixtureWithRootAndroidBp(bp), |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 168 | ).RunTest(t) |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 169 | |
Paul Duffin | d518b7e | 2021-03-17 00:09:35 +0000 | [diff] [blame] | 170 | m := result.Module("foo", "android_arm64_armv8-a").(*pathDepsMutatorTestModule) |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 171 | |
Paul Duffin | d518b7e | 2021-03-17 00:09:35 +0000 | [diff] [blame] | 172 | AssertDeepEquals(t, "deps", test.deps, m.sourceDeps) |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 173 | }) |
| 174 | } |
| 175 | } |