Skip to content

Fix PathAliases auto-generated rules matching nested directories incorrectly - #2120

Open
dephiros wants to merge 1 commit into
coveragepy:mainfrom
dephiros:fix-issue-2072
Open

dephiros wants to merge 1 commit into
coveragepy:mainfrom
dephiros:fix-issue-2072

Conversation

@dephiros

Copy link
Copy Markdown

Summary

When using relative_files=true, the auto-generated path mapping rules used a regex pattern ^(.*[\\/])?{dir1}[\\/] that could match directory names anywhere in a path, not just from the start. This caused paths like pkg/features/templates.py to incorrectly match a rule generated for features/templates.py, resulting in lost coverage data during combine operations.

The Problem

For example, with two files:

  • features/templates.py
  • pkg/features/templates.py

The auto-generated rule for features/ would use regex ^(.*[\\/])?features[\\/], which incorrectly matches pkg/features/templates.py because (.*[\\/])? allows any prefix.

The Fix

Changed the auto-generated regex from:

^(.*[\\/])?{dir1}[\\/]  (matches anywhere)

to:

^{dir1}[\\/]            (anchored to start)

This ensures that nested directories with the same name as a top-level directory are not incorrectly collapsed during coverage combine.

Testing

  • Added a new test test_nested_directories_same_name that verifies the fix
  • All existing PathAliases tests pass (45 tests)
  • All test_files.py tests pass (205 tests)

Fixes #2072

…rrectly

When using relative_files=true, the auto-generated path mapping rules
used a regex pattern that could match paths anywhere, not just from the
start. This caused paths like "pkg/features/templates.py" to incorrectly
match a rule generated for "features/templates.py", resulting in lost
coverage data during combine operations.

The fix changes the auto-generated regex from:
  ^(.*[\\/])?{dir1}[\\/]  (matches anywhere)
to:
  ^{dir1}[\\/]            (anchored to start)

This ensures that nested directories with the same name as a top-level
directory are not incorrectly collapsed during coverage combine.

Fixes coveragepy#2072
@dephiros

Copy link
Copy Markdown
Author

@nedbat , I know you are busy but want to check in with you for a review 🙏

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

coverage combine with relative_files=true collapses nested directories with duplicate names, causing data loss

1 participant