xref: /aosp_15_r20/external/bazelbuild-rules_testing/lib/private/run_environment_info_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"""# RunEnvironmentInfoSubject"""
16
17load(":collection_subject.bzl", "CollectionSubject")
18load(":dict_subject.bzl", "DictSubject")
19
20def _run_environment_info_subject_new(info, *, meta):
21    """Creates a new `RunEnvironmentInfoSubject`
22
23    Method: RunEnvironmentInfoSubject.new
24
25    Args:
26        info: ([`RunEnvironmentInfo`]) provider instance.
27        meta: ([`ExpectMeta`]) of call chain information.
28    """
29
30    # buildifier: disable=uninitialized
31    public = struct(
32        environment = lambda *a, **k: _run_environment_info_subject_environment(self, *a, **k),
33        inherited_environment = lambda *a, **k: _run_environment_info_subject_inherited_environment(self, *a, **k),
34    )
35    self = struct(
36        actual = info,
37        meta = meta,
38    )
39    return public
40
41def _run_environment_info_subject_environment(self):
42    """Creates a `DictSubject` to assert on the environment dict.
43
44    Method: RunEnvironmentInfoSubject.environment
45
46    Args:
47        self: implicitly added
48
49    Returns:
50        [`DictSubject`] of the str->str environment map.
51    """
52    return DictSubject.new(
53        self.actual.environment,
54        meta = self.meta.derive("environment()"),
55    )
56
57def _run_environment_info_subject_inherited_environment(self):
58    """Creates a `CollectionSubject` to assert on the inherited_environment list.
59
60    Method: RunEnvironmentInfoSubject.inherited_environment
61
62    Args:
63        self: implicitly added
64
65    Returns:
66        [`CollectionSubject`] of [`str`]; from the
67        [`RunEnvironmentInfo.inherited_environment`] list.
68    """
69    return CollectionSubject.new(
70        self.actual.inherited_environment,
71        meta = self.meta.derive("inherited_environment()"),
72    )
73
74# We use this name so it shows up nice in docs.
75# buildifier: disable=name-conventions
76RunEnvironmentInfoSubject = struct(
77    new = _run_environment_info_subject_new,
78    environment = _run_environment_info_subject_environment,
79    inherited_environment = _run_environment_info_subject_inherited_environment,
80)
81