Skip to content

Commit

Permalink
Symlink test utils
Browse files Browse the repository at this point in the history
  • Loading branch information
mvukov committed Aug 8, 2024
1 parent 7982433 commit 6d2db95
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 14 deletions.
3 changes: 0 additions & 3 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
```
bazel test //... --@rules_python//python/config_settings:python_version=3.10
```
17 changes: 16 additions & 1 deletion examples/chatter/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -58,14 +59,28 @@ 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",
"@rules_ros//third_party/ros/rostest:publishtest",
],
)

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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<test name="advertisetest"
test-name="advertisetest"
type="../rules_ros~/third_party/ros/rostest/advertisetest">
type="{advertisetest}">
<rosparam>
topics:
- name: /chatter
Expand All @@ -14,7 +14,7 @@

<test name="publishtest"
test-name="publishtest"
type="../rules_ros~/third_party/ros/rostest/publishtest">
type="{publishtest}">
<rosparam>
topics:
- name: /chatter
Expand Down
1 change: 1 addition & 0 deletions third_party/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
exports_files([
"expand_template.bzl",
"symlink.bzl",
])
41 changes: 33 additions & 8 deletions third_party/ros/rostest/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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()
31 changes: 31 additions & 0 deletions third_party/symlink.bzl
Original file line number Diff line number Diff line change
@@ -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.",
)

0 comments on commit 6d2db95

Please sign in to comment.