]> granicus.if.org Git - python/commitdiff
Closes #14158: improved resilience to test files left behind.
authorVinay Sajip <vinay_sajip@yahoo.co.uk>
Fri, 2 Mar 2012 01:01:13 +0000 (01:01 +0000)
committerVinay Sajip <vinay_sajip@yahoo.co.uk>
Fri, 2 Mar 2012 01:01:13 +0000 (01:01 +0000)
Lib/test/regrtest.py
Lib/test/test_base64.py
Lib/test/test_mailbox.py

index 135a90e7727a9cb7d3c1f748ae61f3c00e638868..714a116f87d5d13bc613c7cbe9280742960860ee 100755 (executable)
@@ -677,10 +677,10 @@ def main(tests=None, testdir=None, verbose=0, quiet=False,
         if bad:
             print(count(len(bad), "test"), "failed:")
             printlist(bad)
-        if environment_changed:
-            print("{} altered the execution environment:".format(
-                     count(len(environment_changed), "test")))
-            printlist(environment_changed)
+    if environment_changed:
+        print("{} altered the execution environment:".format(
+                 count(len(environment_changed), "test")))
+        printlist(environment_changed)
     if skipped and not quiet:
         print(count(len(skipped), "test"), "skipped:")
         printlist(skipped)
@@ -890,7 +890,9 @@ class saved_test_environment:
                  'logging._handlers', 'logging._handlerList',
                  'shutil.archive_formats', 'shutil.unpack_formats',
                  'sys.warnoptions', 'threading._dangling',
-                 'multiprocessing.process._dangling')
+                 'multiprocessing.process._dangling',
+                 'support.TESTFN',
+                )
 
     def get_sys_argv(self):
         return id(sys.argv), sys.argv, sys.argv[:]
@@ -1020,6 +1022,21 @@ class saved_test_environment:
         multiprocessing.process._dangling.clear()
         multiprocessing.process._dangling.update(saved)
 
+    def get_support_TESTFN(self):
+        if os.path.isfile(support.TESTFN):
+            result = 'f'
+        elif os.path.isdir(support.TESTFN):
+            result = 'd'
+        else:
+            result = None
+        return result
+    def restore_support_TESTFN(self, saved_value):
+        if saved_value is None:
+            if os.path.isfile(support.TESTFN):
+                os.unlink(support.TESTFN)
+            elif os.path.isdir(support.TESTFN):
+                shutil.rmtree(support.TESTFN)
+
     def resource_info(self):
         for name in self.resources:
             method_suffix = name.replace('.', '_')
index 93c623cc8ab2f02a36f6f3a9e7bd217bfba0f5a6..ca94504b1cd36d6c9ca785064343944c30c21873 100644 (file)
@@ -2,6 +2,7 @@ import unittest
 from test import support
 import base64
 import binascii
+import os
 import sys
 import subprocess
 
@@ -227,6 +228,10 @@ class BaseXYTestCase(unittest.TestCase):
 
 
 class TestMain(unittest.TestCase):
+    def tearDown(self):
+        if os.path.exists(support.TESTFN):
+            os.unlink(support.TESTFN)
+
     def get_output(self, *args, **options):
         args = (sys.executable, '-m', 'base64') + args
         return subprocess.check_output(args, **options)
index ef64366d5ef3e25c1774a6d16493bf48b7ff9dbe..e09aea41d165282cbf98da8dc8f18f0a2e5fda50 100644 (file)
@@ -7,6 +7,7 @@ import email
 import email.message
 import re
 import io
+import shutil
 import tempfile
 from test import support
 import unittest
@@ -38,12 +39,7 @@ class TestBase(unittest.TestCase):
     def _delete_recursively(self, target):
         # Delete a file or delete a directory recursively
         if os.path.isdir(target):
-            for path, dirs, files in os.walk(target, topdown=False):
-                for name in files:
-                    os.remove(os.path.join(path, name))
-                for name in dirs:
-                    os.rmdir(os.path.join(path, name))
-            os.rmdir(target)
+            shutil.rmtree(target)
         elif os.path.exists(target):
             os.remove(target)
 
@@ -2029,6 +2025,10 @@ class MaildirTestCase(unittest.TestCase):
     def setUp(self):
         # create a new maildir mailbox to work with:
         self._dir = support.TESTFN
+        if os.path.isdir(self._dir):
+            shutil.rmtree(self._dir)
+        elif os.path.isfile(self._dir):
+            os.unlink(self._dir)
         os.mkdir(self._dir)
         os.mkdir(os.path.join(self._dir, "cur"))
         os.mkdir(os.path.join(self._dir, "tmp"))