Skip to content

Commit d08e6d0

Browse files
committed
fix: relative_files should keep relative path maps. #1519
1 parent 3f0bce2 commit d08e6d0

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

CHANGES.rst

+5
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

+9-4
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

+12-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
GlobMatcher, ModuleMatcher, PathAliases, TreeMatcher, abs_file,
1818
actual_path, find_python_files, flat_rootname, globs_to_regex,
1919
)
20+
2021
from tests.coveragetest import CoverageTest
22+
from tests.helpers import os_sep
2123

2224

2325
class FilesTest(CoverageTest):
@@ -415,8 +417,16 @@ def test_no_dotslash(self, rel_yn):
415417
# The result shouldn't start with "./" if the map result didn't.
416418
aliases = PathAliases(relative=rel_yn)
417419
aliases.add('*/project', '.')
418-
# Because the map result has no slash, the actual result is os-dependent.
419-
self.assert_mapped(aliases, '/ned/home/project/src/a.py', f'src{os.sep}a.py')
420+
self.assert_mapped(aliases, '/ned/home/project/src/a.py', os_sep('src/a.py'))
421+
422+
def test_relative_pattern(self):
423+
aliases = PathAliases(relative=True)
424+
aliases.add(".tox/*/site-packages", "src")
425+
self.assert_mapped(
426+
aliases,
427+
".tox/py314/site-packages/proj/a.py",
428+
os_sep("src/proj/a.py"),
429+
)
420430

421431
def test_multiple_patterns(self, rel_yn):
422432
# also test the debugfn...

0 commit comments

Comments
 (0)