Skip to content

Commit 3d0f2d5

Browse files
committed
Signed-off-by: Michael Carroll <[email protected]>
1 parent 748de1c commit 3d0f2d5

File tree

9 files changed

+496
-0
lines changed

9 files changed

+496
-0
lines changed

.bazelversion

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
8.2.1

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bazel-*

BUILD.bazel

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
load("@bazel_skylib//lib:selects.bzl", "selects")
2+
load("//osrf_testing_tools_cpp/bazel:defs.bzl", "test_runner_sh_test")
3+
4+
cc_library(
5+
name = "backward-cpp",
6+
srcs = [
7+
"osrf_testing_tools_cpp/src/memory_tools/vendor/bombela/backward-cpp/backward.cpp",
8+
"osrf_testing_tools_cpp/src/memory_tools/vendor/bombela/backward-cpp/backward.hpp"
9+
],
10+
includes = ["osrf_testing_tools_cpp/src"]
11+
)
12+
13+
cc_library(
14+
name = "memory_tools",
15+
srcs = glob(include = [
16+
"osrf_testing_tools_cpp/src/memory_tools/*.cpp",
17+
"osrf_testing_tools_cpp/src/memory_tools/*.hpp"
18+
],
19+
exclude = [
20+
"osrf_testing_tools_cpp/src/memory_tools/memory_tools.cpp"
21+
]),
22+
textual_hdrs = selects.with_or(
23+
{
24+
("@platforms//os:linux"): [
25+
"osrf_testing_tools_cpp/src/memory_tools/impl/linux.cpp",
26+
"osrf_testing_tools_cpp/src/memory_tools/impl/static_allocator.hpp",
27+
"osrf_testing_tools_cpp/src/memory_tools/impl/unix_common.cpp",
28+
"osrf_testing_tools_cpp/src/memory_tools/impl/unix_common.hpp",
29+
],
30+
}
31+
),
32+
hdrs = glob([
33+
"osrf_testing_tools_cpp/include/**/*.hpp"
34+
]),
35+
linkopts = ["-ldl"],
36+
includes = ["osrf_testing_tools_cpp/include"],
37+
deps = [":backward-cpp"],
38+
visibility = ["//visibility:public"]
39+
)
40+
41+
cc_library(
42+
name = "test_runner_headers",
43+
hdrs = [
44+
"osrf_testing_tools_cpp/src/test_runner/execute_process.hpp",
45+
"osrf_testing_tools_cpp/src/test_runner/get_environment_variable.hpp",
46+
"osrf_testing_tools_cpp/src/test_runner/parse_environment_variable.hpp",
47+
"osrf_testing_tools_cpp/src/test_runner/starts_with.hpp",
48+
],
49+
includes = [
50+
"osrf_testing_tools_cpp/src",
51+
],
52+
)
53+
54+
cc_binary(
55+
name = "test_runner",
56+
srcs = [
57+
"osrf_testing_tools_cpp/src/test_runner/main.cpp",
58+
],
59+
deps = [
60+
":test_runner_headers",
61+
]
62+
)
63+
64+
cc_test(
65+
name = "test_parse_environment",
66+
srcs = [
67+
"osrf_testing_tools_cpp/test/test_runner/test_parse_environment_variable.cpp",
68+
],
69+
deps = [
70+
":test_runner_headers",
71+
"@googletest//:gtest_main"
72+
]
73+
)
74+
75+
cc_binary(
76+
name = "assert_env_vars_main",
77+
srcs = [
78+
"osrf_testing_tools_cpp/test/test_runner/assert_env_vars.cpp",
79+
],
80+
deps = [
81+
":test_runner_headers",
82+
"@googletest//:gtest_main"
83+
]
84+
)
85+
86+
test_runner_sh_test(
87+
name = "assert_env_vars",
88+
test_runner = ":test_runner",
89+
test_binary = ":assert_env_vars_main",
90+
test_runner_args = [
91+
"--env", "FOO=bar", "PING=pong",
92+
"--append-env", "FOO=baz"
93+
],
94+
test_args = [
95+
"--env", "FOO=bar:baz", "PING=pong"
96+
]
97+
)
98+
99+
cc_library(
100+
name = "memory_tools_interpose",
101+
srcs = ["osrf_testing_tools_cpp/src/memory_tools/memory_tools.cpp"],
102+
deps = [":memory_tools"]
103+
)
104+
105+
cc_test(
106+
name = "test_memory_tools_main",
107+
srcs = ["osrf_testing_tools_cpp/test/memory_tools/test_memory_tools.cpp"],
108+
malloc = ":memory_tools_interpose",
109+
deps = [
110+
":memory_tools",
111+
"@googletest//:gtest"
112+
],
113+
)

MODULE.bazel

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module(
2+
name = "osrf_testing_tools_cpp",
3+
)
4+
5+
bazel_dep(name = "rules_license", version = "1.0.0")
6+
bazel_dep(name = "bazel_skylib", version = "1.7.1")
7+
bazel_dep(name = "platforms", version = "0.0.10")
8+
bazel_dep(name = "googletest", version = "1.16.0.bcr.1")

MODULE.bazel.lock

Lines changed: 262 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
2+
3+
bzl_library(
4+
name = "defs",
5+
srcs = ["defs.bzl"],
6+
visibility = ["//visibility:public"],
7+
)

osrf_testing_tools_cpp/bazel/defs.bzl

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
def _test_runner_sh_test_impl(ctx):
2+
script_content = """#!/bin/sh
3+
# Script to execute {runner_label} with arguments.
4+
5+
set -e # Exit immediately if a command exits with a non-zero status.
6+
7+
RUNNER_PATH="{runner_path_prefix}/{runner_short_path}"
8+
TEST_PATH="{test_path_prefix}/{test_short_path}"
9+
RUNNER_ARGS=({test_runner_args_string})
10+
TEST_ARGS=({test_args_string})
11+
12+
echo "Executing: $RUNNER_PATH" "${{RUNNER_ARGS[@]}}" "-- $TEST_PATH ${{TEST_ARGS[@]}}"
13+
"$RUNNER_PATH" "${{RUNNER_ARGS[@]}}" -- $TEST_PATH "${{TEST_ARGS[@]}}"
14+
""".format(
15+
runner_label = ctx.attr.test_runner_binary,
16+
runner_path_prefix = ctx.attr.test_runner_binary.label.package if ctx.attr.test_runner_binary.label.package else ".",
17+
runner_short_path = ctx.attr.test_runner_binary.label.name,
18+
test_path_prefix = ctx.attr.test_binary.label.package if ctx.attr.test_binary.label.package else ".",
19+
test_short_path = ctx.attr.test_binary.label.name,
20+
test_runner_args_string = " ".join(["'{}'".format(arg) for arg in ctx.attr.test_runner_args]),
21+
test_args_string = " ".join(["'{}'".format(arg) for arg in ctx.attr.test_args]),
22+
)
23+
24+
script_name = "{}_runner.sh".format(ctx.attr.name)
25+
ctx.actions.write(
26+
output = ctx.outputs.executable,
27+
content = script_content,
28+
is_executable = True,
29+
)
30+
31+
# The runfiles need to include the actual test_runner binary
32+
runfiles = ctx.runfiles(files = [ctx.executable.test_runner_binary, ctx.executable.test_binary])
33+
34+
return [DefaultInfo(
35+
files = depset([ctx.outputs.executable]),
36+
runfiles = runfiles,
37+
)]
38+
39+
_test_runner_sh_test = rule(
40+
implementation = _test_runner_sh_test_impl,
41+
attrs = {
42+
"test_runner_binary": attr.label(
43+
doc = "The test runner binary.",
44+
mandatory = True,
45+
executable = True,
46+
cfg = "exec", # Compile for execution platform
47+
),
48+
"test_binary": attr.label(
49+
doc = "The binary target to be tested.",
50+
mandatory = True,
51+
executable = True,
52+
cfg = "exec", # Compile for execution platform
53+
),
54+
"test_runner_args": attr.string_list(
55+
doc = "A list of arguments to pass to the test_runner.",
56+
default = [],
57+
),
58+
"test_args": attr.string_list(
59+
doc = "A list of arguments to pass to the test.",
60+
default = [],
61+
),
62+
},
63+
executable = True, # Signifies that this rule outputs an executable (the script)
64+
test = True, # Signifies that this rule can be run as a test
65+
)
66+
67+
def test_runner_sh_test(name, test_runner, test_binary, test_runner_args = [], test_args = [], **kwargs):
68+
"""
69+
Generates an sh_test that executes the given test_runner binary with specified arguments.
70+
71+
Args:
72+
name: The name of the sh_test.
73+
test_runner: The label of the cc_binary or other executable target.
74+
args: A list of strings representing the arguments to pass to the test_runner.
75+
**kwargs: Additional arguments to pass to the underlying sh_test rule.
76+
"""
77+
script_name = "{}_generated_script".format(name)
78+
79+
# Using the internal rule to generate the script
80+
_test_runner_sh_test(
81+
name = script_name,
82+
test_runner_binary = test_runner,
83+
test_runner_args = test_runner_args,
84+
test_binary = test_binary,
85+
test_args = test_args,
86+
# Ensure the generated script itself is not the test directly,
87+
# but is used by the native.sh_test
88+
testonly = True, # Mark as testonly if the script itself isn't meant for release
89+
tags = kwargs.pop("tags", []) + ["manual"], # Usually you don't run this rule directly
90+
)
91+
92+
# Create the sh_test rule using the generated script
93+
native.sh_test(
94+
name = name,
95+
srcs = [":" + script_name],
96+
data = [test_runner, test_binary],
97+
)

osrf_testing_tools_cpp/src/memory_tools/memory_tools.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "./impl/linux.cpp"
1818
#include "./impl/unix_common.cpp"
1919

20+
2021
#elif defined(__APPLE__)
2122

2223
#include "./impl/apple.cpp"

osrf_testing_tools_cpp/test/memory_tools/test_memory_tools.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,9 @@ TEST(TestMemoryTools, test_static_allocation_alignment) {
255255
ASSERT_TRUE(allocator.deallocate(memory));
256256
}
257257
}
258+
259+
int main(int argc, char * argv[])
260+
{
261+
::testing::InitGoogleTest(&argc, argv);
262+
return RUN_ALL_TESTS();
263+
}

0 commit comments

Comments
 (0)