blob: 80df6a7cbea836fa237c2ca89693a33c1e1b69d1 [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) {
Jooyung Han12df5fb2019-07-11 16:18:47 +090071 bp := `
72 custom {
73 name: "foo",
74 required: ["bar"],
75 host_required: ["baz"],
76 target_required: ["qux"],
77 }
78 `
79
Colin Cross98be1bb2019-12-13 20:41:13 -080080 config := TestConfig(buildDir, nil, bp, nil)
Jingwen Chencda22c92020-11-23 00:22:30 -050081 config.katiEnabled = true // Enable androidmk Singleton
Colin Cross98be1bb2019-12-13 20:41:13 -080082
Colin Crossae8600b2020-10-29 17:09:13 -070083 ctx := NewTestContext(config)
Colin Cross98be1bb2019-12-13 20:41:13 -080084 ctx.RegisterSingletonType("androidmk", AndroidMkSingleton)
85 ctx.RegisterModuleType("custom", customModuleFactory)
Colin Crossae8600b2020-10-29 17:09:13 -070086 ctx.Register()
Jooyung Han12df5fb2019-07-11 16:18:47 +090087
88 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
89 FailIfErrored(t, errs)
90 _, errs = ctx.PrepareBuildActions(config)
91 FailIfErrored(t, errs)
92
93 m := ctx.ModuleForTests("foo", "").Module().(*customModule)
94
95 assertEqual := func(expected interface{}, actual interface{}) {
96 if !reflect.DeepEqual(expected, actual) {
97 t.Errorf("%q expected, but got %q", expected, actual)
98 }
99 }
100 assertEqual([]string{"bar"}, m.data.Required)
101 assertEqual([]string{"baz"}, m.data.Host_required)
102 assertEqual([]string{"qux"}, m.data.Target_required)
103}
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000104
105func TestGetDistForGoals(t *testing.T) {
106 testCases := []struct {
Paul Duffin0cc047a2020-11-25 16:39:30 +0000107 name string
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000108 bp string
109 expectedAndroidMkLines []string
110 }{
111 {
Paul Duffin0cc047a2020-11-25 16:39:30 +0000112 name: "dist-without-tag",
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000113 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 {
Paul Duffin0cc047a2020-11-25 16:39:30 +0000127 name: "dist-with-tag",
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000128 bp: `
129 custom {
130 name: "foo",
Jingwen Chen84811862020-07-21 11:32:19 +0000131 dist: {
132 targets: ["my_goal"],
133 tag: ".another-tag",
134 }
135 }
136 `,
137 expectedAndroidMkLines: []string{
138 ".PHONY: my_goal\n",
139 "$(call dist-for-goals,my_goal,another.out:another.out)\n",
140 },
141 },
142 {
Paul Duffin0cc047a2020-11-25 16:39:30 +0000143 name: "dists-with-tag",
Jingwen Chen84811862020-07-21 11:32:19 +0000144 bp: `
145 custom {
146 name: "foo",
147 dists: [
148 {
149 targets: ["my_goal"],
150 tag: ".another-tag",
151 },
152 ],
153 }
154 `,
155 expectedAndroidMkLines: []string{
156 ".PHONY: my_goal\n",
157 "$(call dist-for-goals,my_goal,another.out:another.out)\n",
158 },
159 },
160 {
Paul Duffin0cc047a2020-11-25 16:39:30 +0000161 name: "multiple-dists-with-and-without-tag",
Jingwen Chen84811862020-07-21 11:32:19 +0000162 bp: `
163 custom {
164 name: "foo",
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000165 dists: [
166 {
167 targets: ["my_goal"],
168 },
169 {
170 targets: ["my_second_goal", "my_third_goal"],
171 },
172 ],
173 }
174 `,
175 expectedAndroidMkLines: []string{
176 ".PHONY: my_goal\n",
177 "$(call dist-for-goals,my_goal,one.out:one.out)\n",
178 ".PHONY: my_second_goal my_third_goal\n",
179 "$(call dist-for-goals,my_second_goal my_third_goal,one.out:one.out)\n",
180 },
181 },
182 {
Paul Duffin0cc047a2020-11-25 16:39:30 +0000183 name: "dist-plus-dists-without-tags",
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000184 bp: `
185 custom {
186 name: "foo",
187 dist: {
188 targets: ["my_goal"],
189 },
190 dists: [
191 {
192 targets: ["my_second_goal", "my_third_goal"],
193 },
194 ],
195 }
196 `,
197 expectedAndroidMkLines: []string{
198 ".PHONY: my_second_goal my_third_goal\n",
199 "$(call dist-for-goals,my_second_goal my_third_goal,one.out:one.out)\n",
200 ".PHONY: my_goal\n",
201 "$(call dist-for-goals,my_goal,one.out:one.out)\n",
202 },
203 },
204 {
Paul Duffin0cc047a2020-11-25 16:39:30 +0000205 name: "dist-plus-dists-with-tags",
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000206 bp: `
207 custom {
208 name: "foo",
209 dist: {
210 targets: ["my_goal", "my_other_goal"],
211 tag: ".multiple",
212 },
213 dists: [
214 {
215 targets: ["my_second_goal"],
216 tag: ".multiple",
217 },
218 {
219 targets: ["my_third_goal"],
220 dir: "test/dir",
221 },
222 {
223 targets: ["my_fourth_goal"],
224 suffix: ".suffix",
225 },
226 {
227 targets: ["my_fifth_goal"],
228 dest: "new-name",
229 },
230 {
231 targets: ["my_sixth_goal"],
232 dest: "new-name",
233 dir: "some/dir",
234 suffix: ".suffix",
235 },
236 ],
237 }
238 `,
239 expectedAndroidMkLines: []string{
240 ".PHONY: my_second_goal\n",
241 "$(call dist-for-goals,my_second_goal,two.out:two.out)\n",
242 "$(call dist-for-goals,my_second_goal,three/four.out:four.out)\n",
243 ".PHONY: my_third_goal\n",
244 "$(call dist-for-goals,my_third_goal,one.out:test/dir/one.out)\n",
245 ".PHONY: my_fourth_goal\n",
246 "$(call dist-for-goals,my_fourth_goal,one.out:one.suffix.out)\n",
247 ".PHONY: my_fifth_goal\n",
248 "$(call dist-for-goals,my_fifth_goal,one.out:new-name)\n",
249 ".PHONY: my_sixth_goal\n",
250 "$(call dist-for-goals,my_sixth_goal,one.out:some/dir/new-name.suffix)\n",
251 ".PHONY: my_goal my_other_goal\n",
252 "$(call dist-for-goals,my_goal my_other_goal,two.out:two.out)\n",
253 "$(call dist-for-goals,my_goal my_other_goal,three/four.out:four.out)\n",
254 },
255 },
256 }
257
258 for _, testCase := range testCases {
Paul Duffin0cc047a2020-11-25 16:39:30 +0000259 t.Run(testCase.name, func(t *testing.T) {
260 config := TestConfig(buildDir, nil, testCase.bp, nil)
261 config.katiEnabled = true // Enable androidmk Singleton
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000262
Paul Duffin0cc047a2020-11-25 16:39:30 +0000263 ctx := NewTestContext(config)
264 ctx.RegisterSingletonType("androidmk", AndroidMkSingleton)
265 ctx.RegisterModuleType("custom", customModuleFactory)
266 ctx.Register()
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000267
Paul Duffin0cc047a2020-11-25 16:39:30 +0000268 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
269 FailIfErrored(t, errs)
270 _, errs = ctx.PrepareBuildActions(config)
271 FailIfErrored(t, errs)
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000272
Paul Duffin0cc047a2020-11-25 16:39:30 +0000273 module := ctx.ModuleForTests("foo", "").Module().(*customModule)
274 entries := AndroidMkEntriesForTest(t, config, "", module)
275 if len(entries) != 1 {
276 t.Errorf("Expected a single AndroidMk entry, got %d", len(entries))
277 }
278 androidMkLines := entries[0].GetDistForGoals(module)
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000279
Paul Duffin0cc047a2020-11-25 16:39:30 +0000280 if len(androidMkLines) != len(testCase.expectedAndroidMkLines) {
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000281 t.Errorf(
Paul Duffin0cc047a2020-11-25 16:39:30 +0000282 "Expected %d AndroidMk lines, got %d:\n%v",
283 len(testCase.expectedAndroidMkLines),
284 len(androidMkLines),
285 androidMkLines,
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000286 )
287 }
Paul Duffin0cc047a2020-11-25 16:39:30 +0000288 for idx, line := range androidMkLines {
289 expectedLine := testCase.expectedAndroidMkLines[idx]
290 if line != expectedLine {
291 t.Errorf(
292 "Expected AndroidMk line to be '%s', got '%s'",
293 expectedLine,
294 line,
295 )
296 }
297 }
298 })
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000299 }
300}