forked from bazel-contrib/rules_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpy_executable_base_tests.bzl
More file actions
433 lines (380 loc) · 15.2 KB
/
py_executable_base_tests.bzl
File metadata and controls
433 lines (380 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# Copyright 2023 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests common to py_binary and py_test (executable rules)."""
load("@rules_python//python:py_runtime_info.bzl", RulesPythonPyRuntimeInfo = "PyRuntimeInfo")
load("@rules_python_internal//:rules_python_config.bzl", rp_config = "config")
load("@rules_testing//lib:analysis_test.bzl", "analysis_test")
load("@rules_testing//lib:truth.bzl", "matching")
load("@rules_testing//lib:util.bzl", rt_util = "util")
load("//python:py_executable_info.bzl", "PyExecutableInfo")
load("//python/private:common_labels.bzl", "labels") # buildifier: disable=bzl-visibility
load("//python/private:reexports.bzl", "BuiltinPyRuntimeInfo") # buildifier: disable=bzl-visibility
load("//tests/base_rules:base_tests.bzl", "create_base_tests")
load("//tests/base_rules:util.bzl", "WINDOWS_ATTR", pt_util = "util")
load("//tests/support:py_executable_info_subject.bzl", "PyExecutableInfoSubject")
load("//tests/support:support.bzl", "CC_TOOLCHAIN", "CROSSTOOL_TOP")
load("//tests/support/platforms:platforms.bzl", "platform_targets")
_tests = []
def _test_basic_windows(name, config):
rt_util.helper_target(
config.rule,
name = name + "_subject",
srcs = ["main.py"],
main = "main.py",
)
analysis_test(
name = name,
impl = _test_basic_windows_impl,
target = name + "_subject",
config_settings = {
# NOTE: The default for this flag is based on the Bazel host OS, not
# the target platform. For windows, it defaults to true, so force
# it to that to match behavior when this test runs on other
# platforms.
# Pass value to both native and starlark versions of the flag until
# the native one is removed.
"//command_line_option:build_python_zip": "true",
labels.BUILD_PYTHON_ZIP: True,
"//command_line_option:cpu": "windows_x86_64",
"//command_line_option:crosstool_top": CROSSTOOL_TOP,
"//command_line_option:extra_execution_platforms": [platform_targets.WINDOWS_X86_64],
"//command_line_option:extra_toolchains": [CC_TOOLCHAIN],
"//command_line_option:platforms": [platform_targets.WINDOWS_X86_64],
},
attr_values = {},
)
def _test_basic_windows_impl(env, target):
target = env.expect.that_target(target)
target.executable().path().contains(".exe")
target.runfiles().contains_predicate(matching.str_endswith(
target.meta.format_str("/{name}.zip"),
))
target.runfiles().contains_predicate(matching.str_endswith(
target.meta.format_str("/{name}.exe"),
))
_tests.append(_test_basic_windows)
def _test_basic_zip(name, config):
target_compatible_with = select({
# Disable the new test on windows because we have _test_basic_windows.
"@platforms//os:windows": ["@platforms//:incompatible"],
"//conditions:default": [],
})
rt_util.helper_target(
config.rule,
name = name + "_subject",
srcs = ["main.py"],
main = "main.py",
)
analysis_test(
name = name,
impl = _test_basic_zip_impl,
target = name + "_subject",
config_settings = {
# NOTE: The default for this flag is based on the Bazel host OS, not
# the target platform. For windows, it defaults to true, so force
# it to that to match behavior when this test runs on other
# platforms.
# Pass value to both native and starlark versions of the flag until
# the native one is removed.
"//command_line_option:build_python_zip": "true",
labels.BUILD_PYTHON_ZIP: True,
"//command_line_option:cpu": "linux_x86_64",
"//command_line_option:crosstool_top": CROSSTOOL_TOP,
"//command_line_option:extra_execution_platforms": [platform_targets.LINUX_X86_64],
"//command_line_option:extra_toolchains": [CC_TOOLCHAIN],
"//command_line_option:platforms": [platform_targets.LINUX_X86_64],
},
attr_values = {"target_compatible_with": target_compatible_with},
)
def _test_basic_zip_impl(env, target):
target = env.expect.that_target(target)
target.runfiles().contains_predicate(matching.str_endswith(
target.meta.format_str("/{name}.zip"),
))
target.runfiles().contains_predicate(matching.str_endswith(
target.meta.format_str("/{name}"),
))
_tests.append(_test_basic_zip)
def _test_cross_compile_to_unix(name, config):
rt_util.helper_target(
config.rule,
name = name + "_subject",
main_module = "dummy",
)
analysis_test(
name = name,
impl = _test_cross_compile_to_unix_impl,
target = name + "_subject",
# Cross-compilation of py_test fails since the default test toolchain
# requires an execution platform that matches the target platform.
config_settings = {
"//command_line_option:platforms": [platform_targets.EXOTIC_UNIX],
} if rp_config.bazel_9_or_later and not "py_test" in str(config.rule) else {},
expect_failure = True,
)
def _test_cross_compile_to_unix_impl(_env, _target):
pass
_tests.append(_test_cross_compile_to_unix)
def _test_executable_in_runfiles(name, config):
rt_util.helper_target(
config.rule,
name = name + "_subject",
srcs = [name + "_subject.py"],
)
analysis_test(
name = name,
impl = _test_executable_in_runfiles_impl,
target = name + "_subject",
attrs = WINDOWS_ATTR,
)
_tests.append(_test_executable_in_runfiles)
def _test_executable_in_runfiles_impl(env, target):
if pt_util.is_windows(env):
exe = ".exe"
else:
exe = ""
env.expect.that_target(target).runfiles().contains_at_least([
"{workspace}/{package}/{test_name}_subject" + exe,
])
py_exec_info = env.expect.that_target(target).provider(PyExecutableInfo, factory = PyExecutableInfoSubject.new)
py_exec_info.main().path().contains("_subject.py")
py_exec_info.interpreter_path().contains("python")
py_exec_info.runfiles_without_exe().contains_none_of([
"{workspace}/{package}/{test_name}_subject" + exe,
"{workspace}/{package}/{test_name}_subject",
])
def _test_default_main_can_be_generated(name, config):
rt_util.helper_target(
config.rule,
name = name + "_subject",
srcs = [rt_util.empty_file(name + "_subject.py")],
)
analysis_test(
name = name,
impl = _test_default_main_can_be_generated_impl,
target = name + "_subject",
)
_tests.append(_test_default_main_can_be_generated)
def _test_default_main_can_be_generated_impl(env, target):
env.expect.that_target(target).default_outputs().contains(
"{package}/{test_name}_subject.py",
)
def _test_default_main_can_have_multiple_path_segments(name, config):
rt_util.helper_target(
config.rule,
name = name + "/subject",
srcs = [name + "/subject.py"],
)
analysis_test(
name = name,
impl = _test_default_main_can_have_multiple_path_segments_impl,
target = name + "/subject",
)
_tests.append(_test_default_main_can_have_multiple_path_segments)
def _test_default_main_can_have_multiple_path_segments_impl(env, target):
env.expect.that_target(target).default_outputs().contains(
"{package}/{test_name}/subject.py",
)
def _test_default_main_must_be_in_srcs(name, config):
rt_util.helper_target(
config.rule,
name = name + "_subject",
srcs = ["other.py"],
)
analysis_test(
name = name,
impl = _test_default_main_must_be_in_srcs_impl,
target = name + "_subject",
expect_failure = True,
)
_tests.append(_test_default_main_must_be_in_srcs)
def _test_default_main_must_be_in_srcs_impl(env, target):
env.expect.that_target(target).failures().contains_predicate(
matching.str_matches("default*does not appear in srcs"),
)
def _test_default_main_cannot_be_ambiguous(name, config):
rt_util.helper_target(
config.rule,
name = name + "_subject",
srcs = [name + "_subject.py", "other/{}_subject.py".format(name)],
)
analysis_test(
name = name,
impl = _test_default_main_cannot_be_ambiguous_impl,
target = name + "_subject",
expect_failure = True,
)
_tests.append(_test_default_main_cannot_be_ambiguous)
def _test_default_main_cannot_be_ambiguous_impl(env, target):
env.expect.that_target(target).failures().contains_predicate(
matching.str_matches("default main*matches multiple files"),
)
def _test_explicit_main(name, config):
rt_util.helper_target(
config.rule,
name = name + "_subject",
srcs = ["custom.py"],
main = "custom.py",
)
analysis_test(
name = name,
impl = _test_explicit_main_impl,
target = name + "_subject",
)
_tests.append(_test_explicit_main)
def _test_explicit_main_impl(env, target):
# There isn't a direct way to ask what main file was selected, so we
# rely on it being in the default outputs.
env.expect.that_target(target).default_outputs().contains(
"{package}/custom.py",
)
def _test_explicit_main_cannot_be_ambiguous(name, config):
rt_util.helper_target(
config.rule,
name = name + "_subject",
srcs = ["x/foo.py", "y/foo.py"],
main = "foo.py",
)
analysis_test(
name = name,
impl = _test_explicit_main_cannot_be_ambiguous_impl,
target = name + "_subject",
expect_failure = True,
)
_tests.append(_test_explicit_main_cannot_be_ambiguous)
def _test_explicit_main_cannot_be_ambiguous_impl(env, target):
env.expect.that_target(target).failures().contains_predicate(
matching.str_matches("foo.py*matches multiple"),
)
def _test_files_to_build(name, config):
rt_util.helper_target(
config.rule,
name = name + "_subject",
srcs = [name + "_subject.py"],
)
analysis_test(
name = name,
impl = _test_files_to_build_impl,
target = name + "_subject",
attrs = WINDOWS_ATTR,
)
_tests.append(_test_files_to_build)
def _test_files_to_build_impl(env, target):
default_outputs = env.expect.that_target(target).default_outputs()
if pt_util.is_windows(env):
default_outputs.contains("{package}/{test_name}_subject.exe")
else:
default_outputs.contains_exactly([
"{package}/{test_name}_subject",
"{package}/{test_name}_subject.py",
])
# As of Bazel 7, the first default output is the executable, so
# verify that is the case. rules_testing
# DepsetFileSubject.contains_exactly doesn't provide an in_order()
# call, nor access to the underlying depset, so we have to do things
# manually.
first_default_output = target[DefaultInfo].files.to_list()[0]
executable = target[DefaultInfo].files_to_run.executable
env.expect.that_file(first_default_output).equals(executable)
def _test_name_cannot_end_in_py(name, config):
rt_util.helper_target(
config.rule,
name = name + "_subject.py",
srcs = ["main.py"],
)
analysis_test(
name = name,
impl = _test_name_cannot_end_in_py_impl,
target = name + "_subject.py",
expect_failure = True,
)
_tests.append(_test_name_cannot_end_in_py)
def _test_name_cannot_end_in_py_impl(env, target):
env.expect.that_target(target).failures().contains_predicate(
matching.str_matches("name must not end in*.py"),
)
def _test_main_module_bootstrap_system_python(name, config):
rt_util.helper_target(
config.rule,
name = name + "_subject",
main_module = "dummy",
)
analysis_test(
name = name,
impl = _test_main_module_bootstrap_system_python_impl,
target = name + "_subject",
config_settings = {
labels.BOOTSTRAP_IMPL: "system_python",
"//command_line_option:extra_execution_platforms": ["@bazel_tools//tools:host_platform", platform_targets.LINUX_X86_64],
"//command_line_option:platforms": [platform_targets.LINUX_X86_64],
},
)
def _test_main_module_bootstrap_system_python_impl(env, target):
env.expect.that_target(target).default_outputs().contains(
"{package}/{test_name}_subject",
)
_tests.append(_test_main_module_bootstrap_system_python)
def _test_main_module_bootstrap_script(name, config):
rt_util.helper_target(
config.rule,
name = name + "_subject",
main_module = "dummy",
)
analysis_test(
name = name,
impl = _test_main_module_bootstrap_script_impl,
target = name + "_subject",
config_settings = {
labels.BOOTSTRAP_IMPL: "script",
"//command_line_option:extra_execution_platforms": ["@bazel_tools//tools:host_platform", platform_targets.LINUX_X86_64],
"//command_line_option:platforms": [platform_targets.LINUX_X86_64],
},
)
def _test_main_module_bootstrap_script_impl(env, target):
env.expect.that_target(target).default_outputs().contains(
"{package}/{test_name}_subject",
)
_tests.append(_test_main_module_bootstrap_script)
def _test_py_runtime_info_provided(name, config):
rt_util.helper_target(
config.rule,
name = name + "_subject",
srcs = [name + "_subject.py"],
)
analysis_test(
name = name,
impl = _test_py_runtime_info_provided_impl,
target = name + "_subject",
)
def _test_py_runtime_info_provided_impl(env, target):
# Make sure that the rules_python loaded symbol is provided.
env.expect.that_target(target).has_provider(RulesPythonPyRuntimeInfo)
if BuiltinPyRuntimeInfo != None:
# For compatibility during the transition, the builtin PyRuntimeInfo should
# also be provided.
env.expect.that_target(target).has_provider(BuiltinPyRuntimeInfo)
_tests.append(_test_py_runtime_info_provided)
# =====
# You were gonna add a test at the end, weren't you?
# Nope. Please keep them sorted; put it in its alphabetical location.
# Here's the alphabet so you don't have to sing that song in your head:
# A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
# =====
def create_executable_tests(config):
def _executable_with_srcs_wrapper(name, **kwargs):
if not kwargs.get("srcs"):
kwargs["srcs"] = [name + ".py"]
config.rule(name = name, **kwargs)
config = pt_util.struct_with(config, base_test_rule = _executable_with_srcs_wrapper)
return pt_util.create_tests(_tests, config = config) + create_base_tests(config = config)