Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Source paths with trailing slashes causing inconsistent sources in XML report with relative_paths #1608

Merged
merged 2 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions coverage/xmlreport.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ def __init__(self, coverage: Coverage) -> None:
if os.path.exists(src):
if not self.config.relative_files:
src = files.canonical_filename(src)
else:
src = src.rstrip(r"\/")
self.source_paths.add(src)
self.packages: Dict[str, PackageData] = {}
self.xml_out: xml.dom.minidom.Document
Expand Down
15 changes: 14 additions & 1 deletion tests/test_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ def test_accented_directory(self) -> None:

def test_no_duplicate_packages(self) -> None:
self.make_file(
"namespace/package/__init__.py",
"namespace/package/__init__.py",
"from . import sample; from . import test; from .subpackage import test"
)
self.make_file("namespace/package/sample.py", "print('package.sample')")
Expand Down Expand Up @@ -489,6 +489,19 @@ def test_relative_source(self) -> None:
elts = dom.findall(".//sources/source")
assert [elt.text for elt in elts] == ["src"]

def test_relative_source_trailing_slash(self) -> None:
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: str, actual: str, actual_extra: bool = False) -> None:
"""Specialized compare function for our XML files."""
Expand Down