diff --git a/pluginlib/CMakeLists.txt b/pluginlib/CMakeLists.txt index 4290196..940d55e 100644 --- a/pluginlib/CMakeLists.txt +++ b/pluginlib/CMakeLists.txt @@ -105,6 +105,16 @@ if(BUILD_TESTING) "$") endif() + ament_add_gtest(${PROJECT_NAME}_list_plugins_test + test/list_plugins_test.cpp + APPEND_ENV AMENT_PREFIX_PATH=${mock_install_path} + ) + if(TARGET ${PROJECT_NAME}_list_plugins_test) + target_compile_definitions(${PROJECT_NAME}_list_plugins_test PRIVATE + "LIST_PLUGINS_EXECUTABLE=\"$\"") + add_dependencies(${PROJECT_NAME}_list_plugins_test list_plugins "${mock_install_target}") + endif() + endif() ament_package( diff --git a/pluginlib/src/list_plugins.cpp b/pluginlib/src/list_plugins.cpp index 7e5584b..cbfdfee 100644 --- a/pluginlib/src/list_plugins.cpp +++ b/pluginlib/src/list_plugins.cpp @@ -37,13 +37,18 @@ int main(int argc, char ** argv) std::cerr << "Requires two arguments!" << std::endl; std::cerr << "Usage: " << argv[0]; std::cerr << " [package] [base_class]" << std::endl; - return -1; + return 1; } - // Note: You cannot use void if you want to use the templated createSharedInstance methods - pluginlib::ClassLoader cl(argv[1], argv[2]); - for (const auto & declared : cl.getDeclaredClasses()) { - std::cout << declared << std::endl; + try { + // Note: You cannot use void if you want to use the templated createSharedInstance methods + pluginlib::ClassLoader cl(argv[1], argv[2]); + for (const auto & declared : cl.getDeclaredClasses()) { + std::cout << declared << std::endl; + } + } catch (const pluginlib::PluginlibException & ex) { + std::cerr << "Error: " << ex.what() << std::endl; + return 1; } return 0; } diff --git a/pluginlib/test/list_plugins_test.cpp b/pluginlib/test/list_plugins_test.cpp new file mode 100644 index 0000000..f8e0779 --- /dev/null +++ b/pluginlib/test/list_plugins_test.cpp @@ -0,0 +1,133 @@ +// Copyright 2026 Open Source Robotics Foundation, Inc. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// * Neither the name of the copyright holder nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +#include + +#include +#include +#include +#include +#include +#include + +#ifdef _WIN32 +#define popen _popen +#define pclose _pclose +#else +#include +#endif + +namespace +{ + +struct Result +{ + int exit_code; + std::string output; // stdout and stderr +}; + +// Runs list_plugins with 'args' and returns its exit code and output. +Result runListPlugins(const std::string & args) +{ + std::string command = "\"" LIST_PLUGINS_EXECUTABLE "\" " + args + " 2>&1"; +#ifdef _WIN32 + // cmd.exe strips the outer quotes of the command line. + command = "\"" + command + "\""; +#endif + Result result{-1, ""}; + FILE * pipe = popen(command.c_str(), "r"); + if (pipe == nullptr) { + ADD_FAILURE() << "Failed to run: " << command; + return result; + } + std::array buffer; + while (fgets(buffer.data(), static_cast(buffer.size()), pipe) != nullptr) { + result.output += buffer.data(); + } + const int status = pclose(pipe); +#ifdef _WIN32 + result.exit_code = status; +#else + result.exit_code = WIFEXITED(status) ? WEXITSTATUS(status) : -1; +#endif + return result; +} + +std::vector sortedLines(const std::string & text) +{ + std::vector lines; + std::istringstream stream(text); + std::string line; + while (std::getline(stream, line)) { + if (!line.empty() && line.back() == '\r') { + line.pop_back(); + } + if (!line.empty()) { + lines.push_back(line); + } + } + std::sort(lines.begin(), lines.end()); + return lines; +} + +TEST(ListPluginsTest, wrongNumberOfArguments) { + const Result result = runListPlugins("test_pluginlib"); + EXPECT_EQ(1, result.exit_code) << result.output; + EXPECT_NE(std::string::npos, result.output.find("Usage:")) << result.output; +} + +TEST(ListPluginsTest, listsDeclaredClasses) { + // 'test_pluginlib/foo_with_ctor' has a different base class, so it is not listed. + const Result result = runListPlugins("test_pluginlib test_base::Fubar"); + EXPECT_EQ(0, result.exit_code) << result.output; + EXPECT_EQ( + (std::vector{ + "test_pluginlib/bar", "test_pluginlib/foo", "test_pluginlib/none"}), + sortedLines(result.output)); +} + +TEST(ListPluginsTest, unknownBaseClass) { + const Result result = runListPlugins("test_pluginlib test_base::Unknown"); + EXPECT_EQ(0, result.exit_code) << result.output; + EXPECT_TRUE(sortedLines(result.output).empty()) << result.output; +} + +TEST(ListPluginsTest, unknownPackage) { + const Result result = runListPlugins("pluginlib_unknown_package test_base::Fubar"); + EXPECT_EQ(1, result.exit_code) << result.output; + EXPECT_NE(std::string::npos, result.output.find("Error:")) << result.output; + EXPECT_NE(std::string::npos, result.output.find("pluginlib_unknown_package")) << result.output; +} + +} // namespace + +int main(int argc, char ** argv) +{ + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +}