Skip to content

Commit

Permalink
Bash completion for flags (#312)
Browse files Browse the repository at this point in the history
Signed-off-by: Mabel Zhang <mabel@openrobotics.org>
Signed-off-by: Louise Poubel <louise@openrobotics.org>

Co-authored-by: Louise Poubel <louise@openrobotics.org>
  • Loading branch information
mabelzhang and chapulina authored Jun 14, 2022
1 parent 788905f commit 30da9d3
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 19 deletions.
7 changes: 5 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,11 @@ else ()
endif()

#--------------------------------------
# Find ignition-tools
ign_find_package(ignition-tools QUIET)
# 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)
set (IGN_TOOLS_VER 1)

#--------------------------------------
# Find SQLite3
Expand Down
4 changes: 2 additions & 2 deletions log/test/integration/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ foreach(source_file ${aux})
PRIVATE IGN_TRANSPORT_LOG_BUILD_PATH="$<TARGET_FILE_DIR:${BINARY_NAME}>")
endforeach()

# ign log CLI test
if (IGNITION-TOOLS_BINARY_DIRS)
# CLI test
if (HAVE_IGN_TOOLS)
set(IGN_CONFIG_PATH "${CMAKE_BINARY_DIR}/log/test/lib/ruby/ignition")

add_test(ign_log_record_no_overwrite
Expand Down
4 changes: 2 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ foreach(test ${test_list})
# multi-configuration generators, like Visual Studio.
target_compile_definitions(${test} PRIVATE
"DETAIL_IGN_TRANSPORT_TEST_DIR=\"$<TARGET_FILE_DIR:${test}>\""
"IGN_TEST_LIBRARY_PATH=\"$<TARGET_FILE_DIR:${PROJECT_LIBRARY_TARGET_NAME}>\"")

"IGN_TEST_LIBRARY_PATH=\"$<TARGET_FILE_DIR:${PROJECT_LIBRARY_TARGET_NAME}>\""
"PROJECT_SOURCE_DIR=\"${PROJECT_SOURCE_DIR}\"")
endforeach()

if(MSVC)
Expand Down
13 changes: 13 additions & 0 deletions src/cmd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,16 @@ 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(
"transport.bash_completion.sh"
"${CMAKE_CURRENT_BINARY_DIR}/transport${PROJECT_VERSION_MAJOR}.bash_completion.sh" @ONLY)
install(
FILES
${CMAKE_CURRENT_BINARY_DIR}/transport${PROJECT_VERSION_MAJOR}.bash_completion.sh
DESTINATION
${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_DATAROOTDIR}/gz/gz${IGN_TOOLS_VER}.completion.d)
100 changes: 100 additions & 0 deletions src/cmd/transport.bash_completion.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#!/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.

# TODO: In Fortress+, remove --force-version and --versions. Add --help-all.
# Update ../ign_TEST.cc accordingly.
GZ_SERVICE_COMPLETION_LIST="
-h --help
-v --version
-s --service
--reqtype
--reptype
--timeout
-l --list
-i --info
-r --req
--force-version
--versions
"

# TODO: In Fortress+, remove --force-version and --versions. Add
# `-v --version` and --json-output. Update ../ign_TEST.cc accordingly.
GZ_TOPIC_COMPLETION_LIST="
-h --help
-v --version
-t --topic
-m --msgtype
-d --duration
-n --num
-l --list
-i --info
-e --echo
-p --pub
--force-version
--versions
"

function _gz_service
{
if [[ ${COMP_WORDS[COMP_CWORD]} == -* ]]; then
# Specify options (-*) word list for this subcommand
COMPREPLY=($(compgen -W "$GZ_SERVICE_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_service_flags
{
for word in $GZ_SERVICE_COMPLETION_LIST; do
echo "$word"
done
}

function _gz_topic
{
if [[ ${COMP_WORDS[COMP_CWORD]} == -* ]]; then
# Specify options (-*) word list for this subcommand
COMPREPLY=($(compgen -W "$GZ_TOPIC_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_topic_flags
{
for word in $GZ_TOPIC_COMPLETION_LIST; do
echo "$word"
done
}
92 changes: 80 additions & 12 deletions src/ign_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*
*/

#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#include <ignition/msgs.hh>
Expand Down Expand Up @@ -86,7 +88,7 @@ TEST(ignTest, IGN_UTILS_TEST_DISABLED_ON_MAC(TopicList))
g_partition.c_str());

// Check the 'ign topic -l' command.
std::string ign = std::string(IGN_PATH) + "/ign";
std::string ign = std::string(IGN_PATH);

unsigned int retries = 0u;
bool topicFound = false;
Expand Down Expand Up @@ -117,7 +119,7 @@ TEST(ignTest, TopicInfo)
g_partition.c_str());

// Check the 'ign topic -i' command.
std::string ign = std::string(IGN_PATH) + "/ign";
std::string ign = std::string(IGN_PATH);

unsigned int retries = 0u;
bool infoFound = false;
Expand Down Expand Up @@ -153,7 +155,7 @@ TEST(ignTest, ServiceList)
g_partition.c_str());

// Check the 'ign service -l' command.
std::string ign = std::string(IGN_PATH) + "/ign";
std::string ign = std::string(IGN_PATH);

unsigned int retries = 0u;
bool serviceFound = false;
Expand Down Expand Up @@ -184,7 +186,7 @@ TEST(ignTest, ServiceInfo)
g_partition.c_str());

// Check the 'ign service -i' command.
std::string ign = std::string(IGN_PATH) + "/ign";
std::string ign = std::string(IGN_PATH);

unsigned int retries = 0u;
bool infoFound = false;
Expand Down Expand Up @@ -220,7 +222,7 @@ TEST(ignTest, TopicListSameProc)
EXPECT_TRUE(pub.Publish(msg));

// Check the 'ign topic -l' command.
std::string ign = std::string(IGN_PATH) + "/ign";
std::string ign = std::string(IGN_PATH);

unsigned int retries = 0u;
bool topicFound = false;
Expand Down Expand Up @@ -251,7 +253,7 @@ TEST(ignTest, TopicInfoSameProc)
EXPECT_TRUE(pub.Publish(msg));

// Check the 'ign topic -i' command.
std::string ign = std::string(IGN_PATH) + "/ign";
std::string ign = std::string(IGN_PATH);

unsigned int retries = 0u;
bool infoFound = false;
Expand All @@ -276,7 +278,7 @@ TEST(ignTest, ServiceListSameProc)
EXPECT_TRUE(node.Advertise("/foo", srvEcho));

// Check the 'ign service -l' command.
std::string ign = std::string(IGN_PATH) + "/ign";
std::string ign = std::string(IGN_PATH);

unsigned int retries = 0u;
bool serviceFound = false;
Expand All @@ -299,7 +301,7 @@ TEST(ignTest, ServiceInfoSameProc)
EXPECT_TRUE(node.Advertise("/foo", srvEcho));

// Check the 'ign service -i' command.
std::string ign = std::string(IGN_PATH) + "/ign";
std::string ign = std::string(IGN_PATH);

unsigned int retries = 0u;
bool infoFound = false;
Expand All @@ -326,7 +328,7 @@ TEST(ignTest, TopicPublish)
EXPECT_TRUE(node.Subscribe("/bar", topicCB));

// Check the 'ign topic -p' command.
std::string ign = std::string(IGN_PATH) + "/ign";
std::string ign = std::string(IGN_PATH);
std::string output = custom_exec_str(ign +
" topic -t /bar -m ign_msgs.StringMsg -p 'data:\"good_value\"' " +
g_ignVersion);
Expand Down Expand Up @@ -366,7 +368,7 @@ TEST(ignTest, ServiceRequest)
msg.set_data(10);

// Check the 'ign service -r' command.
std::string ign = std::string(IGN_PATH) + "/ign";
std::string ign = std::string(IGN_PATH);
std::string output = custom_exec_str(ign +
" service -s " + service + " --reqtype ign_msgs.Int32 " +
"--reptype ign_msgs.Int32 --timeout 1000 " +
Expand All @@ -388,7 +390,7 @@ TEST(ignTest, TopicEcho)
g_partition.c_str());

// Check the 'ign topic -e' command.
std::string ign = std::string(IGN_PATH) + "/ign";
std::string ign = std::string(IGN_PATH);
std::string output = custom_exec_str(
ign + " topic -e -t /foo -d 1.5 " + g_ignVersion);

Expand All @@ -414,7 +416,7 @@ TEST(ignTest, TopicEchoNum)
g_partition.c_str());

// Check the 'ign topic -e -n' command.
std::string ign = std::string(IGN_PATH) + "/ign";
std::string ign = std::string(IGN_PATH);
std::string output = custom_exec_str(
ign + " topic -e -t /foo -n 2 " + g_ignVersion);

Expand Down Expand Up @@ -443,6 +445,72 @@ TEST(ignTest, TopicEchoNum)
testing::waitAndCleanupFork(pi);
}

//////////////////////////////////////////////////
/// \brief Check 'ign service --help' message and bash completion script for
/// consistent flags
TEST(ignTest, ServiceHelpVsCompletionFlags)
{
// Flags in help message
std::string helpOutput = custom_exec_str("ign service --help" + g_ignVersion);

// Call the output function in the bash completion script
std::filesystem::path scriptPath = PROJECT_SOURCE_DIR;
scriptPath = scriptPath / "src" / "cmd" / "transport.bash_completion.sh";

// Equivalent to:
// sh -c "bash -c \". /path/to/transport.bash_completion.sh;
// _gz_service_flags\""
std::string cmd = "bash -c \". " + scriptPath.string() +
"; _gz_service_flags\"";
std::string scriptOutput = custom_exec_str(cmd);

// Tokenize script output
std::istringstream iss(scriptOutput);
std::vector<std::string> flags((std::istream_iterator<std::string>(iss)),
std::istream_iterator<std::string>());

EXPECT_GT(flags.size(), 0u);

// Match each flag in script output with help message
for (const auto &flag : flags)
{
EXPECT_NE(std::string::npos, helpOutput.find(flag)) << helpOutput;
}
}

//////////////////////////////////////////////////
/// \brief Check 'ign topic --help' message and bash completion script for
/// consistent flags
TEST(ignTest, TopicHelpVsCompletionFlags)
{
// Flags in help message
std::string helpOutput = custom_exec_str("ign topic --help" + g_ignVersion);

// Call the output function in the bash completion script
std::filesystem::path scriptPath = PROJECT_SOURCE_DIR;
scriptPath = scriptPath / "src" / "cmd" / "transport.bash_completion.sh";

// Equivalent to:
// sh -c "bash -c \". /path/to/transport.bash_completion.sh;
// _gz_topic_flags\""
std::string cmd = "bash -c \". " + scriptPath.string() +
"; _gz_topic_flags\"";
std::string scriptOutput = custom_exec_str(cmd);

// Tokenize script output
std::istringstream iss(scriptOutput);
std::vector<std::string> flags((std::istream_iterator<std::string>(iss)),
std::istream_iterator<std::string>());

EXPECT_GT(flags.size(), 0u);

// Match each flag in script output with help message
for (const auto &flag : flags)
{
EXPECT_NE(std::string::npos, helpOutput.find(flag)) << helpOutput;
}
}

/////////////////////////////////////////////////
/// Main
int main(int argc, char **argv)
Expand Down
2 changes: 1 addition & 1 deletion test/test_config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#ifndef IGNITION_TRANSPORT_TEST_CONFIG_HH_
#define IGNITION_TRANSPORT_TEST_CONFIG_HH_

#define IGN_PATH "@IGNITION-TOOLS_BINARY_DIRS@"
#define IGN_PATH "@HAVE_IGN_TOOLS@"
#define IGN_CONFIG_PATH "@CMAKE_BINARY_DIR@/test/conf"
#define IGN_VERSION_FULL "@PROJECT_VERSION_FULL@"

Expand Down

0 comments on commit 30da9d3

Please sign in to comment.