Skip to content

Commit 85e4a3c

Browse files
authored
chore: factor mock mctx/rctx functionality into separate file (#3700)
This factors the various mocking logic spread throughout the code base into a single mocks module. This should make it easier to re-use and add unit tests of other functionality.
1 parent 17d7732 commit 85e4a3c

File tree

11 files changed

+914
-261
lines changed

11 files changed

+914
-261
lines changed

tests/pypi/extension/extension_tests.bzl

Lines changed: 39 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -18,66 +18,53 @@ load("@rules_testing//lib:test_suite.bzl", "test_suite")
1818
load("@rules_testing//lib:truth.bzl", "subjects")
1919
load("//python/private/pypi:extension.bzl", "build_config", "parse_modules") # buildifier: disable=bzl-visibility
2020
load("//python/private/pypi:whl_config_setting.bzl", "whl_config_setting") # buildifier: disable=bzl-visibility
21+
load("//tests/support/mocks:mocks.bzl", "mocks")
2122
load(":pip_parse.bzl", _parse = "pip_parse")
2223

2324
_tests = []
2425

25-
def _mock_mctx(*modules, os_name = "unittest", arch_name = "exotic", environ = {}, read = None):
26-
return struct(
27-
getenv = environ.get,
28-
os = struct(
29-
name = os_name,
30-
arch = arch_name,
31-
),
32-
read = read or (lambda _: """\
26+
def _pypi_mock_mctx(*modules, os_name = "unittest", arch_name = "exotic", environ = {}, read = None):
27+
_ = read # @unused
28+
return mocks.mctx(
29+
modules = list(modules),
30+
os_name = os_name,
31+
arch_name = arch_name,
32+
environ = environ,
33+
mock_files = {
34+
"requirements.txt": """\
3335
simple==0.0.1 \
3436
--hash=sha256:deadbeef \
35-
--hash=sha256:deadbaaf"""),
36-
modules = [
37-
struct(
38-
name = modules[0].name,
39-
tags = modules[0].tags,
40-
is_root = modules[0].is_root,
41-
),
42-
] + [
43-
struct(
44-
name = mod.name,
45-
tags = mod.tags,
46-
is_root = False,
47-
)
48-
for mod in modules[1:]
49-
],
37+
--hash=sha256:deadbaaf""",
38+
},
5039
)
5140

5241
def _mod(*, name, default = [], parse = [], override = [], whl_mods = [], is_root = True):
53-
return struct(
54-
name = name,
55-
tags = struct(
56-
parse = parse,
57-
override = override,
58-
whl_mods = whl_mods,
59-
default = default or [
60-
_default(
61-
platform = "{}_{}{}".format(os, cpu, freethreaded),
62-
os_name = os,
63-
arch_name = cpu,
64-
config_settings = [
65-
"@platforms//os:{}".format(os),
66-
"@platforms//cpu:{}".format(cpu),
67-
],
68-
whl_abi_tags = ["cp{major}{minor}t"] if freethreaded else ["abi3", "cp{major}{minor}"],
69-
whl_platform_tags = whl_platform_tags,
70-
)
71-
for (os, cpu, freethreaded), whl_platform_tags in {
72-
("linux", "x86_64", ""): ["linux_x86_64", "manylinux_*_x86_64"],
73-
("linux", "x86_64", "_freethreaded"): ["linux_x86_64", "manylinux_*_x86_64"],
74-
("linux", "aarch64", ""): ["linux_aarch64", "manylinux_*_aarch64"],
75-
("osx", "aarch64", ""): ["macosx_*_arm64"],
76-
("windows", "aarch64", ""): ["win_arm64"],
77-
}.items()
78-
],
79-
),
42+
return mocks.module(
43+
name,
8044
is_root = is_root,
45+
parse = parse,
46+
override = override,
47+
whl_mods = whl_mods,
48+
default = default or [
49+
_default(
50+
platform = "{}_{}{}".format(os, cpu, freethreaded),
51+
os_name = os,
52+
arch_name = cpu,
53+
config_settings = [
54+
"@platforms//os:{}".format(os),
55+
"@platforms//cpu:{}".format(cpu),
56+
],
57+
whl_abi_tags = ["cp{major}{minor}t"] if freethreaded else ["abi3", "cp{major}{minor}"],
58+
whl_platform_tags = whl_platform_tags,
59+
)
60+
for (os, cpu, freethreaded), whl_platform_tags in {
61+
("linux", "x86_64", ""): ["linux_x86_64", "manylinux_*_x86_64"],
62+
("linux", "x86_64", "_freethreaded"): ["linux_x86_64", "manylinux_*_x86_64"],
63+
("linux", "aarch64", ""): ["linux_aarch64", "manylinux_*_aarch64"],
64+
("osx", "aarch64", ""): ["macosx_*_arm64"],
65+
("windows", "aarch64", ""): ["win_arm64"],
66+
}.items()
67+
],
8168
)
8269

8370
def _parse_modules(env, enable_pipstar = 0, **kwargs):
@@ -140,7 +127,7 @@ def _default(
140127
def _test_simple(env):
141128
pypi = _parse_modules(
142129
env,
143-
module_ctx = _mock_mctx(
130+
module_ctx = _pypi_mock_mctx(
144131
_mod(
145132
name = "rules_python",
146133
parse = [
@@ -187,7 +174,7 @@ _tests.append(_test_simple)
187174
def _test_build_pipstar_platform(env):
188175
config = _build_config(
189176
env,
190-
module_ctx = _mock_mctx(
177+
module_ctx = _pypi_mock_mctx(
191178
_mod(
192179
name = "rules_python",
193180
default = [

0 commit comments

Comments
 (0)