blob: 2df72bda6ac959b985d120cb24b96cfd170004c3 [file] [log] [blame]
Liz Kammer2dd9ca42020-11-25 16:06:39 -08001// Copyright 2020 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 bp2build
16
17import (
18 "android/soong/android"
19 "testing"
20)
21
22func TestGenerateSoongModuleTargets(t *testing.T) {
23 testCases := []struct {
24 bp string
25 expectedBazelTarget string
26 }{
27 {
28 bp: `custom {
29 name: "foo",
30}
31 `,
32 expectedBazelTarget: `soong_module(
33 name = "foo",
34 module_name = "foo",
35 module_type = "custom",
36 module_variant = "",
37 module_deps = [
38 ],
39)`,
40 },
41 {
42 bp: `custom {
43 name: "foo",
44 ramdisk: true,
45}
46 `,
47 expectedBazelTarget: `soong_module(
48 name = "foo",
49 module_name = "foo",
50 module_type = "custom",
51 module_variant = "",
52 module_deps = [
53 ],
54 ramdisk = True,
55)`,
56 },
57 {
58 bp: `custom {
59 name: "foo",
60 owner: "a_string_with\"quotes\"_and_\\backslashes\\\\",
61}
62 `,
63 expectedBazelTarget: `soong_module(
64 name = "foo",
65 module_name = "foo",
66 module_type = "custom",
67 module_variant = "",
68 module_deps = [
69 ],
70 owner = "a_string_with\"quotes\"_and_\\backslashes\\\\",
71)`,
72 },
73 {
74 bp: `custom {
75 name: "foo",
76 required: ["bar"],
77}
78 `,
79 expectedBazelTarget: `soong_module(
80 name = "foo",
81 module_name = "foo",
82 module_type = "custom",
83 module_variant = "",
84 module_deps = [
85 ],
86 required = [
87 "bar",
88 ],
89)`,
90 },
91 {
92 bp: `custom {
93 name: "foo",
94 target_required: ["qux", "bazqux"],
95}
96 `,
97 expectedBazelTarget: `soong_module(
98 name = "foo",
99 module_name = "foo",
100 module_type = "custom",
101 module_variant = "",
102 module_deps = [
103 ],
104 target_required = [
105 "qux",
106 "bazqux",
107 ],
108)`,
109 },
110 {
111 bp: `custom {
112 name: "foo",
113 dist: {
114 targets: ["goal_foo"],
115 tag: ".foo",
116 },
117 dists: [
118 {
119 targets: ["goal_bar"],
120 tag: ".bar",
121 },
122 ],
123}
124 `,
125 expectedBazelTarget: `soong_module(
126 name = "foo",
127 module_name = "foo",
128 module_type = "custom",
129 module_variant = "",
130 module_deps = [
131 ],
132 dist = {
133 "tag": ".foo",
134 "targets": [
135 "goal_foo",
136 ],
137 },
138 dists = [
139 {
140 "tag": ".bar",
141 "targets": [
142 "goal_bar",
143 ],
144 },
145 ],
146)`,
147 },
148 {
149 bp: `custom {
150 name: "foo",
151 required: ["bar"],
152 target_required: ["qux", "bazqux"],
153 ramdisk: true,
154 owner: "custom_owner",
155 dists: [
156 {
157 tag: ".tag",
158 targets: ["my_goal"],
159 },
160 ],
161}
162 `,
163 expectedBazelTarget: `soong_module(
164 name = "foo",
165 module_name = "foo",
166 module_type = "custom",
167 module_variant = "",
168 module_deps = [
169 ],
170 dists = [
171 {
172 "tag": ".tag",
173 "targets": [
174 "my_goal",
175 ],
176 },
177 ],
178 owner = "custom_owner",
179 ramdisk = True,
180 required = [
181 "bar",
182 ],
183 target_required = [
184 "qux",
185 "bazqux",
186 ],
187)`,
188 },
189 }
190
191 dir := "."
192 for _, testCase := range testCases {
193 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
194 ctx := android.NewTestContext(config)
195 ctx.RegisterModuleType("custom", customModuleFactory)
196 ctx.Register()
197
198 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
199 android.FailIfErrored(t, errs)
200 _, errs = ctx.PrepareBuildActions(config)
201 android.FailIfErrored(t, errs)
202
Jingwen Chen73850672020-12-14 08:25:34 -0500203 bazelTargets := GenerateSoongModuleTargets(ctx.Context.Context, false)[dir]
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800204 if g, w := len(bazelTargets), 1; g != w {
205 t.Fatalf("Expected %d bazel target, got %d", w, g)
206 }
207
208 actualBazelTarget := bazelTargets[0]
209 if actualBazelTarget.content != testCase.expectedBazelTarget {
210 t.Errorf(
211 "Expected generated Bazel target to be '%s', got '%s'",
212 testCase.expectedBazelTarget,
Jingwen Chen73850672020-12-14 08:25:34 -0500213 actualBazelTarget.content,
214 )
215 }
216 }
217}
218
219func TestGenerateBazelTargetModules(t *testing.T) {
220 testCases := []struct {
221 bp string
222 expectedBazelTarget string
223 }{
224 {
225 bp: `custom {
226 name: "foo",
227 string_list_prop: ["a", "b"],
228 string_prop: "a",
229}`,
230 expectedBazelTarget: `custom(
231 name = "foo",
232 string_list_prop = [
233 "a",
234 "b",
235 ],
236 string_prop = "a",
237)`,
238 },
239 }
240
241 dir := "."
242 for _, testCase := range testCases {
243 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
244 ctx := android.NewTestContext(config)
245 ctx.RegisterModuleType("custom", customModuleFactory)
246 ctx.RegisterBp2BuildMutator("custom", customBp2BuildMutator)
247 ctx.RegisterForBazelConversion()
248
249 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
250 android.FailIfErrored(t, errs)
251 _, errs = ctx.ResolveDependencies(config)
252 android.FailIfErrored(t, errs)
253
254 bazelTargets := GenerateSoongModuleTargets(ctx.Context.Context, true)[dir]
255 if g, w := len(bazelTargets), 1; g != w {
256 t.Fatalf("Expected %d bazel target, got %d", w, g)
257 }
258
259 actualBazelTarget := bazelTargets[0]
260 if actualBazelTarget.content != testCase.expectedBazelTarget {
261 t.Errorf(
262 "Expected generated Bazel target to be '%s', got '%s'",
263 testCase.expectedBazelTarget,
264 actualBazelTarget.content,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800265 )
266 }
267 }
268}
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500269
270func TestModuleTypeBp2Build(t *testing.T) {
271 testCases := []struct {
272 moduleTypeUnderTest string
273 moduleTypeUnderTestFactory android.ModuleFactory
274 bp string
275 expectedBazelTarget string
276 }{
277 {
278 moduleTypeUnderTest: "filegroup",
279 moduleTypeUnderTestFactory: android.FileGroupFactory,
280 bp: `filegroup {
281 name: "foo",
282 srcs: [],
283}`,
284 expectedBazelTarget: `filegroup(
285 name = "foo",
286 srcs = [
287 ],
288)`,
289 },
290 {
291 moduleTypeUnderTest: "filegroup",
292 moduleTypeUnderTestFactory: android.FileGroupFactory,
293 bp: `filegroup {
294 name: "foo",
295 srcs: ["a", "b"],
296}`,
297 expectedBazelTarget: `filegroup(
298 name = "foo",
299 srcs = [
300 "a",
301 "b",
302 ],
303)`,
304 },
305 }
306
307 dir := "."
308 for _, testCase := range testCases {
309 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
310 ctx := android.NewTestContext(config)
311 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
312 ctx.RegisterForBazelConversion()
313
314 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
315 android.FailIfErrored(t, errs)
316 _, errs = ctx.ResolveDependencies(config)
317 android.FailIfErrored(t, errs)
318
319 bazelTargets := GenerateSoongModuleTargets(ctx.Context.Context, true)[dir]
320 if g, w := len(bazelTargets), 1; g != w {
321 t.Fatalf("Expected %d bazel target, got %d", w, g)
322 }
323
324 actualBazelTarget := bazelTargets[0]
325 if actualBazelTarget.content != testCase.expectedBazelTarget {
326 t.Errorf(
327 "Expected generated Bazel target to be '%s', got '%s'",
328 testCase.expectedBazelTarget,
329 actualBazelTarget.content,
330 )
331 }
332 }
333}