blob: e3345921eacfdb451450c7939b67941188fe71d5 [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
Liz Kammer78cfdaa2021-11-08 12:56:31 -050014func runPythonLibraryTestCase(t *testing.T, tc bp2buildTestCase) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxdc212c02021-08-23 20:21:19 +000015 t.Helper()
Liz Kammer78cfdaa2021-11-08 12:56:31 -050016 testCase := tc
17 testCase.description = fmt.Sprintf(testCase.description, "python_library")
18 testCase.blueprint = fmt.Sprintf(testCase.blueprint, "python_library")
19 testCase.moduleTypeUnderTest = "python_library"
20 testCase.moduleTypeUnderTestFactory = python.PythonLibraryFactory
21 testCase.moduleTypeUnderTestBp2BuildMutator = python.PythonLibraryBp2Build
22 runBp2BuildTestCaseSimple(t, testCase)
23}
24
25func runPythonLibraryHostTestCase(t *testing.T, tc bp2buildTestCase) {
26 t.Helper()
27 testCase := tc
28 testCase.description = fmt.Sprintf(testCase.description, "python_library_host")
29 testCase.blueprint = fmt.Sprintf(testCase.blueprint, "python_library_host")
30 testCase.moduleTypeUnderTest = "python_library_host"
31 testCase.moduleTypeUnderTestFactory = python.PythonLibraryHostFactory
32 testCase.moduleTypeUnderTestBp2BuildMutator = python.PythonLibraryHostBp2Build
33 runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {
34 ctx.RegisterModuleType("python_library", python.PythonLibraryFactory)
35 },
36 testCase)
37}
38
39func runPythonLibraryTestCases(t *testing.T, tc bp2buildTestCase) {
40 t.Helper()
41 runPythonLibraryTestCase(t, tc)
42 runPythonLibraryHostTestCase(t, tc)
43}
44
45func TestSimplePythonLib(t *testing.T) {
46 testCases := []bp2buildTestCase{
47 {
48 description: "simple %s converts to a native py_library",
49 filesystem: map[string]string{
50 "a.py": "",
51 "b/c.py": "",
52 "b/d.py": "",
53 "b/e.py": "",
54 "files/data.txt": "",
55 },
56 blueprint: `%s {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000057 name: "foo",
58 srcs: ["**/*.py"],
59 exclude_srcs: ["b/e.py"],
60 data: ["files/data.txt",],
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux560cb662021-08-26 20:13:29 +000061 libs: ["bar"],
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000062 bazel_module: { bp2build_available: true },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux560cb662021-08-26 20:13:29 +000063}
64 python_library {
65 name: "bar",
66 srcs: ["b/e.py"],
67 bazel_module: { bp2build_available: false },
Liz Kammer78cfdaa2021-11-08 12:56:31 -050068 }`,
69 expectedBazelTargets: []string{
70 makeBazelTarget("py_library", "foo", attrNameToString{
71 "data": `["files/data.txt"]`,
72 "deps": `[":bar"]`,
73 "srcs": `[
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000074 "a.py",
75 "b/c.py",
76 "b/d.py",
Liz Kammer78cfdaa2021-11-08 12:56:31 -050077 ]`,
78 "srcs_version": `"PY3"`,
79 }),
80 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000081 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -050082 {
83 description: "py2 %s converts to a native py_library",
84 blueprint: `%s {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000085 name: "foo",
86 srcs: ["a.py"],
87 version: {
88 py2: {
89 enabled: true,
90 },
91 py3: {
92 enabled: false,
93 },
94 },
95
96 bazel_module: { bp2build_available: true },
Liz Kammer78cfdaa2021-11-08 12:56:31 -050097}`,
98 expectedBazelTargets: []string{
99 makeBazelTarget("py_library", "foo", attrNameToString{
100 "srcs": `["a.py"]`,
101 "srcs_version": `"PY2"`,
102 }),
103 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +0000104 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500105 {
106 description: "py3 %s converts to a native py_library",
107 blueprint: `%s {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +0000108 name: "foo",
109 srcs: ["a.py"],
110 version: {
111 py2: {
112 enabled: false,
113 },
114 py3: {
115 enabled: true,
116 },
117 },
118
119 bazel_module: { bp2build_available: true },
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500120}`,
121 expectedBazelTargets: []string{
122 makeBazelTarget("py_library", "foo", attrNameToString{
123 "srcs": `["a.py"]`,
124 "srcs_version": `"PY3"`,
125 }),
126 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +0000127 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500128 {
129 description: "py2&3 %s converts to a native py_library",
130 blueprint: `%s {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +0000131 name: "foo",
132 srcs: ["a.py"],
133 version: {
134 py2: {
135 enabled: true,
136 },
137 py3: {
138 enabled: true,
139 },
140 },
141
142 bazel_module: { bp2build_available: true },
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500143}`,
144 expectedBazelTargets: []string{
145 // srcs_version is PY2ANDPY3 by default.
146 makeBazelTarget("py_library", "foo", attrNameToString{
147 "srcs": `["a.py"]`,
148 }),
149 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +0000150 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500151 }
152
153 for _, tc := range testCases {
154 t.Run(tc.description, func(t *testing.T) {
155 runPythonLibraryTestCases(t, tc)
156 })
157 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +0000158}
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +0000159
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500160func TestPythonArchVariance(t *testing.T) {
161 runPythonLibraryTestCases(t, bp2buildTestCase{
162 description: "test %s arch variants",
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +0000163 filesystem: map[string]string{
164 "dir/arm.py": "",
165 "dir/x86.py": "",
166 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500167 blueprint: `%s {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +0000168 name: "foo",
169 arch: {
170 arm: {
171 srcs: ["arm.py"],
172 },
173 x86: {
174 srcs: ["x86.py"],
175 },
176 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500177 }`,
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +0000178 expectedBazelTargets: []string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500179 makeBazelTarget("py_library", "foo", attrNameToString{
180 "srcs": `select({
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +0000181 "//build/bazel/platforms/arch:arm": ["arm.py"],
182 "//build/bazel/platforms/arch:x86": ["x86.py"],
183 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500184 })`,
185 "srcs_version": `"PY3"`,
186 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +0000187 },
188 })
189}