Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specifying additional aspects to tut #299

Merged
merged 2 commits into from Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/unittest.bzl
Expand Up @@ -151,7 +151,8 @@ def _make_analysis_test(
expect_failure = False,
attrs = {},
fragments = [],
config_settings = {}):
config_settings = {},
extra_target_under_test_aspects = []):
"""Creates an analysis test rule from its implementation function.

An analysis test verifies the behavior of a "real" rule target by examining
Expand Down Expand Up @@ -189,6 +190,8 @@ def _make_analysis_test(
test and its dependencies. This may be used to essentially change 'build flags' for
the target under test, and may thus be utilized to test multiple targets with different
flags in a single build
extra_target_under_test_aspects: An optional list of aspects to apply to the target_under_test
in addition to those set up by default for the test harness itself.

Returns:
A rule definition that should be stored in a global whose name ends in
Expand All @@ -209,7 +212,7 @@ def _make_analysis_test(
target_attr_kwargs["cfg"] = test_transition

attrs["target_under_test"] = attr.label(
aspects = [_action_retrieving_aspect],
aspects = [_action_retrieving_aspect] + extra_target_under_test_aspects,
mandatory = True,
**target_attr_kwargs
)
Expand Down
53 changes: 53 additions & 0 deletions tests/unittest_tests.bzl
Expand Up @@ -194,6 +194,50 @@ inspect_actions_test = analysistest.make(
_inspect_actions_test,
)

####################################
####### 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_test(ctx):
"""Test verifying aspect run on a target."""
env = analysistest.begin(ctx)

tut = env.ctx.attr.target_under_test
asserts.equals(env, "attached by aspect", tut[_AddedByAspectInfo].value)
return analysistest.end(env)

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,
)

inspect_aspect_test = analysistest.make(
_inspect_aspect_test,
extra_target_under_test_aspects = [example_aspect],
)

########################################
####### inspect_output_dirs_test #######
########################################
Expand Down Expand Up @@ -293,6 +337,15 @@ def unittest_passing_tests_suite():
tags = ["manual"],
)

inspect_aspect_test(
name = "inspect_aspect_test",
target_under_test = ":inspect_aspect_fake_target",
)
inspect_aspect_fake_rule(
name = "inspect_aspect_fake_target",
tags = ["manual"],
)

inspect_output_dirs_test(
name = "inspect_output_dirs_test",
target_under_test = ":inspect_output_dirs_fake_target",
Expand Down