• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (C) 2022 The Android Open Source Project
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
15load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
16
17def _assert_flags_present_in_action(env, action, expected_flags):
18    if action.argv == None:
19        asserts.true(
20            env,
21            False,
22            "expected %s action to have arguments, but argv was None" % (
23                action.mnemonic,
24            ),
25        )
26        return
27    for flag in expected_flags:
28        asserts.true(
29            env,
30            flag in action.argv,
31            "%s action did not contain flag %s; argv: %s" % (
32                action.mnemonic,
33                flag,
34                action.argv,
35            ),
36        )
37
38# Checks for the presence of a set of given flags in a set of given actions
39# non-exclusively. In other words, it confirms that the specified actions
40# contain the given flags, but does not confirm that other actions do not
41# contain them.
42def _action_flags_present_for_mnemonic_nonexclusive_test_impl(ctx):
43    env = analysistest.begin(ctx)
44
45    for action in analysistest.target_actions(env):
46        if action.mnemonic in ctx.attr.mnemonics:
47            _assert_flags_present_in_action(
48                env,
49                action,
50                ctx.attr.expected_flags,
51            )
52
53    return analysistest.end(env)
54
55action_flags_present_for_mnemonic_nonexclusive_test = analysistest.make(
56    _action_flags_present_for_mnemonic_nonexclusive_test_impl,
57    attrs = {
58        "mnemonics": attr.string_list(
59            doc = """
60            Actions with these mnemonics will be expected to have the flags
61            specified in expected_flags
62            """,
63        ),
64        "expected_flags": attr.string_list(doc = "The flags to be checked for"),
65    },
66)
67
68# Checks for the presence of a set of given flags in a set of given actions
69# exclusively. In other words, it confirms that *only* the specified actions
70# contain the specified flags.
71def _action_flags_present_only_for_mnemonic_test_impl(ctx):
72    env = analysistest.begin(ctx)
73
74    actions = analysistest.target_actions(env)
75    found_at_least_one_action = False
76    for action in actions:
77        if action.mnemonic in ctx.attr.mnemonics:
78            found_at_least_one_action = True
79            _assert_flags_present_in_action(
80                env,
81                action,
82                ctx.attr.expected_flags,
83            )
84        elif action.argv != None:
85            for flag in ctx.attr.expected_flags:
86                asserts.false(
87                    env,
88                    flag in action.argv,
89                    "%s action unexpectedly contained flag %s; argv: %s" % (
90                        action.mnemonic,
91                        flag,
92                        action.argv,
93                    ),
94                )
95    asserts.true(
96        env,
97        found_at_least_one_action,
98        "did not find any actions with mnemonic %s" % (
99            ctx.attr.mnemonics,
100        ),
101    )
102    return analysistest.end(env)
103
104def action_flags_present_only_for_mnemonic_test_with_config_settings(config_settings = {}):
105    return analysistest.make(
106        _action_flags_present_only_for_mnemonic_test_impl,
107        attrs = {
108            "mnemonics": attr.string_list(
109                doc = """
110                Actions with these mnemonics will be expected to have the flags
111                specified in expected_flags
112                """,
113            ),
114            "expected_flags": attr.string_list(doc = "The flags to be checked for"),
115        },
116        config_settings = config_settings,
117    )
118
119action_flags_present_only_for_mnemonic_test = action_flags_present_only_for_mnemonic_test_with_config_settings()
120
121# Checks that a given set of flags are NOT present in a given set of actions.
122# Unlike the above test, this test does NOT confirm the absence of flags
123# *exclusively*. It does not confirm that the flags are present in actions
124# other than those specified
125def _action_flags_absent_for_mnemonic_test_impl(ctx):
126    env = analysistest.begin(ctx)
127
128    actions = analysistest.target_actions(env)
129    for action in actions:
130        if action.mnemonic in ctx.attr.mnemonics and action.argv != None:
131            for flag in ctx.attr.expected_absent_flags:
132                asserts.false(
133                    env,
134                    flag in action.argv,
135                    "%s action unexpectedly contained flag %s; argv: %s" % (
136                        action.mnemonic,
137                        flag,
138                        action.argv,
139                    ),
140                )
141
142    return analysistest.end(env)
143
144action_flags_absent_for_mnemonic_test = analysistest.make(
145    _action_flags_absent_for_mnemonic_test_impl,
146    attrs = {
147        "mnemonics": attr.string_list(
148            doc = """
149            Actions with these mnemonics will be expected NOT to have the flags
150            specificed in expected_flags
151            """,
152        ),
153        "expected_absent_flags": attr.string_list(
154            doc = """
155            The flags to be confirmed are absent from the actions in mnemonics
156            """,
157        ),
158    },
159)
160