xref: /aosp_15_r20/external/bazelbuild-rules_testing/lib/private/file_subject.bzl (revision d605057434dcabba796c020773aab68d9790ff9f)
1*d6050574SRomain Jobredeaux# Copyright 2023 The Bazel Authors. All rights reserved.
2*d6050574SRomain Jobredeaux#
3*d6050574SRomain Jobredeaux# Licensed under the Apache License, Version 2.0 (the "License");
4*d6050574SRomain Jobredeaux# you may not use this file except in compliance with the License.
5*d6050574SRomain Jobredeaux# You may obtain a copy of the License at
6*d6050574SRomain Jobredeaux#
7*d6050574SRomain Jobredeaux#     http://www.apache.org/licenses/LICENSE-2.0
8*d6050574SRomain Jobredeaux#
9*d6050574SRomain Jobredeaux# Unless required by applicable law or agreed to in writing, software
10*d6050574SRomain Jobredeaux# distributed under the License is distributed on an "AS IS" BASIS,
11*d6050574SRomain Jobredeaux# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*d6050574SRomain Jobredeaux# See the License for the specific language governing permissions and
13*d6050574SRomain Jobredeaux# limitations under the License.
14*d6050574SRomain Jobredeaux
15*d6050574SRomain Jobredeaux"""# FileSubject"""
16*d6050574SRomain Jobredeaux
17*d6050574SRomain Jobredeauxload(":str_subject.bzl", "StrSubject")
18*d6050574SRomain Jobredeaux
19*d6050574SRomain Jobredeauxdef _file_subject_new(file, meta):
20*d6050574SRomain Jobredeaux    """Creates a FileSubject asserting against the given file.
21*d6050574SRomain Jobredeaux
22*d6050574SRomain Jobredeaux    Method: FileSubject.new
23*d6050574SRomain Jobredeaux
24*d6050574SRomain Jobredeaux    Args:
25*d6050574SRomain Jobredeaux        file: ([`File`]) the file to assert against.
26*d6050574SRomain Jobredeaux        meta: ([`ExpectMeta`])
27*d6050574SRomain Jobredeaux
28*d6050574SRomain Jobredeaux    Returns:
29*d6050574SRomain Jobredeaux        [`FileSubject`] object.
30*d6050574SRomain Jobredeaux    """
31*d6050574SRomain Jobredeaux
32*d6050574SRomain Jobredeaux    # buildifier: disable=uninitialized
33*d6050574SRomain Jobredeaux    public = struct(
34*d6050574SRomain Jobredeaux        # keep sorted start
35*d6050574SRomain Jobredeaux        equals = lambda *a, **k: _file_subject_equals(self, *a, **k),
36*d6050574SRomain Jobredeaux        path = lambda *a, **k: _file_subject_path(self, *a, **k),
37*d6050574SRomain Jobredeaux        short_path_equals = lambda *a, **k: _file_subject_short_path_equals(self, *a, **k),
38*d6050574SRomain Jobredeaux        # keep sorted end
39*d6050574SRomain Jobredeaux    )
40*d6050574SRomain Jobredeaux    self = struct(file = file, meta = meta, public = public)
41*d6050574SRomain Jobredeaux    return public
42*d6050574SRomain Jobredeaux
43*d6050574SRomain Jobredeauxdef _file_subject_equals(self, expected):
44*d6050574SRomain Jobredeaux    """Asserts that `expected` references the same file as `self`.
45*d6050574SRomain Jobredeaux
46*d6050574SRomain Jobredeaux    This uses Bazel's notion of [`File`] equality, which usually includes
47*d6050574SRomain Jobredeaux    the configuration, owning action, internal hash, etc of a `File`. The
48*d6050574SRomain Jobredeaux    particulars of comparison depend on the actual Java type implementing
49*d6050574SRomain Jobredeaux    the `File` object (some ignore owner, for example).
50*d6050574SRomain Jobredeaux
51*d6050574SRomain Jobredeaux    NOTE: This does not compare file content. Starlark cannot read files.
52*d6050574SRomain Jobredeaux
53*d6050574SRomain Jobredeaux    NOTE: Same files generated by different owners are likely considered
54*d6050574SRomain Jobredeaux    not equal to each other. The alternative for this is to assert the
55*d6050574SRomain Jobredeaux    `File.path` paths are equal using [`FileSubject.path()`]
56*d6050574SRomain Jobredeaux
57*d6050574SRomain Jobredeaux    Method: FileSubject.equals
58*d6050574SRomain Jobredeaux    """
59*d6050574SRomain Jobredeaux
60*d6050574SRomain Jobredeaux    if self.file == expected:
61*d6050574SRomain Jobredeaux        return
62*d6050574SRomain Jobredeaux    self.meta.add_failure(
63*d6050574SRomain Jobredeaux        "expected: {}".format(expected),
64*d6050574SRomain Jobredeaux        "actual: {}".format(self.file),
65*d6050574SRomain Jobredeaux    )
66*d6050574SRomain Jobredeaux
67*d6050574SRomain Jobredeauxdef _file_subject_path(self):
68*d6050574SRomain Jobredeaux    """Returns a `StrSubject` asserting on the files `path` value.
69*d6050574SRomain Jobredeaux
70*d6050574SRomain Jobredeaux    Method: FileSubject.path
71*d6050574SRomain Jobredeaux
72*d6050574SRomain Jobredeaux    Returns:
73*d6050574SRomain Jobredeaux        [`StrSubject`] object.
74*d6050574SRomain Jobredeaux    """
75*d6050574SRomain Jobredeaux    return StrSubject.new(
76*d6050574SRomain Jobredeaux        self.file.path,
77*d6050574SRomain Jobredeaux        meta = self.meta.derive("path()"),
78*d6050574SRomain Jobredeaux    )
79*d6050574SRomain Jobredeaux
80*d6050574SRomain Jobredeauxdef _file_subject_short_path_equals(self, path):
81*d6050574SRomain Jobredeaux    """Asserts the file's short path is equal to the given path.
82*d6050574SRomain Jobredeaux
83*d6050574SRomain Jobredeaux    Method: FileSubject.short_path_equals
84*d6050574SRomain Jobredeaux
85*d6050574SRomain Jobredeaux    Args:
86*d6050574SRomain Jobredeaux        self: implicitly added.
87*d6050574SRomain Jobredeaux        path: ([`str`]) the value the file's `short_path` must be equal to.
88*d6050574SRomain Jobredeaux    """
89*d6050574SRomain Jobredeaux    path = self.meta.format_str(path)
90*d6050574SRomain Jobredeaux    if path == self.file.short_path:
91*d6050574SRomain Jobredeaux        return
92*d6050574SRomain Jobredeaux    self.meta.add_failure(
93*d6050574SRomain Jobredeaux        "expected: {}".format(path),
94*d6050574SRomain Jobredeaux        "actual: {}".format(self.file.short_path),
95*d6050574SRomain Jobredeaux    )
96*d6050574SRomain Jobredeaux
97*d6050574SRomain Jobredeaux# We use this name so it shows up nice in docs.
98*d6050574SRomain Jobredeaux# buildifier: disable=name-conventions
99*d6050574SRomain JobredeauxFileSubject = struct(
100*d6050574SRomain Jobredeaux    new = _file_subject_new,
101*d6050574SRomain Jobredeaux    equals = _file_subject_equals,
102*d6050574SRomain Jobredeaux    path = _file_subject_path,
103*d6050574SRomain Jobredeaux    short_path_equals = _file_subject_short_path_equals,
104*d6050574SRomain Jobredeaux)
105