[Python-checkins] distutils2: Added a test for the get_distribution function.

tarek.ziade python-checkins at python.org
Mon Apr 5 23:09:19 CEST 2010


tarek.ziade pushed b0badfa0a9c0 to distutils2:

http://hg.python.org/distutils2/rev/b0badfa0a9c0
changeset:   98:b0badfa0a9c0
user:        pumazi
date:        Wed Mar 31 09:20:42 2010 -0400
summary:     Added a test for the get_distribution function.
files:       src/distutils2/_backport/pkgutil.py, src/distutils2/_backport/tests/test_pkgutil.py

diff --git a/src/distutils2/_backport/pkgutil.py b/src/distutils2/_backport/pkgutil.py
--- a/src/distutils2/_backport/pkgutil.py
+++ b/src/distutils2/_backport/pkgutil.py
@@ -670,6 +670,11 @@
         pass
 
 
+def _normalize_dist_name(name):
+    """Returns a normalized name from the given *name*.
+    :rtype: string"""
+    return name.replace('-', '_')
+
 def distinfo_dirname(name, version):
     """
     The *name* and *version* parameters are converted into their
@@ -689,7 +694,7 @@
     :returns: directory name
     :rtype: string"""
     file_extension = '.dist-info'
-    name = name.replace('-', '_')
+    name = _normalize_dist_name(name)
     normalized_version = suggest_normalized_version(version)
     # Because this is a lookup procedure, something will be returned even if
     #   it is a version that cannot be normalized
@@ -725,7 +730,20 @@
     value is expected. If the directory is not found, ``None`` is returned.
 
     :rtype: :class:`Distribution` or None"""
-    pass
+    name = _normalize_dist_name(name)
+    dist = None
+    for path in sys.path:
+        realpath = os.path.realpath(path)
+        if not os.path.isdir(realpath):
+            continue
+        for dir in os.listdir(realpath):
+            dir_path = os.path.join(realpath, dir)
+            if dir.startswith(name) and os.path.isdir(dir_path):
+                dist = Distribution(dir_path)
+                break
+        if dist is not None:
+            break
+    return dist
 
 def get_file_users(path):
     """
diff --git a/src/distutils2/_backport/tests/test_pkgutil.py b/src/distutils2/_backport/tests/test_pkgutil.py
--- a/src/distutils2/_backport/tests/test_pkgutil.py
+++ b/src/distutils2/_backport/tests/test_pkgutil.py
@@ -12,12 +12,6 @@
 class TestPkgUtilDistribution(unittest2.TestCase):
     """Tests the pkgutil.Distribution class"""
 
-    # def setUp(self):
-    #     super(TestPkgUtil, self).setUp()
-
-    # def tearDown(self):
-    #     super(TestPkgUtil, self).tearDown()
-
     def test_instantiation(self):
         """Test the Distribution class's instantiation provides us with usable
         attributes."""
@@ -41,11 +35,16 @@
 class TestPkgUtilFunctions(unittest2.TestCase):
     """Tests for the new functionality added in PEP 376."""
 
-    # def setUp(self):
-    #     super(TestPkgUtil, self).setUp()
+    def setUp(self):
+        super(TestPkgUtilFunctions, self).setUp()
+        # Setup the path environment with our fake distributions
+        current_path = os.path.abspath(os.path.dirname(__file__))
+        self.sys_path = sys.path[:]
+        sys.path[0:0] = [os.path.join(current_path, 'fake_dists')]
 
-    # def tearDown(self):
-    #     super(TestPkgUtil, self).tearDown()
+    def tearDown(self):
+        super(TestPkgUtilFunctions, self).tearDown()
+        sys.path[:] = self.sys_path
 
     def test_distinfo_dirname(self):
         """Given a name and a version, we expect the distinfo_dirname function
@@ -78,10 +77,6 @@
             ('towel-stuff', '0.1')]
         found_dists = []
 
-        # Setup the path environment with our fake distributions
-        current_path = os.path.abspath(os.path.dirname(__file__))
-        sys.path[0:0] = [os.path.join(current_path, 'fake_dists')]
-
         # Import the function in question
         from distutils2._backport.pkgutil import get_distributions, Distribution
 
@@ -98,6 +93,21 @@
         # Finally, test that we found all that we were looking for
         self.assertListEqual(sorted(found_dists), sorted(fake_dists))
 
+    def test_get_distribution(self):
+        """Lookup a distribution by name."""
+        # Test the lookup of the towel-stuff distribution
+        name = 'towel-stuff' # Note: This is different from the directory name
+
+        # Import the function in question
+        from distutils2._backport.pkgutil import get_distribution, Distribution
+
+        # Lookup the distribution
+        dist = get_distribution(name)
+        self.assertTrue(isinstance(dist, Distribution))
+        self.assertEqual(dist.name, name)
+
+        # Verify that an unknown distribution returns None
+        self.assertEqual(None, get_distribution('bogus'))
 
 
 def test_suite():

--
Repository URL: http://hg.python.org/distutils2


More information about the Python-checkins mailing list