Skip to content

Commit

Permalink
Migrate the remaining cc_shared_library tests to rules_testing
Browse files Browse the repository at this point in the history
RELNOTES:none
PiperOrigin-RevId: 530240916
Change-Id: I44099565c90265c248333380c6fd2000a08eb9ba
  • Loading branch information
oquenchil authored and copybara-github committed May 8, 2023
1 parent 8d2ccd4 commit 24ea6dc
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -352,28 +352,23 @@ linking_order_test(

additional_inputs_test(
name = "additional_inputs_test",
# TODO(bazel-team): Support this test on Windows and Mac.
is_linux = select({
"//src/conditions:linux": True,
"//conditions:default": False,
}),
target_under_test = ":foo_so",
target = ":foo_so",
)

build_failure_test(
name = "link_once_repeated_test_binary",
messages = [
"cc_shared_library/test_cc_shared_library:barX\",",
],
target_under_test = "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets:should_fail_binary",
target = "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets:should_fail_binary",
)

build_failure_test(
name = "link_once_repeated_test_shared_lib",
messages = [
"cc_shared_library/test_cc_shared_library:barX\",",
],
target_under_test = "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets:should_fail_shared_lib",
target = "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets:should_fail_shared_lib",
)

paths_test(
Expand Down Expand Up @@ -443,13 +438,13 @@ cc_library(
build_failure_test(
name = "two_dynamic_deps_same_export_in_so_test",
message = "Two shared libraries in dependencies export the same symbols",
target_under_test = "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets:two_dynamic_deps_same_export_in_so",
target = "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets:two_dynamic_deps_same_export_in_so",
)

build_failure_test(
name = "two_dynamic_deps_same_export_in_binary_test",
message = "Two shared libraries in dependencies link the same library statically",
target_under_test = "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets:two_dynamic_deps_same_export_in_binary",
target = "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets:two_dynamic_deps_same_export_in_binary",
)

analysis_test(
Expand All @@ -460,42 +455,34 @@ analysis_test(

interface_library_output_group_test(
name = "interface_library_output_group_test",
is_windows = select({
"//src/conditions:windows": True,
"//conditions:default": False,
}),
target_under_test = ":foo_so",
target = ":foo_so",
)

runfiles_test(
name = "runfiles_test",
is_linux = select({
"//src/conditions:linux": True,
"//conditions:default": False,
}),
target_under_test = ":python_test",
target = ":python_test",
)

check_linking_action_lib_parameters_test(
name = "check_binary_doesnt_take_already_linked_in_libs",
target_under_test = ":binary",
target = ":binary",
libs_that_shouldnt_be_present = ["foo", "bar"],
)

check_linking_action_lib_parameters_test(
name = "check_shared_lib_doesnt_take_already_linked_in_libs",
target_under_test = ":foo_so",
target = ":foo_so",
libs_that_shouldnt_be_present = ["bar"],
)

build_failure_test(
name = "shared_library_without_deps",
message = "'cc_shared_library' must have at least one dependency in 'deps' (or 'roots')",
target_under_test = "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets:failing_with_no_deps_so",
target = "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets:failing_with_no_deps_so",
)

build_failure_test(
name = "direct_dep_with_only_shared_lib_file",
message = "Do not place libraries which only contain a precompiled dynamic library",
target_under_test = "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets:failing_only_dynamic_lib",
target = "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets:failing_only_dynamic_lib",
)
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

"""Starlark tests for cc_shared_library"""

load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts", "unittest")
load("@bazel_skylib//lib:paths.bzl", "paths")
load("@rules_testing//lib:truth.bzl", "matching")
load("@rules_testing//lib:analysis_test.bzl", "analysis_test")
Expand Down Expand Up @@ -75,79 +74,88 @@ def _linking_order_test_macro(name, target):

linking_order_test = _linking_order_test_macro

def _additional_inputs_test_impl(ctx):
env = analysistest.begin(ctx)

if ctx.attr.is_linux:
target_under_test = analysistest.target_under_test(env)
actions = analysistest.target_actions(env)

def _additional_inputs_test_impl(env, target):
if env.ctx.target_platform_has_constraint(env.ctx.attr._is_linux[platform_common.ConstraintValueInfo]):
found = False
target_action = None
for action in actions:
for action in target.actions:
if action.mnemonic == "CppLink":
target_action = action
break
for arg in target_action.argv:
if arg.find("-Wl,--script=") != -1:
asserts.equals(env, "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/additional_script.txt", arg[13:])
env.expect.that_str(
"src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/additional_script.txt",
).equals(arg[13:])
found = True
break
asserts.true(env, found, "Should have seen option --script=")

return analysistest.end(env)

additional_inputs_test = analysistest.make(
_additional_inputs_test_impl,
attrs = {
"is_linux": attr.bool(),
},
)
env.expect.where(
detail = "Should have seen option --script=",
).that_bool(found).equals(True)

def _build_failure_test_impl(ctx):
env = analysistest.begin(ctx)
def _additional_inputs_test_macro(name, target):
analysis_test(
name = name,
impl = _additional_inputs_test_impl,
target = target,
attrs = {
"_is_linux": attr.label(default = "@platforms//os:linux"),
},
)

if ctx.attr.message:
asserts.expect_failure(env, ctx.attr.message)
additional_inputs_test = _additional_inputs_test_macro

if ctx.attr.messages:
for message in ctx.attr.messages:
asserts.expect_failure(env, message)
def _build_failure_test_impl(env, target):
if env.ctx.attr._message:
env.expect.that_target(target).failures().contains_predicate(matching.contains(env.ctx.attr._message))

return analysistest.end(env)
if env.ctx.attr._messages:
for message in env.ctx.attr._messages:
env.expect.that_target(target).failures().contains_predicate(matching.contains(message))

build_failure_test = analysistest.make(
_build_failure_test_impl,
expect_failure = True,
attrs = {
"message": attr.string(),
"messages": attr.string_list(),
},
)
def _build_failure_test_macro(name, target, message = "", messages = []):
analysis_test(
name = name,
impl = _build_failure_test_impl,
target = target,
expect_failure = True,
attrs = {
"_message": attr.string(default = message),
"_messages": attr.string_list(default = messages),
},
)

def _paths_test_impl(ctx):
env = unittest.begin(ctx)
build_failure_test = _build_failure_test_macro

asserts.false(env, _check_if_target_under_path(Label("//foo"), Label("//bar")))
asserts.false(env, _check_if_target_under_path(Label("@foo//foo"), Label("@bar//bar")))
asserts.false(env, _check_if_target_under_path(Label("//bar"), Label("@foo//bar")))
asserts.true(env, _check_if_target_under_path(Label("@foo//bar"), Label("@foo//bar")))
asserts.true(env, _check_if_target_under_path(Label("@foo//bar:bar"), Label("@foo//bar")))
asserts.true(env, _check_if_target_under_path(Label("//bar:bar"), Label("//bar")))
def _paths_test_impl(env, _):
env.expect.that_bool(_check_if_target_under_path(Label("//foo"), Label("//bar"))).equals(False)
env.expect.that_bool(_check_if_target_under_path(Label("@foo//foo"), Label("@bar//bar"))).equals(False)
env.expect.that_bool(_check_if_target_under_path(Label("//bar"), Label("@foo//bar"))).equals(False)
env.expect.that_bool(_check_if_target_under_path(Label("@foo//bar"), Label("@foo//bar"))).equals(True)
env.expect.that_bool(_check_if_target_under_path(Label("@foo//bar:bar"), Label("@foo//bar"))).equals(True)
env.expect.that_bool(_check_if_target_under_path(Label("//bar:bar"), Label("//bar"))).equals(True)

asserts.false(env, _check_if_target_under_path(Label("@foo//bar/baz"), Label("@foo//bar")))
asserts.false(env, _check_if_target_under_path(Label("@foo//bar/baz"), Label("@foo//bar:__pkg__")))
asserts.true(env, _check_if_target_under_path(Label("@foo//bar/baz"), Label("@foo//bar:__subpackages__")))
asserts.true(env, _check_if_target_under_path(Label("@foo//bar:qux"), Label("@foo//bar:__pkg__")))
env.expect.that_bool(_check_if_target_under_path(Label("@foo//bar/baz"), Label("@foo//bar"))).equals(False)
env.expect.that_bool(_check_if_target_under_path(Label("@foo//bar/baz"), Label("@foo//bar:__pkg__"))).equals(False)
env.expect.that_bool(_check_if_target_under_path(Label("@foo//bar/baz"), Label("@foo//bar:__subpackages__"))).equals(True)
env.expect.that_bool(_check_if_target_under_path(Label("@foo//bar:qux"), Label("@foo//bar:__pkg__"))).equals(True)

asserts.false(env, _check_if_target_under_path(Label("@foo//bar"), Label("@foo//bar/baz:__subpackages__")))
asserts.false(env, _check_if_target_under_path(Label("//bar"), Label("//bar/baz:__pkg__")))
env.expect.that_bool(_check_if_target_under_path(Label("@foo//bar"), Label("@foo//bar/baz:__subpackages__"))).equals(False)
env.expect.that_bool(_check_if_target_under_path(Label("//bar"), Label("//bar/baz:__pkg__"))).equals(False)

asserts.true(env, _check_if_target_under_path(Label("//foo/bar:baz"), Label("//:__subpackages__")))
env.expect.that_bool(_check_if_target_under_path(Label("//foo/bar:baz"), Label("//:__subpackages__"))).equals(True)

return unittest.end(env)
def _paths_test_macro(name):
native.cc_library(
name = "dummy",
)
analysis_test(
name = name,
impl = _paths_test_impl,
target = ":dummy",
)

paths_test = unittest.make(_paths_test_impl)
paths_test = _paths_test_macro

def _debug_files_test_impl(env, target):
expected_files = [
Expand All @@ -168,13 +176,11 @@ def _debug_files_test_impl(env, target):

debug_files_test = _debug_files_test_impl

def _runfiles_test_impl(ctx):
env = analysistest.begin(ctx)
if not ctx.attr.is_linux:
return analysistest.end(env)
def _runfiles_test_impl(env, target):
if not env.ctx.target_platform_has_constraint(env.ctx.attr._is_linux[platform_common.ConstraintValueInfo]):
return

target_under_test = analysistest.target_under_test(env)
expected_suffixes = [
expected_basenames = [
"libfoo_so.so",
"libbar_so.so",
"libdiff_pkg_so.so",
Expand All @@ -184,70 +190,79 @@ def _runfiles_test_impl(ctx):
"Smain_Sstarlark_Stests_Sbuiltins_Ubzl_Scc_Scc_Ushared_Ulibrary_Stest_Ucc_Ushared_Ulibrary/renamed_so_file_copy.so",
"Smain_Sstarlark_Stests_Sbuiltins_Ubzl_Scc_Scc_Ushared_Ulibrary_Stest_Ucc_Ushared_Ulibrary/libdirect_so_file.so",
]
for runfile in target_under_test[DefaultInfo].default_runfiles.files.to_list():
for runfile in target[DefaultInfo].default_runfiles.files.to_list():
# Ignore Python runfiles
if "python" in runfile.path:
continue
found_suffix = False
for expected_suffix in expected_suffixes:
if runfile.path.endswith(expected_suffix):
found_suffix = True
break
asserts.true(env, found_suffix, runfile.path + " not found in expected suffixes:\n" + "\n".join(expected_suffixes))

return analysistest.end(env)
found_basename = False
for expected_basename in expected_basenames:
if runfile.path.endswith(expected_basename):
found_basename = True
break

runfiles_test = analysistest.make(
_runfiles_test_impl,
attrs = {
"is_linux": attr.bool(),
},
)
env.expect.where(
detail = runfile.path + " not found in expected basenames:\n" + "\n".join(expected_basenames),
).that_bool(found_basename).equals(True)

def _interface_library_output_group_test_impl(ctx):
env = analysistest.begin(ctx)
if not ctx.attr.is_windows:
return analysistest.end(env)
def _runfiles_test_macro(name, target):
analysis_test(
name = name,
impl = _runfiles_test_impl,
target = target,
attrs = {
"_is_linux": attr.label(default = "@platforms//os:linux"),
},
)

target_under_test = analysistest.target_under_test(env)
actual_files = []
for interface_library in target_under_test[OutputGroupInfo].interface_library.to_list():
actual_files.append(interface_library.basename)
expected_files = ["foo_so.if.lib"]
asserts.equals(env, expected_files, actual_files)
runfiles_test = _runfiles_test_macro

return analysistest.end(env)
def _interface_library_output_group_test_impl(env, target):
if not env.ctx.target_platform_has_constraint(env.ctx.attr._is_windows[platform_common.ConstraintValueInfo]):
return

interface_library_output_group_test = analysistest.make(
_interface_library_output_group_test_impl,
attrs = {
"is_windows": attr.bool(),
},
)
actual_files = [interface_library.basename for interface_library in target[OutputGroupInfo].interface_library.to_list()]
env.expect.that_collection(actual_files).contains_exactly_predicates([
matching.contains("foo_so.if.lib"),
])

def _check_linking_action_lib_parameters_test_impl(ctx):
env = analysistest.begin(ctx)
def _interface_library_output_group_test_macro(name, target):
analysis_test(
name = name,
impl = _interface_library_output_group_test_impl,
target = target,
attrs = {
"_is_windows": attr.label(default = "@platforms//os:windows"),
},
)

actions = analysistest.target_actions(env)
interface_library_output_group_test = _interface_library_output_group_test_macro

def _check_linking_action_lib_parameters_test_impl(env, target):
target_action = None
for action in actions:
for action in target.actions:
if action.mnemonic == "FileWrite":
target_action = action
break

args = target_action.content.split("\n")
for arg in args:
for bad_lib_entry in ctx.attr.libs_that_shouldnt_be_present:
asserts.true(env, arg.find("{}.".format(bad_lib_entry)) == -1, "Should not have seen library `{}` in command line".format(arg))
for bad_lib_entry in env.ctx.attr._libs_that_shouldnt_be_present:
env.expect.where(
detail = "Should not have seen library `{}` in command line".format(arg),
).that_int(arg.find("{}.".format(bad_lib_entry))).equals(-1)

return analysistest.end(env)
def _check_linking_action_lib_parameters_test_macro(name, target, libs_that_shouldnt_be_present):
analysis_test(
name = name,
impl = _check_linking_action_lib_parameters_test_impl,
target = target,
attrs = {
"_libs_that_shouldnt_be_present": attr.string_list(default = libs_that_shouldnt_be_present),
},
)

check_linking_action_lib_parameters_test = analysistest.make(
_check_linking_action_lib_parameters_test_impl,
attrs = {
"libs_that_shouldnt_be_present": attr.string_list(),
},
)
check_linking_action_lib_parameters_test = _check_linking_action_lib_parameters_test_macro

AspectCcInfo = provider("Takes a cc_info.", fields = {"cc_info": "cc_info"})
WrappedCcInfo = provider("Takes a cc_info.", fields = {"cc_info": "cc_info"})
Expand Down

0 comments on commit 24ea6dc

Please sign in to comment.