Skip to content

Commit

Permalink
Use component (rather than factory) in the serialization of utili…
Browse files Browse the repository at this point in the history
…ty registrations if the registered component is a "global object
  • Loading branch information
d-maurer committed May 17, 2023
1 parent 450f3ec commit 3793b2f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
5 changes: 4 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ Changelog
3.0.2 (unreleased)
------------------

- Nothing changed yet.
- Use ``component`` in the serialization of utility registrations
if the registered component is a "global object".
Fixes
`#6 <https://github.com/zopefoundation/Products.GenericSetup/issues/6>_`.


3.0.1 (2023-03-03)
Expand Down
2 changes: 2 additions & 0 deletions src/Products/GenericSetup/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,8 @@ def _extractUtilities(self):
# This is a five.localsitemanager wrapped utility
factory = _getDottedName(type(aq_base(comp)))
child.setAttribute('factory', factory)
elif comp is _resolveDottedName(_getDottedName(comp)):
child.setAttribute('component', _getDottedName(comp))
else:
factory = _getDottedName(type(comp))
child.setAttribute('factory', factory)
Expand Down
43 changes: 43 additions & 0 deletions src/Products/GenericSetup/tests/test_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,20 @@ def getExcludedInterfaces(self):
"""


_INTERFACE_COMPONENT = b"""\
<?xml version="1.0" encoding="utf-8"?>
<componentregistry>
<adapters/>
<subscribers/>
<utilities>
<utility name="test_interface"
component="Products.GenericSetup.tests.test_components.ITestInterface"
interface="Products.GenericSetup.tests.test_components.ITestInterfaceType"/>
</utilities>
</componentregistry>
"""


class ComponentRegistryXMLAdapterTests(BodyAdapterTestCase, unittest.TestCase):

layer = ExportImportZCMLLayer
Expand Down Expand Up @@ -434,6 +448,27 @@ def test_remove_components(self):
util = queryUtility(IDummyInterface2, name='dummy tool name2')
self.assertFalse(util is None)

def test_export_interface_component(self):
sm = self._obj
sm.registerUtility(ITestInterface,
ITestInterfaceType,
"test_interface")
context = DummySetupEnviron()
adapted = getMultiAdapter((sm, context), IBody)
body = adapted.body
self.assertEqual(body, _INTERFACE_COMPONENT)

def test_import_interface_component(self):
from ..components import importComponentRegistry
sm = self._obj
context = DummyImportContext(sm, False)
context._files['componentregistry.xml'] = _INTERFACE_COMPONENT
importComponentRegistry(context)
self.assertIs(
sm.queryUtility(ITestInterfaceType, name="test_interface"),
ITestInterface)


def setUp(self):
# Create and enable a local component registry
site = Folder()
Expand All @@ -459,6 +494,14 @@ def tearDown(self):
name='dummy')


class ITestInterface(Interface):
"""interface registered as utility."""


class ITestInterfaceType(Interface):
"""interface provided by ``ITestInterface``."""


if PersistentComponents is not None:
def test_suite():
# reimport to make sure tests are run from Products
Expand Down

0 comments on commit 3793b2f

Please sign in to comment.