Rev 4874: Add a helper _ModuleFeature. in http://bazaar.launchpad.net/~jameinel/bzr/2.1.0b4-win32-test-imports

John Arbash Meinel john at arbash-meinel.com
Tue Dec 8 19:20:07 GMT 2009


At http://bazaar.launchpad.net/~jameinel/bzr/2.1.0b4-win32-test-imports

------------------------------------------------------------
revno: 4874
revision-id: john at arbash-meinel.com-20091208191951-ekhcv011dyalvnru
parent: pqm at pqm.ubuntu.com-20091207223256-9e6l1vc6h3jc2eqs
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: 2.1.0b4-win32-test-imports
timestamp: Tue 2009-12-08 13:19:51 -0600
message:
  Add a helper _ModuleFeature.
  
  It is *very* common for us to what to run a test or not based on
  whether a module can be imported. So make it a common base class
  rather than reimplementing it each time.
  
  Also, when the module is available, you can get it from the
  feature object, rather than doing your own import test.
-------------- next part --------------
=== modified file 'bzrlib/tests/__init__.py'
--- a/bzrlib/tests/__init__.py	2009-12-04 10:16:00 +0000
+++ b/bzrlib/tests/__init__.py	2009-12-08 19:19:51 +0000
@@ -4247,6 +4247,37 @@
 UnicodeFilenameFeature = _UnicodeFilenameFeature()
 
 
+class _ModuleFeature(Feature):
+    """This is a feature than describes a module we want to be available.
+
+    Declare the name of the module in __init__(), and then after probing, the
+    module will be available as 'self.module'.
+
+    :ivar module: The module if it is available, else None.
+    """
+
+    def __init__(self, module_name):
+        super(_ModuleFeature, self).__init__()
+        self.module_name = module_name
+
+    def _probe(self):
+        try:
+            self._module = __import__(self.module_name, {}, {}, [''])
+            return True
+        except ImportError:
+            return False
+
+    @property
+    def module(self):
+        if self.available(): # Make sure the probe has been done
+            return self._module
+        return None
+    
+    def feature_name(self):
+        return self.module_name
+
+
+
 def probe_unicode_in_user_encoding():
     """Try to encode several unicode strings to use in unicode-aware tests.
     Return first successfull match.

=== modified file 'bzrlib/tests/test_selftest.py'
--- a/bzrlib/tests/test_selftest.py	2009-12-04 09:09:18 +0000
+++ b/bzrlib/tests/test_selftest.py	2009-12-08 19:19:51 +0000
@@ -2497,6 +2497,22 @@
         self.assertIs(feature, exception.args[0])
 
 
+class Test_ModuleFeature(tests.TestCase):
+
+    def test_available_module(self):
+        feature = tests._ModuleFeature('bzrlib.tests')
+        self.assertEqual('bzrlib.tests', feature.module_name)
+        self.assertEqual('bzrlib.tests', str(feature))
+        self.assertTrue(feature.available())
+        self.assertIs(tests, feature.module)
+
+    def test_unavailable_module(self):
+        feature = tests._ModuleFeature('bzrlib.no_such_module_exists')
+        self.assertEqual('bzrlib.no_such_module_exists', str(feature))
+        self.assertFalse(feature.available())
+        self.assertIs(None, feature.module)
+
+
 class TestSelftestFiltering(tests.TestCase):
 
     def setUp(self):



More information about the bazaar-commits mailing list