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 df1bf08 commit 43085b9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 21 deletions.
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ development at the same time, such as 4.5.x and 5.0.
Unreleased
----------

- Fix: the XML report would have an incorrect ``<source>`` element when using
relative files and the source option ended with a slash (`issue 1541`_).
This is now fixed, thanks to `Kevin Brown-Silva <pull 1608_>`_.

- When the HTML report location is printed to the terminal, it's now a
terminal-compatible URL, so that you can click the location to open the HTML
file in your browser. Finishes `issue 1523`_ thanks to `Ricardo Newbery
Expand All @@ -30,6 +34,8 @@ Unreleased
wildcard changes in 7.x. Thanks, `Brian Grohe <pull 1610_>`_.

.. _issue 1523: https://github.com/nedbat/coveragepy/issues/1523
.. _issue 1541: https://github.com/nedbat/coveragepy/issues/1541
.. _pull 1608: https://github.com/nedbat/coveragepy/pull/1608
.. _pull 1610: https://github.com/nedbat/coveragepy/pull/1610
.. _pull 1613: https://github.com/nedbat/coveragepy/pull/1613

Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ Julian Berman
Julien Voisin
Justas Sadzevičius
Kassandra Keeton
Kevin Brown-Silva
Kjell Braden
Krystian Kichewko
Kyle Altendorf
Expand Down
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: str) -> 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 43085b9

Please sign in to comment.