blob: b8659c6bf7bad73942c069848f3a2d0eb76b4673 [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",
16 python.PythonLibraryFactory, python.PythonLibraryBp2Build)
17}
18
19func TestPythonLibraryHost(t *testing.T) {
20 testPythonLib(t, "python_library_host",
21 python.PythonLibraryHostFactory, python.PythonLibraryHostBp2Build)
22}
23
24func testPythonLib(t *testing.T, modType string,
25 factory android.ModuleFactory, mutator PythonLibBp2Build) {
26 t.Helper()
27 // Simple
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000028 runBp2BuildTestCaseSimple(t, bp2buildTestCase{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxdc212c02021-08-23 20:21:19 +000029 description: fmt.Sprintf("simple %s converts to a native py_library", modType),
30 moduleTypeUnderTest: modType,
31 moduleTypeUnderTestFactory: factory,
32 moduleTypeUnderTestBp2BuildMutator: mutator,
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000033 filesystem: map[string]string{
34 "a.py": "",
35 "b/c.py": "",
36 "b/d.py": "",
37 "b/e.py": "",
38 "files/data.txt": "",
39 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxdc212c02021-08-23 20:21:19 +000040 blueprint: fmt.Sprintf(`%s {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000041 name: "foo",
42 srcs: ["**/*.py"],
43 exclude_srcs: ["b/e.py"],
44 data: ["files/data.txt",],
45 bazel_module: { bp2build_available: true },
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxdc212c02021-08-23 20:21:19 +000046}`, modType),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000047 expectedBazelTargets: []string{`py_library(
48 name = "foo",
49 data = ["files/data.txt"],
50 srcs = [
51 "a.py",
52 "b/c.py",
53 "b/d.py",
54 ],
55 srcs_version = "PY3",
56)`,
57 },
58 })
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000059
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxdc212c02021-08-23 20:21:19 +000060 // PY2
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000061 runBp2BuildTestCaseSimple(t, bp2buildTestCase{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxdc212c02021-08-23 20:21:19 +000062 description: fmt.Sprintf("py2 %s converts to a native py_library", modType),
63 moduleTypeUnderTest: modType,
64 moduleTypeUnderTestFactory: factory,
65 moduleTypeUnderTestBp2BuildMutator: mutator,
66 blueprint: fmt.Sprintf(`%s {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000067 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 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxdc212c02021-08-23 20:21:19 +000079}`, modType),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000080 expectedBazelTargets: []string{`py_library(
81 name = "foo",
82 srcs = ["a.py"],
83 srcs_version = "PY2",
84)`,
85 },
86 })
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000087
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxdc212c02021-08-23 20:21:19 +000088 // PY3
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000089 runBp2BuildTestCaseSimple(t, bp2buildTestCase{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxdc212c02021-08-23 20:21:19 +000090 description: fmt.Sprintf("py3 %s converts to a native py_library", modType),
91 moduleTypeUnderTest: modType,
92 moduleTypeUnderTestFactory: factory,
93 moduleTypeUnderTestBp2BuildMutator: mutator,
94 blueprint: fmt.Sprintf(`%s {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000095 name: "foo",
96 srcs: ["a.py"],
97 version: {
98 py2: {
99 enabled: false,
100 },
101 py3: {
102 enabled: true,
103 },
104 },
105
106 bazel_module: { bp2build_available: true },
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxdc212c02021-08-23 20:21:19 +0000107}`, modType),
108 expectedBazelTargets: []string{`py_library(
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +0000109 name = "foo",
110 srcs = ["a.py"],
111 srcs_version = "PY3",
112)`,
113 },
114 })
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +0000115
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxdc212c02021-08-23 20:21:19 +0000116 // Both
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +0000117 runBp2BuildTestCaseSimple(t, bp2buildTestCase{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxdc212c02021-08-23 20:21:19 +0000118 description: fmt.Sprintf("py2&3 %s converts to a native py_library", modType),
119 moduleTypeUnderTest: modType,
120 moduleTypeUnderTestFactory: factory,
121 moduleTypeUnderTestBp2BuildMutator: mutator,
122 blueprint: fmt.Sprintf(`%s {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +0000123 name: "foo",
124 srcs: ["a.py"],
125 version: {
126 py2: {
127 enabled: true,
128 },
129 py3: {
130 enabled: true,
131 },
132 },
133
134 bazel_module: { bp2build_available: true },
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxdc212c02021-08-23 20:21:19 +0000135}`, modType),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +0000136 expectedBazelTargets: []string{
137 // srcs_version is PY2ANDPY3 by default.
138 `py_library(
139 name = "foo",
140 srcs = ["a.py"],
141)`,
142 },
143 })
144}