blob: bcaf5d7fed83ff5124e495180c28d0fd498125e6 [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
Paul Duffin103aaae2020-11-26 17:36:46 +000070// buildConfigAndCustomModuleFoo creates a config object, processes the supplied
71// bp module and then returns the config and the custom module called "foo".
72func buildConfigAndCustomModuleFoo(t *testing.T, bp string) (Config, *customModule) {
73 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -080074 config := TestConfig(buildDir, nil, bp, nil)
Jingwen Chencda22c92020-11-23 00:22:30 -050075 config.katiEnabled = true // Enable androidmk Singleton
Colin Cross98be1bb2019-12-13 20:41:13 -080076
Colin Crossae8600b2020-10-29 17:09:13 -070077 ctx := NewTestContext(config)
Colin Cross98be1bb2019-12-13 20:41:13 -080078 ctx.RegisterSingletonType("androidmk", AndroidMkSingleton)
79 ctx.RegisterModuleType("custom", customModuleFactory)
Colin Crossae8600b2020-10-29 17:09:13 -070080 ctx.Register()
Jooyung Han12df5fb2019-07-11 16:18:47 +090081
82 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
83 FailIfErrored(t, errs)
84 _, errs = ctx.PrepareBuildActions(config)
85 FailIfErrored(t, errs)
86
Paul Duffin103aaae2020-11-26 17:36:46 +000087 module := ctx.ModuleForTests("foo", "").Module().(*customModule)
88 return config, module
89}
90
91func TestAndroidMkSingleton_PassesUpdatedAndroidMkDataToCustomCallback(t *testing.T) {
92 bp := `
93 custom {
94 name: "foo",
95 required: ["bar"],
96 host_required: ["baz"],
97 target_required: ["qux"],
98 }
99 `
100
101 _, m := buildConfigAndCustomModuleFoo(t, bp)
Jooyung Han12df5fb2019-07-11 16:18:47 +0900102
103 assertEqual := func(expected interface{}, actual interface{}) {
104 if !reflect.DeepEqual(expected, actual) {
105 t.Errorf("%q expected, but got %q", expected, actual)
106 }
107 }
108 assertEqual([]string{"bar"}, m.data.Required)
109 assertEqual([]string{"baz"}, m.data.Host_required)
110 assertEqual([]string{"qux"}, m.data.Target_required)
111}
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000112
113func TestGetDistForGoals(t *testing.T) {
114 testCases := []struct {
Paul Duffin0cc047a2020-11-25 16:39:30 +0000115 name string
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000116 bp string
117 expectedAndroidMkLines []string
118 }{
119 {
Paul Duffin0cc047a2020-11-25 16:39:30 +0000120 name: "dist-without-tag",
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000121 bp: `
122 custom {
123 name: "foo",
124 dist: {
125 targets: ["my_goal"]
126 }
127 }
128 `,
129 expectedAndroidMkLines: []string{
130 ".PHONY: my_goal\n",
131 "$(call dist-for-goals,my_goal,one.out:one.out)\n",
132 },
133 },
134 {
Paul Duffin0cc047a2020-11-25 16:39:30 +0000135 name: "dist-with-tag",
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000136 bp: `
137 custom {
138 name: "foo",
Jingwen Chen84811862020-07-21 11:32:19 +0000139 dist: {
140 targets: ["my_goal"],
141 tag: ".another-tag",
142 }
143 }
144 `,
145 expectedAndroidMkLines: []string{
146 ".PHONY: my_goal\n",
147 "$(call dist-for-goals,my_goal,another.out:another.out)\n",
148 },
149 },
150 {
Paul Duffin0cc047a2020-11-25 16:39:30 +0000151 name: "dists-with-tag",
Jingwen Chen84811862020-07-21 11:32:19 +0000152 bp: `
153 custom {
154 name: "foo",
155 dists: [
156 {
157 targets: ["my_goal"],
158 tag: ".another-tag",
159 },
160 ],
161 }
162 `,
163 expectedAndroidMkLines: []string{
164 ".PHONY: my_goal\n",
165 "$(call dist-for-goals,my_goal,another.out:another.out)\n",
166 },
167 },
168 {
Paul Duffin0cc047a2020-11-25 16:39:30 +0000169 name: "multiple-dists-with-and-without-tag",
Jingwen Chen84811862020-07-21 11:32:19 +0000170 bp: `
171 custom {
172 name: "foo",
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000173 dists: [
174 {
175 targets: ["my_goal"],
176 },
177 {
178 targets: ["my_second_goal", "my_third_goal"],
179 },
180 ],
181 }
182 `,
183 expectedAndroidMkLines: []string{
184 ".PHONY: my_goal\n",
185 "$(call dist-for-goals,my_goal,one.out:one.out)\n",
186 ".PHONY: my_second_goal my_third_goal\n",
187 "$(call dist-for-goals,my_second_goal my_third_goal,one.out:one.out)\n",
188 },
189 },
190 {
Paul Duffin0cc047a2020-11-25 16:39:30 +0000191 name: "dist-plus-dists-without-tags",
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000192 bp: `
193 custom {
194 name: "foo",
195 dist: {
196 targets: ["my_goal"],
197 },
198 dists: [
199 {
200 targets: ["my_second_goal", "my_third_goal"],
201 },
202 ],
203 }
204 `,
205 expectedAndroidMkLines: []string{
206 ".PHONY: my_second_goal my_third_goal\n",
207 "$(call dist-for-goals,my_second_goal my_third_goal,one.out:one.out)\n",
208 ".PHONY: my_goal\n",
209 "$(call dist-for-goals,my_goal,one.out:one.out)\n",
210 },
211 },
212 {
Paul Duffin0cc047a2020-11-25 16:39:30 +0000213 name: "dist-plus-dists-with-tags",
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000214 bp: `
215 custom {
216 name: "foo",
217 dist: {
218 targets: ["my_goal", "my_other_goal"],
219 tag: ".multiple",
220 },
221 dists: [
222 {
223 targets: ["my_second_goal"],
224 tag: ".multiple",
225 },
226 {
227 targets: ["my_third_goal"],
228 dir: "test/dir",
229 },
230 {
231 targets: ["my_fourth_goal"],
232 suffix: ".suffix",
233 },
234 {
235 targets: ["my_fifth_goal"],
236 dest: "new-name",
237 },
238 {
239 targets: ["my_sixth_goal"],
240 dest: "new-name",
241 dir: "some/dir",
242 suffix: ".suffix",
243 },
244 ],
245 }
246 `,
247 expectedAndroidMkLines: []string{
248 ".PHONY: my_second_goal\n",
249 "$(call dist-for-goals,my_second_goal,two.out:two.out)\n",
250 "$(call dist-for-goals,my_second_goal,three/four.out:four.out)\n",
251 ".PHONY: my_third_goal\n",
252 "$(call dist-for-goals,my_third_goal,one.out:test/dir/one.out)\n",
253 ".PHONY: my_fourth_goal\n",
254 "$(call dist-for-goals,my_fourth_goal,one.out:one.suffix.out)\n",
255 ".PHONY: my_fifth_goal\n",
256 "$(call dist-for-goals,my_fifth_goal,one.out:new-name)\n",
257 ".PHONY: my_sixth_goal\n",
258 "$(call dist-for-goals,my_sixth_goal,one.out:some/dir/new-name.suffix)\n",
259 ".PHONY: my_goal my_other_goal\n",
260 "$(call dist-for-goals,my_goal my_other_goal,two.out:two.out)\n",
261 "$(call dist-for-goals,my_goal my_other_goal,three/four.out:four.out)\n",
262 },
263 },
264 }
265
266 for _, testCase := range testCases {
Paul Duffin0cc047a2020-11-25 16:39:30 +0000267 t.Run(testCase.name, func(t *testing.T) {
Paul Duffin103aaae2020-11-26 17:36:46 +0000268 config, module := buildConfigAndCustomModuleFoo(t, testCase.bp)
Paul Duffin0cc047a2020-11-25 16:39:30 +0000269 entries := AndroidMkEntriesForTest(t, config, "", module)
270 if len(entries) != 1 {
271 t.Errorf("Expected a single AndroidMk entry, got %d", len(entries))
272 }
273 androidMkLines := entries[0].GetDistForGoals(module)
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000274
Paul Duffin0cc047a2020-11-25 16:39:30 +0000275 if len(androidMkLines) != len(testCase.expectedAndroidMkLines) {
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000276 t.Errorf(
Paul Duffin0cc047a2020-11-25 16:39:30 +0000277 "Expected %d AndroidMk lines, got %d:\n%v",
278 len(testCase.expectedAndroidMkLines),
279 len(androidMkLines),
280 androidMkLines,
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000281 )
282 }
Paul Duffin0cc047a2020-11-25 16:39:30 +0000283 for idx, line := range androidMkLines {
284 expectedLine := testCase.expectedAndroidMkLines[idx]
285 if line != expectedLine {
286 t.Errorf(
287 "Expected AndroidMk line to be '%s', got '%s'",
288 expectedLine,
289 line,
290 )
291 }
292 }
293 })
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000294 }
295}