Skip to content

Commit

Permalink
refactor: parametrize a test for #1608
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Apr 27, 2023
1 parent 4104428 commit 84dc972
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 21 deletions.
6 changes: 3 additions & 3 deletions coverage/xmlreport.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ def __init__(self, coverage: Coverage) -> None:
if self.config.source:
for src in self.config.source:
if os.path.exists(src):
if not self.config.relative_files:
src = files.canonical_filename(src)
else:
if self.config.relative_files:
src = src.rstrip(r"\/")
else:
src = files.canonical_filename(src)
self.source_paths.add(src)
self.packages: Dict[str, PackageData] = {}
self.xml_out: xml.dom.minidom.Document
Expand Down
24 changes: 6 additions & 18 deletions tests/test_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import pytest

import coverage
from coverage import Coverage
from coverage import Coverage, env
from coverage.exceptions import NoDataError
from coverage.files import abs_file
from coverage.misc import import_local_file
Expand Down Expand Up @@ -476,28 +476,16 @@ def test_source_prefix(self) -> None:
dom = ElementTree.parse("coverage.xml")
self.assert_source(dom, "src")

def test_relative_source(self) -> None:
@pytest.mark.parametrize("trail", ["", "/", "\\"])
def test_relative_source(self, trail) -> None:
if trail == "\\" and not env.WINDOWS:
pytest.skip("trailing backslash is only for Windows")
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 test_relative_source_trailing_slash(self) -> None:
self.make_file("src/mod.py", "print(17)")
cov = coverage.Coverage(source=["src/"])
cov = coverage.Coverage(source=[f"src{trail}"])
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"]
Expand Down

0 comments on commit 84dc972

Please sign in to comment.