Skip to content

Commit 04a1219

Browse files
A GooglerBlaze Rules Copybara
authored andcommitted
Add DepsetFileSubject.contains_none_of
The equivalent of CollectionsSubject.contains_none_of but for depsets. PiperOrigin-RevId: 881924837
1 parent 26a0b7d commit 04a1219

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
allowing tests to assert on it like a regular target-under-test
1414
* Added `ActionSubject.meta`, exposing the subject's `ExpectMeta` object.
1515
* Added `CollectionSubject.contains_no_duplicates()` assertion.
16+
* Added `DepsetFileSubject.contains_none_of()` assertion.
1617

1718
### Added
1819

lib/private/depset_file_subject.bzl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def _depset_file_subject_new(files, meta, container_name = "depset", element_plu
5959
contains_at_least = lambda *a, **k: _depset_file_subject_contains_at_least(self, *a, **k),
6060
contains_at_least_predicates = lambda *a, **k: _depset_file_subject_contains_at_least_predicates(self, *a, **k),
6161
contains_exactly = lambda *a, **k: _depset_file_subject_contains_exactly(self, *a, **k),
62+
contains_none_of = lambda *a, **k: _depset_file_subject_contains_none_of(self, *a, **k),
6263
contains_predicate = lambda *a, **k: _depset_file_subject_contains_predicate(self, *a, **k),
6364
not_contains = lambda *a, **k: _depset_file_subject_not_contains(self, *a, **k),
6465
not_contains_predicate = lambda *a, **k: _depset_file_subject_not_contains_predicate(self, *a, **k),
@@ -253,6 +254,34 @@ def _depset_file_subject_contains_exactly(self, paths):
253254
meta = self.meta,
254255
)
255256

257+
def _depset_file_subject_contains_none_of(self, values):
258+
"""Asserts that the depset of files contains none of the provided paths.
259+
260+
Method: DepsetFileSubject.contains_none_of
261+
262+
Args:
263+
self: implicitly added
264+
values: (['collection'] of ['str'] | collection of ['File']. If string
265+
paths are provided then they are compared to the short path of the
266+
files and are formatted using 'ExpectMeta.format_str' and its
267+
current contextual keywords. Note that, when using `File` objects,
268+
two files' configurations must be the same for them to be
269+
considered equal.
270+
"""
271+
values = to_list(values)
272+
if len(values) < 1 or is_file(values[0]):
273+
actual = self.files
274+
else:
275+
values = [self.meta.format_str(v) for v in values]
276+
actual = self.actual_paths
277+
278+
return CollectionSubject.new(
279+
actual,
280+
meta = self.meta,
281+
container_name = self.container_name,
282+
element_plural_name = self.element_plural_name,
283+
).contains_none_of(values)
284+
256285
def _depset_file_subject_not_contains(self, short_path):
257286
"""Asserts that `short_path` is not in the depset.
258287
@@ -287,6 +316,7 @@ DepsetFileSubject = struct(
287316
contains_at_least_predicates = _depset_file_subject_contains_at_least_predicates,
288317
contains_predicate = _depset_file_subject_contains_predicate,
289318
contains_exactly = _depset_file_subject_contains_exactly,
319+
contains_none_of = _depset_file_subject_contains_none_of,
290320
not_contains = _depset_file_subject_not_contains,
291321
not_contains_predicate = _depset_file_subject_not_contains_predicate,
292322
)

tests/truth_tests.bzl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,6 +1115,16 @@ def _depset_file_subject_test(env, target):
11151115
msg = "DepsetFilesubject.not_contains failure test",
11161116
)
11171117

1118+
subject.contains_none_of(["NOT THERE", "ALSO NOT THERE"])
1119+
_assert_no_failures(fake_env, env = env)
1120+
subject.contains_none_of(["NOT THERE", "{package}/testdata/file1.txt"])
1121+
_assert_failure(
1122+
fake_env,
1123+
["expected not to contain any of", "file1.txt"],
1124+
env = env,
1125+
msg = "DepsetFilesubject.contains_none_of failure test",
1126+
)
1127+
11181128
_end(env, fake_env)
11191129

11201130
_suite.append(depset_file_subject_test)

0 commit comments

Comments
 (0)