Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 1 | package bp2build |
| 2 | |
| 3 | import ( |
Lukacs T. Berki | c1cc3b9 | 2021-05-21 09:37:00 +0200 | [diff] [blame] | 4 | "testing" |
| 5 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 560cb66 | 2021-08-26 20:13:29 +0000 | [diff] [blame] | 6 | "android/soong/android" |
Wei Li | 7d8f618 | 2022-10-11 14:38:16 -0700 | [diff] [blame] | 7 | "android/soong/genrule" |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 8 | "android/soong/python" |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 9 | ) |
| 10 | |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 11 | func runBp2BuildTestCaseWithPythonLibraries(t *testing.T, tc Bp2buildTestCase) { |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 12 | t.Helper() |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 13 | RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 560cb66 | 2021-08-26 20:13:29 +0000 | [diff] [blame] | 14 | ctx.RegisterModuleType("python_library", python.PythonLibraryFactory) |
| 15 | ctx.RegisterModuleType("python_library_host", python.PythonLibraryHostFactory) |
Wei Li | 7d8f618 | 2022-10-11 14:38:16 -0700 | [diff] [blame] | 16 | ctx.RegisterModuleType("genrule", genrule.GenRuleFactory) |
| 17 | ctx.RegisterModuleType("python_defaults", python.DefaultsFactory) |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 560cb66 | 2021-08-26 20:13:29 +0000 | [diff] [blame] | 18 | }, tc) |
| 19 | } |
| 20 | |
Lukacs T. Berki | c1cc3b9 | 2021-05-21 09:37:00 +0200 | [diff] [blame] | 21 | func TestPythonBinaryHostSimple(t *testing.T) { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 22 | runBp2BuildTestCaseWithPythonLibraries(t, Bp2buildTestCase{ |
| 23 | Description: "simple python_binary_host converts to a native py_binary", |
| 24 | ModuleTypeUnderTest: "python_binary_host", |
| 25 | ModuleTypeUnderTestFactory: python.PythonBinaryHostFactory, |
| 26 | Filesystem: map[string]string{ |
Lukacs T. Berki | c1cc3b9 | 2021-05-21 09:37:00 +0200 | [diff] [blame] | 27 | "a.py": "", |
| 28 | "b/c.py": "", |
| 29 | "b/d.py": "", |
| 30 | "b/e.py": "", |
| 31 | "files/data.txt": "", |
| 32 | }, |
Chris Parsons | cd20903 | 2023-09-19 01:12:48 +0000 | [diff] [blame^] | 33 | StubbedBuildDefinitions: []string{"bar"}, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 34 | Blueprint: `python_binary_host { |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 35 | name: "foo", |
| 36 | main: "a.py", |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 37 | srcs: ["**/*.py"], |
| 38 | exclude_srcs: ["b/e.py"], |
| 39 | data: ["files/data.txt",], |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 560cb66 | 2021-08-26 20:13:29 +0000 | [diff] [blame] | 40 | libs: ["bar"], |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 41 | bazel_module: { bp2build_available: true }, |
| 42 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 560cb66 | 2021-08-26 20:13:29 +0000 | [diff] [blame] | 43 | python_library_host { |
| 44 | name: "bar", |
| 45 | srcs: ["b/e.py"], |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 560cb66 | 2021-08-26 20:13:29 +0000 | [diff] [blame] | 46 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 47 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 48 | MakeBazelTarget("py_binary", "foo", AttrNameToString{ |
Cole Faust | 0124336 | 2022-06-02 12:11:12 -0700 | [diff] [blame] | 49 | "data": `["files/data.txt"]`, |
| 50 | "deps": `[":bar"]`, |
| 51 | "main": `"a.py"`, |
| 52 | "imports": `["."]`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 53 | "srcs": `[ |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 54 | "a.py", |
| 55 | "b/c.py", |
| 56 | "b/d.py", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 57 | ]`, |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 58 | "target_compatible_with": `select({ |
| 59 | "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], |
| 60 | "//conditions:default": [], |
| 61 | })`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 62 | }), |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 63 | }, |
Lukacs T. Berki | c1cc3b9 | 2021-05-21 09:37:00 +0200 | [diff] [blame] | 64 | }) |
| 65 | } |
| 66 | |
| 67 | func TestPythonBinaryHostPy2(t *testing.T) { |
Trevor Radcliffe | 1b4b2d9 | 2022-09-01 18:57:01 +0000 | [diff] [blame] | 68 | RunBp2BuildTestCaseSimple(t, Bp2buildTestCase{ |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 69 | Description: "py2 python_binary_host", |
| 70 | ModuleTypeUnderTest: "python_binary_host", |
| 71 | ModuleTypeUnderTestFactory: python.PythonBinaryHostFactory, |
| 72 | Blueprint: `python_binary_host { |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 73 | name: "foo", |
| 74 | srcs: ["a.py"], |
| 75 | version: { |
| 76 | py2: { |
| 77 | enabled: true, |
| 78 | }, |
| 79 | py3: { |
| 80 | enabled: false, |
| 81 | }, |
| 82 | }, |
| 83 | |
| 84 | bazel_module: { bp2build_available: true }, |
| 85 | } |
| 86 | `, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 87 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 88 | MakeBazelTarget("py_binary", "foo", AttrNameToString{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 89 | "python_version": `"PY2"`, |
Cole Faust | 0124336 | 2022-06-02 12:11:12 -0700 | [diff] [blame] | 90 | "imports": `["."]`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 91 | "srcs": `["a.py"]`, |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 92 | "target_compatible_with": `select({ |
| 93 | "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], |
| 94 | "//conditions:default": [], |
| 95 | })`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 96 | }), |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 97 | }, |
Lukacs T. Berki | c1cc3b9 | 2021-05-21 09:37:00 +0200 | [diff] [blame] | 98 | }) |
| 99 | } |
| 100 | |
| 101 | func TestPythonBinaryHostPy3(t *testing.T) { |
Trevor Radcliffe | 1b4b2d9 | 2022-09-01 18:57:01 +0000 | [diff] [blame] | 102 | RunBp2BuildTestCaseSimple(t, Bp2buildTestCase{ |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 103 | Description: "py3 python_binary_host", |
| 104 | ModuleTypeUnderTest: "python_binary_host", |
| 105 | ModuleTypeUnderTestFactory: python.PythonBinaryHostFactory, |
| 106 | Blueprint: `python_binary_host { |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 107 | 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 }, |
| 119 | } |
| 120 | `, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 121 | ExpectedBazelTargets: []string{ |
Lukacs T. Berki | c1cc3b9 | 2021-05-21 09:37:00 +0200 | [diff] [blame] | 122 | // python_version is PY3 by default. |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 123 | MakeBazelTarget("py_binary", "foo", AttrNameToString{ |
Cole Faust | 0124336 | 2022-06-02 12:11:12 -0700 | [diff] [blame] | 124 | "imports": `["."]`, |
| 125 | "srcs": `["a.py"]`, |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 126 | "target_compatible_with": `select({ |
| 127 | "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], |
| 128 | "//conditions:default": [], |
| 129 | })`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 130 | }), |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 131 | }, |
Lukacs T. Berki | c1cc3b9 | 2021-05-21 09:37:00 +0200 | [diff] [blame] | 132 | }) |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 133 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 19d399d | 2021-09-17 20:30:21 +0000 | [diff] [blame] | 134 | |
| 135 | func TestPythonBinaryHostArchVariance(t *testing.T) { |
Trevor Radcliffe | 1b4b2d9 | 2022-09-01 18:57:01 +0000 | [diff] [blame] | 136 | RunBp2BuildTestCaseSimple(t, Bp2buildTestCase{ |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 137 | Description: "test arch variants", |
| 138 | ModuleTypeUnderTest: "python_binary_host", |
| 139 | ModuleTypeUnderTestFactory: python.PythonBinaryHostFactory, |
| 140 | Filesystem: map[string]string{ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 19d399d | 2021-09-17 20:30:21 +0000 | [diff] [blame] | 141 | "dir/arm.py": "", |
| 142 | "dir/x86.py": "", |
| 143 | }, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 144 | Blueprint: `python_binary_host { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 19d399d | 2021-09-17 20:30:21 +0000 | [diff] [blame] | 145 | name: "foo-arm", |
| 146 | arch: { |
| 147 | arm: { |
| 148 | srcs: ["arm.py"], |
| 149 | }, |
| 150 | x86: { |
| 151 | srcs: ["x86.py"], |
| 152 | }, |
| 153 | }, |
| 154 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 155 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 156 | MakeBazelTarget("py_binary", "foo-arm", AttrNameToString{ |
Cole Faust | 0124336 | 2022-06-02 12:11:12 -0700 | [diff] [blame] | 157 | "imports": `["."]`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 158 | "srcs": `select({ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 19d399d | 2021-09-17 20:30:21 +0000 | [diff] [blame] | 159 | "//build/bazel/platforms/arch:arm": ["arm.py"], |
| 160 | "//build/bazel/platforms/arch:x86": ["x86.py"], |
| 161 | "//conditions:default": [], |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 162 | })`, |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 163 | "target_compatible_with": `select({ |
| 164 | "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], |
| 165 | "//conditions:default": [], |
| 166 | })`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 167 | }), |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 19d399d | 2021-09-17 20:30:21 +0000 | [diff] [blame] | 168 | }, |
| 169 | }) |
| 170 | } |
Wei Li | 7d8f618 | 2022-10-11 14:38:16 -0700 | [diff] [blame] | 171 | |
| 172 | func TestPythonBinaryMainIsNotSpecified(t *testing.T) { |
| 173 | runBp2BuildTestCaseWithPythonLibraries(t, Bp2buildTestCase{ |
| 174 | Description: "python_binary_host main label in same package", |
| 175 | ModuleTypeUnderTest: "python_binary_host", |
| 176 | ModuleTypeUnderTestFactory: python.PythonBinaryHostFactory, |
| 177 | Blueprint: `python_binary_host { |
| 178 | name: "foo", |
| 179 | bazel_module: { bp2build_available: true }, |
| 180 | } |
| 181 | `, |
| 182 | ExpectedBazelTargets: []string{ |
| 183 | MakeBazelTarget("py_binary", "foo", AttrNameToString{ |
| 184 | "imports": `["."]`, |
| 185 | "target_compatible_with": `select({ |
| 186 | "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], |
| 187 | "//conditions:default": [], |
| 188 | })`, |
| 189 | }), |
| 190 | }, |
| 191 | }) |
| 192 | } |
| 193 | |
| 194 | func TestPythonBinaryMainIsLabel(t *testing.T) { |
| 195 | runBp2BuildTestCaseWithPythonLibraries(t, Bp2buildTestCase{ |
| 196 | Description: "python_binary_host main label in same package", |
| 197 | ModuleTypeUnderTest: "python_binary_host", |
| 198 | ModuleTypeUnderTestFactory: python.PythonBinaryHostFactory, |
Chris Parsons | cd20903 | 2023-09-19 01:12:48 +0000 | [diff] [blame^] | 199 | StubbedBuildDefinitions: []string{"a"}, |
Wei Li | 7d8f618 | 2022-10-11 14:38:16 -0700 | [diff] [blame] | 200 | Blueprint: `python_binary_host { |
| 201 | name: "foo", |
| 202 | main: ":a", |
| 203 | bazel_module: { bp2build_available: true }, |
| 204 | } |
| 205 | |
| 206 | genrule { |
| 207 | name: "a", |
Wei Li | 7d8f618 | 2022-10-11 14:38:16 -0700 | [diff] [blame] | 208 | } |
| 209 | `, |
| 210 | ExpectedBazelTargets: []string{ |
| 211 | MakeBazelTarget("py_binary", "foo", AttrNameToString{ |
| 212 | "main": `":a"`, |
| 213 | "imports": `["."]`, |
| 214 | "target_compatible_with": `select({ |
| 215 | "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], |
| 216 | "//conditions:default": [], |
| 217 | })`, |
| 218 | }), |
| 219 | }, |
| 220 | }) |
| 221 | } |
| 222 | |
| 223 | func TestPythonBinaryMainIsSubpackageFile(t *testing.T) { |
| 224 | runBp2BuildTestCaseWithPythonLibraries(t, Bp2buildTestCase{ |
| 225 | Description: "python_binary_host main is subpackage file", |
| 226 | ModuleTypeUnderTest: "python_binary_host", |
| 227 | ModuleTypeUnderTestFactory: python.PythonBinaryHostFactory, |
| 228 | Filesystem: map[string]string{ |
| 229 | "a/Android.bp": "", |
| 230 | "a/b.py": "", |
| 231 | }, |
| 232 | Blueprint: `python_binary_host { |
| 233 | name: "foo", |
| 234 | main: "a/b.py", |
| 235 | bazel_module: { bp2build_available: true }, |
| 236 | } |
| 237 | |
| 238 | `, |
| 239 | ExpectedBazelTargets: []string{ |
| 240 | MakeBazelTarget("py_binary", "foo", AttrNameToString{ |
| 241 | "main": `"//a:b.py"`, |
| 242 | "imports": `["."]`, |
| 243 | "target_compatible_with": `select({ |
| 244 | "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], |
| 245 | "//conditions:default": [], |
| 246 | })`, |
| 247 | }), |
| 248 | }, |
| 249 | }) |
| 250 | } |
| 251 | |
| 252 | func TestPythonBinaryMainIsSubDirFile(t *testing.T) { |
| 253 | runBp2BuildTestCaseWithPythonLibraries(t, Bp2buildTestCase{ |
| 254 | Description: "python_binary_host main is file in sub directory that is not Bazel package", |
| 255 | ModuleTypeUnderTest: "python_binary_host", |
| 256 | ModuleTypeUnderTestFactory: python.PythonBinaryHostFactory, |
| 257 | Filesystem: map[string]string{ |
| 258 | "a/b.py": "", |
| 259 | }, |
| 260 | Blueprint: `python_binary_host { |
| 261 | name: "foo", |
| 262 | main: "a/b.py", |
| 263 | bazel_module: { bp2build_available: true }, |
| 264 | } |
| 265 | |
| 266 | `, |
| 267 | ExpectedBazelTargets: []string{ |
| 268 | MakeBazelTarget("py_binary", "foo", AttrNameToString{ |
| 269 | "main": `"a/b.py"`, |
| 270 | "imports": `["."]`, |
| 271 | "target_compatible_with": `select({ |
| 272 | "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], |
| 273 | "//conditions:default": [], |
| 274 | })`, |
| 275 | }), |
| 276 | }, |
| 277 | }) |
| 278 | } |
| 279 | |
| 280 | func TestPythonBinaryDuplicatesInRequired(t *testing.T) { |
| 281 | runBp2BuildTestCaseWithPythonLibraries(t, Bp2buildTestCase{ |
| 282 | Description: "python_binary_host duplicates in required attribute of the module and its defaults", |
| 283 | ModuleTypeUnderTest: "python_binary_host", |
| 284 | ModuleTypeUnderTestFactory: python.PythonBinaryHostFactory, |
Chris Parsons | cd20903 | 2023-09-19 01:12:48 +0000 | [diff] [blame^] | 285 | StubbedBuildDefinitions: []string{"r1", "r2"}, |
Wei Li | 7d8f618 | 2022-10-11 14:38:16 -0700 | [diff] [blame] | 286 | Blueprint: `python_binary_host { |
| 287 | name: "foo", |
| 288 | main: "a.py", |
| 289 | defaults: ["d"], |
| 290 | required: [ |
| 291 | "r1", |
| 292 | ], |
| 293 | bazel_module: { bp2build_available: true }, |
| 294 | } |
| 295 | |
| 296 | python_defaults { |
| 297 | name: "d", |
| 298 | required: [ |
| 299 | "r1", |
| 300 | "r2", |
| 301 | ], |
Chris Parsons | cd20903 | 2023-09-19 01:12:48 +0000 | [diff] [blame^] | 302 | }` + simpleModule("genrule", "r1") + |
| 303 | simpleModule("genrule", "r2"), |
Wei Li | 7d8f618 | 2022-10-11 14:38:16 -0700 | [diff] [blame] | 304 | |
| 305 | ExpectedBazelTargets: []string{ |
| 306 | MakeBazelTarget("py_binary", "foo", AttrNameToString{ |
| 307 | "main": `"a.py"`, |
| 308 | "imports": `["."]`, |
| 309 | "data": `[ |
| 310 | ":r1", |
| 311 | ":r2", |
| 312 | ]`, |
| 313 | "target_compatible_with": `select({ |
| 314 | "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], |
| 315 | "//conditions:default": [], |
| 316 | })`, |
| 317 | }), |
| 318 | }, |
| 319 | }) |
| 320 | } |