[Zope3-checkins] SVN: Zope3/branches/zipimport-support/src/zope/resource/ make sure zope.resource is always tested for the case when pkg_resources

Fred L. Drake, Jr. fdrake at gmail.com
Tue Nov 8 23:10:08 EST 2005


Log message for revision 39999:
  make sure zope.resource is always tested for the case when pkg_resources
  is not available
  

Changed:
  U   Zope3/branches/zipimport-support/src/zope/resource/reference.py
  U   Zope3/branches/zipimport-support/src/zope/resource/tests.py

-=-
Modified: Zope3/branches/zipimport-support/src/zope/resource/reference.py
===================================================================
--- Zope3/branches/zipimport-support/src/zope/resource/reference.py	2005-11-09 03:51:52 UTC (rev 39998)
+++ Zope3/branches/zipimport-support/src/zope/resource/reference.py	2005-11-09 04:10:08 UTC (rev 39999)
@@ -114,10 +114,17 @@
             return self._open_packaged_resource(
                 mode, loader.get_data, filename)
 
-    if pkg_resources:
-        open = open_pkg_resources
-    else:
-        open = open_path_or_loader
+    def open(self, mode="rb"):
+        #
+        # This separate wrapper method is used so that this can always
+        # be tested for the case when pkg_resources is not available.
+        # See tests.py for how the pkg_resources global is
+        # manipulated.
+        #
+        if pkg_resources:
+            return self.open_pkg_resources(mode)
+        else:
+            return self.open_path_or_loader(mode)
 
     def _open_packaged_resource(self, mode, opener, *args):
         try:

Modified: Zope3/branches/zipimport-support/src/zope/resource/tests.py
===================================================================
--- Zope3/branches/zipimport-support/src/zope/resource/tests.py	2005-11-09 03:51:52 UTC (rev 39998)
+++ Zope3/branches/zipimport-support/src/zope/resource/tests.py	2005-11-09 04:10:08 UTC (rev 39999)
@@ -17,10 +17,18 @@
 __docformat__ = "reStructuredText"
 
 import sys
+import unittest
 
+import zope.resource.reference
+
 from zope.testing import doctest
 
+try:
+    import pkg_resources
+except ImportError:
+    pkg_resources = None
 
+
 def sys_path_setUp(self):
     self.__old_path = sys.path[:]
 
@@ -28,6 +36,27 @@
     sys.path[:] = self.__old_path
 
 
+# If the pkg_resources module is available, this setUp/tearDown pair
+# can be used to hack zope.resource to think it isn't.  This is done
+# to ensure test coverage even when the module is present.
+
+def pkg_resources_setUp(self):
+    sys_path_setUp(self)
+    self.__old_pkg_resources = pkg_resources
+    zope.resource.reference.pkg_resources = None
+
+def pkg_resources_tearDown(self):
+    sys_path_tearDown(self)
+    zope.resource.pkg_resources = self.__old_pkg_resources
+
+
 def test_suite():
-    return doctest.DocFileSuite(
-        "README.txt", setUp=sys_path_setUp, tearDown=sys_path_tearDown)
+    suite = unittest.TestSuite()
+    suite.addTest(doctest.DocFileSuite(
+        "README.txt",
+        setUp=sys_path_setUp, tearDown=sys_path_tearDown))
+    if pkg_resources is not None:
+        suite.addTest(doctest.DocFileSuite(
+            "README.txt",
+            setUp=pkg_resources_setUp, tearDown=pkg_resources_tearDown))
+    return suite



More information about the Zope3-Checkins mailing list