2021-11-09 23:13:30 +00:00
|
|
|
"Unit tests for test.bzl"
|
|
|
|
|
|
|
|
load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest")
|
|
|
|
load("//lib/private:utils.bzl", "utils")
|
|
|
|
|
|
|
|
def _to_label_test_impl(ctx):
|
|
|
|
env = unittest.begin(ctx)
|
|
|
|
|
|
|
|
# assert label/string comparisons work as expected
|
|
|
|
asserts.true(env, "//some/label" != Label("//some/label"))
|
|
|
|
asserts.true(env, "//some/label" != Label("//some/other/label"))
|
|
|
|
asserts.true(env, Label("//some/label") == Label("//some/label"))
|
|
|
|
asserts.true(env, "//some/label" != Label("//some/label"))
|
|
|
|
asserts.true(env, "//some/label" != Label("//some/other/label"))
|
|
|
|
|
|
|
|
# assert that to_label can convert from string to label
|
|
|
|
asserts.true(env, utils.to_label("//hello/world") == Label("//hello/world:world"))
|
|
|
|
asserts.true(env, utils.to_label("//hello/world:world") == Label("//hello/world:world"))
|
2022-10-26 22:46:39 +00:00
|
|
|
asserts.true(env, utils.to_label("@//hello/world") == Label("@//hello/world:world"))
|
|
|
|
asserts.true(env, utils.to_label("@//hello/world:world") == Label("@//hello/world:world"))
|
|
|
|
asserts.true(env, utils.to_label("@somewhere//hello/world") == Label("@somewhere//hello/world:world"))
|
|
|
|
asserts.true(env, utils.to_label("@somewhere//hello/world:world") == Label("@somewhere//hello/world:world"))
|
|
|
|
|
|
|
|
# the "@@" repository name syntax applies to Bazel 6 or greater
|
|
|
|
if utils.is_bazel_6_or_greater():
|
|
|
|
asserts.true(env, utils.to_label("@@//hello/world") == Label("@@//hello/world:world"))
|
|
|
|
asserts.true(env, utils.to_label("@@//hello/world:world") == Label("@@//hello/world:world"))
|
|
|
|
asserts.true(env, utils.to_label("@@somewhere//hello/world") == Label("@@somewhere//hello/world:world"))
|
|
|
|
asserts.true(env, utils.to_label("@@somewhere//hello/world:world") == Label("@@somewhere//hello/world:world"))
|
|
|
|
|
|
|
|
# In this context, "@@" should evaluate to the same label as "@"
|
|
|
|
asserts.true(env, utils.to_label("@@//hello/world") == Label("@//hello/world:world"))
|
|
|
|
asserts.true(env, utils.to_label("@@//hello/world:world") == Label("@//hello/world:world"))
|
|
|
|
asserts.true(env, utils.to_label("@@somewhere//hello/world") == Label("@somewhere//hello/world:world"))
|
|
|
|
asserts.true(env, utils.to_label("@@somewhere//hello/world:world") == Label("@somewhere//hello/world:world"))
|
2021-11-09 23:13:30 +00:00
|
|
|
|
|
|
|
# assert that to_label will handle a Label as an argument
|
|
|
|
asserts.true(env, utils.to_label(Label("//hello/world")) == Label("//hello/world:world"))
|
|
|
|
asserts.true(env, utils.to_label(Label("//hello/world:world")) == Label("//hello/world:world"))
|
|
|
|
|
|
|
|
# relative labels
|
|
|
|
for (actual, expected) in ctx.attr.relative_asserts.items():
|
|
|
|
asserts.true(env, actual.label == Label(expected))
|
|
|
|
|
|
|
|
return unittest.end(env)
|
|
|
|
|
|
|
|
def _is_external_label_test_impl(ctx):
|
|
|
|
env = unittest.begin(ctx)
|
|
|
|
|
|
|
|
# assert that labels and strings that are constructed within this workspace return false
|
|
|
|
asserts.false(env, utils.is_external_label("//some/label"))
|
|
|
|
asserts.false(env, utils.is_external_label(Label("//some/label")))
|
2022-10-21 17:03:51 +00:00
|
|
|
asserts.false(env, utils.is_external_label("@//some/label"))
|
2021-11-09 23:13:30 +00:00
|
|
|
asserts.false(env, utils.is_external_label(Label("@aspect_bazel_lib//some/label")))
|
|
|
|
asserts.false(env, ctx.attr.internal_with_workspace_as_string)
|
|
|
|
|
2023-01-14 20:13:02 +00:00
|
|
|
# the "@@" repository name syntax applies to Bazel 6 or greater
|
|
|
|
if utils.is_bazel_6_or_greater():
|
|
|
|
asserts.false(env, utils.is_external_label("@@//some/label"))
|
|
|
|
|
2021-11-09 23:13:30 +00:00
|
|
|
# assert that labels and string that give a workspace return true
|
2023-11-15 23:07:03 +00:00
|
|
|
asserts.true(env, utils.is_external_label(Label("@bazel_skylib//some/label")))
|
2021-11-09 23:13:30 +00:00
|
|
|
asserts.true(env, ctx.attr.external_as_string)
|
|
|
|
|
|
|
|
return unittest.end(env)
|
|
|
|
|
2022-01-18 21:24:44 +00:00
|
|
|
def _propagate_well_known_tags_test_impl(ctx):
|
|
|
|
env = unittest.begin(ctx)
|
|
|
|
|
|
|
|
asserts.equals(env, ["manual", "cpu:12"], utils.propagate_well_known_tags(["foo", "manual", "bar", "cpu:12"]))
|
|
|
|
|
|
|
|
return unittest.end(env)
|
|
|
|
|
2023-09-26 21:29:42 +00:00
|
|
|
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)
|
|
|
|
|
2023-03-04 02:23:17 +00:00
|
|
|
def _consistent_label_str_impl(ctx):
|
|
|
|
env = unittest.begin(ctx)
|
|
|
|
|
|
|
|
asserts.equals(env, "@//foo:bar", utils.consistent_label_str(ctx, Label("//foo:bar")))
|
|
|
|
asserts.equals(env, "@//foo:bar", utils.consistent_label_str(ctx, Label("@//foo:bar")))
|
|
|
|
asserts.equals(env, "@//foo:bar", utils.consistent_label_str(ctx, Label("@aspect_bazel_lib//foo:bar")))
|
2023-11-15 23:07:03 +00:00
|
|
|
asserts.equals(env, "@bazel_skylib//foo:bar", utils.consistent_label_str(ctx, Label("@bazel_skylib//foo:bar")))
|
2023-03-04 02:23:17 +00:00
|
|
|
|
|
|
|
return unittest.end(env)
|
|
|
|
|
2021-11-09 23:13:30 +00:00
|
|
|
to_label_test = unittest.make(
|
|
|
|
_to_label_test_impl,
|
|
|
|
attrs = {
|
|
|
|
"relative_asserts": attr.label_keyed_string_dict(
|
|
|
|
allow_files = True,
|
|
|
|
mandatory = True,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
is_external_label_test = unittest.make(
|
|
|
|
_is_external_label_test_impl,
|
|
|
|
attrs = {
|
|
|
|
"external_as_string": attr.bool(
|
|
|
|
mandatory = True,
|
|
|
|
),
|
|
|
|
"internal_with_workspace_as_string": attr.bool(
|
|
|
|
mandatory = True,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2023-03-04 02:23:17 +00:00
|
|
|
propagate_well_known_tags_test = unittest.make(_propagate_well_known_tags_test_impl)
|
2023-09-26 21:29:42 +00:00
|
|
|
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)
|
2023-03-04 02:23:17 +00:00
|
|
|
consistent_label_str_test = unittest.make(_consistent_label_str_impl)
|
2022-01-18 21:24:44 +00:00
|
|
|
|
2022-07-14 04:39:38 +00:00
|
|
|
# buildifier: disable=function-docstring
|
|
|
|
def file_exists_test():
|
|
|
|
# Tests that must run in the loading phase
|
|
|
|
if utils.file_exists("does-not-exist"):
|
|
|
|
fail("does-not-exist does not exist")
|
|
|
|
if not utils.file_exists("utils_test.bzl"):
|
|
|
|
fail("utils_test.bzl does exist")
|
|
|
|
if (utils.file_exists("copy_to_bin")):
|
|
|
|
fail("copy_to_bin exists, but is a directory")
|
|
|
|
|
2022-07-14 04:04:58 +00:00
|
|
|
# buildifier: disable=function-docstring
|
2021-11-09 23:13:30 +00:00
|
|
|
def utils_test_suite():
|
2023-11-15 23:07:03 +00:00
|
|
|
to_label_test(
|
|
|
|
name = "to_label_tests",
|
|
|
|
relative_asserts = {
|
|
|
|
utils.to_label(":utils_test.bzl"): "//lib/tests:utils_test.bzl",
|
|
|
|
},
|
|
|
|
timeout = "short",
|
|
|
|
# TODO: to_label tests don't work under bzlmod
|
|
|
|
target_compatible_with = select({
|
|
|
|
"@aspect_bazel_lib//lib:bzlmod": ["@platforms//:incompatible"],
|
|
|
|
"//conditions:default": [],
|
|
|
|
}),
|
|
|
|
)
|
2021-11-09 23:13:30 +00:00
|
|
|
|
|
|
|
is_external_label_test(
|
|
|
|
name = "is_external_label_tests",
|
2023-11-15 23:07:03 +00:00
|
|
|
external_as_string = utils.is_external_label("@bazel_skylib//some/label"),
|
2021-11-09 23:13:30 +00:00
|
|
|
internal_with_workspace_as_string = utils.is_external_label("@aspect_bazel_lib//some/label"),
|
2022-08-20 19:53:12 +00:00
|
|
|
timeout = "short",
|
2021-11-09 23:13:30 +00:00
|
|
|
)
|
2022-01-18 21:24:44 +00:00
|
|
|
|
|
|
|
propagate_well_known_tags_test(
|
|
|
|
name = "propagate_well_known_tags_tests",
|
2022-08-20 19:53:12 +00:00
|
|
|
timeout = "short",
|
2022-01-18 21:24:44 +00:00
|
|
|
)
|
2022-07-14 04:04:58 +00:00
|
|
|
|
2023-09-26 21:29:42 +00:00
|
|
|
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",
|
|
|
|
)
|
|
|
|
|
2023-03-04 02:23:17 +00:00
|
|
|
consistent_label_str_test(
|
|
|
|
name = "consistent_label_str_tests",
|
|
|
|
timeout = "short",
|
2023-11-15 23:07:03 +00:00
|
|
|
# TODO: consistent_label_str tests don't work under bzlmod
|
|
|
|
target_compatible_with = select({
|
|
|
|
"@aspect_bazel_lib//lib:bzlmod": ["@platforms//:incompatible"],
|
|
|
|
"//conditions:default": [],
|
|
|
|
}),
|
2023-03-04 02:23:17 +00:00
|
|
|
)
|
|
|
|
|
2022-07-14 04:39:38 +00:00
|
|
|
file_exists_test()
|