Skip to content

Commit 94ccb8c

Browse files
authored
test(mocks): extract mock python extension helper (bazel-contrib#3803)
This mock module was extracted from bazel-contrib#3802. It provides a helper for defining a mock module for the python bzlmod extension, which is standalone and generally useful for testing rules_python's bzlmod extension.
1 parent ef09f63 commit 94ccb8c

2 files changed

Lines changed: 108 additions & 0 deletions

File tree

tests/support/mocks/BUILD.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,10 @@ bzl_library(
1010
srcs = ["mocks.bzl"],
1111
)
1212

13+
bzl_library(
14+
name = "python_ext_bzl",
15+
srcs = ["python_ext.bzl"],
16+
deps = [":mocks_bzl"],
17+
)
18+
1319
mocks_test_suite(name = "mocks_tests")

tests/support/mocks/python_ext.bzl

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
"""Helper for defining a mock module for the python bzlmod extension."""
2+
3+
load(":mocks.bzl", "mocks")
4+
5+
def _module(name = "rules_python", is_root = True, **tags):
6+
"""Creates a mock Bzlmod module struct with defaulted tag lists.
7+
8+
Args:
9+
name: The module name.
10+
is_root: Whether this is the root module.
11+
**tags: Lists of tag objects.
12+
13+
Returns:
14+
A mock module struct.
15+
"""
16+
defaulted_tags = {
17+
"defaults": [],
18+
"override": [],
19+
"single_version_override": [],
20+
"single_version_platform_override": [],
21+
"toolchain": [],
22+
}
23+
defaulted_tags.update(tags)
24+
return mocks.module(name = name, is_root = is_root, **defaulted_tags)
25+
26+
def _override(**kwargs):
27+
"""Creates a mock python.override tag with default values."""
28+
attrs = {
29+
"add_runtime_manifest_urls": [],
30+
"add_target_settings": [],
31+
"available_python_versions": [],
32+
"base_url": "https://github.com/astral-sh/python-build-standalone/releases/download",
33+
"ignore_root_user_error": True,
34+
"minor_mapping": {},
35+
"register_all_versions": False,
36+
"runtime_manifest_sha": "",
37+
}
38+
attrs.update(kwargs)
39+
return mocks.tag(**attrs)
40+
41+
def _defaults(**kwargs):
42+
"""Creates a mock python.defaults tag with default values."""
43+
attrs = {
44+
"python_version": "",
45+
"python_version_env": "",
46+
}
47+
attrs.update(kwargs)
48+
return mocks.tag(**attrs)
49+
50+
def _single_version_override(**kwargs):
51+
"""Creates a mock python.single_version_override tag with default values."""
52+
attrs = {
53+
"distutils": None,
54+
"distutils_content": "",
55+
"patch_strip": 0,
56+
"patches": [],
57+
"python_version": "",
58+
"sha256": {},
59+
"strip_prefix": "python",
60+
"urls": [],
61+
}
62+
attrs.update(kwargs)
63+
return mocks.tag(**attrs)
64+
65+
def _single_version_platform_override(**kwargs):
66+
"""Creates a mock python.single_version_platform_override tag with default values."""
67+
attrs = {
68+
"arch": "",
69+
"coverage_tool": None,
70+
"os_name": "",
71+
"patch_strip": 0,
72+
"patches": [],
73+
"platform": "",
74+
"python_version": "",
75+
"sha256": "",
76+
"strip_prefix": "python",
77+
"target_compatible_with": [],
78+
"target_settings": [],
79+
"urls": [],
80+
}
81+
attrs.update(kwargs)
82+
return mocks.tag(**attrs)
83+
84+
def _toolchain(**kwargs):
85+
"""Creates a mock python.toolchain tag with default values."""
86+
attrs = {
87+
"configure_coverage_tool": False,
88+
"ignore_root_user_error": True,
89+
"is_default": False,
90+
"python_version": "",
91+
}
92+
attrs.update(kwargs)
93+
return mocks.tag(**attrs)
94+
95+
python_ext = struct(
96+
defaults = _defaults,
97+
module = _module,
98+
override = _override,
99+
single_version_override = _single_version_override,
100+
single_version_platform_override = _single_version_platform_override,
101+
toolchain = _toolchain,
102+
)

0 commit comments

Comments
 (0)