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

Clean up testing infra #388

Merged
merged 3 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 6 additions & 3 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ build --noenable_bzlmod
# This is a mandatory flag.
build --incompatible_default_to_explicit_init_py

# Don't let local Python site packages leak into the build and cause problems
common --action_env=PYTHONNOUSERSITE=1

# All code will be compiled with C++17 flag.
# Users can naturally override copts for cc_* targets.
# NOTE: googletest >=v1.13.0 requires min C++14.
Expand All @@ -28,7 +25,13 @@ build --cxxopt=-std=c++17
# Ensure that you don't accidentally make non-hermetic actions/tests
# which depend on remote services. Tag an individual target with
# tags=["requires-network"] to opt-out of the enforcement.
# In ROS context this is important such that each test target is executed
# in a separate network environment.
# This is a mandatory flag.
build --sandbox_default_allow_network=false

# Don't let local Python site packages leak into the build and cause problems
common --action_env=PYTHONNOUSERSITE=1
# Don't let environment variables like $PATH sneak into the build,
# which can cause massive cache misses when they change.
common --incompatible_strict_action_env
Expand Down
3 changes: 0 additions & 3 deletions repositories/ros2_repo_mappings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,6 @@ repositories:
build_file: "@com_github_mvukov_rules_ros2//repositories:ros2cli.BUILD.bazel"
patches:
- "@com_github_mvukov_rules_ros2//repositories/patches:ros2cli_replace-netifaces.patch"
ros_testing:
name: ros2_ros_testing
build_file: "@com_github_mvukov_rules_ros2//repositories:ros_testing.BUILD.bazel"
rosbag2:
name: ros2_rosbag2
build_file: "@com_github_mvukov_rules_ros2//repositories:rosbag2.BUILD.bazel"
Expand Down
9 changes: 0 additions & 9 deletions repositories/ros2_repositories_impl.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -290,15 +290,6 @@ def ros2_repositories_impl():
url = "https://github.com/ros2/ros2cli/archive/refs/tags/0.18.10.tar.gz",
)

maybe(
http_archive,
name = "ros2_ros_testing",
build_file = "@com_github_mvukov_rules_ros2//repositories:ros_testing.BUILD.bazel",
sha256 = "f52dc8d48e3e525597e96e5316e882a03cbed6b2d3024699219c0afc0283a38b",
strip_prefix = "ros_testing-0.4.0",
url = "https://github.com/ros2/ros_testing/archive/refs/tags/0.4.0.tar.gz",
)

maybe(
http_archive,
name = "ros2_rosbag2",
Expand Down
19 changes: 0 additions & 19 deletions repositories/ros_testing.BUILD.bazel

This file was deleted.

32 changes: 12 additions & 20 deletions ros2/pytest_wrapper.py.tpl
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# Adapted from https://gist.github.com/betaboon/c1dd785b5ba468b4df4e382eafff969a

import contextlib
import os
import pathlib
import sys

import coverage
import domain_coordinator
import pytest

{ament_setup}
Expand Down Expand Up @@ -41,28 +39,22 @@ def main() -> None:
if 'ROS_LOG_DIR' not in os.environ:
os.environ['ROS_LOG_DIR'] = bazel_test_output_dir

with contextlib.ExitStack() as stack:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would keep this as an extra line of defense. I also find it good practice to use tempfile and not rely on Bazel for stuff like this. But if you really like it, we can always patch locally.

if 'ROS_DOMAIN_ID' not in os.environ:
domain_id = stack.enter_context(domain_coordinator.domain_id())
os.environ['ROS_DOMAIN_ID'] = str(domain_id)
print(f'Running with ROS_DOMAIN_ID {os.environ["ROS_DOMAIN_ID"]}')
bazel_coverage = os.getenv('COVERAGE') == '1'

bazel_coverage = os.getenv('COVERAGE') == '1'
coverage_session = None
if bazel_coverage:
coverage_session = start_coverage_session()

coverage_session = None
if bazel_coverage:
coverage_session = start_coverage_session()
args = [
'-ra', '-vv', '-p', 'launch_pytest.plugin',
f'--junitxml={os.environ["XML_OUTPUT_FILE"]}'
] + sys.argv[1:]
pytest_exit_code = pytest.main(args)

args = [
'-ra', '-vv', '-p', 'launch_pytest.plugin',
f'--junitxml={os.environ["XML_OUTPUT_FILE"]}'
] + sys.argv[1:]
pytest_exit_code = pytest.main(args)
if coverage_session:
finalize_coverage_session(coverage_session)

if coverage_session:
finalize_coverage_session(coverage_session)

sys.exit(pytest_exit_code)
sys.exit(pytest_exit_code)


if __name__ == '__main__':
Expand Down
2 changes: 0 additions & 2 deletions ros2/test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ def _ros2_launch_testing_test(name, nodes, launch_file, deps, data, idl_deps, **
data = nodes + [launch_file] + data,
main = launch_script,
deps = [
"@ros2_ament_cmake_ros//:domain_coordinator",
"@ros2_launch//:launch_testing",
"@ros2_launch_ros//:launch_testing_ros",
] + deps,
Expand All @@ -78,7 +77,6 @@ def _ros2_launch_pytest_test(name, nodes, launch_file, deps, data, idl_deps, **k
args = kwargs.pop("args", []) + ["$(rootpath :%s)" % launch_file],
deps = [
"@ros2_launch//:launch_pytest",
"@ros2_ament_cmake_ros//:domain_coordinator",
requirement("coverage"),
requirement("pytest"),
requirement("pytest-cov"),
Expand Down
20 changes: 6 additions & 14 deletions ros2/test.py.tpl
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import contextlib
import os
import sys

import domain_coordinator
import launch_testing.launch_test
import launch_testing_ros

{ament_setup}

LAUNCH_FILE = '{launch_file}'

# The package name is intentionally undefined such that ros2test picks up
# the given launch file.
# The package name is intentionally undefined such that
# launch_testing.launch_test picks up the given launch file.
sys.argv = sys.argv[:1] + [
f'--junit-xml={os.environ["XML_OUTPUT_FILE"]}',
LAUNCH_FILE,
Expand All @@ -25,13 +23,7 @@ if 'ROS_HOME' not in os.environ:
if 'ROS_LOG_DIR' not in os.environ:
os.environ['ROS_LOG_DIR'] = bazel_test_output_dir

with contextlib.ExitStack() as stack:
if 'ROS_DOMAIN_ID' not in os.environ:
domain_id = stack.enter_context(domain_coordinator.domain_id())
os.environ['ROS_DOMAIN_ID'] = str(domain_id)
print(f'Running with ROS_DOMAIN_ID {os.environ["ROS_DOMAIN_ID"]}')

parser, args = launch_testing.launch_test.parse_arguments()
exit_code = launch_testing.launch_test.run(
parser, args, test_runner_cls=launch_testing_ros.LaunchTestRunner)
sys.exit(exit_code)
parser, args = launch_testing.launch_test.parse_arguments()
exit_code = launch_testing.launch_test.run(
parser, args, test_runner_cls=launch_testing_ros.LaunchTestRunner)
sys.exit(exit_code)