Commits

Ned Batchelder committed ea92a8c

Refactor, and make it work on older Pythons.

Comments (0)

Files changed (4)

coverage/control.py

 from coverage.files import PathAliases, find_python_files, prep_patterns
 from coverage.html import HtmlReporter
 from coverage.misc import CoverageException, bool_or_none, join_regex
+from coverage.misc import file_be_gone
 from coverage.results import Analysis, Numbers
 from coverage.summary import SummaryReporter
 from coverage.xmlreport import XmlReporter
             if file_to_close:
                 file_to_close.close()
                 if delete_file:
-                    try:
-                        os.remove(self.config.xml_output)
-                    except Exception:
-                        pass
+                    file_be_gone(self.config.xml_output)
 
     def sysinfo(self):
         """Return a list of (key, value) pairs showing internal information."""
 """Coverage data for Coverage."""
 
 import os
-import errno
 
 from coverage.backward import pickle, sorted        # pylint: disable=W0622
 from coverage.files import PathAliases
+from coverage.misc import file_be_gone
 
 
 class CoverageData(object):
         """Erase the data, both in this object, and from its file storage."""
         if self.use_file:
             if self.filename:
-                try:
-                    os.remove(self.filename)
-                except OSError as e:
-                    if e.errno != errno.ENOENT:
-                        raise
+                file_be_gone(self.filename)
         self.lines = {}
         self.arcs = {}
 
 """Miscellaneous stuff for Coverage."""
 
+import errno
 import inspect
+import os
+import sys
+
 from coverage.backward import md5, sorted       # pylint: disable=W0622
 from coverage.backward import string_class, to_bytes
 
         return ""
 
 
+def file_be_gone(path):
+    """Remove a file, and don't get annoyed if it doesn't exist."""
+    try:
+        os.remove(path)
+    except OSError:
+        _, e, _ = sys.exc_info()
+        if e.errno != errno.ENOENT:
+            raise
+
+
 class Hasher(object):
     """Hashes Python data into md5."""
     def __init__(self):

test/test_misc.py

 
 import os, sys
 
-from coverage.misc import Hasher
+from coverage.misc import Hasher, file_be_gone
 from coverage import __version__, __url__
 sys.path.insert(0, os.path.split(__file__)[0]) # Force relative import for Py3k
 from coveragetest import CoverageTest
         self.assertEqual(h1.digest(), h2.digest())
 
 
+class RemoveFileTest(CoverageTest):
+    """Tests of misc.file_be_gone."""
+
+    def test_remove_nonexistent_file(self):
+        # it's ok to try to remove a file that doesn't exist.
+        file_be_gone("not_here.txt")
+
+    def test_remove_actual_file(self):
+        # it really does remove a file that does exist.
+        self.make_file("here.txt", "We are here, we are here, we are here!")
+        file_be_gone("here.txt")
+        self.assert_doesnt_exist("here.txt")
+
+    def test_actual_errors(self):
+        # Errors can still happen.
+        # ". is a directory" on Unix, or "Access denied" on Windows
+        self.assertRaises(OSError, file_be_gone, ".")
+
+
 class SetupPyTest(CoverageTest):
     """Tests of setup.py"""