From 1bd299503a2c9dad294247364968b8e9fee29ada Mon Sep 17 00:00:00 2001 From: Mabel Zhang Date: Sat, 21 May 2022 00:56:46 -0400 Subject: [PATCH 1/5] add tab completion Signed-off-by: Mabel Zhang --- CMakeLists.txt | 3 +++ src/cmd/CMakeLists.txt | 16 ++++++++++++++++ src/cmd/msgs.bash_completion.sh | 27 +++++++++++++++++++++++++++ tools/ign_TEST.cc | 25 +++++++++++++++++++++++++ 4 files changed, 71 insertions(+) create mode 100644 src/cmd/msgs.bash_completion.sh diff --git a/CMakeLists.txt b/CMakeLists.txt index 96671a28..f5d93c01 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -71,6 +71,9 @@ set(IGN_MATH_VER ${ignition-math6_VERSION_MAJOR}) #-------------------------------------- # Find if ign command is available find_program(HAVE_IGN_TOOLS ign) +if (HAVE_IGN_TOOLS) + set(IGN_TOOLS_VER 1) +endif() #-------------------------------------- # Find Tinyxml2 diff --git a/src/cmd/CMakeLists.txt b/src/cmd/CMakeLists.txt index 643bb0d4..ff34131d 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( + "msgs.bash_completion.sh" + "${CMAKE_CURRENT_BINARY_DIR}/msgs${PROJECT_VERSION_MAJOR}.bash_completion.sh" @ONLY) +if (HAVE_IGN_TOOLS) + install( + FILES + ${CMAKE_CURRENT_BINARY_DIR}/msgs${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/msgs.bash_completion.sh b/src/cmd/msgs.bash_completion.sh new file mode 100644 index 00000000..400a3d16 --- /dev/null +++ b/src/cmd/msgs.bash_completion.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash + +# bash tab-completion + +# This is a per-library function definition, used in conjunction with the +# top-level entry point in ign-tools. + +function _gz_msg +{ + if [[ ${COMP_WORDS[COMP_CWORD]} == -* ]]; then + # Specify options (-*) word list for this subcommand + COMPREPLY=($(compgen -W " + -i --info + -l --list + -h --help + --force-version + --versions + " -- "${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 +} diff --git a/tools/ign_TEST.cc b/tools/ign_TEST.cc index fc24160c..d5f18f91 100644 --- a/tools/ign_TEST.cc +++ b/tools/ign_TEST.cc @@ -15,6 +15,7 @@ * */ +#include #include #include #include @@ -89,6 +90,30 @@ TEST(CmdLine, MsgInfo) << output; } +///////////////////////////////////////////////// +TEST(CmdLine, MsgHelpVsCompletionFlags) +{ + // Flags in help message + auto output = custom_exec_str("ign msg --help --force-version " + g_version); + EXPECT_NE(std::string::npos, output.find("--info")) << output; + EXPECT_NE(std::string::npos, output.find("--list")) << output; + EXPECT_NE(std::string::npos, output.find("--help")) << output; + EXPECT_NE(std::string::npos, output.find("--force-version")) << output; + EXPECT_NE(std::string::npos, output.find("--versions")) << output; + + // Flags in bash completion + std::ifstream scriptFile(std::string(PROJECT_SOURCE_PATH) + + "/src/cmd/msgs.bash_completion.sh"); + std::string script((std::istreambuf_iterator(scriptFile)), + std::istreambuf_iterator()); + + EXPECT_NE(std::string::npos, script.find("--info")) << script; + EXPECT_NE(std::string::npos, script.find("--list")) << script; + EXPECT_NE(std::string::npos, script.find("--help")) << script; + EXPECT_NE(std::string::npos, script.find("--force-version")) << script; + EXPECT_NE(std::string::npos, script.find("--versions")) << script; +} + ///////////////////////////////////////////////// /// Main int main(int argc, char **argv) From e3cb342914c07d1b41398936e9d23948680cf677 Mon Sep 17 00:00:00 2001 From: Mabel Zhang Date: Fri, 3 Jun 2022 00:47:28 -0400 Subject: [PATCH 2/5] license Signed-off-by: Mabel Zhang --- src/cmd/msgs.bash_completion.sh | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/cmd/msgs.bash_completion.sh b/src/cmd/msgs.bash_completion.sh index 400a3d16..f3c23d69 100644 --- a/src/cmd/msgs.bash_completion.sh +++ b/src/cmd/msgs.bash_completion.sh @@ -1,4 +1,19 @@ #!/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 From 744a6a2a1c77299919047a53141bdb992d1b5cbc Mon Sep 17 00:00:00 2001 From: Mabel Zhang Date: Fri, 3 Jun 2022 19:01:49 -0400 Subject: [PATCH 3/5] use std path Signed-off-by: Mabel Zhang --- tools/ign_TEST.cc | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tools/ign_TEST.cc b/tools/ign_TEST.cc index d5f18f91..b2a8a01e 100644 --- a/tools/ign_TEST.cc +++ b/tools/ign_TEST.cc @@ -15,6 +15,7 @@ * */ +#include #include #include #include @@ -102,8 +103,10 @@ TEST(CmdLine, MsgHelpVsCompletionFlags) EXPECT_NE(std::string::npos, output.find("--versions")) << output; // Flags in bash completion - std::ifstream scriptFile(std::string(PROJECT_SOURCE_PATH) + - "/src/cmd/msgs.bash_completion.sh"); + std::filesystem::path scriptPath = PROJECT_SOURCE_PATH; + scriptPath = scriptPath / "src" / "cmd" / "msgs.bash_completion.sh"; + std::ifstream scriptFile(scriptPath); + std::string script((std::istreambuf_iterator(scriptFile)), std::istreambuf_iterator()); From 50f59d5e85529db5de9266a87189fdbb65dde2ff Mon Sep 17 00:00:00 2001 From: Mabel Zhang Date: Tue, 7 Jun 2022 03:19:47 -0400 Subject: [PATCH 4/5] do not hard code flags in test Signed-off-by: Mabel Zhang --- src/cmd/msgs.bash_completion.sh | 24 +++++++++++++++------- tools/ign_TEST.cc | 36 ++++++++++++++++++--------------- 2 files changed, 37 insertions(+), 23 deletions(-) diff --git a/src/cmd/msgs.bash_completion.sh b/src/cmd/msgs.bash_completion.sh index f3c23d69..85dcc9e9 100644 --- a/src/cmd/msgs.bash_completion.sh +++ b/src/cmd/msgs.bash_completion.sh @@ -20,17 +20,20 @@ # This is a per-library function definition, used in conjunction with the # top-level entry point in ign-tools. +GZ_MSGS_COMPLETION_LIST=" + -i --info + -l --list + -h --help + --force-version + --versions +" + function _gz_msg { if [[ ${COMP_WORDS[COMP_CWORD]} == -* ]]; then # Specify options (-*) word list for this subcommand - COMPREPLY=($(compgen -W " - -i --info - -l --list - -h --help - --force-version - --versions - " -- "${COMP_WORDS[COMP_CWORD]}" )) + COMPREPLY=($(compgen -W "$GZ_MSGS_COMPLETION_LIST" \ + -- "${COMP_WORDS[COMP_CWORD]}" )) return else # Just use bash default auto-complete, because we never have two @@ -40,3 +43,10 @@ function _gz_msg return fi } + +function _gz_msgs_flags +{ + for word in $GZ_MSGS_COMPLETION_LIST; do + echo "$word" + done +} diff --git a/tools/ign_TEST.cc b/tools/ign_TEST.cc index b2a8a01e..f707c72c 100644 --- a/tools/ign_TEST.cc +++ b/tools/ign_TEST.cc @@ -95,26 +95,30 @@ TEST(CmdLine, MsgInfo) TEST(CmdLine, MsgHelpVsCompletionFlags) { // Flags in help message - auto output = custom_exec_str("ign msg --help --force-version " + g_version); - EXPECT_NE(std::string::npos, output.find("--info")) << output; - EXPECT_NE(std::string::npos, output.find("--list")) << output; - EXPECT_NE(std::string::npos, output.find("--help")) << output; - EXPECT_NE(std::string::npos, output.find("--force-version")) << output; - EXPECT_NE(std::string::npos, output.find("--versions")) << output; - - // Flags in bash completion + auto helpOutput = custom_exec_str("ign msg --help --force-version " + + g_version); + + // Call the output function in the bash completion script std::filesystem::path scriptPath = PROJECT_SOURCE_PATH; scriptPath = scriptPath / "src" / "cmd" / "msgs.bash_completion.sh"; - std::ifstream scriptFile(scriptPath); - std::string script((std::istreambuf_iterator(scriptFile)), - std::istreambuf_iterator()); + // Equivalent to: + // sh -c "bash -c \". /path/to/msgs.bash_completion.sh; _gz_msgs_flags\"" + std::string cmd = "bash -c \". " + scriptPath.string() + "; _gz_msgs_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); - EXPECT_NE(std::string::npos, script.find("--info")) << script; - EXPECT_NE(std::string::npos, script.find("--list")) << script; - EXPECT_NE(std::string::npos, script.find("--help")) << script; - EXPECT_NE(std::string::npos, script.find("--force-version")) << script; - EXPECT_NE(std::string::npos, script.find("--versions")) << script; + // Match each flag in script output with help message + for (std::string flag : flags) + { + EXPECT_NE(std::string::npos, helpOutput.find(flag)) << helpOutput; + } } ///////////////////////////////////////////////// From 385141ed6bc10b1f880c6e49f047a4a9be4509dc Mon Sep 17 00:00:00 2001 From: Louise Poubel Date: Tue, 14 Jun 2022 16:30:58 -0700 Subject: [PATCH 5/5] find_program, link to fs Signed-off-by: Louise Poubel --- CMakeLists.txt | 14 ++++++++++---- src/cmd/CMakeLists.txt | 12 +++++------- tools/CMakeLists.txt | 7 +++++-- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f5d93c01..a42f9761 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,12 @@ find_package(ignition-cmake2 2.8.0 REQUIRED) #============================================================================ ign_configure_project() +if (UNIX AND NOT APPLE) + set (EXTRA_TEST_LIB_DEPS stdc++fs) +else() + set (EXTRA_TEST_LIB_DEPS) +endif() + #============================================================================ # Set project-specific options #============================================================================ @@ -69,11 +75,11 @@ ign_find_package(ignition-math6 REQUIRED) set(IGN_MATH_VER ${ignition-math6_VERSION_MAJOR}) #-------------------------------------- -# Find if ign command is available +# Find if command is available. This is used to enable tests. +# Note that CLI files are installed regardless of whether the dependency is +# available during build time find_program(HAVE_IGN_TOOLS ign) -if (HAVE_IGN_TOOLS) - set(IGN_TOOLS_VER 1) -endif() +set(IGN_TOOLS_VER 1) #-------------------------------------- # Find Tinyxml2 diff --git a/src/cmd/CMakeLists.txt b/src/cmd/CMakeLists.txt index ff34131d..ec263899 100644 --- a/src/cmd/CMakeLists.txt +++ b/src/cmd/CMakeLists.txt @@ -51,10 +51,8 @@ install(FILES ${cmd_script_generated} DESTINATION lib/ruby/ignition) configure_file( "msgs.bash_completion.sh" "${CMAKE_CURRENT_BINARY_DIR}/msgs${PROJECT_VERSION_MAJOR}.bash_completion.sh" @ONLY) -if (HAVE_IGN_TOOLS) - install( - FILES - ${CMAKE_CURRENT_BINARY_DIR}/msgs${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}/msgs${PROJECT_VERSION_MAJOR}.bash_completion.sh + DESTINATION + ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_DATAROOTDIR}/gz/gz${IGN_TOOLS_VER}.completion.d) diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 65cd2d4c..e2ddab86 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -12,7 +12,10 @@ if (MSVC) list(REMOVE_ITEM test_sources ign_TEST.cc) endif() - if (HAVE_IGN_TOOLS) - ign_build_tests(TYPE UNIT SOURCES ${test_sources}) + ign_build_tests( + TYPE UNIT + SOURCES ${test_sources} + LIB_DEPS + ${EXTRA_TEST_LIB_DEPS}) endif ()