changeset 316:ee2593e7e670

Tests: add test for plugin-unload, #559
author David Demelier <markand@malikania.fr>
date Sun, 23 Oct 2016 11:12:17 +0200
parents 8cf706edd262
children 249ccc1e4840
files libirccd/irccd/cmd-plugin-unload.cpp libirccd/irccd/cmd-plugin-unload.hpp tests/CMakeLists.txt tests/cmd-plugin-unload/CMakeLists.txt tests/cmd-plugin-unload/main.cpp
diffstat 5 files changed, 111 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/libirccd/irccd/cmd-plugin-unload.cpp	Sun Oct 23 11:06:57 2016 +0200
+++ b/libirccd/irccd/cmd-plugin-unload.cpp	Sun Oct 23 11:12:17 2016 +0200
@@ -19,6 +19,7 @@
 #include "cmd-plugin-unload.hpp"
 #include "irccd.hpp"
 #include "service-plugin.hpp"
+#include "transport.hpp"
 #include "util.hpp"
 
 namespace irccd {
@@ -26,32 +27,14 @@
 namespace command {
 
 PluginUnloadCommand::PluginUnloadCommand()
-    : Command("plugin-unload", "Plugins", "Unload a plugin")
+    : Command("plugin-unload")
 {
 }
 
-std::vector<Command::Arg> PluginUnloadCommand::args() const
-{
-    return {{ "plugin", true }};
-}
-
-std::vector<Command::Property> PluginUnloadCommand::properties() const
+void PluginUnloadCommand::exec(Irccd &irccd, TransportClient &client, const nlohmann::json &args)
 {
-    return {{ "plugin", { nlohmann::json::value_t::string }}};
-}
-
-nlohmann::json PluginUnloadCommand::request(Irccdctl &, const CommandRequest &args) const
-{
-    return nlohmann::json::object({{ "plugin", args.arg(0) }});
-}
-
-nlohmann::json PluginUnloadCommand::exec(Irccd &irccd, const nlohmann::json &request) const
-{
-    Command::exec(irccd, request);
-
-    irccd.plugins().unload(request["plugin"].get<std::string>());
-
-    return nlohmann::json::object();
+    irccd.plugins().unload(util::json::requireIdentifier(args, "plugin"));
+    client.success("plugin-unload");
 }
 
 } // !command
--- a/libirccd/irccd/cmd-plugin-unload.hpp	Sun Oct 23 11:06:57 2016 +0200
+++ b/libirccd/irccd/cmd-plugin-unload.hpp	Sun Oct 23 11:12:17 2016 +0200
@@ -41,24 +41,9 @@
     IRCCD_EXPORT PluginUnloadCommand();
 
     /**
-     * \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/tests/CMakeLists.txt	Sun Oct 23 11:06:57 2016 +0200
+++ b/tests/CMakeLists.txt	Sun Oct 23 11:12:17 2016 +0200
@@ -24,6 +24,7 @@
     add_subdirectory(cmd-plugin-info)
     add_subdirectory(cmd-plugin-load)
     add_subdirectory(cmd-plugin-reload)
+    add_subdirectory(cmd-plugin-unload)
     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-unload/CMakeLists.txt	Sun Oct 23 11:12:17 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-unload
+    SOURCES main.cpp
+    LIBRARIES libirccd libirccdctl
+)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/cmd-plugin-unload/main.cpp	Sun Oct 23 11:12:17 2016 +0200
@@ -0,0 +1,81 @@
+/*
+ * main.cpp -- test plugin-unload 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-unload.hpp>
+#include <command-tester.hpp>
+#include <server-tester.hpp>
+#include <service-plugin.hpp>
+#include <plugin.hpp>
+
+using namespace irccd;
+using namespace irccd::command;
+
+namespace {
+
+bool called = false;
+
+class CustomPlugin : public Plugin {
+public:
+    CustomPlugin()
+        : Plugin("test", "")
+    {
+    }
+
+    void onUnload(Irccd &) override
+    {
+        called = true;
+    }
+};
+
+class PluginUnloadCommandTest : public CommandTester {
+public:
+    PluginUnloadCommandTest()
+        : CommandTester(std::make_unique<PluginUnloadCommand>())
+    {
+        called = false;
+    }
+};
+
+TEST_F(PluginUnloadCommandTest, basic)
+{
+    try {
+        m_irccd.plugins().add(std::make_unique<CustomPlugin>());
+        m_irccdctl.client().request({
+            { "command", "plugin-unload" },
+            { "plugin", "test" }
+        });
+
+        poll([&] () {
+            return called;
+        });
+
+        ASSERT_TRUE(called);
+        ASSERT_TRUE(m_irccd.plugins().list().empty());
+    } catch (const std::exception &ex) {
+        FAIL() << ex.what();
+    }
+}
+
+} // !namespace
+
+int main(int argc, char **argv)
+{
+    testing::InitGoogleTest(&argc, argv);
+
+    return RUN_ALL_TESTS();
+}