Skip to content
File

Blob: src/workerd/server/tests/python/vendor_pkg_tests/vendor_test.bzl

3.4 KB
1load("//:build/python_metadata.bzl", "BUNDLE_VERSION_INFO")
2load("//src/workerd/server/tests/python:py_wd_test.bzl", "compute_python_flags", "py_wd_test")
3 
4def _vendored_py_wd_test(name, version, test_template, main_py_file, vendored_srcs_target_prefix, level, data, **kwds):
5 """Creates a Python Workers test which includes vendored packages in its bundle, the
6 http_archive target containing the vendored sources should be specified in `vendored_srcs_target_prefix`.
7 
8 Args:
9 name: Name of the test
10 version: The version of the package bundle
11 test_template: The .wd-test template file
12 main_py_file: The main Python file for the test
13 vendored_srcs_target_prefix: The prefix of the Bazel target containing the vendored sources
14 """
15 vendored_srcs_target = vendored_srcs_target_prefix + "_" + version + "//:all_srcs"
16 
17 # Generate module list
18 module_list_name = name + "_modules_string" + "_" + version
19 substitution_name = name + "_perform_substitution" + "_" + version
20 native.genrule(
21 name = substitution_name,
22 srcs = [
23 test_template,
24 vendored_srcs_target,
25 "//src/workerd/server/tests/python/vendor_pkg_tests:generate_modules.py",
26 ],
27 outs = [name + ".test.generated" + "_" + version],
28 cmd = """
29 # Create a file with all the file paths to avoid Windows command line length limits
30 echo "$(locations %s)" > paths.txt
31 $(execpath @python_3_13//:python3) \
32 $(location //src/workerd/server/tests/python/vendor_pkg_tests:generate_modules.py) \
33 --level=%s \
34 --template=$(location %s) \
35 --out=$@ \
36 @paths.txt
37 """ % (vendored_srcs_target, level, test_template),
38 tools = ["@python_3_13//:python3"],
39 )
40 
41 # Create the py_wd_test
42 py_wd_test(
43 name = name,
44 src = ":" + substitution_name,
45 python_flags = [version],
46 data = data + [
47 main_py_file,
48 vendored_srcs_target,
49 ],
50 # Disable on windows because of flakiness
51 # TODO fix this
52 target_compatible_with = select({
53 "@platforms//os:windows": ["@platforms//:incompatible"],
54 "//conditions:default": [],
55 }),
56 **kwds
57 )
58 
59def vendored_py_wd_test(
60 name,
61 test_template = None,
62 main_py_file = None,
63 vendored_srcs_target_prefix = None,
64 python_flags = "all",
65 skip_python_flags = [],
66 vendored_package_name = None,
67 level = 1,
68 data = [],
69 **kwds):
70 python_flags = compute_python_flags(python_flags, skip_python_flags)
71 bzl_name = "%s_vendor_test" % name
72 if test_template == None:
73 test_template = "%s_vendor.wd-test" % name
74 if main_py_file == None:
75 main_py_file = "%s.py" % name
76 if vendored_package_name == None:
77 vendored_package_name = name
78 if vendored_srcs_target_prefix == None:
79 vendored_srcs_target_prefix = "@%s_src" % vendored_package_name
80 
81 for flag in python_flags:
82 info = BUNDLE_VERSION_INFO[flag]
83 if vendored_package_name not in info["vendored_packages_for_tests"]:
84 fail("Not found", vendored_package_name, "in", info["vendored_packages_for_tests"])
85 _vendored_py_wd_test(bzl_name, info["name"], test_template, main_py_file, vendored_srcs_target_prefix, level = level, data = data, **kwds)