From 39a4e88f07ab8bd4b1fe2e2c6fa9ec6a45b9e61f Mon Sep 17 00:00:00 2001 From: Rik Heijdens Date: Thu, 3 Dec 2020 18:23:23 +0100 Subject: [PATCH] Add a test that reproduces airflow#12785 Added a test case to reproduce the issue reported in https://github.com/apache/airflow/issues/12785 --- tests/plugins/test_plugins_manager.py | 29 +++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/plugins/test_plugins_manager.py b/tests/plugins/test_plugins_manager.py index 117df989c4c23..c0a4ba03e7f15 100644 --- a/tests/plugins/test_plugins_manager.py +++ b/tests/plugins/test_plugins_manager.py @@ -15,6 +15,7 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. +import importlib import logging import unittest from unittest import mock @@ -213,6 +214,34 @@ def test_entrypoint_plugin_errors_dont_raise_exceptions(self, caplog): assert "Failed to import plugin test-entrypoint" in received_logs assert ("test.plugins.test_plugins_manager", "my_fake_module not found") in import_errors.items() + def test_registering_plugin_macros(self): + """ + Tests whether macros that originate from plugins are being registered correctly. + """ + from airflow import macros + from airflow.plugins_manager import integrate_macros_plugins + + def custom_macro(): + return 'foo' + + class MacroPlugin(AirflowPlugin): + name = 'macro_plugin' + macros = [custom_macro] + + with mock_plugin_manager(plugins=[MacroPlugin()]): + # Ensure the macros for the plugin have been integrated. + integrate_macros_plugins() + # Test whether the modules have been created as expected. + plugin_macros = importlib.import_module(f"airflow.macros.{MacroPlugin.name}") + for macro in MacroPlugin.macros: + # Verify that the macros added by the plugin are being set correctly + # on the plugin's macro module. + assert hasattr(plugin_macros, macro.__name__) + # Verify that the symbol table in airflow.macros has been updated with an entry for + # this plugin, this is necessary in order to allow the plugin's macros to be used when + # rendering templates. + assert hasattr(macros, MacroPlugin.name) + class TestPluginsDirectorySource(unittest.TestCase): def test_should_return_correct_path_name(self):