blob: 6b261052c5633d6da855ec621e779cbbb922c452 [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
Liz Kammer78cfdaa2021-11-08 12:56:31 -050021 runBp2BuildTestCaseSimple(t, testCase)
22}
23
24func runPythonLibraryHostTestCase(t *testing.T, tc bp2buildTestCase) {
25 t.Helper()
26 testCase := tc
27 testCase.description = fmt.Sprintf(testCase.description, "python_library_host")
28 testCase.blueprint = fmt.Sprintf(testCase.blueprint, "python_library_host")
29 testCase.moduleTypeUnderTest = "python_library_host"
30 testCase.moduleTypeUnderTestFactory = python.PythonLibraryHostFactory
Liz Kammer78cfdaa2021-11-08 12:56:31 -050031 runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {
32 ctx.RegisterModuleType("python_library", python.PythonLibraryFactory)
33 },
34 testCase)
35}
36
37func runPythonLibraryTestCases(t *testing.T, tc bp2buildTestCase) {
38 t.Helper()
39 runPythonLibraryTestCase(t, tc)
40 runPythonLibraryHostTestCase(t, tc)
41}
42
43func TestSimplePythonLib(t *testing.T) {
44 testCases := []bp2buildTestCase{
45 {
46 description: "simple %s converts to a native py_library",
47 filesystem: map[string]string{
48 "a.py": "",
49 "b/c.py": "",
50 "b/d.py": "",
51 "b/e.py": "",
52 "files/data.txt": "",
53 },
54 blueprint: `%s {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000055 name: "foo",
56 srcs: ["**/*.py"],
57 exclude_srcs: ["b/e.py"],
58 data: ["files/data.txt",],
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux560cb662021-08-26 20:13:29 +000059 libs: ["bar"],
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000060 bazel_module: { bp2build_available: true },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux560cb662021-08-26 20:13:29 +000061}
62 python_library {
63 name: "bar",
64 srcs: ["b/e.py"],
65 bazel_module: { bp2build_available: false },
Liz Kammer78cfdaa2021-11-08 12:56:31 -050066 }`,
67 expectedBazelTargets: []string{
68 makeBazelTarget("py_library", "foo", attrNameToString{
69 "data": `["files/data.txt"]`,
70 "deps": `[":bar"]`,
71 "srcs": `[
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000072 "a.py",
73 "b/c.py",
74 "b/d.py",
Liz Kammer78cfdaa2021-11-08 12:56:31 -050075 ]`,
76 "srcs_version": `"PY3"`,
77 }),
78 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000079 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -050080 {
81 description: "py2 %s converts to a native py_library",
82 blueprint: `%s {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000083 name: "foo",
84 srcs: ["a.py"],
85 version: {
86 py2: {
87 enabled: true,
88 },
89 py3: {
90 enabled: false,
91 },
92 },
93
94 bazel_module: { bp2build_available: true },
Liz Kammer78cfdaa2021-11-08 12:56:31 -050095}`,
96 expectedBazelTargets: []string{
97 makeBazelTarget("py_library", "foo", attrNameToString{
98 "srcs": `["a.py"]`,
99 "srcs_version": `"PY2"`,
100 }),
101 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +0000102 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500103 {
104 description: "py3 %s converts to a native py_library",
105 blueprint: `%s {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +0000106 name: "foo",
107 srcs: ["a.py"],
108 version: {
109 py2: {
110 enabled: false,
111 },
112 py3: {
113 enabled: true,
114 },
115 },
116
117 bazel_module: { bp2build_available: true },
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500118}`,
119 expectedBazelTargets: []string{
120 makeBazelTarget("py_library", "foo", attrNameToString{
121 "srcs": `["a.py"]`,
122 "srcs_version": `"PY3"`,
123 }),
124 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +0000125 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500126 {
127 description: "py2&3 %s converts to a native py_library",
128 blueprint: `%s {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +0000129 name: "foo",
130 srcs: ["a.py"],
131 version: {
132 py2: {
133 enabled: true,
134 },
135 py3: {
136 enabled: true,
137 },
138 },
139
140 bazel_module: { bp2build_available: true },
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500141}`,
142 expectedBazelTargets: []string{
143 // srcs_version is PY2ANDPY3 by default.
144 makeBazelTarget("py_library", "foo", attrNameToString{
145 "srcs": `["a.py"]`,
146 }),
147 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +0000148 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500149 }
150
151 for _, tc := range testCases {
152 t.Run(tc.description, func(t *testing.T) {
153 runPythonLibraryTestCases(t, tc)
154 })
155 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +0000156}
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +0000157
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500158func TestPythonArchVariance(t *testing.T) {
159 runPythonLibraryTestCases(t, bp2buildTestCase{
160 description: "test %s arch variants",
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +0000161 filesystem: map[string]string{
162 "dir/arm.py": "",
163 "dir/x86.py": "",
164 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500165 blueprint: `%s {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +0000166 name: "foo",
167 arch: {
168 arm: {
169 srcs: ["arm.py"],
170 },
171 x86: {
172 srcs: ["x86.py"],
173 },
174 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500175 }`,
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +0000176 expectedBazelTargets: []string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500177 makeBazelTarget("py_library", "foo", attrNameToString{
178 "srcs": `select({
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +0000179 "//build/bazel/platforms/arch:arm": ["arm.py"],
180 "//build/bazel/platforms/arch:x86": ["x86.py"],
181 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500182 })`,
183 "srcs_version": `"PY3"`,
184 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +0000185 },
186 })
187}