Commits

pumazi  committed 265757f

Test for Distribution.get_distinfo_file.

  • Participants
  • Parent commits a15b682

Comments (0)

Files changed (2)

File src/distutils2/_backport/pkgutil.py

 import os.path
 from csv import reader as csv_reader
 from types import ModuleType
+from distutils2.errors import DistutilsError
 from distutils2.metadata import DistributionMetadata
 from distutils2.version import suggest_normalized_version
 
 # PEP 376 Implementation #
 ##########################
 
+DIST_FILES = ('INSTALLER', 'METADATA', 'RECORD', 'REQUESTED',)
+
 class Distribution(object):
     """Created with the *path* of the ``.dist-info`` directory provided to the
     constructor. It reads the metadata contained in METADATA when it is
                            mode (r).
         :rtype: file object
         """
-        pass
+        open_flags = 'r'
+        if binary:
+            open_flags += 'b'
+
+        # Check if it is an absolute path
+        if path.find(os.sep) >= 0:
+            # it's an absolute path?
+            distinfo_dirname, path = path.split(os.sep)[-2:]
+            if distinfo_dirname != self.path.split(os.sep)[-1]:
+                raise DistutilsError("Requested dist-info file does not "
+                    "belong to the %s distribution. '%s' was requested." \
+                    % (self.name, os.sep.join([distinfo_dirname, path])))
+
+        # The file must be relative
+        if path not in DIST_FILES:
+            raise DistutilsError("Requested an invalid dist-info file: "
+                "%s" % path)
+
+        # Convert the relative path back to absolute
+        path = os.path.join(self.path, path)
+        return open(path, open_flags)
 
     def get_distinfo_files(self, local=False):
         """

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

         self.assertTrue(dist.uses(true_path))
         self.assertFalse(dist.uses(false_path))
 
+    def test_get_distinfo_file(self):
+        """Test the retrieval of dist-info file objects."""
+        from distutils2._backport.pkgutil import Distribution
+        distinfo_name = 'choxie-2.0.0.9'
+        other_distinfo_name = 'grammar-1.0a4'
+        distinfo_dir = os.path.join(self.fake_dists_path,
+            distinfo_name + '.dist-info')
+        dist = Distribution(distinfo_dir)
+        # Test for known good file matches
+        distinfo_files = [
+            # Relative paths
+            'INSTALLER', 'METADATA',
+            # Absolute paths
+            os.path.join(distinfo_dir, 'RECORD'),
+            os.path.join(distinfo_dir, 'REQUESTED'),
+            ]
+
+        for distfile in distinfo_files:
+            value = dist.get_distinfo_file(distfile)
+            self.assertTrue(isinstance(value, file))
+            # Is it the correct file?
+            self.assertEqual(value.name, os.path.join(distinfo_dir, distfile))
+
+        from distutils2.errors import DistutilsError
+        # Test an absolute path that is part of another distributions dist-info
+        other_distinfo_file = os.path.join(self.fake_dists_path,
+            other_distinfo_name + '.dist-info', 'REQUESTED')
+        self.assertRaises(DistutilsError, dist.get_distinfo_file,
+            other_distinfo_file)
+        # Test for a file that does not exist and should not exist
+        self.assertRaises(DistutilsError, dist.get_distinfo_file, 'ENTRYPOINTS')
+
 
 class TestPkgUtilFunctions(unittest2.TestCase):
     """Tests for the new functionality added in PEP 376."""