diff options
author | Blaze Rules Copybara <blaze-rules@google.com> | 2023-07-07 16:03:26 -0700 |
---|---|---|
committer | Blaze Rules Copybara <blaze-rules@google.com> | 2023-07-07 16:03:26 -0700 |
commit | 3694f0e1f59e29f72fae4c3f4e6e84e35b04036b (patch) | |
tree | bbc6a691d5d14444ada17a046094039f885d4ec7 | |
parent | 74f8bd5f6e3bb7955aa0b1e07922d0445bbb6ffc (diff) | |
parent | 3e20ab53269b569dfa03ec24a33d0c953882b9df (diff) | |
download | bazelbuild-rules_testing-3694f0e1f59e29f72fae4c3f4e6e84e35b04036b.tar.gz |
Merge pull request #55 from rickeylev:common.attrs
PiperOrigin-RevId: 546406547
-rw-r--r-- | lib/private/analysis_test.bzl | 8 | ||||
-rw-r--r-- | tests/analysis_test_tests.bzl | 46 |
2 files changed, 53 insertions, 1 deletions
diff --git a/lib/private/analysis_test.bzl b/lib/private/analysis_test.bzl index d54d932..c4c95ac 100644 --- a/lib/private/analysis_test.bzl +++ b/lib/private/analysis_test.bzl @@ -17,6 +17,7 @@ Support for testing analysis phase logic, such as rules. """ +load("@bazel_skylib//lib:dicts.bzl", "dicts") load("//lib:truth.bzl", "truth") load("//lib:util.bzl", "recursive_testing_aspect", "testing_aspect") load("//lib/private:util.bzl", "get_test_name_from_function") @@ -100,6 +101,7 @@ def analysis_test( impl, expect_failure = False, attrs = {}, + attr_values = {}, fragments = [], config_settings = {}, extra_target_under_test_aspects = [], @@ -135,6 +137,10 @@ def analysis_test( to fail. Assertions can be made on the underlying failure using truth.expect_failure attrs: An optional dictionary to supplement the attrs passed to the unit test's `rule()` constructor. + attr_values: An optional dictionary of kwargs to pass onto the + analysis test target itself (e.g. common attributes like `tags`, + `target_compatible_with`, or attributes from `attrs`). Note that these + are for the analysis test target itself, not the target under test. fragments: An optional list of fragment names that can be used to give rules access to language-specific parts of configuration. config_settings: A dictionary of configuration settings to change for the target under @@ -183,5 +189,5 @@ def analysis_test( wrapped_impl, attrs = attrs, fragments = fragments, - attr_values = {"target": target}, + attr_values = dicts.add(attr_values, {"target": target}), ) diff --git a/tests/analysis_test_tests.bzl b/tests/analysis_test_tests.bzl index 61350b0..2592a81 100644 --- a/tests/analysis_test_tests.bzl +++ b/tests/analysis_test_tests.bzl @@ -209,11 +209,57 @@ def _inspect_output_dirs_fake_rule(ctx): 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, |