blob: 356d52e8c5e1f364394dd072288d97c96cd15dec [file] [log] [blame]
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +00001package bp2build
2
3import (
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxdc212c02021-08-23 20:21:19 +00004 "fmt"
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +00005 "testing"
6
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxdc212c02021-08-23 20:21:19 +00007 "android/soong/android"
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +00008 "android/soong/python"
9)
10
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxdc212c02021-08-23 20:21:19 +000011// TODO(alexmarquez): Should be lifted into a generic Bp2Build file
12type PythonLibBp2Build func(ctx android.TopDownMutatorContext)
13
Sam Delmerico75539d62022-01-31 14:37:29 +000014type pythonLibBp2BuildTestCase struct {
15 description string
16 filesystem map[string]string
17 blueprint string
18 expectedBazelTargets []testBazelTarget
19}
20
21func convertPythonLibTestCaseToBp2build_Host(tc pythonLibBp2BuildTestCase) bp2buildTestCase {
22 for i := range tc.expectedBazelTargets {
23 tc.expectedBazelTargets[i].attrs["target_compatible_with"] = `select({
24 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
25 "//conditions:default": [],
26 })`
27 }
28
29 return convertPythonLibTestCaseToBp2build(tc)
30}
31
32func convertPythonLibTestCaseToBp2build(tc pythonLibBp2BuildTestCase) bp2buildTestCase {
33 var bp2BuildTargets []string
34 for _, t := range tc.expectedBazelTargets {
35 bp2BuildTargets = append(bp2BuildTargets, makeBazelTarget(t.typ, t.name, t.attrs))
36 }
37 return bp2buildTestCase{
38 description: tc.description,
39 filesystem: tc.filesystem,
40 blueprint: tc.blueprint,
41 expectedBazelTargets: bp2BuildTargets,
42 }
43}
44
45func runPythonLibraryTestCase(t *testing.T, tc pythonLibBp2BuildTestCase) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxdc212c02021-08-23 20:21:19 +000046 t.Helper()
Sam Delmerico75539d62022-01-31 14:37:29 +000047 testCase := convertPythonLibTestCaseToBp2build(tc)
Liz Kammer78cfdaa2021-11-08 12:56:31 -050048 testCase.description = fmt.Sprintf(testCase.description, "python_library")
49 testCase.blueprint = fmt.Sprintf(testCase.blueprint, "python_library")
50 testCase.moduleTypeUnderTest = "python_library"
51 testCase.moduleTypeUnderTestFactory = python.PythonLibraryFactory
Sam Delmerico75539d62022-01-31 14:37:29 +000052
Liz Kammer78cfdaa2021-11-08 12:56:31 -050053 runBp2BuildTestCaseSimple(t, testCase)
54}
55
Sam Delmerico75539d62022-01-31 14:37:29 +000056func runPythonLibraryHostTestCase(t *testing.T, tc pythonLibBp2BuildTestCase) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -050057 t.Helper()
Sam Delmerico75539d62022-01-31 14:37:29 +000058 testCase := convertPythonLibTestCaseToBp2build_Host(tc)
Liz Kammer78cfdaa2021-11-08 12:56:31 -050059 testCase.description = fmt.Sprintf(testCase.description, "python_library_host")
60 testCase.blueprint = fmt.Sprintf(testCase.blueprint, "python_library_host")
61 testCase.moduleTypeUnderTest = "python_library_host"
62 testCase.moduleTypeUnderTestFactory = python.PythonLibraryHostFactory
Liz Kammer78cfdaa2021-11-08 12:56:31 -050063 runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {
64 ctx.RegisterModuleType("python_library", python.PythonLibraryFactory)
65 },
66 testCase)
67}
68
Sam Delmerico75539d62022-01-31 14:37:29 +000069func runPythonLibraryTestCases(t *testing.T, tc pythonLibBp2BuildTestCase) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -050070 t.Helper()
71 runPythonLibraryTestCase(t, tc)
72 runPythonLibraryHostTestCase(t, tc)
73}
74
75func TestSimplePythonLib(t *testing.T) {
Sam Delmerico75539d62022-01-31 14:37:29 +000076 testCases := []pythonLibBp2BuildTestCase{
Liz Kammer78cfdaa2021-11-08 12:56:31 -050077 {
78 description: "simple %s converts to a native py_library",
79 filesystem: map[string]string{
80 "a.py": "",
81 "b/c.py": "",
82 "b/d.py": "",
83 "b/e.py": "",
84 "files/data.txt": "",
85 },
86 blueprint: `%s {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000087 name: "foo",
88 srcs: ["**/*.py"],
89 exclude_srcs: ["b/e.py"],
90 data: ["files/data.txt",],
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux560cb662021-08-26 20:13:29 +000091 libs: ["bar"],
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000092 bazel_module: { bp2build_available: true },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux560cb662021-08-26 20:13:29 +000093}
94 python_library {
95 name: "bar",
96 srcs: ["b/e.py"],
97 bazel_module: { bp2build_available: false },
Liz Kammer78cfdaa2021-11-08 12:56:31 -050098 }`,
Sam Delmerico75539d62022-01-31 14:37:29 +000099 expectedBazelTargets: []testBazelTarget{
100 {
101 typ: "py_library",
102 name: "foo",
103 attrs: attrNameToString{
104 "data": `["files/data.txt"]`,
105 "deps": `[":bar"]`,
106 "srcs": `[
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +0000107 "a.py",
108 "b/c.py",
109 "b/d.py",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500110 ]`,
Sam Delmerico75539d62022-01-31 14:37:29 +0000111 "srcs_version": `"PY3"`,
112 },
113 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500114 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +0000115 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500116 {
117 description: "py2 %s converts to a native py_library",
118 blueprint: `%s {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +0000119 name: "foo",
120 srcs: ["a.py"],
121 version: {
122 py2: {
123 enabled: true,
124 },
125 py3: {
126 enabled: false,
127 },
128 },
129
130 bazel_module: { bp2build_available: true },
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500131}`,
Sam Delmerico75539d62022-01-31 14:37:29 +0000132 expectedBazelTargets: []testBazelTarget{
133 {
134 typ: "py_library",
135 name: "foo",
136 attrs: attrNameToString{
137 "srcs": `["a.py"]`,
138 "srcs_version": `"PY2"`,
139 },
140 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500141 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +0000142 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500143 {
144 description: "py3 %s converts to a native py_library",
145 blueprint: `%s {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +0000146 name: "foo",
147 srcs: ["a.py"],
148 version: {
149 py2: {
150 enabled: false,
151 },
152 py3: {
153 enabled: true,
154 },
155 },
156
157 bazel_module: { bp2build_available: true },
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500158}`,
Sam Delmerico75539d62022-01-31 14:37:29 +0000159 expectedBazelTargets: []testBazelTarget{
160 {
161 typ: "py_library",
162 name: "foo",
163 attrs: attrNameToString{
164 "srcs": `["a.py"]`,
165 "srcs_version": `"PY3"`,
166 },
167 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500168 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +0000169 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500170 {
171 description: "py2&3 %s converts to a native py_library",
172 blueprint: `%s {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +0000173 name: "foo",
174 srcs: ["a.py"],
175 version: {
176 py2: {
177 enabled: true,
178 },
179 py3: {
180 enabled: true,
181 },
182 },
183
184 bazel_module: { bp2build_available: true },
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500185}`,
Sam Delmerico75539d62022-01-31 14:37:29 +0000186 expectedBazelTargets: []testBazelTarget{
187 {
188 // srcs_version is PY2ANDPY3 by default.
189 typ: "py_library",
190 name: "foo",
191 attrs: attrNameToString{
192 "srcs": `["a.py"]`,
193 },
194 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500195 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +0000196 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500197 }
198
199 for _, tc := range testCases {
200 t.Run(tc.description, func(t *testing.T) {
201 runPythonLibraryTestCases(t, tc)
202 })
203 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +0000204}
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +0000205
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500206func TestPythonArchVariance(t *testing.T) {
Sam Delmerico75539d62022-01-31 14:37:29 +0000207 runPythonLibraryTestCases(t, pythonLibBp2BuildTestCase{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500208 description: "test %s arch variants",
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +0000209 filesystem: map[string]string{
210 "dir/arm.py": "",
211 "dir/x86.py": "",
212 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500213 blueprint: `%s {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +0000214 name: "foo",
215 arch: {
216 arm: {
217 srcs: ["arm.py"],
218 },
219 x86: {
220 srcs: ["x86.py"],
221 },
222 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500223 }`,
Sam Delmerico75539d62022-01-31 14:37:29 +0000224 expectedBazelTargets: []testBazelTarget{
225 {
226 typ: "py_library",
227 name: "foo",
228 attrs: attrNameToString{
229 "srcs": `select({
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +0000230 "//build/bazel/platforms/arch:arm": ["arm.py"],
231 "//build/bazel/platforms/arch:x86": ["x86.py"],
232 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500233 })`,
Sam Delmerico75539d62022-01-31 14:37:29 +0000234 "srcs_version": `"PY3"`,
235 },
236 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +0000237 },
238 })
239}