• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2019 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"""Unit tests for unittest.bzl."""
16
17load("//lib:unittest.bzl", "analysistest", "asserts", "unittest")
18
19###################################
20####### fail_basic_test ###########
21###################################
22def _basic_failing_test(ctx):
23    """Unit tests for a basic library verification test that fails."""
24    env = unittest.begin(ctx)
25
26    asserts.equals(env, 1, 2)
27
28    return unittest.end(env)
29
30basic_failing_test = unittest.make(_basic_failing_test)
31
32###################################
33####### basic_passing_test ########
34###################################
35def _basic_passing_test(ctx):
36    """Unit tests for a basic library verification test."""
37    env = unittest.begin(ctx)
38
39    asserts.equals(env, 1, 1)
40
41    return unittest.end(env)
42
43basic_passing_test = unittest.make(_basic_passing_test)
44
45###################################
46####### change_setting_test #######
47###################################
48def _change_setting_test(ctx):
49    """Test to verify that an analysis test may change configuration."""
50    env = analysistest.begin(ctx)
51
52    dep_min_os_version = analysistest.target_under_test(env)[_ChangeSettingInfo].min_os_version
53    asserts.equals(env, "1234.5678", dep_min_os_version)
54
55    return analysistest.end(env)
56
57_ChangeSettingInfo = provider()
58
59def _change_setting_fake_rule(ctx):
60    return [_ChangeSettingInfo(min_os_version = ctx.fragments.cpp.minimum_os_version())]
61
62change_setting_fake_rule = rule(
63    implementation = _change_setting_fake_rule,
64    fragments = ["cpp"],
65)
66
67change_setting_test = analysistest.make(
68    _change_setting_test,
69    config_settings = {
70        "//command_line_option:minimum_os_version": "1234.5678",
71    },
72)
73
74####################################
75####### failure_testing_test #######
76####################################
77def _failure_testing_test(ctx):
78    """Test to verify that an analysis test may verify a rule fails with fail()."""
79    env = analysistest.begin(ctx)
80
81    asserts.expect_failure(env, "This rule should never work")
82
83    return analysistest.end(env)
84
85def _failure_testing_fake_rule(ctx):
86    ignore = [ctx]
87    fail("This rule should never work")
88
89failure_testing_fake_rule = rule(
90    implementation = _failure_testing_fake_rule,
91)
92
93failure_testing_test = analysistest.make(
94    _failure_testing_test,
95    expect_failure = True,
96)
97
98############################################
99####### fail_unexpected_passing_test #######
100############################################
101def _fail_unexpected_passing_test(ctx):
102    """Test that fails by expecting an error that never occurs."""
103    env = analysistest.begin(ctx)
104
105    asserts.expect_failure(env, "Oh no, going to fail")
106
107    return analysistest.end(env)
108
109def _fail_unexpected_passing_fake_rule(ctx):
110    _ignore = [ctx]
111    return []
112
113fail_unexpected_passing_fake_rule = rule(
114    implementation = _fail_unexpected_passing_fake_rule,
115)
116
117fail_unexpected_passing_test = analysistest.make(
118    _fail_unexpected_passing_test,
119    expect_failure = True,
120)
121
122################################################
123####### change_setting_with_failure_test #######
124################################################
125def _change_setting_with_failure_test(ctx):
126    """Test verifying failure while changing configuration."""
127    env = analysistest.begin(ctx)
128
129    asserts.expect_failure(env, "unexpected minimum_os_version!!!")
130
131    return analysistest.end(env)
132
133def _change_setting_with_failure_fake_rule(ctx):
134    if ctx.fragments.cpp.minimum_os_version() == "error_error":
135        fail("unexpected minimum_os_version!!!")
136    return []
137
138change_setting_with_failure_fake_rule = rule(
139    implementation = _change_setting_with_failure_fake_rule,
140    fragments = ["cpp"],
141)
142
143change_setting_with_failure_test = analysistest.make(
144    _change_setting_with_failure_test,
145    expect_failure = True,
146    config_settings = {
147        "//command_line_option:minimum_os_version": "error_error",
148    },
149)
150
151####################################
152####### inspect_actions_test #######
153####################################
154def _inspect_actions_test(ctx):
155    """Test verifying actions registered by a target."""
156    env = analysistest.begin(ctx)
157
158    actions = analysistest.target_actions(env)
159    asserts.equals(env, 1, len(actions))
160    action_output = actions[0].outputs.to_list()[0]
161    asserts.equals(env, "out.txt", action_output.basename)
162    return analysistest.end(env)
163
164def _inspect_actions_fake_rule(ctx):
165    out_file = ctx.actions.declare_file("out.txt")
166    ctx.actions.run_shell(
167        command = "echo 'hello' > %s" % out_file.basename,
168        outputs = [out_file],
169    )
170    return [DefaultInfo(files = depset([out_file]))]
171
172inspect_actions_fake_rule = rule(
173    implementation = _inspect_actions_fake_rule,
174)
175
176inspect_actions_test = analysistest.make(
177    _inspect_actions_test,
178)
179
180########################################
181####### inspect_output_dirs_test #######
182########################################
183_OutputDirInfo = provider(fields = ["bin_path"])
184
185def _inspect_output_dirs_test(ctx):
186    """Test verifying output directories used by a test."""
187    env = analysistest.begin(ctx)
188
189    # Assert that the output bin dir observed by the aspect added by analysistest
190    # is the same as those observed by the rule directly, even when that's
191    # under a config transition and therefore not the same as the bin dir
192    # used by the test rule.
193    bin_path = analysistest.target_bin_dir_path(env)
194    target_under_test = analysistest.target_under_test(env)
195    asserts.false(env, not bin_path, "bin dir path not found.")
196    asserts.false(
197        env,
198        bin_path == ctx.bin_dir.path,
199        "test bin dir (%s) expected to differ with target_under_test bin dir (%s)." % (bin_path, ctx.bin_dir.path),
200    )
201    asserts.equals(env, bin_path, target_under_test[_OutputDirInfo].bin_path)
202    return analysistest.end(env)
203
204def _inspect_output_dirs_fake_rule(ctx):
205    return [
206        _OutputDirInfo(
207            bin_path = ctx.bin_dir.path,
208        ),
209    ]
210
211inspect_output_dirs_fake_rule = rule(
212    implementation = _inspect_output_dirs_fake_rule,
213)
214
215inspect_output_dirs_test = analysistest.make(
216    _inspect_output_dirs_test,
217    # The output directories differ between the test and target under test when
218    # the target under test is under a config transition.
219    config_settings = {
220        "//command_line_option:minimum_os_version": "1234.5678",
221    },
222)
223
224#########################################
225
226# buildifier: disable=unnamed-macro
227def unittest_passing_tests_suite():
228    """Creates the test targets and test suite for passing unittest.bzl tests.
229
230    Not all tests are included. Some unittest.bzl tests verify a test fails
231    when assertions are not met. Such tests must be run in an e2e shell test.
232    This suite only includes tests which verify success tests.
233    """
234    unittest.suite(
235        "unittest_tests",
236        basic_passing_test,
237    )
238
239    change_setting_test(
240        name = "change_setting_test",
241        target_under_test = ":change_setting_fake_target",
242    )
243    change_setting_fake_rule(
244        name = "change_setting_fake_target",
245        tags = ["manual"],
246    )
247
248    failure_testing_test(
249        name = "failure_testing_test",
250        target_under_test = ":failure_testing_fake_target",
251    )
252    failure_testing_fake_rule(
253        name = "failure_testing_fake_target",
254        tags = ["manual"],
255    )
256
257    change_setting_with_failure_test(
258        name = "change_setting_with_failure_test",
259        target_under_test = ":change_setting_with_failure_fake_target",
260    )
261    change_setting_with_failure_fake_rule(
262        name = "change_setting_with_failure_fake_target",
263        tags = ["manual"],
264    )
265
266    inspect_actions_test(
267        name = "inspect_actions_test",
268        target_under_test = ":inspect_actions_fake_target",
269    )
270    inspect_actions_fake_rule(
271        name = "inspect_actions_fake_target",
272        tags = ["manual"],
273    )
274
275    inspect_output_dirs_test(
276        name = "inspect_output_dirs_test",
277        target_under_test = ":inspect_output_dirs_fake_target",
278    )
279    inspect_output_dirs_fake_rule(
280        name = "inspect_output_dirs_fake_target",
281        tags = ["manual"],
282    )
283