summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2017-05-05 07:58:26 -0400
committerNed Batchelder <ned@nedbatchelder.com>2017-05-05 07:58:26 -0400
commit80cf759b1504fc75e1135cc15a69a827a2439a1f (patch)
tree98a2de38c0450223a8bd8df19db676b3ebfa5143
parent05ae0472830008dad1bd2ee53e4b55338bdc929f (diff)
downloadpython-coveragepy-git-80cf759b1504fc75e1135cc15a69a827a2439a1f.tar.gz
Make sure xml reports specify valid source filenames. #526
-rw-r--r--CHANGES.rst5
-rw-r--r--coverage/xmlreport.py2
-rw-r--r--tests/test_xml.py15
3 files changed, 15 insertions, 7 deletions
diff --git a/CHANGES.rst b/CHANGES.rst
index 0cf9bff7..f3f88f03 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -14,6 +14,10 @@ Unreleased
instead of the correct ``pkg/__init__.py``. This is now fixed. Thanks, Dirk
Thomas.
+- XML reports could produce ``<source>`` and ``<class>`` lines that together
+ didn't specify a valid source file path, as described in `issue 526`_. This
+ is now fixed.
+
- Namespace packages are no longer warned as having no code, as described in
`issue 572`_.
@@ -28,6 +32,7 @@ Unreleased
- Running ``coverage xml`` in a directory named with non-ASCII characters would
fail under Python 2, as reported in `issue 573`_. This is now fixed.
+.. _issue 526: https://bitbucket.org/ned/coveragepy/issues/526/generated-xml-invalid-paths-for-cobertura
.. _issue 572: https://bitbucket.org/ned/coveragepy/issues/572/no-python-source-warning-for-namespace
.. _issue 573: https://bitbucket.org/ned/coveragepy/issues/573/cant-generate-xml-report-if-some-source
.. _issue 575: https://bitbucket.org/ned/coveragepy/issues/575/running-doctest-prevents-complete-coverage
diff --git a/coverage/xmlreport.py b/coverage/xmlreport.py
index b5a33ddc..3b651d46 100644
--- a/coverage/xmlreport.py
+++ b/coverage/xmlreport.py
@@ -166,7 +166,7 @@ class XmlReporter(Reporter):
xclass.appendChild(xlines)
xclass.setAttribute("name", os.path.relpath(rel_name, dirname))
- xclass.setAttribute("filename", fr.relative_filename().replace("\\", "/"))
+ xclass.setAttribute("filename", rel_name.replace("\\", "/"))
xclass.setAttribute("complexity", "0")
branch_stats = analysis.branch_stats()
diff --git a/tests/test_xml.py b/tests/test_xml.py
index c3493e7b..b49debc9 100644
--- a/tests/test_xml.py
+++ b/tests/test_xml.py
@@ -60,6 +60,11 @@ class XmlTestHelpers(CoverageTest):
filename = here("f{0}.py".format(i))
self.make_file(filename, "# {0}\n".format(filename))
+ def assert_source(self, xml, src):
+ """Assert that the XML has a <source> element with `src`."""
+ src = abs_file(src)
+ self.assertRegex(xml, r'<source>\s*{0}\s*</source>'.format(re.escape(src)))
+
class XmlReportTest(XmlTestHelpers, CoverageTest):
"""Tests of the XML reports from coverage.py."""
@@ -146,11 +151,6 @@ class XmlReportTest(XmlTestHelpers, CoverageTest):
init_line = re_line(xml, 'filename="sub/__init__.py"')
self.assertIn('line-rate="1"', init_line)
- def assert_source(self, xml, src):
- """Assert that the XML has a <source> element with `src`."""
- src = abs_file(src)
- self.assertRegex(xml, r'<source>\s*{0}\s*</source>'.format(re.escape(src)))
-
def test_curdir_source(self):
# With no source= option, the XML report should explain that the source
# is in the current directory.
@@ -284,13 +284,16 @@ class XmlPackageStructureTest(XmlTestHelpers, CoverageTest):
def test_source_prefix(self):
# https://bitbucket.org/ned/coveragepy/issues/465
+ # https://bitbucket.org/ned/coveragepy/issues/526/generated-xml-invalid-paths-for-cobertura
self.make_file("src/mod.py", "print(17)")
cov = coverage.Coverage(source=["src"])
self.start_import_stop(cov, "mod", modfile="src/mod.py")
self.assert_package_and_class_tags(cov, """\
<package name=".">
- <class filename="src/mod.py" name="mod.py">
+ <class filename="mod.py" name="mod.py">
""")
+ xml = self.stdout()
+ self.assert_source(xml, "src")
def clean(text, scrub=None):