blob: b69c4eab7ba58c04eb51e2abc4cea65ba42663c0 [file] [log] [blame]
Jingwen Chen13b9b422021-03-08 07:32:28 -05001package bp2build
2
3import (
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +02004 "testing"
5
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux560cb662021-08-26 20:13:29 +00006 "android/soong/android"
Wei Li7d8f6182022-10-11 14:38:16 -07007 "android/soong/genrule"
Jingwen Chen13b9b422021-03-08 07:32:28 -05008 "android/soong/python"
Jingwen Chen13b9b422021-03-08 07:32:28 -05009)
10
Sam Delmerico3177a6e2022-06-21 19:28:33 +000011func runBp2BuildTestCaseWithPythonLibraries(t *testing.T, tc Bp2buildTestCase) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -050012 t.Helper()
Sam Delmerico3177a6e2022-06-21 19:28:33 +000013 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux560cb662021-08-26 20:13:29 +000014 ctx.RegisterModuleType("python_library", python.PythonLibraryFactory)
15 ctx.RegisterModuleType("python_library_host", python.PythonLibraryHostFactory)
Wei Li7d8f6182022-10-11 14:38:16 -070016 ctx.RegisterModuleType("genrule", genrule.GenRuleFactory)
17 ctx.RegisterModuleType("python_defaults", python.DefaultsFactory)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux560cb662021-08-26 20:13:29 +000018 }, tc)
19}
20
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020021func TestPythonBinaryHostSimple(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +000022 runBp2BuildTestCaseWithPythonLibraries(t, Bp2buildTestCase{
23 Description: "simple python_binary_host converts to a native py_binary",
24 ModuleTypeUnderTest: "python_binary_host",
25 ModuleTypeUnderTestFactory: python.PythonBinaryHostFactory,
26 Filesystem: map[string]string{
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020027 "a.py": "",
28 "b/c.py": "",
29 "b/d.py": "",
30 "b/e.py": "",
31 "files/data.txt": "",
32 },
Chris Parsonscd209032023-09-19 01:12:48 +000033 StubbedBuildDefinitions: []string{"bar"},
Sam Delmerico3177a6e2022-06-21 19:28:33 +000034 Blueprint: `python_binary_host {
Jingwen Chen13b9b422021-03-08 07:32:28 -050035 name: "foo",
36 main: "a.py",
Jingwen Chenb4628eb2021-04-08 14:40:57 +000037 srcs: ["**/*.py"],
38 exclude_srcs: ["b/e.py"],
39 data: ["files/data.txt",],
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux560cb662021-08-26 20:13:29 +000040 libs: ["bar"],
Jingwen Chen13b9b422021-03-08 07:32:28 -050041 bazel_module: { bp2build_available: true },
42}
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux560cb662021-08-26 20:13:29 +000043 python_library_host {
44 name: "bar",
45 srcs: ["b/e.py"],
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux560cb662021-08-26 20:13:29 +000046 }`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +000047 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +000048 MakeBazelTarget("py_binary", "foo", AttrNameToString{
Cole Faust01243362022-06-02 12:11:12 -070049 "data": `["files/data.txt"]`,
50 "deps": `[":bar"]`,
51 "main": `"a.py"`,
52 "imports": `["."]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -050053 "srcs": `[
Jingwen Chen13b9b422021-03-08 07:32:28 -050054 "a.py",
55 "b/c.py",
56 "b/d.py",
Liz Kammer78cfdaa2021-11-08 12:56:31 -050057 ]`,
Sam Delmerico75539d62022-01-31 14:37:29 +000058 "target_compatible_with": `select({
59 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
60 "//conditions:default": [],
61 })`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -050062 }),
Jingwen Chen13b9b422021-03-08 07:32:28 -050063 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020064 })
65}
66
67func TestPythonBinaryHostPy2(t *testing.T) {
Trevor Radcliffe1b4b2d92022-09-01 18:57:01 +000068 RunBp2BuildTestCaseSimple(t, Bp2buildTestCase{
Sam Delmerico3177a6e2022-06-21 19:28:33 +000069 Description: "py2 python_binary_host",
70 ModuleTypeUnderTest: "python_binary_host",
71 ModuleTypeUnderTestFactory: python.PythonBinaryHostFactory,
72 Blueprint: `python_binary_host {
Jingwen Chen13b9b422021-03-08 07:32:28 -050073 name: "foo",
74 srcs: ["a.py"],
75 version: {
76 py2: {
77 enabled: true,
78 },
79 py3: {
80 enabled: false,
81 },
82 },
83
84 bazel_module: { bp2build_available: true },
85}
86`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +000087 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +000088 MakeBazelTarget("py_binary", "foo", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -050089 "python_version": `"PY2"`,
Cole Faust01243362022-06-02 12:11:12 -070090 "imports": `["."]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -050091 "srcs": `["a.py"]`,
Sam Delmerico75539d62022-01-31 14:37:29 +000092 "target_compatible_with": `select({
93 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
94 "//conditions:default": [],
95 })`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -050096 }),
Jingwen Chen13b9b422021-03-08 07:32:28 -050097 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020098 })
99}
100
101func TestPythonBinaryHostPy3(t *testing.T) {
Trevor Radcliffe1b4b2d92022-09-01 18:57:01 +0000102 RunBp2BuildTestCaseSimple(t, Bp2buildTestCase{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000103 Description: "py3 python_binary_host",
104 ModuleTypeUnderTest: "python_binary_host",
105 ModuleTypeUnderTestFactory: python.PythonBinaryHostFactory,
106 Blueprint: `python_binary_host {
Jingwen Chen13b9b422021-03-08 07:32:28 -0500107 name: "foo",
108 srcs: ["a.py"],
109 version: {
110 py2: {
111 enabled: false,
112 },
113 py3: {
114 enabled: true,
115 },
116 },
117
118 bazel_module: { bp2build_available: true },
119}
120`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000121 ExpectedBazelTargets: []string{
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200122 // python_version is PY3 by default.
Alixe06d75b2022-08-31 18:28:19 +0000123 MakeBazelTarget("py_binary", "foo", AttrNameToString{
Cole Faust01243362022-06-02 12:11:12 -0700124 "imports": `["."]`,
125 "srcs": `["a.py"]`,
Sam Delmerico75539d62022-01-31 14:37:29 +0000126 "target_compatible_with": `select({
127 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
128 "//conditions:default": [],
129 })`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500130 }),
Jingwen Chen13b9b422021-03-08 07:32:28 -0500131 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200132 })
Jingwen Chen13b9b422021-03-08 07:32:28 -0500133}
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +0000134
135func TestPythonBinaryHostArchVariance(t *testing.T) {
Trevor Radcliffe1b4b2d92022-09-01 18:57:01 +0000136 RunBp2BuildTestCaseSimple(t, Bp2buildTestCase{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000137 Description: "test arch variants",
138 ModuleTypeUnderTest: "python_binary_host",
139 ModuleTypeUnderTestFactory: python.PythonBinaryHostFactory,
140 Filesystem: map[string]string{
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +0000141 "dir/arm.py": "",
142 "dir/x86.py": "",
143 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000144 Blueprint: `python_binary_host {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +0000145 name: "foo-arm",
146 arch: {
147 arm: {
148 srcs: ["arm.py"],
149 },
150 x86: {
151 srcs: ["x86.py"],
152 },
153 },
154 }`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000155 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000156 MakeBazelTarget("py_binary", "foo-arm", AttrNameToString{
Cole Faust01243362022-06-02 12:11:12 -0700157 "imports": `["."]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500158 "srcs": `select({
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +0000159 "//build/bazel/platforms/arch:arm": ["arm.py"],
160 "//build/bazel/platforms/arch:x86": ["x86.py"],
161 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500162 })`,
Sam Delmerico75539d62022-01-31 14:37:29 +0000163 "target_compatible_with": `select({
164 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
165 "//conditions:default": [],
166 })`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500167 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +0000168 },
169 })
170}
Wei Li7d8f6182022-10-11 14:38:16 -0700171
172func TestPythonBinaryMainIsNotSpecified(t *testing.T) {
173 runBp2BuildTestCaseWithPythonLibraries(t, Bp2buildTestCase{
174 Description: "python_binary_host main label in same package",
175 ModuleTypeUnderTest: "python_binary_host",
176 ModuleTypeUnderTestFactory: python.PythonBinaryHostFactory,
177 Blueprint: `python_binary_host {
178 name: "foo",
179 bazel_module: { bp2build_available: true },
180}
181`,
182 ExpectedBazelTargets: []string{
183 MakeBazelTarget("py_binary", "foo", AttrNameToString{
184 "imports": `["."]`,
185 "target_compatible_with": `select({
186 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
187 "//conditions:default": [],
188 })`,
189 }),
190 },
191 })
192}
193
194func TestPythonBinaryMainIsLabel(t *testing.T) {
195 runBp2BuildTestCaseWithPythonLibraries(t, Bp2buildTestCase{
196 Description: "python_binary_host main label in same package",
197 ModuleTypeUnderTest: "python_binary_host",
198 ModuleTypeUnderTestFactory: python.PythonBinaryHostFactory,
Chris Parsonscd209032023-09-19 01:12:48 +0000199 StubbedBuildDefinitions: []string{"a"},
Wei Li7d8f6182022-10-11 14:38:16 -0700200 Blueprint: `python_binary_host {
201 name: "foo",
202 main: ":a",
203 bazel_module: { bp2build_available: true },
204}
205
206genrule {
207 name: "a",
Wei Li7d8f6182022-10-11 14:38:16 -0700208}
209`,
210 ExpectedBazelTargets: []string{
211 MakeBazelTarget("py_binary", "foo", AttrNameToString{
212 "main": `":a"`,
213 "imports": `["."]`,
214 "target_compatible_with": `select({
215 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
216 "//conditions:default": [],
217 })`,
218 }),
219 },
220 })
221}
222
223func TestPythonBinaryMainIsSubpackageFile(t *testing.T) {
224 runBp2BuildTestCaseWithPythonLibraries(t, Bp2buildTestCase{
225 Description: "python_binary_host main is subpackage file",
226 ModuleTypeUnderTest: "python_binary_host",
227 ModuleTypeUnderTestFactory: python.PythonBinaryHostFactory,
228 Filesystem: map[string]string{
229 "a/Android.bp": "",
230 "a/b.py": "",
231 },
232 Blueprint: `python_binary_host {
233 name: "foo",
234 main: "a/b.py",
235 bazel_module: { bp2build_available: true },
236}
237
238`,
239 ExpectedBazelTargets: []string{
240 MakeBazelTarget("py_binary", "foo", AttrNameToString{
241 "main": `"//a:b.py"`,
242 "imports": `["."]`,
243 "target_compatible_with": `select({
244 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
245 "//conditions:default": [],
246 })`,
247 }),
248 },
249 })
250}
251
252func TestPythonBinaryMainIsSubDirFile(t *testing.T) {
253 runBp2BuildTestCaseWithPythonLibraries(t, Bp2buildTestCase{
254 Description: "python_binary_host main is file in sub directory that is not Bazel package",
255 ModuleTypeUnderTest: "python_binary_host",
256 ModuleTypeUnderTestFactory: python.PythonBinaryHostFactory,
257 Filesystem: map[string]string{
258 "a/b.py": "",
259 },
260 Blueprint: `python_binary_host {
261 name: "foo",
262 main: "a/b.py",
263 bazel_module: { bp2build_available: true },
264}
265
266`,
267 ExpectedBazelTargets: []string{
268 MakeBazelTarget("py_binary", "foo", AttrNameToString{
269 "main": `"a/b.py"`,
270 "imports": `["."]`,
271 "target_compatible_with": `select({
272 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
273 "//conditions:default": [],
274 })`,
275 }),
276 },
277 })
278}
279
280func TestPythonBinaryDuplicatesInRequired(t *testing.T) {
281 runBp2BuildTestCaseWithPythonLibraries(t, Bp2buildTestCase{
282 Description: "python_binary_host duplicates in required attribute of the module and its defaults",
283 ModuleTypeUnderTest: "python_binary_host",
284 ModuleTypeUnderTestFactory: python.PythonBinaryHostFactory,
Chris Parsonscd209032023-09-19 01:12:48 +0000285 StubbedBuildDefinitions: []string{"r1", "r2"},
Wei Li7d8f6182022-10-11 14:38:16 -0700286 Blueprint: `python_binary_host {
287 name: "foo",
288 main: "a.py",
289 defaults: ["d"],
290 required: [
291 "r1",
292 ],
293 bazel_module: { bp2build_available: true },
294}
295
296python_defaults {
297 name: "d",
298 required: [
299 "r1",
300 "r2",
301 ],
Chris Parsonscd209032023-09-19 01:12:48 +0000302}` + simpleModule("genrule", "r1") +
303 simpleModule("genrule", "r2"),
Wei Li7d8f6182022-10-11 14:38:16 -0700304
305 ExpectedBazelTargets: []string{
306 MakeBazelTarget("py_binary", "foo", AttrNameToString{
307 "main": `"a.py"`,
308 "imports": `["."]`,
309 "data": `[
310 ":r1",
311 ":r2",
312 ]`,
313 "target_compatible_with": `select({
314 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
315 "//conditions:default": [],
316 })`,
317 }),
318 },
319 })
320}