Skip to content

Rosidl cli generate proto test #25

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

Open
wants to merge 4 commits into
base: streamline-file-generation
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions rosidl_typesupport_protobuf_c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ if(BUILD_TESTING)
target_link_libraries(test_wstring_conversion
${PROJECT_NAME})
endif()
find_package(ament_cmake_pytest REQUIRED)
Copy link
Owner

Choose a reason for hiding this comment

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

Needed in package.xml too

ament_add_pytest_test(test_rosidl_cli_ts_pb_c test/test_rosidl_cli_ts_pb_c.py)
endif()

ament_package(
Expand Down
1 change: 1 addition & 0 deletions rosidl_typesupport_protobuf_c/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<exec_depend>rosidl_typesupport_interface</exec_depend>

<test_depend>ament_cmake_gtest</test_depend>
<test_depend>ament_cmake_pytest</test_depend>

<member_of_group>rosidl_typesupport_c_packages</member_of_group>

Expand Down
72 changes: 72 additions & 0 deletions rosidl_typesupport_protobuf_c/rosidl_typesupport_protobuf_c/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import pathlib

from ament_index_python import get_package_share_directory

from rosidl_cli.command.generate.extensions import GenerateCommandExtension
from rosidl_cli.command.helpers import generate_visibility_control_file
from rosidl_cli.command.helpers import legacy_generator_arguments_file
from rosidl_cli.command.translate.api import translate

from rosidl_typesupport_protobuf_c import generate_typesupport_protobuf_c


class GenerateProtobufCTypesupport(GenerateCommandExtension):

def generate(
self,
package_name,
interface_files,
include_paths,
output_path
):
generated_files = []

package_share_path = pathlib.Path(
get_package_share_directory('rosidl_typesupport_protobuf_c'))
templates_path = package_share_path / 'resource'

# Normalize interface definition format to .idl
idl_interface_files = []
non_idl_interface_files = []
for path in interface_files:
if not path.endswith('.idl'):
non_idl_interface_files.append(path)
else:
idl_interface_files.append(path)
if non_idl_interface_files:
idl_interface_files.extend(translate(
package_name=package_name,
interface_files=non_idl_interface_files,
include_paths=include_paths,
output_format='idl',
output_path=output_path / 'tmp',
))

# Generate visibility control file
visibility_control_file_template_path = \
'rosidl_typesupport_protobuf_c__visibility_control.h.in'
visibility_control_file_template_path = \
templates_path / visibility_control_file_template_path
visibility_control_file_path = \
'rosidl_typesupport_protobuf_c__visibility_control.h'
visibility_control_file_path = \
output_path / 'msg' / visibility_control_file_path

generate_visibility_control_file(
package_name=package_name,
template_path=visibility_control_file_template_path,
output_path=visibility_control_file_path
)
generated_files.append(visibility_control_file_path)

# Generate code
with legacy_generator_arguments_file(
package_name=package_name,
interface_files=idl_interface_files,
include_paths=include_paths,
templates_path=templates_path,
output_path=output_path
) as path_to_arguments_file:
generated_files.extend(generate_typesupport_protobuf_c(path_to_arguments_file))

return generated_files
3 changes: 3 additions & 0 deletions rosidl_typesupport_protobuf_c/setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[options.entry_points]
rosidl_cli.command.generate.typesupport_extensions =
protobuf_c = rosidl_typesupport_protobuf_c.cli:GenerateProtobufCTypesupport
30 changes: 30 additions & 0 deletions rosidl_typesupport_protobuf_c/test/test_rosidl_cli_ts_pb_c.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import pytest
import os
import pathlib
import shutil
import subprocess

from ament_index_python import get_package_share_directory

def test_ts_files_generation():
build_then_install('/tmp/pb')
files_exists('/tmp/pb')

def build_then_install(output_path):
if os.path.exists(output_path):
shutil.rmtree(output_path)
os.makedirs(output_path)

package_share_path = pathlib.Path(
get_package_share_directory('std_msgs'))

subprocess.run([
'rosidl', 'generate', '-ts', 'protobuf_c', '-o', output_path, 'std_msgs', './msg/String.msg'
], cwd=package_share_path, check=True)

def files_exists(output_path):
assert pathlib.Path('/tmp/pb/tmp/msg/String.idl').exists()
assert pathlib.Path('/tmp/pb/msg/rosidl_typesupport_protobuf_c__visibility_control.h').exists()
assert pathlib.Path('/tmp/pb/msg/string__rosidl_typesupport_protobuf_c.hpp').exists()
assert pathlib.Path('/tmp/pb/msg/detail/string__type_support.cpp').exists()

2 changes: 2 additions & 0 deletions rosidl_typesupport_protobuf_cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ if(BUILD_TESTING)
target_link_libraries(test_wstring_conversion
${PROJECT_NAME})
endif()
find_package(ament_cmake_pytest REQUIRED)
ament_add_pytest_test(test_rosidl_cli_ts_pb_cpp test/test_rosidl_cli_ts_pb_cpp.py)
endif()

ament_package(CONFIG_EXTRAS "cmake/rosidl_typesupport_protobuf_cpp-extras.cmake.in")
Expand Down
1 change: 1 addition & 0 deletions rosidl_typesupport_protobuf_cpp/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<exec_depend>rosidl_typesupport_protobuf</exec_depend>

<test_depend>ament_cmake_gtest</test_depend>
<test_depend>ament_cmake_pytest</test_depend>

<member_of_group>rosidl_typesupport_cpp_packages</member_of_group>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
# limitations under the License.
#
# ================================= Apache 2.0 =================================

from rosidl_cmake import generate_files

from rosidl_pycommon import generate_files

def get_template_mapping():
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import pathlib

from ament_index_python import get_package_share_directory

from rosidl_cli.command.generate.extensions import GenerateCommandExtension
from rosidl_cli.command.helpers import generate_visibility_control_file
from rosidl_cli.command.helpers import legacy_generator_arguments_file
from rosidl_cli.command.translate.api import translate

from rosidl_typesupport_protobuf_cpp import generate_cpp


class GenerateProtobufCppTypesupport(GenerateCommandExtension):

def generate(
self,
package_name,
interface_files,
include_paths,
output_path
):
generated_files = []

package_share_path = pathlib.Path(
get_package_share_directory('rosidl_typesupport_protobuf_cpp'))
templates_path = package_share_path / 'resource'

# Normalize interface definition format to .idl
idl_interface_files = []
non_idl_interface_files = []
for path in interface_files:
if not path.endswith('.idl'):
non_idl_interface_files.append(path)
else:
idl_interface_files.append(path)
if non_idl_interface_files:
idl_interface_files.extend(translate(
package_name=package_name,
interface_files=non_idl_interface_files,
include_paths=include_paths,
output_format='idl',
output_path=output_path / 'tmp',
))

# Generate visibility control file
visibility_control_file_template_path = \
'rosidl_typesupport_protobuf_cpp__visibility_control.h.in'
visibility_control_file_template_path = \
templates_path / visibility_control_file_template_path
visibility_control_file_path = \
'rosidl_typesupport_protobuf_cpp__visibility_control.h'
visibility_control_file_path = \
output_path / 'msg' / visibility_control_file_path

generate_visibility_control_file(
package_name=package_name,
template_path=visibility_control_file_template_path,
output_path=visibility_control_file_path
)
generated_files.append(visibility_control_file_path)

# Generate code
with legacy_generator_arguments_file(
package_name=package_name,
interface_files=idl_interface_files,
include_paths=include_paths,
templates_path=templates_path,
output_path=output_path
) as path_to_arguments_file:
generated_files.extend(generate_cpp(path_to_arguments_file))

return generated_files
3 changes: 3 additions & 0 deletions rosidl_typesupport_protobuf_cpp/setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[options.entry_points]
rosidl_cli.command.generate.typesupport_extensions =
protobuf_cpp = rosidl_typesupport_protobuf_cpp.cli:GenerateProtobufCppTypesupport
31 changes: 31 additions & 0 deletions rosidl_typesupport_protobuf_cpp/test/test_rosidl_cli_ts_pb_cpp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import pytest
import os
import pathlib
import shutil
import subprocess

from ament_index_python import get_package_share_directory

def test_ts_files_generation():
build_then_install('/tmp/pb')
files_exists('/tmp/pb')

def build_then_install(output_path):
if os.path.exists(output_path):
shutil.rmtree(output_path)
os.makedirs(output_path)

package_share_path = pathlib.Path(
get_package_share_directory('std_msgs'))

subprocess.run([
'rosidl', 'generate', '-ts', 'protobuf_cpp', '-o', output_path, 'std_msgs', './msg/String.msg'
], cwd=package_share_path, check=True)

def files_exists(output_path):
assert pathlib.Path('/tmp/pb/tmp/msg/String.idl').exists()
assert pathlib.Path('/tmp/pb/msg/rosidl_typesupport_protobuf_cpp__visibility_control.h').exists()
assert pathlib.Path('/tmp/pb/msg/string__rosidl_typesupport_protobuf_cpp.hpp').exists()
assert pathlib.Path('/tmp/pb/msg/string__typeadapter_protobuf_cpp.hpp').exists()
assert pathlib.Path('/tmp/pb/msg/detail/string__type_support.cpp').exists()

Loading