blob: e8b9bc41cc727579f2c6eee2da8096d7915f5314 [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
Sam Delmerico3177a6e2022-06-21 19:28:33 +000010func runBp2BuildTestCaseWithPythonLibraries(t *testing.T, tc Bp2buildTestCase) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -050011 t.Helper()
Sam Delmerico3177a6e2022-06-21 19:28:33 +000012 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux560cb662021-08-26 20:13:29 +000013 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) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +000019 runBp2BuildTestCaseWithPythonLibraries(t, Bp2buildTestCase{
20 Description: "simple python_binary_host converts to a native py_binary",
21 ModuleTypeUnderTest: "python_binary_host",
22 ModuleTypeUnderTestFactory: python.PythonBinaryHostFactory,
23 Filesystem: map[string]string{
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020024 "a.py": "",
25 "b/c.py": "",
26 "b/d.py": "",
27 "b/e.py": "",
28 "files/data.txt": "",
29 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +000030 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 }`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +000044 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +000045 MakeBazelTarget("py_binary", "foo", AttrNameToString{
Cole Faust01243362022-06-02 12:11:12 -070046 "data": `["files/data.txt"]`,
47 "deps": `[":bar"]`,
48 "main": `"a.py"`,
49 "imports": `["."]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -050050 "srcs": `[
Jingwen Chen13b9b422021-03-08 07:32:28 -050051 "a.py",
52 "b/c.py",
53 "b/d.py",
Liz Kammer78cfdaa2021-11-08 12:56:31 -050054 ]`,
Sam Delmerico75539d62022-01-31 14:37:29 +000055 "target_compatible_with": `select({
56 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
57 "//conditions:default": [],
58 })`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -050059 }),
Jingwen Chen13b9b422021-03-08 07:32:28 -050060 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020061 })
62}
63
64func TestPythonBinaryHostPy2(t *testing.T) {
Trevor Radcliffe1b4b2d92022-09-01 18:57:01 +000065 RunBp2BuildTestCaseSimple(t, Bp2buildTestCase{
Sam Delmerico3177a6e2022-06-21 19:28:33 +000066 Description: "py2 python_binary_host",
67 ModuleTypeUnderTest: "python_binary_host",
68 ModuleTypeUnderTestFactory: python.PythonBinaryHostFactory,
69 Blueprint: `python_binary_host {
Jingwen Chen13b9b422021-03-08 07:32:28 -050070 name: "foo",
71 srcs: ["a.py"],
72 version: {
73 py2: {
74 enabled: true,
75 },
76 py3: {
77 enabled: false,
78 },
79 },
80
81 bazel_module: { bp2build_available: true },
82}
83`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +000084 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +000085 MakeBazelTarget("py_binary", "foo", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -050086 "python_version": `"PY2"`,
Cole Faust01243362022-06-02 12:11:12 -070087 "imports": `["."]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -050088 "srcs": `["a.py"]`,
Sam Delmerico75539d62022-01-31 14:37:29 +000089 "target_compatible_with": `select({
90 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
91 "//conditions:default": [],
92 })`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -050093 }),
Jingwen Chen13b9b422021-03-08 07:32:28 -050094 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020095 })
96}
97
98func TestPythonBinaryHostPy3(t *testing.T) {
Trevor Radcliffe1b4b2d92022-09-01 18:57:01 +000099 RunBp2BuildTestCaseSimple(t, Bp2buildTestCase{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000100 Description: "py3 python_binary_host",
101 ModuleTypeUnderTest: "python_binary_host",
102 ModuleTypeUnderTestFactory: python.PythonBinaryHostFactory,
103 Blueprint: `python_binary_host {
Jingwen Chen13b9b422021-03-08 07:32:28 -0500104 name: "foo",
105 srcs: ["a.py"],
106 version: {
107 py2: {
108 enabled: false,
109 },
110 py3: {
111 enabled: true,
112 },
113 },
114
115 bazel_module: { bp2build_available: true },
116}
117`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000118 ExpectedBazelTargets: []string{
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200119 // python_version is PY3 by default.
Alixe06d75b2022-08-31 18:28:19 +0000120 MakeBazelTarget("py_binary", "foo", AttrNameToString{
Cole Faust01243362022-06-02 12:11:12 -0700121 "imports": `["."]`,
122 "srcs": `["a.py"]`,
Sam Delmerico75539d62022-01-31 14:37:29 +0000123 "target_compatible_with": `select({
124 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
125 "//conditions:default": [],
126 })`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500127 }),
Jingwen Chen13b9b422021-03-08 07:32:28 -0500128 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200129 })
Jingwen Chen13b9b422021-03-08 07:32:28 -0500130}
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +0000131
132func TestPythonBinaryHostArchVariance(t *testing.T) {
Trevor Radcliffe1b4b2d92022-09-01 18:57:01 +0000133 RunBp2BuildTestCaseSimple(t, Bp2buildTestCase{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000134 Description: "test arch variants",
135 ModuleTypeUnderTest: "python_binary_host",
136 ModuleTypeUnderTestFactory: python.PythonBinaryHostFactory,
137 Filesystem: map[string]string{
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +0000138 "dir/arm.py": "",
139 "dir/x86.py": "",
140 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000141 Blueprint: `python_binary_host {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +0000142 name: "foo-arm",
143 arch: {
144 arm: {
145 srcs: ["arm.py"],
146 },
147 x86: {
148 srcs: ["x86.py"],
149 },
150 },
151 }`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000152 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000153 MakeBazelTarget("py_binary", "foo-arm", AttrNameToString{
Cole Faust01243362022-06-02 12:11:12 -0700154 "imports": `["."]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500155 "srcs": `select({
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +0000156 "//build/bazel/platforms/arch:arm": ["arm.py"],
157 "//build/bazel/platforms/arch:x86": ["x86.py"],
158 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500159 })`,
Sam Delmerico75539d62022-01-31 14:37:29 +0000160 "target_compatible_with": `select({
161 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
162 "//conditions:default": [],
163 })`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500164 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +0000165 },
166 })
167}