]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Stop trying to write into the stdlib during packaging tests (#12331).
authorÉric Araujo <merwok@netwok.org>
Sun, 31 Jul 2011 18:47:47 +0000 (20:47 +0200)
committerÉric Araujo <merwok@netwok.org>
Sun, 31 Jul 2011 18:47:47 +0000 (20:47 +0200)
This prevents tests from failing when run from a Python installed in a
read-only directory.  The code is a bit uglier; shutil.copytree calls
copystat on directories behind our back, so I had to add an os.walk
with os.chmod (*and* os.path.join!) calls.  shutil, I am disappoint.

This changeset is dedicated to the hundreds of neurons that were lost
while I was debugging this on an otherwise fine afternoon.

Lib/packaging/tests/test_database.py
Misc/NEWS

index 119fa236242be5c1c2908e3a554a35cf75e49f68..9baf3960c674f918fc56d380f7ecbc5b3d183fc9 100644 (file)
@@ -39,19 +39,39 @@ def record_pieces(file):
     return [path, digest, size]
 
 
-class CommonDistributionTests:
-    """Mixin used to test the interface common to both Distribution classes.
-
-    Derived classes define cls, sample_dist, dirs and records.  These
-    attributes are used in test methods.  See source code for details.
-    """
+class FakeDistsMixin:
 
     def setUp(self):
-        super(CommonDistributionTests, self).setUp()
+        super(FakeDistsMixin, self).setUp()
         self.addCleanup(enable_cache)
         disable_cache()
-        self.fake_dists_path = os.path.abspath(
+
+        # make a copy that we can write into for our fake installed
+        # distributions
+        tmpdir = tempfile.mkdtemp()
+        self.addCleanup(shutil.rmtree, tmpdir)
+        self.fake_dists_path = os.path.join(tmpdir, 'fake_dists')
+        fake_dists_src = os.path.abspath(
             os.path.join(os.path.dirname(__file__), 'fake_dists'))
+        shutil.copytree(fake_dists_src, self.fake_dists_path)
+        # XXX ugly workaround: revert copystat calls done by shutil behind our
+        # back (to avoid getting a read-only copy of a read-only file).  we
+        # could pass a custom copy_function to change the mode of files, but
+        # shutil gives no control over the mode of directories :(
+        for root, dirs, files in os.walk(self.fake_dists_path):
+            os.chmod(root, 0o755)
+            for f in files:
+                os.chmod(os.path.join(root, f), 0o644)
+            for d in dirs:
+                os.chmod(os.path.join(root, d), 0o755)
+
+
+class CommonDistributionTests(FakeDistsMixin):
+    """Mixin used to test the interface common to both Distribution classes.
+
+    Derived classes define cls, sample_dist, dirs and records.  These
+    attributes are used in test methods.  See source code for details.
+    """
 
     def test_instantiation(self):
         # check that useful attributes are here
@@ -110,6 +130,7 @@ class TestDistribution(CommonDistributionTests, unittest.TestCase):
 
         self.records = {}
         for distinfo_dir in self.dirs:
+
             record_file = os.path.join(distinfo_dir, 'RECORD')
             with open(record_file, 'w') as file:
                 record_writer = csv.writer(
@@ -138,12 +159,6 @@ class TestDistribution(CommonDistributionTests, unittest.TestCase):
                     record_data[path] = md5_, size
             self.records[distinfo_dir] = record_data
 
-    def tearDown(self):
-        for distinfo_dir in self.dirs:
-            record_file = os.path.join(distinfo_dir, 'RECORD')
-            open(record_file, 'wb').close()
-        super(TestDistribution, self).tearDown()
-
     def test_instantiation(self):
         super(TestDistribution, self).test_instantiation()
         self.assertIsInstance(self.dist.requested, bool)
@@ -252,20 +267,13 @@ class TestEggInfoDistribution(CommonDistributionTests,
 
 
 class TestDatabase(support.LoggingCatcher,
+                   FakeDistsMixin,
                    unittest.TestCase):
 
     def setUp(self):
         super(TestDatabase, self).setUp()
-        disable_cache()
-        # Setup the path environment with our fake distributions
-        current_path = os.path.abspath(os.path.dirname(__file__))
-        self.fake_dists_path = os.path.join(current_path, 'fake_dists')
         sys.path.insert(0, self.fake_dists_path)
-
-    def tearDown(self):
-        sys.path.remove(self.fake_dists_path)
-        enable_cache()
-        super(TestDatabase, self).tearDown()
+        self.addCleanup(sys.path.remove, self.fake_dists_path)
 
     def test_distinfo_dirname(self):
         # Given a name and a version, we expect the distinfo_dirname function
index 3f5aa290f7c693967dc233ac37e4ba9beb13ce8f..afe4894d29c9d4ab787c39df31f95356a69500b4 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -1147,6 +1147,9 @@ Extension Modules
 Tests
 -----
 
+- Issue #12331: The test suite for the packaging module can now run from an
+  installed Python.
+
 - Issue #12331: The test suite for lib2to3 can now run from an installed
   Python.