Skip to content

XML output incorrect when relative_files is True and source paths have a trailing slash #1541

Closed
@KennyChenBasis

Description

@KennyChenBasis

Describe the bug
When relative_files is True and source paths have a trailing slash, the XML output will have an extra source that's empty, and filenames will include the correct source's path (filenames won't be relative to the correct source). These incorrect filenames cause problems for downstream processes like SonarQube.

To Reproduce

  1. Python 3.8
  2. coverage.py >=7.0.0
  3. Packages:
# Editable install with no version control (add==0.0.0)
-e /home/user/test-coverage/add
attrs==22.2.0
coverage==7.0.5
exceptiongroup==1.1.0
iniconfig==2.0.0
packaging==23.0
pluggy==1.0.0
pytest==7.2.1
pytest-cov==4.0.0
tomli==2.0.1
  1. Example .coveragerc:
[run]
relative_files = True
source = add/src/
  1. pytest --cov --cov-config=.coveragerc --cov-report=xml

Let me know if you need more info for part 4.

Expected behavior
No extra empty source, and correct filenames, like the XML output from 6.5.0.

Additional context

XML output when source has trailing slash
<?xml version="1.0" ?>
<coverage version="7.0.5" timestamp="1674512790155" lines-valid="2" lines-covered="2" line-rate="1" branches-covered="0" branches-valid="0" branch-rate="0" complexity="0">
	<!-- Generated by coverage.py: https://coverage.readthedocs.io -->
	<!-- Based on https://raw.githubusercontent.com/cobertura/web/master/htdocs/xml/coverage-04.dtd -->
	<sources>
		<source></source>
		<source>add/src/</source>
	</sources>
	<packages>
		<package name="add.src.add" line-rate="1" branch-rate="0" complexity="0">
			<classes>
				<class name="add.py" filename="add/src/add/add.py" complexity="0" line-rate="1" branch-rate="0">
					<methods/>
					<lines>
						<line number="1" hits="1"/>
						<line number="2" hits="1"/>
					</lines>
				</class>
			</classes>
		</package>
	</packages>
</coverage>
XML output when source has no trailing slash
<?xml version="1.0" ?>
<coverage version="7.0.5" timestamp="1674512868920" lines-valid="2" lines-covered="2" line-rate="1" branches-covered="0" branches-valid="0" branch-rate="0" complexity="0">
	<!-- Generated by coverage.py: https://coverage.readthedocs.io -->
	<!-- Based on https://raw.githubusercontent.com/cobertura/web/master/htdocs/xml/coverage-04.dtd -->
	<sources>
		<source>add/src</source>
	</sources>
	<packages>
		<package name="add" line-rate="1" branch-rate="0" complexity="0">
			<classes>
				<class name="add.py" filename="add/add.py" complexity="0" line-rate="1" branch-rate="0">
					<methods/>
					<lines>
						<line number="1" hits="1"/>
						<line number="2" hits="1"/>
					</lines>
				</class>
			</classes>
		</package>
	</packages>
</coverage>

I expect that it was introduced in this commit 45cf793. I suspect that before 7.0.0, the source paths were unconditionally "canonicalized", which removes a trailing slash, etc. When matching against filenames, a slash can be added with no problem. However, the linked commit introduced a change to not canonicalize when relative_files is True. If the source path has a trailing slash, then when an additional slash is added, the matches will fail.

I assume this is a bug and not an intended change?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggood firstThis could be a good first contributionxml

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions