From 6d2db95c7ecbe1c90e8fc27291f47bdcb966c478 Mon Sep 17 00:00:00 2001 From: Milan Vukov Date: Thu, 8 Aug 2024 23:29:18 +0200 Subject: [PATCH] Symlink test utils --- examples/README.md | 3 -- examples/chatter/BUILD.bazel | 17 +++++++- ...r_tests.launch => talker_tests.launch.tpl} | 4 +- third_party/BUILD.bazel | 1 + third_party/ros/rostest/BUILD.bazel | 41 +++++++++++++++---- third_party/symlink.bzl | 31 ++++++++++++++ 6 files changed, 83 insertions(+), 14 deletions(-) rename examples/chatter/{talker_tests.launch => talker_tests.launch.tpl} (77%) create mode 100644 third_party/symlink.bzl diff --git a/examples/README.md b/examples/README.md index 333d03a..e69de29 100644 --- a/examples/README.md +++ b/examples/README.md @@ -1,3 +0,0 @@ -``` -bazel test //... --@rules_python//python/config_settings:python_version=3.10 -``` diff --git a/examples/chatter/BUILD.bazel b/examples/chatter/BUILD.bazel index d60919f..1567cdb 100644 --- a/examples/chatter/BUILD.bazel +++ b/examples/chatter/BUILD.bazel @@ -18,6 +18,7 @@ load( load("@rules_ros//ros:launch.bzl", "ros_launch") load("@rules_ros//ros:test.bzl", "ros_test") load("@rules_ros//ros:topic.bzl", "ros_topic") +load("@rules_ros//third_party:expand_template.bzl", "expand_template") # Handling of ROS messages & services resembles to some extent Bazel's rules for # handling protobuf messages (e.g. proto_library and cc_proto_library). @@ -58,7 +59,7 @@ cc_ros_binary( # Defines tests for the talker node. ros_test( name = "talker_tests", - launch_file = "talker_tests.launch", + launch_file = ":talker_tests.launch", nodes = [ ":talker", "@rules_ros//third_party/ros/rostest:advertisetest", @@ -66,6 +67,20 @@ ros_test( ], ) +expand_template( + name = "talker_tests_launch", + out = "talker_tests.launch", + data = [ + "@rules_ros//third_party/ros/rostest:advertisetest", + "@rules_ros//third_party/ros/rostest:publishtest", + ], + substitutions = { + "{advertisetest}": "$(rootpath @rules_ros//third_party/ros/rostest:advertisetest)", + "{publishtest}": "$(rootpath @rules_ros//third_party/ros/rostest:publishtest)", + }, + template = "talker_tests.launch.tpl", +) + # Defines a C++ listener ROS node. cc_ros_binary( name = "listener", diff --git a/examples/chatter/talker_tests.launch b/examples/chatter/talker_tests.launch.tpl similarity index 77% rename from examples/chatter/talker_tests.launch rename to examples/chatter/talker_tests.launch.tpl index aaccd50..b68622e 100644 --- a/examples/chatter/talker_tests.launch +++ b/examples/chatter/talker_tests.launch.tpl @@ -3,7 +3,7 @@ + type="{advertisetest}"> topics: - name: /chatter @@ -14,7 +14,7 @@ + type="{publishtest}"> topics: - name: /chatter diff --git a/third_party/BUILD.bazel b/third_party/BUILD.bazel index 107fccc..7a0c019 100644 --- a/third_party/BUILD.bazel +++ b/third_party/BUILD.bazel @@ -1,3 +1,4 @@ exports_files([ "expand_template.bzl", + "symlink.bzl", ]) diff --git a/third_party/ros/rostest/BUILD.bazel b/third_party/ros/rostest/BUILD.bazel index ba6d292..364715c 100644 --- a/third_party/ros/rostest/BUILD.bazel +++ b/third_party/ros/rostest/BUILD.bazel @@ -1,5 +1,6 @@ load("@rules_python//python:defs.bzl", "py_binary", "py_library") load("//build_tools:pylint.bzl", "pylint") +load("//third_party:symlink.bzl", "symlink") py_library( name = "rostest", @@ -19,38 +20,62 @@ py_library( ) py_binary( - name = "advertisetest", + name = "advertisetest_impl", srcs = ["advertisetest.py"], - visibility = ["//visibility:public"], + main = "advertisetest.py", deps = [ ":rostest", "@rules_ros//third_party/ros/rosservice", ], ) +symlink( + name = "advertisetest", + executable = ":advertisetest_impl", + visibility = ["//visibility:public"], +) + py_binary( - name = "hztest", + name = "hztest_impl", srcs = ["hztest.py"], - visibility = ["//visibility:public"], + main = "hztest.py", deps = [ ":rostest", ], ) +symlink( + name = "hztest", + executable = ":hztest_impl", + visibility = ["//visibility:public"], +) + py_binary( - name = "paramtest", + name = "paramtest_impl", srcs = ["paramtest.py"], - visibility = ["//visibility:public"], + main = "paramtest.py", deps = [ ":rostest", ], ) +symlink( + name = "paramtest", + executable = ":paramtest_impl", + visibility = ["//visibility:public"], +) + py_binary( - name = "publishtest", + name = "publishtest_impl", srcs = ["publishtest.py"], - visibility = ["//visibility:public"], + main = "publishtest.py", deps = [":rostest"], ) +symlink( + name = "publishtest", + executable = ":publishtest_impl", + visibility = ["//visibility:public"], +) + pylint() diff --git a/third_party/symlink.bzl b/third_party/symlink.bzl new file mode 100644 index 0000000..207f226 --- /dev/null +++ b/third_party/symlink.bzl @@ -0,0 +1,31 @@ +# Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 +# Adapted from https://github.com/digital-asset/daml/pull/8428 + +def _symlink_impl(ctx): + executable = ctx.actions.declare_file(ctx.label.name) + ctx.actions.symlink( + output = executable, + target_file = ctx.executable.executable, + is_executable = True, + ) + + runfiles = ctx.runfiles(files = [ctx.executable.executable]) + runfiles = runfiles.merge(ctx.attr.executable[DefaultInfo].default_runfiles) + return [DefaultInfo( + executable = executable, + files = depset(direct = [executable]), + runfiles = runfiles, + )] + +symlink = rule( + _symlink_impl, + attrs = { + "executable": attr.label( + executable = True, + cfg = "target", + ), + }, + executable = True, + doc = "Creates a new target for the given executable.", +)