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

feat: add propagate_common_{,test_,binary_}rule_attributes to lib/utils. #553

Merged
merged 4 commits into from
Sep 26, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions docs/utils.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

94 changes: 94 additions & 0 deletions lib/private/utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,97 @@ def _maybe_http_archive(**kwargs):
"""
maybe(_http_archive, **kwargs)

_COMMON_RULE_ATTRIBUTES = [
"compatible_with",
"deprecation",
"distribs",
"exec_compatible_with",
"exec_properties",
"features",
"restricted_to",
"tags",
thesayyn marked this conversation as resolved.
Show resolved Hide resolved
"target_compatible_with",
"testonly",
"toolchains",
"visibility",
]

_COMMON_TEST_RULE_ATTRIBUTES = _COMMON_RULE_ATTRIBUTES + [
"args",
"env",
"env_inherit",
"size",
"timeout",
"flaky",
"shard_count",
"local",
]

_COMMON_BINARY_RULE_ATTRIBUTES = _COMMON_RULE_ATTRIBUTES + [
"args",
"env",
"output_licenses",
]

def _propagate_common_rule_attributes(attrs):
"""Returns a dict of rule parameters filtered from the input dict that only contains the onces that are common to all rules

These are listed in Bazel's documentation:
https://bazel.build/reference/be/common-definitions#common-attributes

Args:
attrs: Dict of parameters to filter

Returns:
The dict of parameters, containing only common attributes
"""

return {
k: attrs[k]
for k in attrs
if k in _COMMON_RULE_ATTRIBUTES
}

def _propagate_common_test_rule_attributes(attrs):
"""Returns a dict of rule parameters filtered from the input dict that only contains the onces that are common to all test rules

These are listed in Bazel's documentation:
https://bazel.build/reference/be/common-definitions#common-attributes
https://bazel.build/reference/be/common-definitions#common-attributes-tests

Args:
attrs: Dict of parameters to filter

Returns:
The dict of parameters, containing only common test attributes
"""

return {
k: attrs[k]
for k in attrs
if k in _COMMON_TEST_RULE_ATTRIBUTES
}

def _propagate_common_binary_rule_attributes(attrs):
"""Returns a dict of rule parameters filtered from the input dict that only contains the onces that are common to all binary rules

These are listed in Bazel's documentation:
https://bazel.build/reference/be/common-definitions#common-attributes
https://bazel.build/reference/be/common-definitions#common-attributes-binary

Args:
attrs: Dict of parameters to filter

Returns:
The dict of parameters, containing only common binary attributes
"""

return {
k: attrs[k]
for k in attrs
if k in _COMMON_RULE_ATTRIBUTES or k in _COMMON_BINARY_RULE_ATTRIBUTES
}

utils = struct(
default_timeout = _default_timeout,
file_exists = _file_exists,
Expand All @@ -257,6 +348,9 @@ utils = struct(
maybe_http_archive = _maybe_http_archive,
path_to_workspace_root = _path_to_workspace_root,
propagate_well_known_tags = _propagate_well_known_tags,
propagate_common_rule_attributes = _propagate_common_rule_attributes,
propagate_common_test_rule_attributes = _propagate_common_test_rule_attributes,
propagate_common_binary_rule_attributes = _propagate_common_binary_rule_attributes,
to_label = _to_label,
consistent_label_str = _consistent_label_str,
)
75 changes: 75 additions & 0 deletions lib/tests/utils_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,63 @@ def _propagate_well_known_tags_test_impl(ctx):

return unittest.end(env)

def _propagate_common_rule_attributes_test_impl(ctx):
env = unittest.begin(ctx)

asserts.equals(env, {
"features": ["dbg"],
"testonly": True,
"visibility": ["//visibility:private"],
}, utils.propagate_common_rule_attributes({
"features": ["dbg"],
"testonly": True,
"visibility": ["//visibility:private"],
"platform": ["//:myplatform"],
"env": {"PATH": "/usr/bin:/bin"},
"size": "small",
}))

return unittest.end(env)

def _propagate_common_test_rule_attributes_test_impl(ctx):
env = unittest.begin(ctx)

asserts.equals(env, {
"features": ["dbg"],
"testonly": True,
"visibility": ["//visibility:private"],
"env": {"PATH": "/usr/bin:/bin"},
"size": "small",
}, utils.propagate_common_test_rule_attributes({
"features": ["dbg"],
"testonly": True,
"visibility": ["//visibility:private"],
"platform": ["//:myplatform"],
"env": {"PATH": "/usr/bin:/bin"},
"size": "small",
}))

return unittest.end(env)

def _propagate_common_binary_rule_attributes_test_impl(ctx):
env = unittest.begin(ctx)

asserts.equals(env, {
"features": ["dbg"],
"testonly": True,
"visibility": ["//visibility:private"],
"env": {"PATH": "/usr/bin:/bin"},
}, utils.propagate_common_binary_rule_attributes({
"features": ["dbg"],
"testonly": True,
"visibility": ["//visibility:private"],
"platform": ["//:myplatform"],
"env": {"PATH": "/usr/bin:/bin"},
"size": "small",
}))

return unittest.end(env)

def _consistent_label_str_impl(ctx):
env = unittest.begin(ctx)

Expand Down Expand Up @@ -104,6 +161,9 @@ is_external_label_test = unittest.make(
)

propagate_well_known_tags_test = unittest.make(_propagate_well_known_tags_test_impl)
propagate_common_rule_attributes_test = unittest.make(_propagate_common_rule_attributes_test_impl)
propagate_common_test_rule_attributes_test = unittest.make(_propagate_common_test_rule_attributes_test_impl)
propagate_common_binary_rule_attributes_test = unittest.make(_propagate_common_binary_rule_attributes_test_impl)
consistent_label_str_test = unittest.make(_consistent_label_str_impl)

# buildifier: disable=function-docstring
Expand Down Expand Up @@ -134,6 +194,21 @@ def utils_test_suite():
timeout = "short",
)

propagate_common_rule_attributes_test(
name = "propagate_common_rule_attribute_tests",
timeout = "short",
)

propagate_common_test_rule_attributes_test(
name = "propagate_common_test_rule_attribute_tests",
timeout = "short",
)

propagate_common_binary_rule_attributes_test(
name = "propagate_common_binary_rule_attribute_tests",
timeout = "short",
)

consistent_label_str_test(
name = "consistent_label_str_tests",
timeout = "short",
Expand Down
3 changes: 3 additions & 0 deletions lib/utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@ is_external_label = utils.is_external_label
maybe_http_archive = utils.maybe_http_archive
path_to_workspace_root = utils.path_to_workspace_root
propagate_well_known_tags = utils.propagate_well_known_tags
propagate_common_rule_attributes = utils.propagate_common_rule_attributes
propagate_common_test_rule_attributes = utils.propagate_common_test_rule_attributes
propagate_common_binary_rule_attributes = utils.propagate_common_binary_rule_attributes
to_label = utils.to_label
consistent_label_str = utils.consistent_label_str