From 43085b9d5378ef343ac214fd15e8f7c2ceffa95f Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Thu, 27 Apr 2023 07:12:30 -0400 Subject: refactor: parametrize a test for #1608 --- CHANGES.rst | 6 ++++++ CONTRIBUTORS.txt | 1 + coverage/xmlreport.py | 6 +++--- tests/test_xml.py | 24 ++++++------------------ 4 files changed, 16 insertions(+), 21 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 5f89576f..a001a9e6 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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 ```` element when using + relative files and the source option ended with a slash (`issue 1541`_). + This is now fixed, thanks to `Kevin Brown-Silva `_. + - 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 @@ -30,6 +34,8 @@ Unreleased wildcard changes in 7.x. Thanks, `Brian Grohe `_. .. _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 diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index b02f7add..bfdca0aa 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -101,6 +101,7 @@ Julian Berman Julien Voisin Justas Sadzevičius Kassandra Keeton +Kevin Brown-Silva Kjell Braden Krystian Kichewko Kyle Altendorf diff --git a/coverage/xmlreport.py b/coverage/xmlreport.py index b2180266..82e60fc1 100644 --- a/coverage/xmlreport.py +++ b/coverage/xmlreport.py @@ -67,10 +67,10 @@ class XmlReporter: 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 diff --git a/tests/test_xml.py b/tests/test_xml.py index 0f2cff41..73105320 100644 --- a/tests/test_xml.py +++ b/tests/test_xml.py @@ -15,7 +15,7 @@ from xml.etree import ElementTree 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 @@ -476,28 +476,16 @@ class XmlPackageStructureTest(XmlTestHelpers, CoverageTest): 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"] -- cgit v1.2.1