blob: b6f45e53ee0de9927095faae8be0f8fc251b3e69 [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
14func TestPythonLibrary(t *testing.T) {
15 testPythonLib(t, "python_library",
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux560cb662021-08-26 20:13:29 +000016 python.PythonLibraryFactory, python.PythonLibraryBp2Build,
17 func(ctx android.RegistrationContext) {})
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxdc212c02021-08-23 20:21:19 +000018}
19
20func TestPythonLibraryHost(t *testing.T) {
21 testPythonLib(t, "python_library_host",
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux560cb662021-08-26 20:13:29 +000022 python.PythonLibraryHostFactory, python.PythonLibraryHostBp2Build,
23 func(ctx android.RegistrationContext) {
24 ctx.RegisterModuleType("python_library", python.PythonLibraryFactory)
25 })
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxdc212c02021-08-23 20:21:19 +000026}
27
28func testPythonLib(t *testing.T, modType string,
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux560cb662021-08-26 20:13:29 +000029 factory android.ModuleFactory, mutator PythonLibBp2Build,
30 registration func(ctx android.RegistrationContext)) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxdc212c02021-08-23 20:21:19 +000031 t.Helper()
32 // Simple
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux560cb662021-08-26 20:13:29 +000033 runBp2BuildTestCase(t, registration, bp2buildTestCase{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxdc212c02021-08-23 20:21:19 +000034 description: fmt.Sprintf("simple %s converts to a native py_library", modType),
35 moduleTypeUnderTest: modType,
36 moduleTypeUnderTestFactory: factory,
37 moduleTypeUnderTestBp2BuildMutator: mutator,
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000038 filesystem: map[string]string{
39 "a.py": "",
40 "b/c.py": "",
41 "b/d.py": "",
42 "b/e.py": "",
43 "files/data.txt": "",
44 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxdc212c02021-08-23 20:21:19 +000045 blueprint: fmt.Sprintf(`%s {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000046 name: "foo",
47 srcs: ["**/*.py"],
48 exclude_srcs: ["b/e.py"],
49 data: ["files/data.txt",],
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux560cb662021-08-26 20:13:29 +000050 libs: ["bar"],
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000051 bazel_module: { bp2build_available: true },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux560cb662021-08-26 20:13:29 +000052}
53 python_library {
54 name: "bar",
55 srcs: ["b/e.py"],
56 bazel_module: { bp2build_available: false },
57 }`, modType),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000058 expectedBazelTargets: []string{`py_library(
59 name = "foo",
60 data = ["files/data.txt"],
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux560cb662021-08-26 20:13:29 +000061 deps = [":bar"],
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000062 srcs = [
63 "a.py",
64 "b/c.py",
65 "b/d.py",
66 ],
67 srcs_version = "PY3",
68)`,
69 },
70 })
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000071
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxdc212c02021-08-23 20:21:19 +000072 // PY2
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000073 runBp2BuildTestCaseSimple(t, bp2buildTestCase{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxdc212c02021-08-23 20:21:19 +000074 description: fmt.Sprintf("py2 %s converts to a native py_library", modType),
75 moduleTypeUnderTest: modType,
76 moduleTypeUnderTestFactory: factory,
77 moduleTypeUnderTestBp2BuildMutator: mutator,
78 blueprint: fmt.Sprintf(`%s {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000079 name: "foo",
80 srcs: ["a.py"],
81 version: {
82 py2: {
83 enabled: true,
84 },
85 py3: {
86 enabled: false,
87 },
88 },
89
90 bazel_module: { bp2build_available: true },
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxdc212c02021-08-23 20:21:19 +000091}`, modType),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000092 expectedBazelTargets: []string{`py_library(
93 name = "foo",
94 srcs = ["a.py"],
95 srcs_version = "PY2",
96)`,
97 },
98 })
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000099
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxdc212c02021-08-23 20:21:19 +0000100 // PY3
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +0000101 runBp2BuildTestCaseSimple(t, bp2buildTestCase{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxdc212c02021-08-23 20:21:19 +0000102 description: fmt.Sprintf("py3 %s converts to a native py_library", modType),
103 moduleTypeUnderTest: modType,
104 moduleTypeUnderTestFactory: factory,
105 moduleTypeUnderTestBp2BuildMutator: mutator,
106 blueprint: fmt.Sprintf(`%s {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +0000107 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 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxdc212c02021-08-23 20:21:19 +0000119}`, modType),
120 expectedBazelTargets: []string{`py_library(
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +0000121 name = "foo",
122 srcs = ["a.py"],
123 srcs_version = "PY3",
124)`,
125 },
126 })
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +0000127
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxdc212c02021-08-23 20:21:19 +0000128 // Both
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +0000129 runBp2BuildTestCaseSimple(t, bp2buildTestCase{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxdc212c02021-08-23 20:21:19 +0000130 description: fmt.Sprintf("py2&3 %s converts to a native py_library", modType),
131 moduleTypeUnderTest: modType,
132 moduleTypeUnderTestFactory: factory,
133 moduleTypeUnderTestBp2BuildMutator: mutator,
134 blueprint: fmt.Sprintf(`%s {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +0000135 name: "foo",
136 srcs: ["a.py"],
137 version: {
138 py2: {
139 enabled: true,
140 },
141 py3: {
142 enabled: true,
143 },
144 },
145
146 bazel_module: { bp2build_available: true },
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxdc212c02021-08-23 20:21:19 +0000147}`, modType),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +0000148 expectedBazelTargets: []string{
149 // srcs_version is PY2ANDPY3 by default.
150 `py_library(
151 name = "foo",
152 srcs = ["a.py"],
153)`,
154 },
155 })
156}