[Zope3-checkins] SVN: Zope3/trunk/src/zope/component/ Test and fix for http://www.zope.org/Collectors/Zope3-dev/674

Gary Poster gary at zope.com
Thu Aug 3 22:35:14 EDT 2006


Log message for revision 69345:
  Test and fix for http://www.zope.org/Collectors/Zope3-dev/674
  

Changed:
  U   Zope3/trunk/src/zope/component/globalregistry.py
  A   Zope3/trunk/src/zope/component/standalonetests.py
  U   Zope3/trunk/src/zope/component/tests.py

-=-
Modified: Zope3/trunk/src/zope/component/globalregistry.py
===================================================================
--- Zope3/trunk/src/zope/component/globalregistry.py	2006-08-03 21:27:52 UTC (rev 69344)
+++ Zope3/trunk/src/zope/component/globalregistry.py	2006-08-04 02:35:12 UTC (rev 69345)
@@ -169,3 +169,12 @@
 
 def provideHandler(factory, adapts=None):
     base.registerHandler(factory, adapts, event=False)
+
+import zope.component._api # see http://www.zope.org/Collectors/Zope3-dev/674
+# Ideally, we will switch to an explicit adapter hook registration.  For now,
+# if you provide an adapter, we want to make sure that the adapter hook is
+# registered, and that registration depends on code in _api, which itself
+# depends on code in this module.  So, for now, we do another of these nasty
+# circular import workarounds.  See also standalonetests.py, as run by
+# tests.py in StandaloneTests, for a test that fails without this hack, and
+# succeeds with it.

Added: Zope3/trunk/src/zope/component/standalonetests.py
===================================================================
--- Zope3/trunk/src/zope/component/standalonetests.py	2006-08-03 21:27:52 UTC (rev 69344)
+++ Zope3/trunk/src/zope/component/standalonetests.py	2006-08-04 02:35:12 UTC (rev 69345)
@@ -0,0 +1,52 @@
+import unittest
+import doctest
+import sys
+
+if __name__ == "__main__":
+    paths = sys.argv.pop().split(':')
+    sys.path.extend(paths)
+
+from zope import interface
+from zope.component.testing import setUp, tearDown
+
+class I1(interface.Interface):
+    pass
+
+class I2(interface.Interface):
+    pass
+
+class Ob(object):
+    interface.implements(I1)
+    def __repr__(self):
+        return '<instance Ob>'
+
+ob = Ob()
+
+class Comp(object):
+    interface.implements(I2)
+    def __init__(self, context):
+        self.context = context
+
+def providing_adapter_sets_adapter_hook():
+    """
+    A side effect of importing installs the adapter hook.  See
+    http://www.zope.org/Collectors/Zope3-dev/674.
+
+      >>> import zope.component
+      >>> zope.component.provideAdapter(Comp, (I1,), I2)
+      >>> res = 0 #zope.component.getAdapter
+      >>> adapter = I2(ob)
+      >>> adapter.__class__ is Comp
+      True
+      >>> adapter.context is ob
+      True
+    """
+
+
+def test_suite():
+    return unittest.TestSuite((
+        doctest.DocTestSuite(setUp=setUp, tearDown=tearDown),
+        ))
+
+if __name__ == "__main__":
+    unittest.main(defaultTest='test_suite')


Property changes on: Zope3/trunk/src/zope/component/standalonetests.py
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: Zope3/trunk/src/zope/component/tests.py
===================================================================
--- Zope3/trunk/src/zope/component/tests.py	2006-08-03 21:27:52 UTC (rev 69344)
+++ Zope3/trunk/src/zope/component/tests.py	2006-08-04 02:35:12 UTC (rev 69345)
@@ -925,8 +925,26 @@
 
     """
 
+class StandaloneTests(unittest.TestCase):
+    def testStandalone(self):
+        import subprocess
+        import sys
+        import os
+        import StringIO
+        import tempfile
+        executable = os.path.abspath(sys.executable)
+        program = os.path.join(os.path.dirname(__file__), 'standalonetests.py')
+        paths = ':'.join(sys.path)
+        command = "%(executable)s %(program)s %(paths)s" % {
+            'executable': executable, 'program': program, 'paths': paths}
+        t = tempfile.TemporaryFile()
+        res = subprocess.call([executable, program, paths], stdout=t, stderr=t)
+        t.seek(0)
+        lines = t.readlines()
+        t.close()
+        if lines[-1][-3:-1] != 'OK':
+            self.fail(''.join(lines))
 
-
 def tearDownRegistryTests(tests):
     import zope.event
     zope.event.subscribers.pop()
@@ -959,6 +977,7 @@
                              setUp=setUp, tearDown=tearDown),
         doctest.DocFileSuite('zcml.txt',
                              setUp=setUp, tearDown=tearDown),
+        unittest.makeSuite(StandaloneTests),
         ))
 
 if __name__ == "__main__":



More information about the Zope3-Checkins mailing list