Skip to content

Commit 43085b9

Browse files
committedApr 27, 2023
refactor: parametrize a test for #1608
1 parent df1bf08 commit 43085b9

File tree

4 files changed

+16
-21
lines changed

4 files changed

+16
-21
lines changed
 

‎CHANGES.rst

+6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ development at the same time, such as 4.5.x and 5.0.
2020
Unreleased
2121
----------
2222

23+
- Fix: the XML report would have an incorrect ``<source>`` element when using
24+
relative files and the source option ended with a slash (`issue 1541`_).
25+
This is now fixed, thanks to `Kevin Brown-Silva <pull 1608_>`_.
26+
2327
- When the HTML report location is printed to the terminal, it's now a
2428
terminal-compatible URL, so that you can click the location to open the HTML
2529
file in your browser. Finishes `issue 1523`_ thanks to `Ricardo Newbery
@@ -30,6 +34,8 @@ Unreleased
3034
wildcard changes in 7.x. Thanks, `Brian Grohe <pull 1610_>`_.
3135

3236
.. _issue 1523: https://github.com/nedbat/coveragepy/issues/1523
37+
.. _issue 1541: https://github.com/nedbat/coveragepy/issues/1541
38+
.. _pull 1608: https://github.com/nedbat/coveragepy/pull/1608
3339
.. _pull 1610: https://github.com/nedbat/coveragepy/pull/1610
3440
.. _pull 1613: https://github.com/nedbat/coveragepy/pull/1613
3541

‎CONTRIBUTORS.txt

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ Julian Berman
101101
Julien Voisin
102102
Justas Sadzevičius
103103
Kassandra Keeton
104+
Kevin Brown-Silva
104105
Kjell Braden
105106
Krystian Kichewko
106107
Kyle Altendorf

‎coverage/xmlreport.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ def __init__(self, coverage: Coverage) -> None:
6767
if self.config.source:
6868
for src in self.config.source:
6969
if os.path.exists(src):
70-
if not self.config.relative_files:
71-
src = files.canonical_filename(src)
72-
else:
70+
if self.config.relative_files:
7371
src = src.rstrip(r"\/")
72+
else:
73+
src = files.canonical_filename(src)
7474
self.source_paths.add(src)
7575
self.packages: Dict[str, PackageData] = {}
7676
self.xml_out: xml.dom.minidom.Document

‎tests/test_xml.py

+6-18
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import pytest
1616

1717
import coverage
18-
from coverage import Coverage
18+
from coverage import Coverage, env
1919
from coverage.exceptions import NoDataError
2020
from coverage.files import abs_file
2121
from coverage.misc import import_local_file
@@ -476,28 +476,16 @@ def test_source_prefix(self) -> None:
476476
dom = ElementTree.parse("coverage.xml")
477477
self.assert_source(dom, "src")
478478

479-
def test_relative_source(self) -> None:
479+
@pytest.mark.parametrize("trail", ["", "/", "\\"])
480+
def test_relative_source(self, trail: str) -> None:
481+
if trail == "\\" and not env.WINDOWS:
482+
pytest.skip("trailing backslash is only for Windows")
480483
self.make_file("src/mod.py", "print(17)")
481-
cov = coverage.Coverage(source=["src"])
482-
cov.set_option("run:relative_files", True)
483-
self.start_import_stop(cov, "mod", modfile="src/mod.py")
484-
cov.xml_report()
485-
486-
with open("coverage.xml") as x:
487-
print(x.read())
488-
dom = ElementTree.parse("coverage.xml")
489-
elts = dom.findall(".//sources/source")
490-
assert [elt.text for elt in elts] == ["src"]
491-
492-
def test_relative_source_trailing_slash(self) -> None:
493-
self.make_file("src/mod.py", "print(17)")
494-
cov = coverage.Coverage(source=["src/"])
484+
cov = coverage.Coverage(source=[f"src{trail}"])
495485
cov.set_option("run:relative_files", True)
496486
self.start_import_stop(cov, "mod", modfile="src/mod.py")
497487
cov.xml_report()
498488

499-
with open("coverage.xml") as x:
500-
print(x.read())
501489
dom = ElementTree.parse("coverage.xml")
502490
elts = dom.findall(".//sources/source")
503491
assert [elt.text for elt in elts] == ["src"]

0 commit comments

Comments
 (0)
Please sign in to comment.