Skip to content

Commit 18c5057

Browse files
committed
test: add a first unit test
1 parent 5cfea2b commit 18c5057

File tree

4 files changed

+152
-6
lines changed

4 files changed

+152
-6
lines changed

examples/bzlmod/MODULE.bazel.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

python/private/pypi/extension.bzl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -295,14 +295,16 @@ def _create_whl_repos(
295295
whl_map = whl_map,
296296
)
297297

298-
def parse_modules(module_ctx, _fail = fail, simpleapi_download = simpleapi_download):
298+
def parse_modules(module_ctx, _fail = fail, simpleapi_download = simpleapi_download, available_interpreters = INTERPRETER_LABELS):
299299
"""Implementation of parsing the tag classes for the extension and return a struct for registering repositories.
300300
301301
Args:
302302
module_ctx: {type}`module_ctx` module context.
303303
_fail: {type}`function` the failure function, mainly for testing.
304304
simpleapi_download: {type}`function` the function to download from PyPI. See
305305
{obj}`simpleapi_download` for the API docs.
306+
available_interpreters: {type}`dict[str, Label]` The available registered interpreters
307+
to use during the `repository_rule` phase. Used for testing.
306308
307309
Returns:
308310
A struct with the following attributes:
@@ -460,7 +462,7 @@ You cannot use both the additive_build_content and additive_build_content_file a
460462
python_name = "python_{}_host".format(
461463
pip_attr.python_version.replace(".", "_"),
462464
)
463-
if python_name not in INTERPRETER_LABELS:
465+
if python_name not in available_interpreters:
464466
fail((
465467
"Unable to find interpreter for pip hub '{hub_name}' for " +
466468
"python_version={version}: Make sure a corresponding " +
@@ -470,9 +472,9 @@ You cannot use both the additive_build_content and additive_build_content_file a
470472
hub_name = hub_name,
471473
version = pip_attr.python_version,
472474
python_name = python_name,
473-
labels = " \n".join(INTERPRETER_LABELS),
475+
labels = " \n".join(available_interpreters),
474476
))
475-
python_interpreter_target = INTERPRETER_LABELS[python_name]
477+
python_interpreter_target = available_interpreters[python_name]
476478

477479
# NOTE @aignas 2024-08-02: , we will execute any interpreter that we find either
478480
# in the PATH or if specified as a label. We will configure the env

tests/pypi/extension/BUILD.bazel

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright 2024 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
load(":extension_tests.bzl", "extension_test_suite")
16+
17+
extension_test_suite(name = "extension_tests")
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# Copyright 2024 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
""
16+
17+
load("@rules_testing//lib:test_suite.bzl", "test_suite")
18+
load("//python/private/pypi:extension.bzl", "parse_modules") # buildifier: disable=bzl-visibility
19+
20+
_tests = []
21+
22+
def _mock_mctx(*modules, environ = {}, read = None):
23+
return struct(
24+
os = struct(
25+
environ = environ,
26+
name = "unittest",
27+
arch = "exotic",
28+
),
29+
read = read or {}.get,
30+
modules = [
31+
struct(
32+
name = modules[0].name,
33+
tags = modules[0].tags,
34+
is_root = modules[0].is_root,
35+
),
36+
] + [
37+
struct(
38+
name = mod.name,
39+
tags = mod.tags,
40+
is_root = False,
41+
)
42+
for mod in modules[1:]
43+
],
44+
)
45+
46+
def _mod(*, name, parse = [], override = [], whl_mods = [], is_root = True):
47+
return struct(
48+
name = name,
49+
tags = struct(
50+
parse = parse,
51+
override = override,
52+
whl_mods = whl_mods,
53+
),
54+
is_root = is_root,
55+
)
56+
57+
def _parse(
58+
*,
59+
hub_name,
60+
python_version,
61+
experimental_index_url = "",
62+
requirements_lock = None,
63+
**kwargs):
64+
return struct(
65+
hub_name = hub_name,
66+
python_version = python_version,
67+
requirements_lock = requirements_lock,
68+
# Constants for now
69+
# TODO @aignas 2024-10-21: cover with tests
70+
auth_patterns = {},
71+
download_only = False,
72+
enable_implicit_namespace_pkgs = False,
73+
environment = {},
74+
envsubst = {},
75+
experimental_index_url = experimental_index_url,
76+
experimental_requirement_cycles = {},
77+
experimental_target_platforms = [],
78+
extra_pip_args = [],
79+
isolated = False,
80+
netrc = None,
81+
pip_data_exclude = None,
82+
python_interpreter = None,
83+
python_interpreter_target = None,
84+
quiet = False,
85+
requirements_by_platform = {},
86+
requirements_darwin = None,
87+
requirements_linux = None,
88+
requirements_windows = None,
89+
timeout = 42,
90+
whl_modifications = {},
91+
_evaluate_markers_srcs = [],
92+
**kwargs
93+
)
94+
95+
def _test_simple(env):
96+
pypi = parse_modules(
97+
module_ctx = _mock_mctx(
98+
_mod(name = "rules_python", parse = [_parse(
99+
hub_name = "pypi",
100+
python_version = "3.15",
101+
requirements_lock = "requirements.txt",
102+
)]),
103+
read = {
104+
"requirements.txt": "",
105+
}.get,
106+
),
107+
available_interpreters = {
108+
"python_3_15_host": "unit_test_interpreter_target",
109+
},
110+
)
111+
112+
env.expect.that_dict(pypi.exposed_packages).contains_exactly({"pypi": []})
113+
env.expect.that_dict(pypi.hub_group_map).contains_exactly({"pypi": {}})
114+
env.expect.that_dict(pypi.hub_whl_map).contains_exactly({})
115+
env.expect.that_bool(pypi.is_reproducible).equals(True)
116+
env.expect.that_dict(pypi.whl_libraries).contains_exactly({})
117+
env.expect.that_dict(pypi.whl_mods).contains_exactly({})
118+
119+
_tests.append(_test_simple)
120+
121+
def extension_test_suite(name):
122+
"""Create the test suite.
123+
124+
Args:
125+
name: the name of the test suite
126+
"""
127+
test_suite(name = name, basic_tests = _tests)

0 commit comments

Comments
 (0)