blob: 01b6aa2685b3fd8fd94cf1da4422128687be5029 [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"
Jingwen Chen13b9b422021-03-08 07:32:28 -05007 "android/soong/python"
Jingwen Chen13b9b422021-03-08 07:32:28 -05008)
9
Liz Kammer78cfdaa2021-11-08 12:56:31 -050010func runBp2BuildTestCaseWithPythonLibraries(t *testing.T, tc bp2buildTestCase) {
11 t.Helper()
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux560cb662021-08-26 20:13:29 +000012 runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {
13 ctx.RegisterModuleType("python_library", python.PythonLibraryFactory)
14 ctx.RegisterModuleType("python_library_host", python.PythonLibraryHostFactory)
15 }, tc)
16}
17
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020018func TestPythonBinaryHostSimple(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -050019 runBp2BuildTestCaseWithPythonLibraries(t, bp2buildTestCase{
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020020 description: "simple python_binary_host converts to a native py_binary",
21 moduleTypeUnderTest: "python_binary_host",
22 moduleTypeUnderTestFactory: python.PythonBinaryHostFactory,
23 moduleTypeUnderTestBp2BuildMutator: python.PythonBinaryBp2Build,
24 filesystem: map[string]string{
25 "a.py": "",
26 "b/c.py": "",
27 "b/d.py": "",
28 "b/e.py": "",
29 "files/data.txt": "",
30 },
31 blueprint: `python_binary_host {
Jingwen Chen13b9b422021-03-08 07:32:28 -050032 name: "foo",
33 main: "a.py",
Jingwen Chenb4628eb2021-04-08 14:40:57 +000034 srcs: ["**/*.py"],
35 exclude_srcs: ["b/e.py"],
36 data: ["files/data.txt",],
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux560cb662021-08-26 20:13:29 +000037 libs: ["bar"],
Jingwen Chen13b9b422021-03-08 07:32:28 -050038 bazel_module: { bp2build_available: true },
39}
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux560cb662021-08-26 20:13:29 +000040 python_library_host {
41 name: "bar",
42 srcs: ["b/e.py"],
43 bazel_module: { bp2build_available: true },
44 }`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -050045 expectedBazelTargets: []string{
46 makeBazelTarget("py_binary", "foo", attrNameToString{
47 "data": `["files/data.txt"]`,
48 "deps": `[":bar"]`,
49 "main": `"a.py"`,
50 "srcs": `[
Jingwen Chen13b9b422021-03-08 07:32:28 -050051 "a.py",
52 "b/c.py",
53 "b/d.py",
Liz Kammer78cfdaa2021-11-08 12:56:31 -050054 ]`,
55 }),
Jingwen Chen13b9b422021-03-08 07:32:28 -050056 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020057 })
58}
59
60func TestPythonBinaryHostPy2(t *testing.T) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxce0a07e2021-08-23 16:17:32 +000061 runBp2BuildTestCaseSimple(t, bp2buildTestCase{
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020062 description: "py2 python_binary_host",
63 moduleTypeUnderTest: "python_binary_host",
64 moduleTypeUnderTestFactory: python.PythonBinaryHostFactory,
65 moduleTypeUnderTestBp2BuildMutator: python.PythonBinaryBp2Build,
66 blueprint: `python_binary_host {
Jingwen Chen13b9b422021-03-08 07:32:28 -050067 name: "foo",
68 srcs: ["a.py"],
69 version: {
70 py2: {
71 enabled: true,
72 },
73 py3: {
74 enabled: false,
75 },
76 },
77
78 bazel_module: { bp2build_available: true },
79}
80`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -050081 expectedBazelTargets: []string{
82 makeBazelTarget("py_binary", "foo", attrNameToString{
83 "python_version": `"PY2"`,
84 "srcs": `["a.py"]`,
85 }),
Jingwen Chen13b9b422021-03-08 07:32:28 -050086 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020087 })
88}
89
90func TestPythonBinaryHostPy3(t *testing.T) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxce0a07e2021-08-23 16:17:32 +000091 runBp2BuildTestCaseSimple(t, bp2buildTestCase{
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020092 description: "py3 python_binary_host",
93 moduleTypeUnderTest: "python_binary_host",
94 moduleTypeUnderTestFactory: python.PythonBinaryHostFactory,
95 moduleTypeUnderTestBp2BuildMutator: python.PythonBinaryBp2Build,
96 blueprint: `python_binary_host {
Jingwen Chen13b9b422021-03-08 07:32:28 -050097 name: "foo",
98 srcs: ["a.py"],
99 version: {
100 py2: {
101 enabled: false,
102 },
103 py3: {
104 enabled: true,
105 },
106 },
107
108 bazel_module: { bp2build_available: true },
109}
110`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200111 expectedBazelTargets: []string{
112 // python_version is PY3 by default.
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500113 makeBazelTarget("py_binary", "foo", attrNameToString{
114 "srcs": `["a.py"]`,
115 }),
Jingwen Chen13b9b422021-03-08 07:32:28 -0500116 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200117 })
Jingwen Chen13b9b422021-03-08 07:32:28 -0500118}
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +0000119
120func TestPythonBinaryHostArchVariance(t *testing.T) {
121 runBp2BuildTestCaseSimple(t, bp2buildTestCase{
122 description: "test arch variants",
123 moduleTypeUnderTest: "python_binary_host",
124 moduleTypeUnderTestFactory: python.PythonBinaryHostFactory,
125 moduleTypeUnderTestBp2BuildMutator: python.PythonBinaryBp2Build,
126 filesystem: map[string]string{
127 "dir/arm.py": "",
128 "dir/x86.py": "",
129 },
130 blueprint: `python_binary_host {
131 name: "foo-arm",
132 arch: {
133 arm: {
134 srcs: ["arm.py"],
135 },
136 x86: {
137 srcs: ["x86.py"],
138 },
139 },
140 }`,
141 expectedBazelTargets: []string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500142 makeBazelTarget("py_binary", "foo-arm", attrNameToString{
143 "srcs": `select({
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +0000144 "//build/bazel/platforms/arch:arm": ["arm.py"],
145 "//build/bazel/platforms/arch:x86": ["x86.py"],
146 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500147 })`,
148 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +0000149 },
150 })
151}