RikHeijdens commented on a change in pull request #12788:
URL: https://github.com/apache/airflow/pull/12788#discussion_r535512072



##########
File path: tests/plugins/test_plugins_manager.py
##########
@@ -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)

Review comment:
       Simply reloading the module using `importlib.reload()` will not be 
sufficient as the module's symbol table (dictionary) is 
[retained](https://docs.python.org/3/library/importlib.html#importlib.reload).
   
   > When a module is reloaded, its dictionary (containing the module’s global 
variables) is retained. Redefinitions of names will override the old 
definitions, so this is generally not a problem. If the new version of a module 
does not define a name that was defined by the old version, the old definition 
remains.
   
   I think we'll have to delete the module from `sys.modules` and import it 
again in order to properly remove the entries that are being added by this test 
case.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to