From f8c7a7572ff5d213dcfc830698d4252b518d7973 Mon Sep 17 00:00:00 2001 From: Mabel Zhang Date: Tue, 7 Jun 2022 03:50:21 -0400 Subject: [PATCH 1/3] add tab completion Signed-off-by: Mabel Zhang --- cmake/SearchForStuff.cmake | 4 +++ src/cmd/CMakeLists.txt | 16 ++++++++++ src/cmd/sdf.bash_completion.sh | 54 ++++++++++++++++++++++++++++++++++ src/ign_TEST.cc | 33 +++++++++++++++++++++ 4 files changed, 107 insertions(+) create mode 100644 src/cmd/sdf.bash_completion.sh diff --git a/cmake/SearchForStuff.cmake b/cmake/SearchForStuff.cmake index 416e7d21a..6aee66c02 100644 --- a/cmake/SearchForStuff.cmake +++ b/cmake/SearchForStuff.cmake @@ -86,6 +86,10 @@ else() generating aggregated SDFormat descriptions for sdformat.org, please install \ ignition-tools.") endif() +if (ignition-tools_FOUND) + set (HAVE_IGN_TOOLS TRUE) + set (IGN_TOOLS_VER 1) +endif() ################################################ # Find the Python interpreter for running the diff --git a/src/cmd/CMakeLists.txt b/src/cmd/CMakeLists.txt index 89534f7aa..17f3f40f2 100644 --- a/src/cmd/CMakeLists.txt +++ b/src/cmd/CMakeLists.txt @@ -42,3 +42,19 @@ file(GENERATE # Install the ruby command line library in an unversioned location. install(FILES ${cmd_script_generated} DESTINATION lib/ruby/ignition) + + +#=============================================================================== +# Bash completion + +# Tack version onto and install the bash completion script +configure_file( + "sdf.bash_completion.sh" + "${CMAKE_CURRENT_BINARY_DIR}/sdf${PROJECT_VERSION_MAJOR}.bash_completion.sh" @ONLY) +if (HAVE_IGN_TOOLS) + install( + FILES + ${CMAKE_CURRENT_BINARY_DIR}/sdf${PROJECT_VERSION_MAJOR}.bash_completion.sh + DESTINATION + ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_DATAROOTDIR}/gz/gz${IGN_TOOLS_VER}.completion.d) +endif() diff --git a/src/cmd/sdf.bash_completion.sh b/src/cmd/sdf.bash_completion.sh new file mode 100644 index 000000000..53f545be8 --- /dev/null +++ b/src/cmd/sdf.bash_completion.sh @@ -0,0 +1,54 @@ +#!/usr/bin/env bash +# +# Copyright (C) 2022 Open Source Robotics Foundation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# bash tab-completion + +# This is a per-library function definition, used in conjunction with the +# top-level entry point in ign-tools. + +GZ_SDF_COMPLETION_LIST=" + -k --check + -d --describe + -p --print + --inertial-stats + -h --help + --force-version + --versions +" + +function _gz_sdf +{ + if [[ ${COMP_WORDS[COMP_CWORD]} == -* ]]; then + # Specify options (-*) word list for this subcommand + COMPREPLY=($(compgen -W "$GZ_SDF_COMPLETION_LIST" \ + -- "${COMP_WORDS[COMP_CWORD]}" )) + return + else + # Just use bash default auto-complete, because we never have two + # subcommands in the same line. If that is ever needed, change here to + # detect subsequent subcommands + COMPREPLY=($(compgen -o default -- "${COMP_WORDS[COMP_CWORD]}")) + return + fi +} + +function _gz_sdf_flags +{ + for word in $GZ_SDF_COMPLETION_LIST; do + echo "$word" + done +} diff --git a/src/ign_TEST.cc b/src/ign_TEST.cc index 3d34dc367..262e7a840 100644 --- a/src/ign_TEST.cc +++ b/src/ign_TEST.cc @@ -15,7 +15,9 @@ * */ +#include #include +#include #include #include #include @@ -815,6 +817,37 @@ TEST(inertial_stats, SDF) } } +////////////////////////////////////////////////// +/// \brief Check help message and bash completion script for consistent flags +TEST(HelpVsCompletionFlags, SDF) +{ + // Flags in help message + std::string helpOutput = custom_exec_str(IgnCommand() + " sdf --help"); + + // Call the output function in the bash completion script + std::filesystem::path scriptPath = PROJECT_SOURCE_PATH; + scriptPath = scriptPath / "src" / "cmd" / "sdf.bash_completion.sh"; + + // Equivalent to: + // sh -c "bash -c \". /path/to/sdf.bash_completion.sh; _gz_sdf_flags\"" + std::string cmd = "bash -c \". " + scriptPath.string() + + "; _gz_sdf_flags\""; + std::string scriptOutput = custom_exec_str(cmd); + + // Tokenize script output + std::istringstream iss(scriptOutput); + std::vector flags((std::istream_iterator(iss)), + std::istream_iterator()); + + EXPECT_GT(flags.size(), 0u); + + // Match each flag in script output with help message + for (std::string flag : flags) + { + EXPECT_NE(std::string::npos, helpOutput.find(flag)) << helpOutput; + } +} + ///////////////////////////////////////////////// /// Main int main(int argc, char **argv) From 16a7a6739d3b889f85a5a737a10250e933f60822 Mon Sep 17 00:00:00 2001 From: Mabel Zhang Date: Tue, 7 Jun 2022 12:06:45 -0400 Subject: [PATCH 2/3] extra line Signed-off-by: Mabel Zhang --- src/cmd/CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/src/cmd/CMakeLists.txt b/src/cmd/CMakeLists.txt index 17f3f40f2..5f1eca389 100644 --- a/src/cmd/CMakeLists.txt +++ b/src/cmd/CMakeLists.txt @@ -43,7 +43,6 @@ file(GENERATE # Install the ruby command line library in an unversioned location. install(FILES ${cmd_script_generated} DESTINATION lib/ruby/ignition) - #=============================================================================== # Bash completion From 488d2ac46cde76abc43f747afe3d00f2a37d39b7 Mon Sep 17 00:00:00 2001 From: Louise Poubel Date: Tue, 14 Jun 2022 17:12:03 -0700 Subject: [PATCH 3/3] Use sdf::Filesystem, const ref, always install Signed-off-by: Louise Poubel --- cmake/SearchForStuff.cmake | 6 ++++-- src/cmd/CMakeLists.txt | 12 +++++------- src/ign_TEST.cc | 11 ++++++----- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/cmake/SearchForStuff.cmake b/cmake/SearchForStuff.cmake index 6aee66c02..3ae8d19c6 100644 --- a/cmake/SearchForStuff.cmake +++ b/cmake/SearchForStuff.cmake @@ -88,8 +88,10 @@ ignition-tools.") endif() if (ignition-tools_FOUND) set (HAVE_IGN_TOOLS TRUE) - set (IGN_TOOLS_VER 1) endif() +# Note that CLI files are installed regardless of whether the dependency is +# available during build time +set (IGN_TOOLS_VER 1) ################################################ # Find the Python interpreter for running the @@ -132,7 +134,7 @@ endmacro() ######################################## # Find ignition cmake2 -# Only for using the testing macros and creating the codecheck target, not +# Only for using the testing macros and creating the codecheck target, not # really being use to configure the whole project find_package(ignition-cmake2 REQUIRED) set(IGN_CMAKE_VER ${ignition-cmake2_VERSION_MAJOR}) diff --git a/src/cmd/CMakeLists.txt b/src/cmd/CMakeLists.txt index 5f1eca389..0cb4ae6ef 100644 --- a/src/cmd/CMakeLists.txt +++ b/src/cmd/CMakeLists.txt @@ -50,10 +50,8 @@ install(FILES ${cmd_script_generated} DESTINATION lib/ruby/ignition) configure_file( "sdf.bash_completion.sh" "${CMAKE_CURRENT_BINARY_DIR}/sdf${PROJECT_VERSION_MAJOR}.bash_completion.sh" @ONLY) -if (HAVE_IGN_TOOLS) - install( - FILES - ${CMAKE_CURRENT_BINARY_DIR}/sdf${PROJECT_VERSION_MAJOR}.bash_completion.sh - DESTINATION - ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_DATAROOTDIR}/gz/gz${IGN_TOOLS_VER}.completion.d) -endif() +install( + FILES + ${CMAKE_CURRENT_BINARY_DIR}/sdf${PROJECT_VERSION_MAJOR}.bash_completion.sh + DESTINATION + ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_DATAROOTDIR}/gz/gz${IGN_TOOLS_VER}.completion.d) diff --git a/src/ign_TEST.cc b/src/ign_TEST.cc index 262e7a840..6b5a452e4 100644 --- a/src/ign_TEST.cc +++ b/src/ign_TEST.cc @@ -15,13 +15,13 @@ * */ -#include #include #include #include #include #include +#include "sdf/Filesystem.hh" #include "sdf/parser.hh" #include "sdf/SDFImpl.hh" #include "sdf/sdf_config.h" @@ -825,12 +825,13 @@ TEST(HelpVsCompletionFlags, SDF) std::string helpOutput = custom_exec_str(IgnCommand() + " sdf --help"); // Call the output function in the bash completion script - std::filesystem::path scriptPath = PROJECT_SOURCE_PATH; - scriptPath = scriptPath / "src" / "cmd" / "sdf.bash_completion.sh"; + std::string scriptPath = PROJECT_SOURCE_PATH; + scriptPath = sdf::filesystem::append(scriptPath, "src", "cmd", + "sdf.bash_completion.sh"); // Equivalent to: // sh -c "bash -c \". /path/to/sdf.bash_completion.sh; _gz_sdf_flags\"" - std::string cmd = "bash -c \". " + scriptPath.string() + + std::string cmd = "bash -c \". " + scriptPath + "; _gz_sdf_flags\""; std::string scriptOutput = custom_exec_str(cmd); @@ -842,7 +843,7 @@ TEST(HelpVsCompletionFlags, SDF) EXPECT_GT(flags.size(), 0u); // Match each flag in script output with help message - for (std::string flag : flags) + for (const auto &flag : flags) { EXPECT_NE(std::string::npos, helpOutput.find(flag)) << helpOutput; }