aboutsummaryrefslogtreecommitdiff
path: root/tests/analysis_test_tests.bzl
blob: 2592a81827f9ce7330c630949e4e573cefcb43bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# Copyright 2022 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Unit tests for analysis_test.bzl."""

load("//lib:analysis_test.bzl", "analysis_test", "test_suite")
load("//lib:truth.bzl", "matching")
load("//lib:util.bzl", "TestingAspectInfo")

###################################
####### change_setting_test #######
###################################

_ChangeSettingInfo = provider(
    doc = "min_os_version for change_setting_test",
    fields = ["min_os_version"],
)

def _change_setting_fake_rule(ctx):
    return [_ChangeSettingInfo(min_os_version = ctx.fragments.cpp.minimum_os_version())]

change_setting_fake_rule = rule(
    implementation = _change_setting_fake_rule,
    fragments = ["cpp"],
)

def test_change_setting(name):
    """Test to verify that an analysis test may change configuration."""
    change_setting_fake_rule(name = name + "_fake_target", tags = ["manual"])

    analysis_test(
        name = name,
        target = name + "_fake_target",
        impl = _test_change_setting,
        config_settings = {
            "//command_line_option:minimum_os_version": "1234.5678",
        },
    )

def _test_change_setting(env, target):
    dep_min_os_version = target[_ChangeSettingInfo].min_os_version
    env.expect.that_str(dep_min_os_version).equals("1234.5678")

####################################
####### failure_testing_test #######
####################################

def _failure_testing_fake_rule(_ctx):
    fail("This rule should never work")

failure_testing_fake_rule = rule(
    implementation = _failure_testing_fake_rule,
)

def test_failure_testing(name):
    """Test to verify that an analysis test may verify a rule fails with fail()."""
    failure_testing_fake_rule(name = name + "_fake_target", tags = ["manual"])

    analysis_test(
        name = name,
        target = name + "_fake_target",
        impl = _test_failure_testing,
        expect_failure = True,
    )

def _test_failure_testing(env, target):
    env.expect.that_target(target).failures().contains_predicate(matching.contains("This rule should never work"))

############################################
####### fail_unexpected_passing_test #######
############################################

def _fail_unexpected_passing_fake_rule(_ctx):
    return []

fail_unexpected_passing_fake_rule = rule(
    implementation = _fail_unexpected_passing_fake_rule,
)

# @unused # TODO(ilist): add a shell test checking it fails
def test_fail_unexpected_passing(name):
    """Test that fails by expecting an error that never occurs."""
    fail_unexpected_passing_fake_rule(name = name + "_fake_target", tags = ["manual"])

    analysis_test(
        name = name,
        target = name + "_fake_target",
        impl = _test_fail_unexpected_passing,
        expect_failure = True,
    )

def _test_fail_unexpected_passing(env, target):
    env.expect.that_target(target).failures().contains_predicate(matching.contains("Oh no, going to fail"))

################################################
####### change_setting_with_failure_test #######
################################################
def _change_setting_with_failure_fake_rule(ctx):
    if ctx.fragments.cpp.minimum_os_version() == "error_error":
        fail("unexpected minimum_os_version!!!")
    return []

change_setting_with_failure_fake_rule = rule(
    implementation = _change_setting_with_failure_fake_rule,
    fragments = ["cpp"],
)

def test_change_setting_with_failure(name):
    change_setting_with_failure_fake_rule(name = name + "_fake_target", tags = ["manual"])

    analysis_test(
        name = name,
        target = name + "_fake_target",
        impl = _test_change_setting_with_failure,
        expect_failure = True,
        config_settings = {
            "//command_line_option:minimum_os_version": "error_error",
        },
    )

def _test_change_setting_with_failure(env, target):
    """Test verifying failure while changing configuration."""
    env.expect.that_target(target).failures().contains_predicate(
        matching.contains("unexpected minimum_os_version!!!"),
    )

####################################
####### inspect_actions_test #######
####################################
def _inspect_actions_fake_rule(ctx):
    out_file = ctx.actions.declare_file("out.txt")
    ctx.actions.run_shell(
        command = "echo 'hello' > %s" % out_file.basename,
        outputs = [out_file],
    )
    return [DefaultInfo(files = depset([out_file]))]

inspect_actions_fake_rule = rule(implementation = _inspect_actions_fake_rule)

def test_inspect_actions(name):
    """Test verifying actions registered by a target."""
    inspect_actions_fake_rule(name = name + "_fake_target", tags = ["manual"])

    analysis_test(name = name, target = name + "_fake_target", impl = _test_inspect_actions)

def _test_inspect_actions(env, target):
    env.expect.that_int(len(target[TestingAspectInfo].actions)).equals(1)
    action_output = target[TestingAspectInfo].actions[0].outputs.to_list()[0]
    env.expect.that_str(action_output.basename).equals("out.txt")

####################################
####### inspect_aspect_test #######
####################################
_AddedByAspectInfo = provider(
    doc = "Example provider added by example aspect",
    fields = {"value": "(str)"},
)

def _example_aspect_impl(_target, _ctx):
    return [_AddedByAspectInfo(value = "attached by aspect")]

example_aspect = aspect(implementation = _example_aspect_impl)

def _inspect_aspect_fake_rule(ctx):
    out_file = ctx.actions.declare_file("out.txt")
    ctx.actions.run_shell(
        command = "echo 'hello' > %s" % out_file.basename,
        outputs = [out_file],
    )
    return [DefaultInfo(files = depset([out_file]))]

inspect_aspect_fake_rule = rule(implementation = _inspect_aspect_fake_rule)

def test_inspect_aspect(name):
    """Test verifying aspect run on a target."""
    inspect_aspect_fake_rule(name = name + "_fake_target", tags = ["manual"])

    analysis_test(
        name = name,
        target = name + "_fake_target",
        impl = _test_inspect_aspect,
        extra_target_under_test_aspects = [example_aspect],
    )

def _test_inspect_aspect(env, target):
    env.expect.that_str(target[_AddedByAspectInfo].value).equals("attached by aspect")

########################################
####### inspect_output_dirs_test #######
########################################
_OutputDirInfo = provider(
    doc = "bin_path for inspect_output_dirs_test",
    fields = ["bin_path"],
)

def _inspect_output_dirs_fake_rule(ctx):
    return [_OutputDirInfo(bin_path = ctx.bin_dir.path)]

inspect_output_dirs_fake_rule = rule(implementation = _inspect_output_dirs_fake_rule)

########################################
####### common_attributes_test #######
########################################

def _test_common_attributes(name):
    native.filegroup(name = name + "_subject")
    _toolchain_template_vars(name = name + "_toolchain_template_vars")
    analysis_test(
        name = name,
        impl = _test_common_attributes_impl,
        target = name + "_subject",
        attr_values = dict(
            features = ["some-feature"],
            tags = ["taga", "tagb"],
            visibility = ["//visibility:private"],
            toolchains = [name + "_toolchain_template_vars"],
            # An empty list means "compatible with everything"
            target_compatible_with = [],
        ),
    )

def _test_common_attributes_impl(env, target):
    _ = target  # @unused
    ctx = env.ctx
    expect = env.expect

    expect.that_collection(ctx.attr.tags).contains_at_least(["taga", "tagb"])

    expect.that_collection(ctx.attr.features).contains_exactly(["some-feature"])

    expect.that_collection(ctx.attr.visibility).contains_exactly([
        Label("//visibility:private"),
    ])

    expect.that_collection(ctx.attr.target_compatible_with).contains_exactly([])

    expanded = ctx.expand_make_variables("cmd", "$(key)", {})
    expect.that_str(expanded).equals("value")

def _toolchain_template_vars_impl(ctx):
    _ = ctx  # @unused
    return [platform_common.TemplateVariableInfo({"key": "value"})]

_toolchain_template_vars = rule(implementation = _toolchain_template_vars_impl)

def analysis_test_test_suite(name):
    test_suite(
        name = name,
        tests = [
            test_change_setting,
            _test_common_attributes,
            test_failure_testing,
            test_change_setting_with_failure,
            test_inspect_actions,
            test_inspect_aspect,
        ],
    )