Skip to content

Commit d150114

Browse files
A GooglerBlaze Rules Copybara
authored andcommitted
Implement collection subjects that apply formatting
This is already done on depsets and it results in much nicer asserts. Using it on action().argv(). PiperOrigin-RevId: 737542932
1 parent c786da4 commit d150114

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
## Changed
88
* Add `{bindir}` to formatting variables.
99
* Format values in `ActionsSubject.contains_flag_values()`.
10+
* `CollectionSubject` accepts new constructor parameter `format`. When used
11+
all asserts are formatted.
12+
* `Actionsubjects.argv()` formats all asserts, for example
13+
`action.argv().contains_at_least(["-f", "{bindir}/{package}/input.txt"])`.
1014

1115
### Added
1216
* Nothing yet

lib/private/action_subject.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ def _action_subject_argv(self):
106106
meta,
107107
container_name = "argv",
108108
sortable = False,
109+
format = True,
109110
)
110111

111112
def _action_subject_contains_at_least_args(self, args):

lib/private/collection_subject.bzl

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ def _collection_subject_new(
4949
meta,
5050
container_name = "values",
5151
sortable = True,
52-
element_plural_name = "elements"):
52+
element_plural_name = "elements",
53+
format = False):
5354
"""Creates a "CollectionSubject" struct.
5455
5556
Method: CollectionSubject.new
@@ -63,6 +64,7 @@ def _collection_subject_new(
6364
container_name: ([`str`]) conceptual name of the container.
6465
sortable: ([`bool`]) True if output should be sorted for display, False if not.
6566
element_plural_name: ([`str`]) the plural word for the values in the container.
67+
format: ([`bool`]) True if asserted values should be formatted.
6668
6769
Returns:
6870
[`CollectionSubject`].
@@ -94,6 +96,7 @@ def _collection_subject_new(
9496
sortable = sortable,
9597
contains_predicate = public.contains_predicate,
9698
contains_at_least_predicates = public.contains_at_least_predicates,
99+
format = format,
97100
)
98101
return public
99102

@@ -120,6 +123,8 @@ def _collection_subject_contains(self, expected):
120123
self: implicitly added.
121124
expected: ([`str`]) the value that must be present.
122125
"""
126+
if self.format:
127+
expected = self.meta.format_str(expected)
123128
matcher = matching.equals_wrapper(expected)
124129
return self.contains_predicate(matcher)
125130

@@ -145,6 +150,8 @@ def _collection_subject_contains_exactly(self, expected):
145150
[`Ordered`] (see `_ordered_incorrectly_new`).
146151
"""
147152
expected = to_list(expected)
153+
if self.format:
154+
expected = [self.meta.format_str(v) for v in expected]
148155
return check_contains_exactly(
149156
actual_container = self.actual,
150157
expect_contains = expected,
@@ -234,6 +241,8 @@ def _collection_subject_contains_none_of(self, values):
234241
self: implicitly added
235242
values: ([`collection`]) values of which none of are allowed to exist.
236243
"""
244+
if self.format:
245+
values = self.meta.format_str(values)
237246
check_contains_none_of(
238247
collection = self.actual,
239248
none_of = values,
@@ -262,7 +271,7 @@ def _collection_subject_contains_predicate(self, matcher):
262271
meta = self.meta,
263272
)
264273

265-
def _collection_subject_contains_at_least(self, expect_contains):
274+
def _collection_subject_contains_at_least(self, expected):
266275
"""Assert that the collection is a subset of the given predicates.
267276
268277
Method: CollectionSubject.contains_at_least
@@ -274,14 +283,17 @@ def _collection_subject_contains_at_least(self, expect_contains):
274283
275284
Args:
276285
self: implicitly added.
277-
expect_contains: ([`list`]) values that must be in the collection.
286+
expected: ([`list`]) values that must be in the collection.
278287
279288
Returns:
280289
[`Ordered`] (see `_ordered_incorrectly_new`).
281290
"""
291+
expected = to_list(expected)
292+
if self.format:
293+
expected = [self.meta.format_str(v) for v in expected]
282294
matchers = [
283-
matching.equals_wrapper(expected)
284-
for expected in to_list(expect_contains)
295+
matching.equals_wrapper(e)
296+
for e in expected
285297
]
286298
return self.contains_at_least_predicates(matchers)
287299

@@ -321,6 +333,8 @@ def _collection_subject_contains_at_least_predicates(self, matchers):
321333
return ordered
322334

323335
def _collection_subject_not_contains(self, value):
336+
if self.format:
337+
value = self.meta.format_str(value)
324338
check_not_contains_predicate(
325339
self.actual,
326340
matcher = matching.equals_wrapper(value),

tests/truth_tests.bzl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,21 @@ def _action_subject_test(env, target):
7575
msg = "check contains_flag_values success",
7676
)
7777

78+
subject.argv().contains_at_least([
79+
"arg1",
80+
"--boolflag",
81+
"--arg1flag",
82+
"arg1value",
83+
"--arg2flag=arg2value",
84+
"--generated_input",
85+
"{bindir}/{package}/input.gen.txt",
86+
])
87+
_assert_no_failures(
88+
fake_env,
89+
env = env,
90+
msg = "check argv success",
91+
)
92+
7893
subject.contains_flag_values([
7994
("--missingflag", "whatever"),
8095
("--arg1flag", "wrongvalue"),

0 commit comments

Comments
 (0)