changeset 314:427bbbcb50d1

Tests: add test for plugin-load, #559
author David Demelier <markand@malikania.fr>
date Sun, 23 Oct 2016 10:57:06 +0200
parents d2b02e31478d
children 8cf706edd262
files libirccd/irccd/cmd-plugin-load.cpp libirccd/irccd/cmd-plugin-load.hpp libirccd/irccd/service-plugin.cpp libirccd/irccd/service-plugin.hpp tests/CMakeLists.txt tests/cmd-plugin-load/CMakeLists.txt tests/cmd-plugin-load/main.cpp
diffstat 7 files changed, 121 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- 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<Command::Arg> PluginLoadCommand::args() const
-{
-    return {{ "plugin", true }};
-}
-
-std::vector<Command::Property> 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
--- 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<Arg> args() const override;
-
-    /**
-     * \copydoc Command::properties
-     */
-    IRCCD_EXPORT std::vector<Property> 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
--- 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<PluginLoader> loader)
+{
+    m_loaders.push_back(std::move(loader));
+}
+
 void PluginService::setConfig(const std::string &name, PluginConfig config)
 {
     m_config.emplace(name, std::move(config));
--- 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> plugin);
 
     /**
+     * Add a loader.
+     *
+     * \param loader the loader
+     */
+    IRCCD_EXPORT void addLoader(std::unique_ptr<PluginLoader> loader);
+
+    /**
      * Configure a plugin.
      *
      * If the plugin is already loaded, its configuration is updated.
--- 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)
--- /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 <markand@malikania.fr>
+#
+# 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
+)
--- /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 <markand@malikania.fr>
+ *
+ * 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 <cmd-plugin-load.hpp>
+#include <command-tester.hpp>
+#include <server-tester.hpp>
+#include <service-plugin.hpp>
+#include <plugin.hpp>
+
+using namespace irccd;
+using namespace irccd::command;
+
+namespace {
+
+class CustomLoader : public PluginLoader {
+public:
+    std::shared_ptr<Plugin> open(const std::string &,
+                                 const std::string &) noexcept override
+    {
+        return nullptr;
+    }
+
+    std::shared_ptr<Plugin> find(const std::string &id) noexcept override
+    {
+        return std::make_unique<Plugin>(id, "");
+    }
+};
+
+class PluginLoadCommandTest : public CommandTester {
+public:
+    PluginLoadCommandTest()
+        : CommandTester(std::make_unique<PluginLoadCommand>())
+    {
+        m_irccd.plugins().addLoader(std::make_unique<CustomLoader>());
+    }
+};
+
+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();
+}