Skip to content

Commit db007bf

Browse files
A GooglerBlaze Rules Copybara
authored andcommitted
Add an StrSubject for basename to FileSubject
PiperOrigin-RevId: 739232312
1 parent 3909cc0 commit db007bf

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
* `TargetSubject.provider()` and `TargetSubject.has_provider()` accept a
1010
`provider_name` for better error messaging.
11-
11+
* Added `FileSubject.basename()` and `FileSubject.actual`
1212
### Added
1313

1414
* Nothing yet

lib/private/file_subject.bzl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ def _file_subject_new(file, meta):
3232
# buildifier: disable=uninitialized
3333
public = struct(
3434
# keep sorted start
35+
actual = file,
36+
basename = lambda *a, **k: _file_subject_basename(self, *a, **k),
3537
equals = lambda *a, **k: _file_subject_equals(self, *a, **k),
3638
path = lambda *a, **k: _file_subject_path(self, *a, **k),
3739
short_path_equals = lambda *a, **k: _file_subject_short_path_equals(self, *a, **k),
@@ -40,6 +42,19 @@ def _file_subject_new(file, meta):
4042
self = struct(file = file, meta = meta, public = public)
4143
return public
4244

45+
def _file_subject_basename(self):
46+
"""Returns a `StrSubject` asserting on the files `basename` value.
47+
48+
Method: FileSubject.basename
49+
50+
Returns:
51+
[`StrSubject`] object.
52+
"""
53+
return StrSubject.new(
54+
self.file.basename,
55+
meta = self.meta.derive("basename()"),
56+
)
57+
4358
def _file_subject_equals(self, expected):
4459
"""Asserts that `expected` references the same file as `self`.
4560
@@ -100,5 +115,6 @@ FileSubject = struct(
100115
new = _file_subject_new,
101116
equals = _file_subject_equals,
102117
path = _file_subject_path,
118+
basename = _file_subject_basename,
103119
short_path_equals = _file_subject_short_path_equals,
104120
)

tests/truth_tests.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,6 +1206,7 @@ def _file_subject_test(env, target):
12061206
expect = truth.expect(fake_env)
12071207
subject = expect.that_file(target.files.to_list()[0])
12081208
subject.short_path_equals(package + "/testdata/file1.txt")
1209+
subject.basename().equals("file1.txt")
12091210
_assert_no_failures(fake_env, env = env)
12101211

12111212
subject.short_path_equals("landon-and-hope-forever.txt")

0 commit comments

Comments
 (0)