From 8bd46a6549e3dd7cde7e7dee1d3248d0e3bfcfd6 Mon Sep 17 00:00:00 2001 From: Wouter Franken Date: Sun, 19 Feb 2023 13:08:41 +0100 Subject: [PATCH 1/2] Fix dispatcher when no plugins are given When any plugin is using the dispatcher for nested actions it should pass all plugins again to avoid that any plugin as subtask would be handled. Currently this means a lot of plugins are broken because they don't pass plugins to the dispatcher. To fix this, take all Plugin subclasses when no plugin is given (we should at least have the default plugins). --- dotbot/dispatcher.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/dotbot/dispatcher.py b/dotbot/dispatcher.py index 76994c3..2b56783 100644 --- a/dotbot/dispatcher.py +++ b/dotbot/dispatcher.py @@ -18,7 +18,10 @@ class Dispatcher(object): ): self._log = Messenger() self._setup_context(base_directory, options) - plugins = plugins or [] + if plugins == None: + plugins = Plugin.__subclasses__() + else: + plugins = plugins or [] self._plugins = [plugin(self._context) for plugin in plugins] self._only = only self._skip = skip From e2c455213c86ef2fb8231ed20d3887fecf063d32 Mon Sep 17 00:00:00 2001 From: Wouter Franken Date: Sun, 19 Feb 2023 13:09:00 +0100 Subject: [PATCH 2/2] Add plugin dispatch test Add test to verify that a plugin can run dispatch to handle subtasks of external plugins. --- tests/dotbot_plugin_dispatch.py | 17 +++++++++++++++++ tests/test_dispatcher.py | 22 ++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 tests/dotbot_plugin_dispatch.py create mode 100644 tests/test_dispatcher.py diff --git a/tests/dotbot_plugin_dispatch.py b/tests/dotbot_plugin_dispatch.py new file mode 100644 index 0000000..de630e6 --- /dev/null +++ b/tests/dotbot_plugin_dispatch.py @@ -0,0 +1,17 @@ +"""Test that a plugin can call dispatcher for subtasks. + +The plugin calls dispatch with his data. +""" + +import os.path + +import dotbot + + +class Dispatch(dotbot.Plugin): + def can_handle(self, directive): + return directive == "dispatch" + + def handle(self, directive, data): + dispatcher = dotbot.dispatcher.Dispatcher(self._context.base_directory()) + return dispatcher.dispatch(data) diff --git a/tests/test_dispatcher.py b/tests/test_dispatcher.py new file mode 100644 index 0000000..53c7c02 --- /dev/null +++ b/tests/test_dispatcher.py @@ -0,0 +1,22 @@ +import os +import shutil + +import pytest + + +def test_plugin_dispatcher(capfd, home, dotfiles, run_dotbot): + """Verify that plugins can call dispatcher without explicitly specifying plugins.""" + + dotfiles.makedirs("plugins") + plugin_file = os.path.join( + os.path.dirname(os.path.abspath(__file__)), "dotbot_plugin_dispatch.py" + ) + shutil.copy(plugin_file, os.path.join(dotfiles.directory, "plugins", "dispatch.py")) + dotfiles.write_config( + [ + {"dispatch": [{"create": ["~/a"]}]}, + ] + ) + run_dotbot("--plugin-dir", os.path.join(dotfiles.directory, "plugins")) + + assert os.path.exists(os.path.join(home, "a"))