# HG changeset patch # User David Demelier # Date 1477213026 -7200 # Node ID 427bbbcb50d1459884ae23cec7ba0ee160c9cf9e # Parent d2b02e31478d31ae2a9c7fe3c6c77cebe3b42568 Tests: add test for plugin-load, #559 diff -r d2b02e31478d -r 427bbbcb50d1 libirccd/irccd/cmd-plugin-load.cpp --- a/libirccd/irccd/cmd-plugin-load.cpp Sun Oct 23 10:38:42 2016 +0200 +++ b/libirccd/irccd/cmd-plugin-load.cpp Sun Oct 23 10:57:06 2016 +0200 @@ -19,6 +19,7 @@ #include "cmd-plugin-load.hpp" #include "irccd.hpp" #include "service-plugin.hpp" +#include "transport.hpp" #include "util.hpp" namespace irccd { @@ -26,32 +27,14 @@ namespace command { PluginLoadCommand::PluginLoadCommand() - : Command("plugin-load", "Plugins", "Load a plugin") + : Command("plugin-load") { } -std::vector PluginLoadCommand::args() const -{ - return {{ "plugin", true }}; -} - -std::vector PluginLoadCommand::properties() const +void PluginLoadCommand::exec(Irccd &irccd, TransportClient &client, const nlohmann::json &args) { - return {{ "plugin", { nlohmann::json::value_t::string }}}; -} - -nlohmann::json PluginLoadCommand::request(Irccdctl &, const CommandRequest &args) const -{ - return nlohmann::json::object({{ "plugin", args.arg(0) }}); -} - -nlohmann::json PluginLoadCommand::exec(Irccd &irccd, const nlohmann::json &request) const -{ - Command::exec(irccd, request); - - irccd.plugins().load(request["plugin"]); - - return nlohmann::json::object(); + irccd.plugins().load(util::json::requireIdentifier(args, "plugin")); + client.success("plugin-load"); } } // !command diff -r d2b02e31478d -r 427bbbcb50d1 libirccd/irccd/cmd-plugin-load.hpp --- a/libirccd/irccd/cmd-plugin-load.hpp Sun Oct 23 10:38:42 2016 +0200 +++ b/libirccd/irccd/cmd-plugin-load.hpp Sun Oct 23 10:57:06 2016 +0200 @@ -41,24 +41,9 @@ IRCCD_EXPORT PluginLoadCommand(); /** - * \copydoc Command::args - */ - IRCCD_EXPORT std::vector args() const override; - - /** - * \copydoc Command::properties - */ - IRCCD_EXPORT std::vector properties() const override; - - /** - * \copydoc Command::request - */ - IRCCD_EXPORT nlohmann::json request(Irccdctl &irccdctl, const CommandRequest &args) const override; - - /** * \copydoc Command::exec */ - IRCCD_EXPORT nlohmann::json exec(Irccd &irccd, const nlohmann::json &request) const override; + IRCCD_EXPORT void exec(Irccd &irccd, TransportClient &client, const nlohmann::json &args) override; }; } // !command diff -r d2b02e31478d -r 427bbbcb50d1 libirccd/irccd/service-plugin.cpp --- a/libirccd/irccd/service-plugin.cpp Sun Oct 23 10:38:42 2016 +0200 +++ b/libirccd/irccd/service-plugin.cpp Sun Oct 23 10:57:06 2016 +0200 @@ -75,6 +75,11 @@ m_plugins.push_back(std::move(plugin)); } +void PluginService::addLoader(std::unique_ptr loader) +{ + m_loaders.push_back(std::move(loader)); +} + void PluginService::setConfig(const std::string &name, PluginConfig config) { m_config.emplace(name, std::move(config)); diff -r d2b02e31478d -r 427bbbcb50d1 libirccd/irccd/service-plugin.hpp --- a/libirccd/irccd/service-plugin.hpp Sun Oct 23 10:38:42 2016 +0200 +++ b/libirccd/irccd/service-plugin.hpp Sun Oct 23 10:57:06 2016 +0200 @@ -104,6 +104,13 @@ IRCCD_EXPORT void add(std::shared_ptr plugin); /** + * Add a loader. + * + * \param loader the loader + */ + IRCCD_EXPORT void addLoader(std::unique_ptr loader); + + /** * Configure a plugin. * * If the plugin is already loaded, its configuration is updated. diff -r d2b02e31478d -r 427bbbcb50d1 tests/CMakeLists.txt --- a/tests/CMakeLists.txt Sun Oct 23 10:38:42 2016 +0200 +++ b/tests/CMakeLists.txt Sun Oct 23 10:57:06 2016 +0200 @@ -22,6 +22,7 @@ if (WITH_TESTS) add_subdirectory(cmd-plugin-config) add_subdirectory(cmd-plugin-info) + add_subdirectory(cmd-plugin-load) add_subdirectory(cmd-server-cmode) add_subdirectory(cmd-server-cnotice) add_subdirectory(cmd-server-connect) diff -r d2b02e31478d -r 427bbbcb50d1 tests/cmd-plugin-load/CMakeLists.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/cmd-plugin-load/CMakeLists.txt Sun Oct 23 10:57:06 2016 +0200 @@ -0,0 +1,23 @@ +# +# CMakeLists.txt -- CMake build system for irccd +# +# Copyright (c) 2013-2016 David Demelier +# +# Permission to use, copy, modify, and/or distribute this software for any +# purpose with or without fee is hereby granted, provided that the above +# copyright notice and this permission notice appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +# + +irccd_define_test( + NAME cmd-plugin-load + SOURCES main.cpp + LIBRARIES libirccd libirccdctl +) diff -r d2b02e31478d -r 427bbbcb50d1 tests/cmd-plugin-load/main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/cmd-plugin-load/main.cpp Sun Oct 23 10:57:06 2016 +0200 @@ -0,0 +1,79 @@ +/* + * main.cpp -- test plugin-load remote command + * + * Copyright (c) 2013-2016 David Demelier + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include +#include +#include + +using namespace irccd; +using namespace irccd::command; + +namespace { + +class CustomLoader : public PluginLoader { +public: + std::shared_ptr open(const std::string &, + const std::string &) noexcept override + { + return nullptr; + } + + std::shared_ptr find(const std::string &id) noexcept override + { + return std::make_unique(id, ""); + } +}; + +class PluginLoadCommandTest : public CommandTester { +public: + PluginLoadCommandTest() + : CommandTester(std::make_unique()) + { + m_irccd.plugins().addLoader(std::make_unique()); + } +}; + +TEST_F(PluginLoadCommandTest, basic) +{ + try { + m_irccdctl.client().request({ + { "command", "plugin-load" }, + { "plugin", "foo" } + }); + + poll([&] () { + return m_irccd.plugins().has("foo"); + }); + + ASSERT_FALSE(m_irccd.plugins().list().empty()); + ASSERT_TRUE(m_irccd.plugins().has("foo")); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } +} + +} // !namespace + +int main(int argc, char **argv) +{ + testing::InitGoogleTest(&argc, argv); + + return RUN_ALL_TESTS(); +}