xref: /aosp_15_r20/external/bazelbuild-rules_testing/lib/private/file_subject.bzl (revision d605057434dcabba796c020773aab68d9790ff9f)
1# Copyright 2023 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"""# FileSubject"""
16
17load(":str_subject.bzl", "StrSubject")
18
19def _file_subject_new(file, meta):
20    """Creates a FileSubject asserting against the given file.
21
22    Method: FileSubject.new
23
24    Args:
25        file: ([`File`]) the file to assert against.
26        meta: ([`ExpectMeta`])
27
28    Returns:
29        [`FileSubject`] object.
30    """
31
32    # buildifier: disable=uninitialized
33    public = struct(
34        # keep sorted start
35        equals = lambda *a, **k: _file_subject_equals(self, *a, **k),
36        path = lambda *a, **k: _file_subject_path(self, *a, **k),
37        short_path_equals = lambda *a, **k: _file_subject_short_path_equals(self, *a, **k),
38        # keep sorted end
39    )
40    self = struct(file = file, meta = meta, public = public)
41    return public
42
43def _file_subject_equals(self, expected):
44    """Asserts that `expected` references the same file as `self`.
45
46    This uses Bazel's notion of [`File`] equality, which usually includes
47    the configuration, owning action, internal hash, etc of a `File`. The
48    particulars of comparison depend on the actual Java type implementing
49    the `File` object (some ignore owner, for example).
50
51    NOTE: This does not compare file content. Starlark cannot read files.
52
53    NOTE: Same files generated by different owners are likely considered
54    not equal to each other. The alternative for this is to assert the
55    `File.path` paths are equal using [`FileSubject.path()`]
56
57    Method: FileSubject.equals
58    """
59
60    if self.file == expected:
61        return
62    self.meta.add_failure(
63        "expected: {}".format(expected),
64        "actual: {}".format(self.file),
65    )
66
67def _file_subject_path(self):
68    """Returns a `StrSubject` asserting on the files `path` value.
69
70    Method: FileSubject.path
71
72    Returns:
73        [`StrSubject`] object.
74    """
75    return StrSubject.new(
76        self.file.path,
77        meta = self.meta.derive("path()"),
78    )
79
80def _file_subject_short_path_equals(self, path):
81    """Asserts the file's short path is equal to the given path.
82
83    Method: FileSubject.short_path_equals
84
85    Args:
86        self: implicitly added.
87        path: ([`str`]) the value the file's `short_path` must be equal to.
88    """
89    path = self.meta.format_str(path)
90    if path == self.file.short_path:
91        return
92    self.meta.add_failure(
93        "expected: {}".format(path),
94        "actual: {}".format(self.file.short_path),
95    )
96
97# We use this name so it shows up nice in docs.
98# buildifier: disable=name-conventions
99FileSubject = struct(
100    new = _file_subject_new,
101    equals = _file_subject_equals,
102    path = _file_subject_path,
103    short_path_equals = _file_subject_short_path_equals,
104)
105