blob: 8574dc96b91b9c4d59b18ce438a13c1d5480d215 [file] [log] [blame]
Jooyung Han12df5fb2019-07-11 16:18:47 +09001// 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
15package android
16
17import (
Jingwen Chen40fd90a2020-06-15 05:24:19 +000018 "fmt"
Jooyung Han12df5fb2019-07-11 16:18:47 +090019 "io"
20 "reflect"
21 "testing"
22)
23
24type customModule struct {
25 ModuleBase
Jingwen Chen40fd90a2020-06-15 05:24:19 +000026 data AndroidMkData
27 distFiles TaggedDistFiles
Jooyung Han12df5fb2019-07-11 16:18:47 +090028}
29
30func (m *customModule) GenerateAndroidBuildActions(ctx ModuleContext) {
Jingwen Chen40fd90a2020-06-15 05:24:19 +000031 m.distFiles = m.GenerateTaggedDistFiles(ctx)
Jooyung Han12df5fb2019-07-11 16:18:47 +090032}
33
34func (m *customModule) AndroidMk() AndroidMkData {
35 return AndroidMkData{
36 Custom: func(w io.Writer, name, prefix, moduleDir string, data AndroidMkData) {
37 m.data = data
38 },
39 }
40}
41
Jingwen Chen40fd90a2020-06-15 05:24:19 +000042func (m *customModule) OutputFiles(tag string) (Paths, error) {
43 switch tag {
44 case "":
45 return PathsForTesting("one.out"), nil
46 case ".multiple":
47 return PathsForTesting("two.out", "three/four.out"), nil
Jingwen Chen84811862020-07-21 11:32:19 +000048 case ".another-tag":
49 return PathsForTesting("another.out"), nil
Jingwen Chen40fd90a2020-06-15 05:24:19 +000050 default:
51 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
52 }
53}
54
55func (m *customModule) AndroidMkEntries() []AndroidMkEntries {
56 return []AndroidMkEntries{
57 {
58 Class: "CUSTOM_MODULE",
59 DistFiles: m.distFiles,
60 },
61 }
62}
63
Jooyung Han12df5fb2019-07-11 16:18:47 +090064func customModuleFactory() Module {
65 module := &customModule{}
66 InitAndroidModule(module)
67 return module
68}
69
70func TestAndroidMkSingleton_PassesUpdatedAndroidMkDataToCustomCallback(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -070071 t.Parallel()
Jooyung Han12df5fb2019-07-11 16:18:47 +090072 bp := `
73 custom {
74 name: "foo",
75 required: ["bar"],
76 host_required: ["baz"],
77 target_required: ["qux"],
78 }
79 `
80
Colin Cross98be1bb2019-12-13 20:41:13 -080081 config := TestConfig(buildDir, nil, bp, nil)
82 config.inMake = true // Enable androidmk Singleton
83
84 ctx := NewTestContext()
85 ctx.RegisterSingletonType("androidmk", AndroidMkSingleton)
86 ctx.RegisterModuleType("custom", customModuleFactory)
87 ctx.Register(config)
Jooyung Han12df5fb2019-07-11 16:18:47 +090088
89 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
90 FailIfErrored(t, errs)
91 _, errs = ctx.PrepareBuildActions(config)
92 FailIfErrored(t, errs)
93
94 m := ctx.ModuleForTests("foo", "").Module().(*customModule)
95
96 assertEqual := func(expected interface{}, actual interface{}) {
97 if !reflect.DeepEqual(expected, actual) {
98 t.Errorf("%q expected, but got %q", expected, actual)
99 }
100 }
101 assertEqual([]string{"bar"}, m.data.Required)
102 assertEqual([]string{"baz"}, m.data.Host_required)
103 assertEqual([]string{"qux"}, m.data.Target_required)
104}
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000105
106func TestGetDistForGoals(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -0700107 t.Parallel()
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000108 testCases := []struct {
109 bp string
110 expectedAndroidMkLines []string
111 }{
112 {
113 bp: `
114 custom {
115 name: "foo",
116 dist: {
117 targets: ["my_goal"]
118 }
119 }
120 `,
121 expectedAndroidMkLines: []string{
122 ".PHONY: my_goal\n",
123 "$(call dist-for-goals,my_goal,one.out:one.out)\n",
124 },
125 },
126 {
127 bp: `
128 custom {
129 name: "foo",
Jingwen Chen84811862020-07-21 11:32:19 +0000130 dist: {
131 targets: ["my_goal"],
132 tag: ".another-tag",
133 }
134 }
135 `,
136 expectedAndroidMkLines: []string{
137 ".PHONY: my_goal\n",
138 "$(call dist-for-goals,my_goal,another.out:another.out)\n",
139 },
140 },
141 {
142 bp: `
143 custom {
144 name: "foo",
145 dists: [
146 {
147 targets: ["my_goal"],
148 tag: ".another-tag",
149 },
150 ],
151 }
152 `,
153 expectedAndroidMkLines: []string{
154 ".PHONY: my_goal\n",
155 "$(call dist-for-goals,my_goal,another.out:another.out)\n",
156 },
157 },
158 {
159 bp: `
160 custom {
161 name: "foo",
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000162 dists: [
163 {
164 targets: ["my_goal"],
165 },
166 {
167 targets: ["my_second_goal", "my_third_goal"],
168 },
169 ],
170 }
171 `,
172 expectedAndroidMkLines: []string{
173 ".PHONY: my_goal\n",
174 "$(call dist-for-goals,my_goal,one.out:one.out)\n",
175 ".PHONY: my_second_goal my_third_goal\n",
176 "$(call dist-for-goals,my_second_goal my_third_goal,one.out:one.out)\n",
177 },
178 },
179 {
180 bp: `
181 custom {
182 name: "foo",
183 dist: {
184 targets: ["my_goal"],
185 },
186 dists: [
187 {
188 targets: ["my_second_goal", "my_third_goal"],
189 },
190 ],
191 }
192 `,
193 expectedAndroidMkLines: []string{
194 ".PHONY: my_second_goal my_third_goal\n",
195 "$(call dist-for-goals,my_second_goal my_third_goal,one.out:one.out)\n",
196 ".PHONY: my_goal\n",
197 "$(call dist-for-goals,my_goal,one.out:one.out)\n",
198 },
199 },
200 {
201 bp: `
202 custom {
203 name: "foo",
204 dist: {
205 targets: ["my_goal", "my_other_goal"],
206 tag: ".multiple",
207 },
208 dists: [
209 {
210 targets: ["my_second_goal"],
211 tag: ".multiple",
212 },
213 {
214 targets: ["my_third_goal"],
215 dir: "test/dir",
216 },
217 {
218 targets: ["my_fourth_goal"],
219 suffix: ".suffix",
220 },
221 {
222 targets: ["my_fifth_goal"],
223 dest: "new-name",
224 },
225 {
226 targets: ["my_sixth_goal"],
227 dest: "new-name",
228 dir: "some/dir",
229 suffix: ".suffix",
230 },
231 ],
232 }
233 `,
234 expectedAndroidMkLines: []string{
235 ".PHONY: my_second_goal\n",
236 "$(call dist-for-goals,my_second_goal,two.out:two.out)\n",
237 "$(call dist-for-goals,my_second_goal,three/four.out:four.out)\n",
238 ".PHONY: my_third_goal\n",
239 "$(call dist-for-goals,my_third_goal,one.out:test/dir/one.out)\n",
240 ".PHONY: my_fourth_goal\n",
241 "$(call dist-for-goals,my_fourth_goal,one.out:one.suffix.out)\n",
242 ".PHONY: my_fifth_goal\n",
243 "$(call dist-for-goals,my_fifth_goal,one.out:new-name)\n",
244 ".PHONY: my_sixth_goal\n",
245 "$(call dist-for-goals,my_sixth_goal,one.out:some/dir/new-name.suffix)\n",
246 ".PHONY: my_goal my_other_goal\n",
247 "$(call dist-for-goals,my_goal my_other_goal,two.out:two.out)\n",
248 "$(call dist-for-goals,my_goal my_other_goal,three/four.out:four.out)\n",
249 },
250 },
251 }
252
253 for _, testCase := range testCases {
254 config := TestConfig(buildDir, nil, testCase.bp, nil)
255 config.inMake = true // Enable androidmk Singleton
256
257 ctx := NewTestContext()
258 ctx.RegisterSingletonType("androidmk", AndroidMkSingleton)
259 ctx.RegisterModuleType("custom", customModuleFactory)
260 ctx.Register(config)
261
262 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
263 FailIfErrored(t, errs)
264 _, errs = ctx.PrepareBuildActions(config)
265 FailIfErrored(t, errs)
266
267 module := ctx.ModuleForTests("foo", "").Module().(*customModule)
268 entries := AndroidMkEntriesForTest(t, config, "", module)
269 if len(entries) != 1 {
270 t.Errorf("Expected a single AndroidMk entry, got %d", len(entries))
271 }
272 androidMkLines := entries[0].GetDistForGoals(module)
273
274 if len(androidMkLines) != len(testCase.expectedAndroidMkLines) {
275 t.Errorf(
276 "Expected %d AndroidMk lines, got %d:\n%v",
277 len(testCase.expectedAndroidMkLines),
278 len(androidMkLines),
279 androidMkLines,
280 )
281 }
282 for idx, line := range androidMkLines {
283 expectedLine := testCase.expectedAndroidMkLines[idx]
284 if line != expectedLine {
285 t.Errorf(
286 "Expected AndroidMk line to be '%s', got '%s'",
287 line,
288 expectedLine,
289 )
290 }
291 }
292 }
293}