Skip to content

Commit 08398cd

Browse files
authored
Allow specifying additional aspects to tut (#299)
1 parent df3c9e2 commit 08398cd

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

lib/unittest.bzl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ def _make_analysis_test(
151151
expect_failure = False,
152152
attrs = {},
153153
fragments = [],
154-
config_settings = {}):
154+
config_settings = {},
155+
extra_target_under_test_aspects = []):
155156
"""Creates an analysis test rule from its implementation function.
156157
157158
An analysis test verifies the behavior of a "real" rule target by examining
@@ -189,6 +190,8 @@ def _make_analysis_test(
189190
test and its dependencies. This may be used to essentially change 'build flags' for
190191
the target under test, and may thus be utilized to test multiple targets with different
191192
flags in a single build
193+
extra_target_under_test_aspects: An optional list of aspects to apply to the target_under_test
194+
in addition to those set up by default for the test harness itself.
192195
193196
Returns:
194197
A rule definition that should be stored in a global whose name ends in
@@ -209,7 +212,7 @@ def _make_analysis_test(
209212
target_attr_kwargs["cfg"] = test_transition
210213

211214
attrs["target_under_test"] = attr.label(
212-
aspects = [_action_retrieving_aspect],
215+
aspects = [_action_retrieving_aspect] + extra_target_under_test_aspects,
213216
mandatory = True,
214217
**target_attr_kwargs
215218
)

tests/unittest_tests.bzl

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,50 @@ inspect_actions_test = analysistest.make(
194194
_inspect_actions_test,
195195
)
196196

197+
####################################
198+
####### inspect_aspect_test #######
199+
####################################
200+
_AddedByAspectInfo = provider(
201+
doc = "Example provider added by example aspect",
202+
fields = {
203+
"value": "(str)",
204+
},
205+
)
206+
207+
def _example_aspect_impl(target, ctx):
208+
return [
209+
_AddedByAspectInfo(value = "attached by aspect"),
210+
]
211+
212+
example_aspect = aspect(
213+
implementation = _example_aspect_impl,
214+
)
215+
216+
def _inspect_aspect_test(ctx):
217+
"""Test verifying aspect run on a target."""
218+
env = analysistest.begin(ctx)
219+
220+
tut = env.ctx.attr.target_under_test
221+
asserts.equals(env, "attached by aspect", tut[_AddedByAspectInfo].value)
222+
return analysistest.end(env)
223+
224+
def _inspect_aspect_fake_rule(ctx):
225+
out_file = ctx.actions.declare_file("out.txt")
226+
ctx.actions.run_shell(
227+
command = "echo 'hello' > %s" % out_file.basename,
228+
outputs = [out_file],
229+
)
230+
return [DefaultInfo(files = depset([out_file]))]
231+
232+
inspect_aspect_fake_rule = rule(
233+
implementation = _inspect_aspect_fake_rule,
234+
)
235+
236+
inspect_aspect_test = analysistest.make(
237+
_inspect_aspect_test,
238+
extra_target_under_test_aspects = [example_aspect],
239+
)
240+
197241
########################################
198242
####### inspect_output_dirs_test #######
199243
########################################
@@ -293,6 +337,15 @@ def unittest_passing_tests_suite():
293337
tags = ["manual"],
294338
)
295339

340+
inspect_aspect_test(
341+
name = "inspect_aspect_test",
342+
target_under_test = ":inspect_aspect_fake_target",
343+
)
344+
inspect_aspect_fake_rule(
345+
name = "inspect_aspect_fake_target",
346+
tags = ["manual"],
347+
)
348+
296349
inspect_output_dirs_test(
297350
name = "inspect_output_dirs_test",
298351
target_under_test = ":inspect_output_dirs_fake_target",

0 commit comments

Comments
 (0)