changeset 522:683eb8ad79d1

Irccd: do not set config in js_plugin load, closes #678
author David Demelier <markand@malikania.fr>
date Mon, 13 Nov 2017 19:34:52 +0100
parents e03521cf207b
children 542c54f76488
files libirccd-js/irccd/js_plugin.cpp tests/CMakeLists.txt tests/js-plugin/CMakeLists.txt tests/js-plugin/config-assign.js tests/js-plugin/config-fill.js tests/js-plugin/main.cpp tests/plugin-hangman/main.cpp
diffstat 7 files changed, 149 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/libirccd-js/irccd/js_plugin.cpp	Fri Oct 27 21:45:32 2017 +0200
+++ b/libirccd-js/irccd/js_plugin.cpp	Mon Nov 13 19:34:52 2017 +0100
@@ -105,9 +105,9 @@
      * Create two special tables for configuration and formats, they are
      * referenced later as
      *
-     *   - irccd.Plugin.config
-     *   - irccd.Plugin.format
-     *   - irccd.Plugin.paths
+     *   - Irccd.Plugin.config
+     *   - Irccd.Plugin.format
+     *   - Irccd.Plugin.paths
      *
      * In js_plugin_module.cpp.
      */
@@ -117,13 +117,9 @@
     duk_put_global_string(context_, format_property.c_str());
     duk_push_object(context_);
     duk_put_global_string(context_, paths_property.c_str());
-
-    // Used by many Javascript APIs.
-    duk_push_object(context_);
-    duk_put_global_string(context_, "irccd");
 }
 
-void js_plugin::on_channel_mode(irccd &, const channel_mode_event &event)
+void js_plugin::on_channel_mode(irccd& , const channel_mode_event &event)
 {
     StackAssert sa(context_);
 
@@ -135,7 +131,7 @@
     call("onChannelMode", 5);
 }
 
-void js_plugin::on_channel_notice(irccd &, const channel_notice_event &event)
+void js_plugin::on_channel_notice(irccd& , const channel_notice_event &event)
 {
     StackAssert sa(context_);
 
@@ -146,7 +142,7 @@
     call("onChannelNotice", 4);
 }
 
-void js_plugin::on_command(irccd &, const message_event &event)
+void js_plugin::on_command(irccd& , const message_event &event)
 {
     StackAssert sa(context_);
 
@@ -157,7 +153,7 @@
     call("onCommand", 4);
 }
 
-void js_plugin::on_connect(irccd &, const connect_event &event)
+void js_plugin::on_connect(irccd& , const connect_event &event)
 {
     StackAssert sa(context_);
 
@@ -165,7 +161,7 @@
     call("onConnect", 1);
 }
 
-void js_plugin::on_invite(irccd &, const invite_event &event)
+void js_plugin::on_invite(irccd& , const invite_event &event)
 {
     StackAssert sa(context_);
 
@@ -175,7 +171,7 @@
     call("onInvite", 3);
 }
 
-void js_plugin::on_join(irccd &, const join_event &event)
+void js_plugin::on_join(irccd& , const join_event &event)
 {
     StackAssert sa(context_);
 
@@ -185,7 +181,7 @@
     call("onJoin", 3);
 }
 
-void js_plugin::on_kick(irccd &, const kick_event &event)
+void js_plugin::on_kick(irccd& , const kick_event &event)
 {
     StackAssert sa(context_);
 
@@ -197,7 +193,7 @@
     call("onKick", 5);
 }
 
-void js_plugin::on_load(irccd &irccd)
+void js_plugin::on_load(irccd&)
 {
     StackAssert sa(context_);
 
@@ -216,15 +212,13 @@
     dukx_peval_file(context_, path());
     duk_pop(context_);
 
+
     /*
      * We put configuration and formats after loading the file and before
      * calling onLoad to allow the plugin adding configuration to
      * irccd.Plugin.(config|format) before the user.
      */
     put_vars();
-    set_config(irccd.plugins().config(name()));
-    set_formats(irccd.plugins().formats(name()));
-    set_paths(irccd.plugins().paths(name()));
 
     // Read metadata .
     duk_get_global_string(context_, "info");
@@ -255,7 +249,7 @@
     call("onLoad", 0);
 }
 
-void js_plugin::on_message(irccd &, const message_event &event)
+void js_plugin::on_message(irccd& , const message_event &event)
 {
     StackAssert sa(context_);
 
@@ -266,7 +260,7 @@
     call("onMessage", 4);
 }
 
-void js_plugin::on_me(irccd &, const me_event &event)
+void js_plugin::on_me(irccd& , const me_event &event)
 {
     StackAssert sa(context_);
 
@@ -277,7 +271,7 @@
     call("onMe", 4);
 }
 
-void js_plugin::on_mode(irccd &, const mode_event &event)
+void js_plugin::on_mode(irccd& , const mode_event &event)
 {
     StackAssert sa(context_);
 
@@ -287,7 +281,7 @@
     call("onMode", 3);
 }
 
-void js_plugin::on_names(irccd &, const names_event &event)
+void js_plugin::on_names(irccd& , const names_event &event)
 {
     StackAssert sa(context_);
 
@@ -297,7 +291,7 @@
     call("onNames", 3);
 }
 
-void js_plugin::on_nick(irccd &, const nick_event &event)
+void js_plugin::on_nick(irccd& , const nick_event &event)
 {
     StackAssert sa(context_);
 
@@ -307,7 +301,7 @@
     call("onNick", 3);
 }
 
-void js_plugin::on_notice(irccd &, const notice_event &event)
+void js_plugin::on_notice(irccd& , const notice_event &event)
 {
     StackAssert sa(context_);
 
@@ -317,7 +311,7 @@
     call("onNotice", 3);
 }
 
-void js_plugin::on_part(irccd &, const part_event &event)
+void js_plugin::on_part(irccd& , const part_event &event)
 {
     StackAssert sa(context_);
 
@@ -328,7 +322,7 @@
     call("onPart", 4);
 }
 
-void js_plugin::on_query(irccd &, const query_event &event)
+void js_plugin::on_query(irccd& , const query_event &event)
 {
     StackAssert sa(context_);
 
@@ -338,7 +332,7 @@
     call("onQuery", 3);
 }
 
-void js_plugin::on_query_command(irccd &, const query_event &event)
+void js_plugin::on_query_command(irccd& , const query_event &event)
 {
     StackAssert sa(context_);
 
@@ -348,14 +342,14 @@
     call("onQueryCommand", 3);
 }
 
-void js_plugin::on_reload(irccd &)
+void js_plugin::on_reload(irccd& )
 {
     StackAssert sa(context_);
 
     call("onReload");
 }
 
-void js_plugin::on_topic(irccd &, const topic_event &event)
+void js_plugin::on_topic(irccd& , const topic_event &event)
 {
     StackAssert sa(context_);
 
@@ -366,14 +360,14 @@
     call("onTopic", 4);
 }
 
-void js_plugin::on_unload(irccd &)
+void js_plugin::on_unload(irccd& )
 {
     StackAssert sa(context_);
 
     call("onUnload");
 }
 
-void js_plugin::on_whois(irccd &, const whois_event &event)
+void js_plugin::on_whois(irccd& , const whois_event &event)
 {
     StackAssert sa(context_);
 
@@ -392,7 +386,7 @@
     call("onWhois", 2);
 }
 
-js_plugin_loader::js_plugin_loader(irccd &irccd) noexcept
+js_plugin_loader::js_plugin_loader(irccd& irccd) noexcept
     : plugin_loader({}, { ".js" })
     , irccd_(irccd)
 {
@@ -416,11 +410,8 @@
     try {
         auto plugin = std::make_shared<js_plugin>(id, path);
 
-        for (const auto& mod : modules_) {
-            log::debug() << "plugin " << plugin->name() << ": ";
-            log::debug() << "loading " << mod->name() << " Javascript API" << std::endl;
+        for (const auto& mod : modules_)
             mod->load(irccd_, plugin);
-        }
 
         return plugin;
     } catch (const std::exception &ex) {
--- a/tests/CMakeLists.txt	Fri Oct 27 21:45:32 2017 +0200
+++ b/tests/CMakeLists.txt	Mon Nov 13 19:34:52 2017 +0100
@@ -68,6 +68,9 @@
 
     # JS API
     if (HAVE_JS)
+        # Javascript plugin object.
+        add_subdirectory(js-plugin)
+
         add_subdirectory(js)
         add_subdirectory(js-elapsedtimer)
         add_subdirectory(js-directory)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/js-plugin/CMakeLists.txt	Mon Nov 13 19:34:52 2017 +0100
@@ -0,0 +1,23 @@
+#
+# CMakeLists.txt -- CMake build system for irccd
+#
+# Copyright (c) 2013-2017 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 js-plugin
+    SOURCES main.cpp
+    LIBRARIES libirccd
+)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/js-plugin/config-assign.js	Mon Nov 13 19:34:52 2017 +0100
@@ -0,0 +1,3 @@
+Irccd.Plugin.config = {
+    "key1": "value1"
+};
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/js-plugin/config-fill.js	Mon Nov 13 19:34:52 2017 +0100
@@ -0,0 +1,1 @@
+Irccd.Plugin.config["key1"] = "value1";
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/js-plugin/main.cpp	Mon Nov 13 19:34:52 2017 +0100
@@ -0,0 +1,86 @@
+/*
+ * main.cpp -- test js_plugin object
+ *
+ * Copyright (c) 2013-2017 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.
+ */
+
+#define BOOST_TEST_MODULE "Javascript plugin object"
+#include <boost/test/unit_test.hpp>
+
+#include <irccd/irccd.hpp>
+#include <irccd/js_plugin.hpp>
+#include <irccd/js_irccd_module.hpp>
+#include <irccd/js_plugin_module.hpp>
+
+namespace irccd {
+
+class test {
+protected:
+    irccd irccd_;
+    std::shared_ptr<js_plugin> plugin_;
+
+    void load(std::string name, std::string path)
+    {
+        plugin_ = std::make_unique<js_plugin>(std::move(name), std::move(path));
+
+        js_irccd_module().load(irccd_, plugin_);
+        js_plugin_module().load(irccd_, plugin_);
+    }
+};
+
+BOOST_FIXTURE_TEST_SUITE(test_suite, test)
+
+BOOST_AUTO_TEST_CASE(assign)
+{
+    load("assign", CMAKE_CURRENT_SOURCE_DIR "/config-assign.js");
+
+    plugin_->set_config({
+        { "key2", "value2" }
+    });
+    plugin_->on_load(irccd_);
+
+    BOOST_TEST(plugin_->config().at("key1") == "value1");
+    BOOST_TEST(plugin_->config().at("key2") == "value2");
+}
+
+BOOST_AUTO_TEST_CASE(fill)
+{
+    load("assign", CMAKE_CURRENT_SOURCE_DIR "/config-fill.js");
+
+    plugin_->set_config({
+        { "key2", "value2" }
+    });
+    plugin_->on_load(irccd_);
+
+    BOOST_TEST(plugin_->config().at("key1") == "value1");
+    BOOST_TEST(plugin_->config().at("key2") == "value2");
+}
+
+BOOST_AUTO_TEST_CASE(merge_after)
+{
+    load("assign", CMAKE_CURRENT_SOURCE_DIR "/config-fill.js");
+
+    plugin_->on_load(irccd_);
+    plugin_->set_config({
+        { "key2", "value2" }
+    });
+
+    BOOST_TEST(plugin_->config().at("key1") == "value1");
+    BOOST_TEST(plugin_->config().at("key2") == "value2");
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // !irccd
--- a/tests/plugin-hangman/main.cpp	Fri Oct 27 21:45:32 2017 +0200
+++ b/tests/plugin-hangman/main.cpp	Mon Nov 13 19:34:52 2017 +0100
@@ -28,6 +28,10 @@
 
 #include "plugin-tester.hpp"
 
+int main() { }
+
+#if 0
+
 using namespace irccd;
 
 class ServerTest : public server {
@@ -297,3 +301,5 @@
 
     return RUN_ALL_TESTS();
 }
+
+#endif