Skip to content

Commit a628782

Browse files
committed
implement a custom subject
1 parent 1f0a1a7 commit a628782

File tree

2 files changed

+122
-7
lines changed

2 files changed

+122
-7
lines changed

tests/pypi/extension/extension_tests.bzl

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
load("@rules_testing//lib:test_suite.bzl", "test_suite")
1818
load("//python/private/pypi:extension.bzl", "parse_modules") # buildifier: disable=bzl-visibility
19+
load(":parse_modules_subject.bzl", "parse_modules_subject")
1920

2021
_tests = []
2122

@@ -54,6 +55,12 @@ def _mod(*, name, parse = [], override = [], whl_mods = [], is_root = True):
5455
is_root = is_root,
5556
)
5657

58+
def _parse_modules(env, **kwargs):
59+
return parse_modules_subject(
60+
parse_modules(**kwargs),
61+
meta = env.expect.meta,
62+
)
63+
5764
def _parse(
5865
*,
5966
hub_name,
@@ -93,7 +100,8 @@ def _parse(
93100
)
94101

95102
def _test_simple(env):
96-
pypi = parse_modules(
103+
pypi = _parse_modules(
104+
env,
97105
module_ctx = _mock_mctx(
98106
_mod(name = "rules_python", parse = [_parse(
99107
hub_name = "pypi",
@@ -109,12 +117,12 @@ def _test_simple(env):
109117
},
110118
)
111119

112-
env.expect.that_dict(pypi.exposed_packages).contains_exactly({})
113-
env.expect.that_dict(pypi.hub_group_map).contains_exactly({})
114-
env.expect.that_dict(pypi.hub_whl_map).contains_exactly({"pypi": {}})
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({})
120+
pypi.is_reproducible().equals(True)
121+
pypi.exposed_packages().contains_exactly({})
122+
pypi.hub_group_map().contains_exactly({})
123+
pypi.hub_whl_map().contains_exactly({"pypi": {}})
124+
pypi.whl_libraries().contains_exactly({})
125+
pypi.whl_mods().contains_exactly({})
118126

119127
_tests.append(_test_simple)
120128

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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:truth.bzl", "subjects")
18+
19+
def parse_modules_subject(info, *, meta):
20+
"""Creates a new `parse_modules_subject` for the parse_modules result instance.
21+
22+
Method: parse_modules_subject.new
23+
24+
Args:
25+
info: The parse_modules result object
26+
meta: ExpectMeta object.
27+
28+
Returns:
29+
A `parse_modules_subject` struct
30+
"""
31+
32+
# buildifier: disable=uninitialized
33+
public = struct(
34+
# go/keep-sorted start
35+
is_reproducible = lambda *a, **k: _subject_is_reproducible(self, *a, **k),
36+
exposed_packages = lambda *a, **k: _subject_exposed_packages(self, *a, **k),
37+
hub_group_map = lambda *a, **k: _subject_hub_group_map(self, *a, **k),
38+
hub_whl_map = lambda *a, **k: _subject_hub_whl_map(self, *a, **k),
39+
whl_libraries = lambda *a, **k: _subject_whl_libraries(self, *a, **k),
40+
whl_mods = lambda *a, **k: _subject_whl_mods(self, *a, **k),
41+
# go/keep-sorted end
42+
)
43+
self = struct(
44+
actual = info,
45+
meta = meta,
46+
)
47+
return public
48+
49+
def _subject_is_reproducible(self):
50+
"""Returns a `BoolSubject` for the `is_reproducible` attribute.
51+
52+
Method: parse_modules_subject.direct_pyc_files
53+
"""
54+
return subjects.bool(
55+
self.actual.is_reproducible,
56+
meta = self.meta.derive("is_reproducible()"),
57+
)
58+
59+
def _subject_exposed_packages(self):
60+
"""Returns a `BoolSubject` for the `exposed_packages` attribute.
61+
62+
Method: parse_modules_subject.direct_pyc_files
63+
"""
64+
return subjects.dict(
65+
self.actual.exposed_packages,
66+
meta = self.meta.derive("exposed_packages()"),
67+
)
68+
69+
def _subject_hub_group_map(self):
70+
"""Returns a `BoolSubject` for the `hub_group_map` attribute.
71+
72+
Method: parse_modules_subject.direct_pyc_files
73+
"""
74+
return subjects.dict(
75+
self.actual.hub_group_map,
76+
meta = self.meta.derive("hub_group_map()"),
77+
)
78+
79+
def _subject_hub_whl_map(self):
80+
"""Returns a `BoolSubject` for the `hub_whl_map` attribute.
81+
82+
Method: parse_modules_subject.direct_pyc_files
83+
"""
84+
return subjects.dict(
85+
self.actual.hub_whl_map,
86+
meta = self.meta.derive("hub_whl_map()"),
87+
)
88+
89+
def _subject_whl_libraries(self):
90+
"""Returns a `BoolSubject` for the `whl_libraries` attribute.
91+
92+
Method: parse_modules_subject.direct_pyc_files
93+
"""
94+
return subjects.dict(
95+
self.actual.whl_libraries,
96+
meta = self.meta.derive("whl_libraries()"),
97+
)
98+
99+
def _subject_whl_mods(self):
100+
"""Returns a `BoolSubject` for the `whl_mods` attribute.
101+
102+
Method: parse_modules_subject.direct_pyc_files
103+
"""
104+
return subjects.dict(
105+
self.actual.whl_mods,
106+
meta = self.meta.derive("whl_mods()"),
107+
)

0 commit comments

Comments
 (0)