blob: dfa11d1be14aab6bb0a68982019895c72b6d630a [file] [log] [blame]
Jingwen Chen13b9b422021-03-08 07:32:28 -05001package bp2build
2
3import (
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +02004 "testing"
5
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux560cb662021-08-26 20:13:29 +00006 "android/soong/android"
Jingwen Chen13b9b422021-03-08 07:32:28 -05007 "android/soong/python"
Jingwen Chen13b9b422021-03-08 07:32:28 -05008)
9
Liz Kammer78cfdaa2021-11-08 12:56:31 -050010func runBp2BuildTestCaseWithPythonLibraries(t *testing.T, tc bp2buildTestCase) {
11 t.Helper()
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux560cb662021-08-26 20:13:29 +000012 runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {
13 ctx.RegisterModuleType("python_library", python.PythonLibraryFactory)
14 ctx.RegisterModuleType("python_library_host", python.PythonLibraryHostFactory)
15 }, tc)
16}
17
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020018func TestPythonBinaryHostSimple(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -050019 runBp2BuildTestCaseWithPythonLibraries(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -040020 description: "simple python_binary_host converts to a native py_binary",
21 moduleTypeUnderTest: "python_binary_host",
22 moduleTypeUnderTestFactory: python.PythonBinaryHostFactory,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020023 filesystem: map[string]string{
24 "a.py": "",
25 "b/c.py": "",
26 "b/d.py": "",
27 "b/e.py": "",
28 "files/data.txt": "",
29 },
30 blueprint: `python_binary_host {
Jingwen Chen13b9b422021-03-08 07:32:28 -050031 name: "foo",
32 main: "a.py",
Jingwen Chenb4628eb2021-04-08 14:40:57 +000033 srcs: ["**/*.py"],
34 exclude_srcs: ["b/e.py"],
35 data: ["files/data.txt",],
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux560cb662021-08-26 20:13:29 +000036 libs: ["bar"],
Jingwen Chen13b9b422021-03-08 07:32:28 -050037 bazel_module: { bp2build_available: true },
38}
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux560cb662021-08-26 20:13:29 +000039 python_library_host {
40 name: "bar",
41 srcs: ["b/e.py"],
Liz Kammerbe46fcc2021-11-01 15:32:43 -040042 bazel_module: { bp2build_available: false },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux560cb662021-08-26 20:13:29 +000043 }`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -050044 expectedBazelTargets: []string{
45 makeBazelTarget("py_binary", "foo", attrNameToString{
46 "data": `["files/data.txt"]`,
47 "deps": `[":bar"]`,
48 "main": `"a.py"`,
49 "srcs": `[
Jingwen Chen13b9b422021-03-08 07:32:28 -050050 "a.py",
51 "b/c.py",
52 "b/d.py",
Liz Kammer78cfdaa2021-11-08 12:56:31 -050053 ]`,
Sam Delmerico75539d62022-01-31 14:37:29 +000054 "target_compatible_with": `select({
55 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
56 "//conditions:default": [],
57 })`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -050058 }),
Jingwen Chen13b9b422021-03-08 07:32:28 -050059 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020060 })
61}
62
63func TestPythonBinaryHostPy2(t *testing.T) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxce0a07e2021-08-23 16:17:32 +000064 runBp2BuildTestCaseSimple(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -040065 description: "py2 python_binary_host",
66 moduleTypeUnderTest: "python_binary_host",
67 moduleTypeUnderTestFactory: python.PythonBinaryHostFactory,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020068 blueprint: `python_binary_host {
Jingwen Chen13b9b422021-03-08 07:32:28 -050069 name: "foo",
70 srcs: ["a.py"],
71 version: {
72 py2: {
73 enabled: true,
74 },
75 py3: {
76 enabled: false,
77 },
78 },
79
80 bazel_module: { bp2build_available: true },
81}
82`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -050083 expectedBazelTargets: []string{
84 makeBazelTarget("py_binary", "foo", attrNameToString{
85 "python_version": `"PY2"`,
86 "srcs": `["a.py"]`,
Sam Delmerico75539d62022-01-31 14:37:29 +000087 "target_compatible_with": `select({
88 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
89 "//conditions:default": [],
90 })`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -050091 }),
Jingwen Chen13b9b422021-03-08 07:32:28 -050092 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020093 })
94}
95
96func TestPythonBinaryHostPy3(t *testing.T) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxce0a07e2021-08-23 16:17:32 +000097 runBp2BuildTestCaseSimple(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -040098 description: "py3 python_binary_host",
99 moduleTypeUnderTest: "python_binary_host",
100 moduleTypeUnderTestFactory: python.PythonBinaryHostFactory,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200101 blueprint: `python_binary_host {
Jingwen Chen13b9b422021-03-08 07:32:28 -0500102 name: "foo",
103 srcs: ["a.py"],
104 version: {
105 py2: {
106 enabled: false,
107 },
108 py3: {
109 enabled: true,
110 },
111 },
112
113 bazel_module: { bp2build_available: true },
114}
115`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200116 expectedBazelTargets: []string{
117 // python_version is PY3 by default.
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500118 makeBazelTarget("py_binary", "foo", attrNameToString{
119 "srcs": `["a.py"]`,
Sam Delmerico75539d62022-01-31 14:37:29 +0000120 "target_compatible_with": `select({
121 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
122 "//conditions:default": [],
123 })`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500124 }),
Jingwen Chen13b9b422021-03-08 07:32:28 -0500125 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200126 })
Jingwen Chen13b9b422021-03-08 07:32:28 -0500127}
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +0000128
129func TestPythonBinaryHostArchVariance(t *testing.T) {
130 runBp2BuildTestCaseSimple(t, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400131 description: "test arch variants",
132 moduleTypeUnderTest: "python_binary_host",
133 moduleTypeUnderTestFactory: python.PythonBinaryHostFactory,
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +0000134 filesystem: map[string]string{
135 "dir/arm.py": "",
136 "dir/x86.py": "",
137 },
138 blueprint: `python_binary_host {
139 name: "foo-arm",
140 arch: {
141 arm: {
142 srcs: ["arm.py"],
143 },
144 x86: {
145 srcs: ["x86.py"],
146 },
147 },
148 }`,
149 expectedBazelTargets: []string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500150 makeBazelTarget("py_binary", "foo-arm", attrNameToString{
151 "srcs": `select({
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +0000152 "//build/bazel/platforms/arch:arm": ["arm.py"],
153 "//build/bazel/platforms/arch:x86": ["x86.py"],
154 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500155 })`,
Sam Delmerico75539d62022-01-31 14:37:29 +0000156 "target_compatible_with": `select({
157 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
158 "//conditions:default": [],
159 })`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500160 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +0000161 },
162 })
163}