Skip to content

Commit 78be386

Browse files
committed
fix: relative_files should keep relative path maps. #1519
1 parent ffc701a commit 78be386

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

CHANGES.rst

Lines changed: 5 additions & 0 deletions
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: when using the ``[run] relative_files = True`` setting, a relative
24+
``[paths]`` pattern was still being made absolute. This is now fixed,
25+
closing `issue 1519`_.
26+
2327
- Fix: if Python doesn't provide tomllib, then TOML configuration files can
2428
only be read if coverage.py is installed with the ``[toml]`` extra.
2529
Coverage.py will raise an error if toml support is not installed when it sees
@@ -41,6 +45,7 @@ Unreleased
4145

4246
.. _issue 1515: https://github.com/nedbat/coveragepy/issues/1515
4347
.. _issue 1516: https://github.com/nedbat/coveragepy/issues/1516
48+
.. _issue 1519: https://github.com/nedbat/coveragepy/issues/1519
4449

4550

4651
.. _changes_7-0-1:

coverage/files.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,11 @@ class PathAliases:
395395
map a path through those aliases to produce a unified path.
396396
397397
"""
398-
def __init__(self, debugfn:Optional[Callable[[str], None]]=None, relative:bool=False) -> None:
398+
def __init__(
399+
self,
400+
debugfn: Optional[Callable[[str], None]]=None,
401+
relative: bool=False,
402+
) -> None:
399403
# A list of (original_pattern, regex, result)
400404
self.aliases: List[Tuple[str, Regex, str]] = []
401405
self.debugfn = debugfn or (lambda msg: 0)
@@ -431,10 +435,11 @@ def add(self, pattern: str, result: str) -> None:
431435
if pattern.endswith("*"):
432436
raise ConfigError("Pattern must not end with wildcards.")
433437

434-
# The pattern is meant to match a filepath. Let's make it absolute
438+
# The pattern is meant to match a file path. Let's make it absolute
435439
# unless it already is, or is meant to match any prefix.
436-
if not pattern.startswith('*') and not isabs_anywhere(pattern + pattern_sep):
437-
pattern = abs_file(pattern)
440+
if not self.relative:
441+
if not pattern.startswith('*') and not isabs_anywhere(pattern + pattern_sep):
442+
pattern = abs_file(pattern)
438443
if not pattern.endswith(pattern_sep):
439444
pattern += pattern_sep
440445

tests/test_files.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,11 @@ def test_no_dotslash(self, rel_yn):
418418
# Because the map result has no slash, the actual result is os-dependent.
419419
self.assert_mapped(aliases, '/ned/home/project/src/a.py', f'src{os.sep}a.py')
420420

421+
def test_relative_pattern(self):
422+
aliases = PathAliases(relative=True)
423+
aliases.add(".tox/*/site-packages", "src")
424+
self.assert_mapped(aliases, ".tox/py314/site-packages/proj/a.py", "src/proj/a.py")
425+
421426
def test_multiple_patterns(self, rel_yn):
422427
# also test the debugfn...
423428
msgs = []

0 commit comments

Comments
 (0)