Fix entrypoint discovery
authorKarl O. Pinc <kop@karlpinc.com>
Wed, 20 Jan 2021 23:25:24 +0000 (17:25 -0600)
committerKarl O. Pinc <kop@karlpinc.com>
Wed, 20 Jan 2021 23:25:24 +0000 (17:25 -0600)
src/pgwui_common/plugin.py
tests/test_plugin.py

index 2025be0fcdfa7f45e82a4689fd5f324ed1b9f973..8608c1f9eb1a5ea85eaeff950ea07be69764c738 100644 (file)
 import pkg_resources
 
 
+def get_component(module):
+    '''Return the python distribution (package name) from a module name
+    '''
+    return module.split('.')[0]
+
+
 def find_pgwui_components():
     '''Return list of all pgwui component names as strings
     '''
-    return [entry_point.resolve().__name__ for entry_point in
+    return [get_component(entry_point.resolve().__name__) for entry_point in
             pkg_resources.iter_entry_points('pgwui.components')]
 
 
@@ -39,5 +45,5 @@ def find_pgwui_check_settings():
     check_settings = dict()
     for entry_point in pkg_resources.iter_entry_points('pgwui.check_settings'):
         callable = entry_point.resolve()
-        check_settings[callable.__name__] = callable
+        check_settings[get_component(callable.__module__)] = callable
     return check_settings
index 10fdfff09de6e98be2b3b4dccdbc93a00f5bce3b..ad638af73a3a4124d52f26fcba185b9bbbba2b61 100644 (file)
 
 import pytest
 
+from pgwui_testing import testing
 from pgwui_common import plugin
 
 
 # Helper classes
 class MockEntryPoint():
     def __init__(self, val):
+        self.__module__ = val
         self.__name__ = val
 
     def resolve(self):
@@ -41,14 +43,29 @@ class MockPkgResources():
         return [MockEntryPoint(name) for name in self.entry_points]
 
 
+# get_component()
+
+@pytest.mark.unittest
+def test_get_component():
+    '''Returns the expected value
+    '''
+    result = plugin.get_component('package.module')
+    assert result == 'package'
+
+
+mock_get_component = testing.make_mock_fixture(
+    plugin, 'get_component')
+
+
 # find_pgwui_components()
 
 @pytest.mark.unittest
-def test_find_pgwui_components(monkeypatch):
+def test_find_pgwui_components(mock_get_component, monkeypatch):
     '''Returns list of entry points via iter_entry_points()
     '''
     entry_points = ['a', 'b', 'c']
 
+    mock_get_component.side_effect = lambda x: x
     monkeypatch.setattr(
         plugin, 'pkg_resources', MockPkgResources(entry_points))
 
@@ -59,11 +76,12 @@ def test_find_pgwui_components(monkeypatch):
 
 # find_pgwui_check_settings
 @pytest.mark.unittest
-def test_find_pgwui_check_settings(monkeypatch):
+def test_find_pgwui_check_settings(mock_get_component, monkeypatch):
     '''Returns a dict, keyed by name, of entry points
     '''
     entry_points = ['a', 'b', 'c']
 
+    mock_get_component.side_effect = lambda x: x
     monkeypatch.setattr(
         plugin, 'pkg_resources', MockPkgResources(entry_points))