diff --git a/tests/plugin/_plugins/another_ordered_hook_plugin.py b/tests/plugin/_plugins/another_ordered_hook_plugin.py new file mode 100644 index 00000000..64431c2b --- /dev/null +++ b/tests/plugin/_plugins/another_ordered_hook_plugin.py @@ -0,0 +1,6 @@ +def callback(*args, **kwargs): + pass + +__plugin_hooks__ = { + "some.ordered.callback": (callback, 100) +} diff --git a/tests/plugin/_plugins/hook_plugin.py b/tests/plugin/_plugins/hook_plugin.py index 03b1057f..7aeb1b30 100644 --- a/tests/plugin/_plugins/hook_plugin.py +++ b/tests/plugin/_plugins/hook_plugin.py @@ -7,4 +7,7 @@ def hook_startup(): __plugin_name__ = "Hook Plugin" __plugin_description__ = "Test hook plugin" -__plugin_hooks__ = {'octoprint.core.startup': hook_startup} \ No newline at end of file +__plugin_hooks__ = { + 'octoprint.core.startup': hook_startup, + 'some.ordered.callback': hook_startup +} diff --git a/tests/plugin/_plugins/mixed_plugin/__init__.py b/tests/plugin/_plugins/mixed_plugin/__init__.py index aed5a5ed..ed651173 100644 --- a/tests/plugin/_plugins/mixed_plugin/__init__.py +++ b/tests/plugin/_plugins/mixed_plugin/__init__.py @@ -9,9 +9,13 @@ import octoprint.plugin class TestMixedPlugin(octoprint.plugin.StartupPlugin, octoprint.plugin.SettingsPlugin): - pass + def get_sorting_key(self, context=None): + if context == "sorting_test": + return 100 + else: + return None __plugin_name__ = "Mixed Plugin" __plugin_description__ = "Test mixed plugin" -__plugin_implementation__ = TestMixedPlugin() \ No newline at end of file +__plugin_implementation__ = TestMixedPlugin() diff --git a/tests/plugin/_plugins/one_ordered_hook_plugin.py b/tests/plugin/_plugins/one_ordered_hook_plugin.py new file mode 100644 index 00000000..3d68dcd6 --- /dev/null +++ b/tests/plugin/_plugins/one_ordered_hook_plugin.py @@ -0,0 +1,6 @@ +def callback(*args, **kwargs): + pass + +__plugin_hooks__ = { + "some.ordered.callback": (callback, 10) +} diff --git a/tests/plugin/_plugins/startup_plugin.py b/tests/plugin/_plugins/startup_plugin.py index b75ca851..50e43804 100644 --- a/tests/plugin/_plugins/startup_plugin.py +++ b/tests/plugin/_plugins/startup_plugin.py @@ -4,9 +4,13 @@ import octoprint.plugin class TestStartupPlugin(octoprint.plugin.StartupPlugin): - pass + def get_sorting_key(self, context=None): + if context == "sorting_test": + return 10 + else: + return None __plugin_name__ = "Startup Plugin" __plugin_description__ = "Test startup plugin" -__plugin_implementation__ = TestStartupPlugin() \ No newline at end of file +__plugin_implementation__ = TestStartupPlugin() diff --git a/tests/plugin/test_core.py b/tests/plugin/test_core.py index 0667785d..b76110af 100644 --- a/tests/plugin/test_core.py +++ b/tests/plugin/test_core.py @@ -23,8 +23,8 @@ class PluginTestCase(unittest.TestCase): self.plugin_manager.initialize_implementations() def test_plugin_loading(self): - self.assertEquals(5, len(self.plugin_manager.enabled_plugins)) - self.assertEquals(1, len(self.plugin_manager.plugin_hooks)) + self.assertEquals(7, len(self.plugin_manager.enabled_plugins)) + self.assertEquals(2, len(self.plugin_manager.plugin_hooks)) self.assertEquals(4, len(self.plugin_manager.plugin_implementations)) self.assertEquals(3, len(self.plugin_manager.plugin_implementations_by_type)) @@ -32,6 +32,10 @@ class PluginTestCase(unittest.TestCase): self.assertTrue("octoprint.core.startup" in self.plugin_manager.plugin_hooks) self.assertEquals(1, len(self.plugin_manager.plugin_hooks["octoprint.core.startup"])) + # ordered hook plugins + self.assertTrue("some.ordered.callback" in self.plugin_manager.plugin_hooks) + self.assertEquals(3, len(self.plugin_manager.plugin_hooks["some.ordered.callback"])) + # TestStartupPlugin & TestMixedPlugin self.assertTrue(octoprint.plugin.StartupPlugin in self.plugin_manager.plugin_implementations_by_type) self.assertEquals(2, len(self.plugin_manager.plugin_implementations_by_type[octoprint.plugin.StartupPlugin])) @@ -119,18 +123,31 @@ class PluginTestCase(unittest.TestCase): hooks = self.plugin_manager.get_hooks("octoprint.printing.print") self.assertEquals(0, len(hooks)) - def test_get_implementation(self): + def test_sorted_hooks(self): + hooks = self.plugin_manager.get_hooks("some.ordered.callback") + self.assertEquals(3, len(hooks)) + self.assertListEqual(["one_ordered_hook_plugin", "another_ordered_hook_plugin", "hook_plugin"], hooks.keys()) + + def test_get_implementations(self): implementations = self.plugin_manager.get_implementations(octoprint.plugin.StartupPlugin) - self.assertEquals(2, len(implementations)) # startup_plugin, mixed_plugin + self.assertListEqual(["mixed_plugin", "startup_plugin"], map(lambda x: x._identifier, implementations)) implementations = self.plugin_manager.get_implementations(octoprint.plugin.SettingsPlugin) - self.assertEquals(2, len(implementations)) # settings_plugin, mixed_plugin + self.assertListEqual(["mixed_plugin", "settings_plugin"], map(lambda x: x._identifier, implementations)) implementations = self.plugin_manager.get_implementations(octoprint.plugin.StartupPlugin, octoprint.plugin.SettingsPlugin) - self.assertEquals(1, len(implementations)) # mixed_plugin + self.assertListEqual(["mixed_plugin"], map(lambda x: x._identifier, implementations)) implementations = self.plugin_manager.get_implementations(octoprint.plugin.AssetPlugin) - self.assertEquals(1, len(implementations)) # deprecated_plugin, but only first implementation! + self.assertListEqual(["deprecated_plugin"], map(lambda x: x._identifier, implementations)) + + def test_get_filtered_implementations(self): + implementations = self.plugin_manager.get_filtered_implementations(lambda x: x._identifier.startswith("startup"), octoprint.plugin.StartupPlugin) + self.assertEquals(1, len(implementations)) + + def test_get_sorted_implementations(self): + implementations = self.plugin_manager.get_implementations(octoprint.plugin.StartupPlugin, sorting_context="sorting_test") + self.assertListEqual(["startup_plugin", "mixed_plugin"], map(lambda x: x._identifier, implementations)) def test_client_registration(self): def test_client(*args, **kwargs):