Skip to content

Commit df1bf08

Browse files
authoredApr 27, 2023
fix: source paths with trailing slashes causing inconsistent sources in XML report with relative_paths (#1608)
* Added failing test for source with trailing slash This test is nearly identical to the one above it, with the only change being the source that is used. This may end up turning into a fixture instead if the tests end up being identical after the fix is made. * Strip trailing slash for relative source paths This fixes an issue introduced in 45cf793 where using `relative_files=True` and `src` with a trailing slash, the source inserted as `<sources>` in the XML report would also have a trailing slash. This also fixes an issue introduced in the same commit where an empty `<source>` would be inserted as well for cases where the `src` has a trailing slash.
1 parent 74b1758 commit df1bf08

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed
 

‎coverage/xmlreport.py

+2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ def __init__(self, coverage: Coverage) -> None:
6969
if os.path.exists(src):
7070
if not self.config.relative_files:
7171
src = files.canonical_filename(src)
72+
else:
73+
src = src.rstrip(r"\/")
7274
self.source_paths.add(src)
7375
self.packages: Dict[str, PackageData] = {}
7476
self.xml_out: xml.dom.minidom.Document

‎tests/test_xml.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ def test_accented_directory(self) -> None:
320320

321321
def test_no_duplicate_packages(self) -> None:
322322
self.make_file(
323-
"namespace/package/__init__.py",
323+
"namespace/package/__init__.py",
324324
"from . import sample; from . import test; from .subpackage import test"
325325
)
326326
self.make_file("namespace/package/sample.py", "print('package.sample')")
@@ -489,6 +489,19 @@ def test_relative_source(self) -> None:
489489
elts = dom.findall(".//sources/source")
490490
assert [elt.text for elt in elts] == ["src"]
491491

492+
def test_relative_source_trailing_slash(self) -> None:
493+
self.make_file("src/mod.py", "print(17)")
494+
cov = coverage.Coverage(source=["src/"])
495+
cov.set_option("run:relative_files", True)
496+
self.start_import_stop(cov, "mod", modfile="src/mod.py")
497+
cov.xml_report()
498+
499+
with open("coverage.xml") as x:
500+
print(x.read())
501+
dom = ElementTree.parse("coverage.xml")
502+
elts = dom.findall(".//sources/source")
503+
assert [elt.text for elt in elts] == ["src"]
504+
492505

493506
def compare_xml(expected: str, actual: str, actual_extra: bool = False) -> None:
494507
"""Specialized compare function for our XML files."""

0 commit comments

Comments
 (0)
Please sign in to comment.