Commits

pumazi  committed b0badfa

Added a test for the get_distribution function.

  • Participants
  • Parent commits 87e2cd6

Comments (0)

Files changed (2)

File src/distutils2/_backport/pkgutil.py

         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
     :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
     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):
     """

File src/distutils2/_backport/tests/test_pkgutil.py

 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."""
 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
             ('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
 
         # 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():