Skip to content

Commit f668d6f

Browse files
committed
relative_files makes XML store relative paths. #948.
1 parent fa2e0e4 commit f668d6f

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

CHANGES.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ Unreleased
2626

2727
- Updated Python 3.9 support to 3.9a4.
2828

29+
- If using the ``[run] relative_files`` setting, the XML report will use
30+
relative files in the ``<source>`` elements indicating the location of source
31+
code. Closes `issue 948`_.
32+
2933
- The textual summary report could report missing lines with negative line
3034
numbers on PyPy3 7.1 (`issue 943`_). This is now fixed.
3135

@@ -39,6 +43,7 @@ Unreleased
3943
.. _issue 943: https://github.com/nedbat/coveragepy/issues/943
4044
.. _issue 944: https://github.com/nedbat/coveragepy/issues/944
4145
.. _pull request 945: https://github.com/nedbat/coveragepy/pull/945
46+
.. _issue 948: https://github.com/nedbat/coveragepy/issues/948
4247
.. _issue 949: https://github.com/nedbat/coveragepy/issues/949
4348

4449

coverage/xmlreport.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ def __init__(self, coverage):
4141
if self.config.source:
4242
for src in self.config.source:
4343
if os.path.exists(src):
44-
self.source_paths.add(files.canonical_filename(src))
44+
if not self.config.relative_files:
45+
src = files.canonical_filename(src)
46+
self.source_paths.add(src)
4547
self.packages = {}
4648
self.xml_out = None
4749

@@ -144,18 +146,18 @@ def xml_file(self, fr, analysis, has_arcs):
144146
# are populated later. Note that a package == a directory.
145147
filename = fr.filename.replace("\\", "/")
146148
for source_path in self.source_paths:
149+
source_path = files.canonical_filename(source_path)
147150
if filename.startswith(source_path.replace("\\", "/") + "/"):
148151
rel_name = filename[len(source_path)+1:]
149152
break
150153
else:
151154
rel_name = fr.relative_filename()
155+
self.source_paths.add(fr.filename[:-len(rel_name)].rstrip(r"\/"))
152156

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

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

161163
xclass = self.xml_out.createElement("class")

tests/test_xml.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,19 @@ def test_source_prefix(self):
370370
dom = ElementTree.parse("coverage.xml")
371371
self.assert_source(dom, "src")
372372

373+
def test_relative_source(self):
374+
self.make_file("src/mod.py", "print(17)")
375+
cov = coverage.Coverage(source=["src"])
376+
cov.set_option("run:relative_files", True)
377+
self.start_import_stop(cov, "mod", modfile="src/mod.py")
378+
cov.xml_report()
379+
380+
with open("coverage.xml") as x:
381+
print(x.read())
382+
dom = ElementTree.parse("coverage.xml")
383+
elts = dom.findall(".//sources/source")
384+
assert [elt.text for elt in elts] == ["src"]
385+
373386

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

0 commit comments

Comments
 (0)