Commits

Ronald Oussoren committed 383513c

slight code cleanup, raises bridgesupport coverage to 100%

Comments (0)

Files changed (2)

pyobjc-core/Lib/objc/_bridgesupport.py

             for p in objc.protocolsForProcess():
                 setattr(globals['protocols'], p.__name__, p)
 
+
+def safe_resource_exists(package, resource):
+    try:
+        return  pkg_resources.resource_exists(package, resource)
+    except ImportError:
+        # resource_exists raises ImportError when it cannot find 
+        # the first argument.
+        return False
+
 def initFrameworkWrapper(frameworkName,
         frameworkPath, frameworkIdentifier, globals, inlineTab=None, 
         scan_classes=None, frameworkResourceName=None):
     # Look for metadata in the Python wrapper and prefer that over the
     # data in the framework or in system locations. 
     # Needed because the system bridgesupport files are buggy.
-    try:
-        exists = pkg_resources.resource_exists(
-                frameworkResourceName, "PyObjC.bridgesupport")
-   
-    except ImportError:
-        # resource_exists raises ImportError when it cannot find 
-        # the first argument.
-        exists = False
-
-    if exists:
+    if safe_resource_exists(frameworkResourceName, "PyObjC.bridgesupport"):
         data = pkg_resources.resource_string(frameworkResourceName,
             "PyObjC.bridgesupport")
         if data:
             _parseBridgeSupport(data, globals, frameworkName)
 
         # Check if we have additional metadata bundled with PyObjC
-        try:
-            exists = pkg_resources.resource_exists(
-                frameworkResourceName, "PyObjCOverrides.bridgesupport")
-        except ImportError:
-            # resource_exists raises ImportError when it cannot find 
-            # the first argument.
-            exists = False
-    
-        if exists:
+        if safe_resource_exists(frameworkResourceName, "PyObjCOverrides.bridgesupport"):
             data = pkg_resources.resource_string(frameworkResourceName,
                 "PyObjCOverrides.bridgesupport")
             if data:
                 _parseBridgeSupport(data, globals, frameworkName)
             
             # Check if we have additional metadata bundled with PyObjC
-            try:
-                exists = pkg_resources.resource_exists(
-                    frameworkResourceName, "PyObjCOverrides.bridgesupport")
-            except ImportError:
-                # resource_exists raises ImportError when it cannot find 
-                # the first argument.
-                exists = False
-
-            if exists:
+            if safe_resource_exists(frameworkResourceName, "PyObjCOverrides.bridgesupport"):
                 data = pkg_resources.resource_string(frameworkResourceName,
                     "PyObjCOverrides.bridgesupport")
                 if data:

pyobjc-core/PyObjCTest/test_bridgesupport.py

 
             # XXX: The following path's aren't properly tested at the moment:
             # 8. Use the 'frameworkResourceName' parameter
-            # 11. pkg_resources.resource_exists raises ImportError
+
+
+    def test_safe_resource_exists(self):
+        with Patcher() as p:
+            return_value = False
+            exception  = None
+
+            def resource_exists(resource, path):
+                if exception: raise exception
+                return return_value
+            p.patch("pkg_resources.resource_exists", resource_exists)
+
+            return_value = False
+            exception  = None
+            self.assertEquals(bridgesupport.safe_resource_exists("a", "b"), False)
+
+            return_value = True
+            exception  = None
+            self.assertEquals(bridgesupport.safe_resource_exists("a", "b"), True)
+
+            return_value = True
+            exception  = ImportError
+            self.assertRaises(ImportError, bridgesupport.safe_resource_exists, "a", "b")
 
 
     def test_real_loader(self):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.