blob: 8db784d6b35e050916e63c9b3cd99fbae0195e85 [file] [log] [blame]
Jingwen Chen69d4cbe2020-08-07 14:16:34 +00001// 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 main
16
17import (
18 "android/soong/android"
19 "io/ioutil"
20 "os"
21 "testing"
22)
23
24var buildDir string
25
26func setUp() {
27 var err error
28 buildDir, err = ioutil.TempDir("", "bazel_overlay_test")
29 if err != nil {
30 panic(err)
31 }
32}
33
34func tearDown() {
35 os.RemoveAll(buildDir)
36}
37
38func TestMain(m *testing.M) {
39 run := func() int {
40 setUp()
41 defer tearDown()
42
43 return m.Run()
44 }
45
46 os.Exit(run())
47}
48
49type customModule struct {
50 android.ModuleBase
51}
52
53func (m *customModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
54 // nothing for now.
55}
56
57func customModuleFactory() android.Module {
58 module := &customModule{}
59 android.InitAndroidModule(module)
60 return module
61}
62
63func TestGenerateBazelOverlayFromBlueprint(t *testing.T) {
64 testCases := []struct {
65 bp string
66 expectedBazelTarget string
67 }{
68 {
69 bp: `custom {
70 name: "foo",
71}
72 `,
73 expectedBazelTarget: `soong_module(
74 name = "foo",
75 module_name = "foo",
76 module_type = "custom",
77 module_variant = "",
Jingwen Chence3d46f2020-08-25 05:34:43 +000078 module_deps = [
Jingwen Chen69d4cbe2020-08-07 14:16:34 +000079 ],
80)`,
81 },
82 {
83 bp: `custom {
84 name: "foo",
85 ramdisk: true,
86}
87 `,
88 expectedBazelTarget: `soong_module(
89 name = "foo",
90 module_name = "foo",
91 module_type = "custom",
92 module_variant = "",
Jingwen Chence3d46f2020-08-25 05:34:43 +000093 module_deps = [
Jingwen Chen69d4cbe2020-08-07 14:16:34 +000094 ],
95 ramdisk = True,
96)`,
97 },
98 {
99 bp: `custom {
100 name: "foo",
101 owner: "a_string_with\"quotes\"_and_\\backslashes\\\\",
102}
103 `,
104 expectedBazelTarget: `soong_module(
105 name = "foo",
106 module_name = "foo",
107 module_type = "custom",
108 module_variant = "",
Jingwen Chence3d46f2020-08-25 05:34:43 +0000109 module_deps = [
Jingwen Chen69d4cbe2020-08-07 14:16:34 +0000110 ],
111 owner = "a_string_with\"quotes\"_and_\\backslashes\\\\",
112)`,
113 },
114 {
115 bp: `custom {
116 name: "foo",
117 required: ["bar"],
118}
119 `,
120 expectedBazelTarget: `soong_module(
121 name = "foo",
122 module_name = "foo",
123 module_type = "custom",
124 module_variant = "",
Jingwen Chence3d46f2020-08-25 05:34:43 +0000125 module_deps = [
Jingwen Chen69d4cbe2020-08-07 14:16:34 +0000126 ],
127 required = [
128 "bar",
129 ],
130)`,
131 },
132 {
133 bp: `custom {
134 name: "foo",
135 target_required: ["qux", "bazqux"],
136}
137 `,
138 expectedBazelTarget: `soong_module(
139 name = "foo",
140 module_name = "foo",
141 module_type = "custom",
142 module_variant = "",
Jingwen Chence3d46f2020-08-25 05:34:43 +0000143 module_deps = [
Jingwen Chen69d4cbe2020-08-07 14:16:34 +0000144 ],
145 target_required = [
146 "qux",
147 "bazqux",
148 ],
149)`,
150 },
151 {
152 bp: `custom {
153 name: "foo",
154 dist: {
155 targets: ["goal_foo"],
156 tag: ".foo",
157 },
158 dists: [
159 {
160 targets: ["goal_bar"],
161 tag: ".bar",
162 },
163 ],
164}
165 `,
166 expectedBazelTarget: `soong_module(
167 name = "foo",
168 module_name = "foo",
169 module_type = "custom",
170 module_variant = "",
Jingwen Chence3d46f2020-08-25 05:34:43 +0000171 module_deps = [
Jingwen Chen69d4cbe2020-08-07 14:16:34 +0000172 ],
173 dist = {
174 "tag": ".foo",
175 "targets": [
176 "goal_foo",
177 ],
178 },
179 dists = [
180 {
181 "tag": ".bar",
182 "targets": [
183 "goal_bar",
184 ],
185 },
186 ],
187)`,
188 },
189 {
190 bp: `custom {
191 name: "foo",
192 required: ["bar"],
193 target_required: ["qux", "bazqux"],
194 ramdisk: true,
195 owner: "custom_owner",
196 dists: [
197 {
198 tag: ".tag",
199 targets: ["my_goal"],
200 },
201 ],
202}
203 `,
204 expectedBazelTarget: `soong_module(
205 name = "foo",
206 module_name = "foo",
207 module_type = "custom",
208 module_variant = "",
Jingwen Chence3d46f2020-08-25 05:34:43 +0000209 module_deps = [
Jingwen Chen69d4cbe2020-08-07 14:16:34 +0000210 ],
211 dists = [
212 {
213 "tag": ".tag",
214 "targets": [
215 "my_goal",
216 ],
217 },
218 ],
219 owner = "custom_owner",
220 ramdisk = True,
221 required = [
222 "bar",
223 ],
224 target_required = [
225 "qux",
226 "bazqux",
227 ],
228)`,
229 },
230 }
231
232 for _, testCase := range testCases {
233 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
234 ctx := android.NewTestContext()
235 ctx.RegisterModuleType("custom", customModuleFactory)
236 ctx.Register(config)
237
238 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
239 android.FailIfErrored(t, errs)
240 _, errs = ctx.PrepareBuildActions(config)
241 android.FailIfErrored(t, errs)
242
243 module := ctx.ModuleForTests("foo", "").Module().(*customModule)
244 blueprintCtx := ctx.Context.Context
245
246 actualBazelTarget := generateSoongModuleTarget(blueprintCtx, module)
247 if actualBazelTarget != testCase.expectedBazelTarget {
248 t.Errorf(
249 "Expected generated Bazel target to be '%s', got '%s'",
250 testCase.expectedBazelTarget,
251 actualBazelTarget,
252 )
253 }
254 }
255}