Skip to content

Build osrf_testing_tools_cpp as a bazel module #93

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

Draft
wants to merge 1 commit into
base: rolling
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions .bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
8.2.1
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bazel-*
113 changes: 113 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
load("@bazel_skylib//lib:selects.bzl", "selects")
load("//osrf_testing_tools_cpp/bazel:defs.bzl", "test_runner_sh_test")

cc_library(
name = "backward-cpp",
srcs = [
"osrf_testing_tools_cpp/src/memory_tools/vendor/bombela/backward-cpp/backward.cpp",
"osrf_testing_tools_cpp/src/memory_tools/vendor/bombela/backward-cpp/backward.hpp"
],
includes = ["osrf_testing_tools_cpp/src"]
)

cc_library(
name = "memory_tools",
srcs = glob(include = [
"osrf_testing_tools_cpp/src/memory_tools/*.cpp",
"osrf_testing_tools_cpp/src/memory_tools/*.hpp"
],
exclude = [
"osrf_testing_tools_cpp/src/memory_tools/memory_tools.cpp"
]),
textual_hdrs = selects.with_or(
{
("@platforms//os:linux"): [
"osrf_testing_tools_cpp/src/memory_tools/impl/linux.cpp",
"osrf_testing_tools_cpp/src/memory_tools/impl/static_allocator.hpp",
"osrf_testing_tools_cpp/src/memory_tools/impl/unix_common.cpp",
"osrf_testing_tools_cpp/src/memory_tools/impl/unix_common.hpp",
],
}
),
hdrs = glob([
"osrf_testing_tools_cpp/include/**/*.hpp"
]),
linkopts = ["-ldl"],
includes = ["osrf_testing_tools_cpp/include"],
deps = [":backward-cpp"],
visibility = ["//visibility:public"]
)

cc_library(
name = "test_runner_headers",
hdrs = [
"osrf_testing_tools_cpp/src/test_runner/execute_process.hpp",
"osrf_testing_tools_cpp/src/test_runner/get_environment_variable.hpp",
"osrf_testing_tools_cpp/src/test_runner/parse_environment_variable.hpp",
"osrf_testing_tools_cpp/src/test_runner/starts_with.hpp",
],
includes = [
"osrf_testing_tools_cpp/src",
],
)

cc_binary(
name = "test_runner",
srcs = [
"osrf_testing_tools_cpp/src/test_runner/main.cpp",
],
deps = [
":test_runner_headers",
]
)

cc_test(
name = "test_parse_environment",
srcs = [
"osrf_testing_tools_cpp/test/test_runner/test_parse_environment_variable.cpp",
],
deps = [
":test_runner_headers",
"@googletest//:gtest_main"
]
)

cc_binary(
name = "assert_env_vars_main",
srcs = [
"osrf_testing_tools_cpp/test/test_runner/assert_env_vars.cpp",
],
deps = [
":test_runner_headers",
"@googletest//:gtest_main"
]
)

test_runner_sh_test(
name = "assert_env_vars",
test_runner = ":test_runner",
test_binary = ":assert_env_vars_main",
test_runner_args = [
"--env", "FOO=bar", "PING=pong",
"--append-env", "FOO=baz"
],
test_args = [
"--env", "FOO=bar:baz", "PING=pong"
]
)

cc_library(
name = "memory_tools_interpose",
srcs = ["osrf_testing_tools_cpp/src/memory_tools/memory_tools.cpp"],
deps = [":memory_tools"]
)

cc_test(
name = "test_memory_tools_main",
srcs = ["osrf_testing_tools_cpp/test/memory_tools/test_memory_tools.cpp"],
malloc = ":memory_tools_interpose",
deps = [
":memory_tools",
"@googletest//:gtest"
],
)
8 changes: 8 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module(
name = "osrf_testing_tools_cpp",
)

bazel_dep(name = "rules_license", version = "1.0.0")
bazel_dep(name = "bazel_skylib", version = "1.7.1")
bazel_dep(name = "platforms", version = "0.0.10")
bazel_dep(name = "googletest", version = "1.16.0.bcr.1")
262 changes: 262 additions & 0 deletions MODULE.bazel.lock

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions osrf_testing_tools_cpp/bazel/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")

bzl_library(
name = "defs",
srcs = ["defs.bzl"],
visibility = ["//visibility:public"],
)
97 changes: 97 additions & 0 deletions osrf_testing_tools_cpp/bazel/defs.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
def _test_runner_sh_test_impl(ctx):
script_content = """#!/bin/sh
# Script to execute {runner_label} with arguments.

set -e # Exit immediately if a command exits with a non-zero status.

RUNNER_PATH="{runner_path_prefix}/{runner_short_path}"
TEST_PATH="{test_path_prefix}/{test_short_path}"
RUNNER_ARGS=({test_runner_args_string})
TEST_ARGS=({test_args_string})

echo "Executing: $RUNNER_PATH" "${{RUNNER_ARGS[@]}}" "-- $TEST_PATH ${{TEST_ARGS[@]}}"
"$RUNNER_PATH" "${{RUNNER_ARGS[@]}}" -- $TEST_PATH "${{TEST_ARGS[@]}}"
""".format(
runner_label = ctx.attr.test_runner_binary,
runner_path_prefix = ctx.attr.test_runner_binary.label.package if ctx.attr.test_runner_binary.label.package else ".",
runner_short_path = ctx.attr.test_runner_binary.label.name,
test_path_prefix = ctx.attr.test_binary.label.package if ctx.attr.test_binary.label.package else ".",
test_short_path = ctx.attr.test_binary.label.name,
test_runner_args_string = " ".join(["'{}'".format(arg) for arg in ctx.attr.test_runner_args]),
test_args_string = " ".join(["'{}'".format(arg) for arg in ctx.attr.test_args]),
)

script_name = "{}_runner.sh".format(ctx.attr.name)
ctx.actions.write(
output = ctx.outputs.executable,
content = script_content,
is_executable = True,
)

# The runfiles need to include the actual test_runner binary
runfiles = ctx.runfiles(files = [ctx.executable.test_runner_binary, ctx.executable.test_binary])

return [DefaultInfo(
files = depset([ctx.outputs.executable]),
runfiles = runfiles,
)]

_test_runner_sh_test = rule(
implementation = _test_runner_sh_test_impl,
attrs = {
"test_runner_binary": attr.label(
doc = "The test runner binary.",
mandatory = True,
executable = True,
cfg = "exec", # Compile for execution platform
),
"test_binary": attr.label(
doc = "The binary target to be tested.",
mandatory = True,
executable = True,
cfg = "exec", # Compile for execution platform
),
"test_runner_args": attr.string_list(
doc = "A list of arguments to pass to the test_runner.",
default = [],
),
"test_args": attr.string_list(
doc = "A list of arguments to pass to the test.",
default = [],
),
},
executable = True, # Signifies that this rule outputs an executable (the script)
test = True, # Signifies that this rule can be run as a test
)

def test_runner_sh_test(name, test_runner, test_binary, test_runner_args = [], test_args = [], **kwargs):
"""
Generates an sh_test that executes the given test_runner binary with specified arguments.

Args:
name: The name of the sh_test.
test_runner: The label of the cc_binary or other executable target.
args: A list of strings representing the arguments to pass to the test_runner.
**kwargs: Additional arguments to pass to the underlying sh_test rule.
"""
script_name = "{}_generated_script".format(name)

# Using the internal rule to generate the script
_test_runner_sh_test(
name = script_name,
test_runner_binary = test_runner,
test_runner_args = test_runner_args,
test_binary = test_binary,
test_args = test_args,
# Ensure the generated script itself is not the test directly,
# but is used by the native.sh_test
testonly = True, # Mark as testonly if the script itself isn't meant for release
tags = kwargs.pop("tags", []) + ["manual"], # Usually you don't run this rule directly
)

# Create the sh_test rule using the generated script
native.sh_test(
name = name,
srcs = [":" + script_name],
data = [test_runner, test_binary],
)
1 change: 1 addition & 0 deletions osrf_testing_tools_cpp/src/memory_tools/memory_tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "./impl/linux.cpp"
#include "./impl/unix_common.cpp"


#elif defined(__APPLE__)

#include "./impl/apple.cpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,9 @@ TEST(TestMemoryTools, test_static_allocation_alignment) {
ASSERT_TRUE(allocator.deallocate(memory));
}
}

int main(int argc, char * argv[])
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}