Commits

Ned Batchelder committed 8e97e98

A failure during XML reporting would leave an empty XML file behind. #210

Comments (0)

Files changed (3)

   HTML report would fail with a UnicodeDecodeError. This issue (`issue 193`_)
   is now fixed.
 
+- If `coverage xml` fails because there is no data to report, it used to
+  create a zero-length XML file.  Now it doesn't, fixing `issue 210`_.
+
 - Running coverage under a debugger is unlikely to work, but it shouldn't fail
   with "TypeError: 'NoneType' object is not iterable".  Fixes `issue 201`_.
 
 .. _issue 201: https://bitbucket.org/ned/coveragepy/issue/201/coverage-using-django-14-with-pydb-on
 .. _issue 205: https://bitbucket.org/ned/coveragepy/issue/205/make-pydoc-coverage-more-friendly
 .. _issue 206: https://bitbucket.org/ned/coveragepy/issue/206/pydoc-coveragecoverage-fails-with-an-error
+.. _issue 210: https://bitbucket.org/ned/coveragepy/issue/210/if-theres-no-coverage-data-coverage-xml
 
 
 Version 3.5.3 --- 29 September 2012

coverage/control.py

             xml_output=outfile,
             )
         file_to_close = None
+        delete_file = False
         if self.config.xml_output:
             if self.config.xml_output == '-':
                 outfile = sys.stdout
         try:
             reporter = XmlReporter(self, self.config)
             return reporter.report(morfs, outfile=outfile)
+        except CoverageException:
+            delete_file = True
+            raise
         finally:
             if file_to_close:
                 file_to_close.close()
+                if delete_file:
+                    try:
+                        os.remove(self.config.xml_output)
+                    except Exception:
+                        pass
 
     def sysinfo(self):
         """Return a list of (key, value) pairs showing internal information."""
 class XmlReportTest(CoverageTest):
     """Tests of the XML reports from coverage.py."""
 
-    def setUp(self):
-        super(XmlReportTest, self).setUp()
+    def run_mycode(self):
+        """Run mycode.py, so we can report on it."""
         self.make_file("mycode.py", "print('hello')\n")
         self.run_command("coverage run mycode.py")
 
     def test_default_file_placement(self):
+        self.run_mycode()
         self.run_command("coverage xml")
         self.assert_exists("coverage.xml")
 
     def test_argument_affects_xml_placement(self):
+        self.run_mycode()
         self.run_command("coverage xml -o put_it_there.xml")
         self.assert_doesnt_exist("coverage.xml")
         self.assert_exists("put_it_there.xml")
 
     def test_config_affects_xml_placement(self):
+        self.run_mycode()
         self.make_file(".coveragerc", "[xml]\noutput = xml.out\n")
         self.run_command("coverage xml")
         self.assert_doesnt_exist("coverage.xml")
         self.assert_exists("xml.out")
+
+    def test_no_data(self):
+        self.run_command("coverage xml")
+        self.assert_doesnt_exist("coverage.xml")