|
| 1 | +# Copyright (c) 2022 FZI Forschungszentrum Informatik |
| 2 | +# |
| 3 | +# Redistribution and use in source and binary forms, with or without |
| 4 | +# modification, are permitted provided that the following conditions are met: |
| 5 | +# |
| 6 | +# * Redistributions of source code must retain the above copyright |
| 7 | +# notice, this list of conditions and the following disclaimer. |
| 8 | +# |
| 9 | +# * Redistributions in binary form must reproduce the above copyright |
| 10 | +# notice, this list of conditions and the following disclaimer in the |
| 11 | +# documentation and/or other materials provided with the distribution. |
| 12 | +# |
| 13 | +# * Neither the name of the {copyright_holder} nor the names of its |
| 14 | +# contributors may be used to endorse or promote products derived from |
| 15 | +# this software without specific prior written permission. |
| 16 | +# |
| 17 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 21 | +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 | +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 25 | +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 26 | +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 27 | +# POSSIBILITY OF SUCH DAMAGE. |
| 28 | +# |
| 29 | +# Author: Lukas Sackewitz |
| 30 | + |
| 31 | +import os |
| 32 | +import shutil |
| 33 | +import subprocess |
| 34 | +import tempfile |
| 35 | + |
| 36 | +from ament_index_python.packages import get_package_share_directory |
| 37 | + |
| 38 | + |
| 39 | +def test_urdf_xacro(): |
| 40 | + # General Arguments |
| 41 | + description_package = "ros2_control_demo_example_10" |
| 42 | + description_file = "rrbot.urdf.xacro" |
| 43 | + |
| 44 | + description_file_path = os.path.join( |
| 45 | + get_package_share_directory(description_package), "urdf", description_file |
| 46 | + ) |
| 47 | + |
| 48 | + (_, tmp_urdf_output_file) = tempfile.mkstemp(suffix=".urdf") |
| 49 | + |
| 50 | + # Compose `xacro` and `check_urdf` command |
| 51 | + xacro_command = ( |
| 52 | + f"{shutil.which('xacro')}" f" {description_file_path}" f" > {tmp_urdf_output_file}" |
| 53 | + ) |
| 54 | + check_urdf_command = f"{shutil.which('check_urdf')} {tmp_urdf_output_file}" |
| 55 | + |
| 56 | + # Try to call processes but finally remove the temp file |
| 57 | + try: |
| 58 | + xacro_process = subprocess.run( |
| 59 | + xacro_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True |
| 60 | + ) |
| 61 | + |
| 62 | + assert xacro_process.returncode == 0, " --- XACRO command failed ---" |
| 63 | + |
| 64 | + check_urdf_process = subprocess.run( |
| 65 | + check_urdf_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True |
| 66 | + ) |
| 67 | + |
| 68 | + assert ( |
| 69 | + check_urdf_process.returncode == 0 |
| 70 | + ), "\n --- URDF check failed! --- \nYour xacro does not unfold into a proper urdf robot description. Please check your xacro file." |
| 71 | + |
| 72 | + finally: |
| 73 | + os.remove(tmp_urdf_output_file) |
| 74 | + |
| 75 | + |
| 76 | +if __name__ == "__main__": |
| 77 | + test_urdf_xacro() |
0 commit comments