Skip to content

Commit

Permalink
relative_files makes XML store relative paths. #948.
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Mar 15, 2020
1 parent fa2e0e4 commit f668d6f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
Expand Up @@ -26,6 +26,10 @@ Unreleased

- Updated Python 3.9 support to 3.9a4.

- If using the ``[run] relative_files`` setting, the XML report will use
relative files in the ``<source>`` elements indicating the location of source
code. Closes `issue 948`_.

- The textual summary report could report missing lines with negative line
numbers on PyPy3 7.1 (`issue 943`_). This is now fixed.

Expand All @@ -39,6 +43,7 @@ Unreleased
.. _issue 943: https://github.com/nedbat/coveragepy/issues/943
.. _issue 944: https://github.com/nedbat/coveragepy/issues/944
.. _pull request 945: https://github.com/nedbat/coveragepy/pull/945
.. _issue 948: https://github.com/nedbat/coveragepy/issues/948
.. _issue 949: https://github.com/nedbat/coveragepy/issues/949


Expand Down
8 changes: 5 additions & 3 deletions coverage/xmlreport.py
Expand Up @@ -41,7 +41,9 @@ def __init__(self, coverage):
if self.config.source:
for src in self.config.source:
if os.path.exists(src):
self.source_paths.add(files.canonical_filename(src))
if not self.config.relative_files:
src = files.canonical_filename(src)
self.source_paths.add(src)
self.packages = {}
self.xml_out = None

Expand Down Expand Up @@ -144,18 +146,18 @@ def xml_file(self, fr, analysis, has_arcs):
# are populated later. Note that a package == a directory.
filename = fr.filename.replace("\\", "/")
for source_path in self.source_paths:
source_path = files.canonical_filename(source_path)
if filename.startswith(source_path.replace("\\", "/") + "/"):
rel_name = filename[len(source_path)+1:]
break
else:
rel_name = fr.relative_filename()
self.source_paths.add(fr.filename[:-len(rel_name)].rstrip(r"\/"))

dirname = os.path.dirname(rel_name) or u"."
dirname = "/".join(dirname.split("/")[:self.config.xml_package_depth])
package_name = dirname.replace("/", ".")

if rel_name != fr.filename:
self.source_paths.add(fr.filename[:-len(rel_name)].rstrip(r"\/"))
package = self.packages.setdefault(package_name, [{}, 0, 0, 0, 0])

xclass = self.xml_out.createElement("class")
Expand Down
13 changes: 13 additions & 0 deletions tests/test_xml.py
Expand Up @@ -370,6 +370,19 @@ def test_source_prefix(self):
dom = ElementTree.parse("coverage.xml")
self.assert_source(dom, "src")

def test_relative_source(self):
self.make_file("src/mod.py", "print(17)")
cov = coverage.Coverage(source=["src"])
cov.set_option("run:relative_files", True)
self.start_import_stop(cov, "mod", modfile="src/mod.py")
cov.xml_report()

with open("coverage.xml") as x:
print(x.read())
dom = ElementTree.parse("coverage.xml")
elts = dom.findall(".//sources/source")
assert [elt.text for elt in elts] == ["src"]


def compare_xml(expected, actual, **kwargs):
"""Specialized compare function for our XML files."""
Expand Down

0 comments on commit f668d6f

Please sign in to comment.