Skip to content

Commit

Permalink
fix: relative_files should keep relative path maps. #1519
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Jan 2, 2023
1 parent 3f0bce2 commit d08e6d0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
Expand Up @@ -20,6 +20,10 @@ development at the same time, such as 4.5.x and 5.0.
Unreleased
----------

- Fix: when using the ``[run] relative_files = True`` setting, a relative
``[paths]`` pattern was still being made absolute. This is now fixed,
closing `issue 1519`_.

- Fix: if Python doesn't provide tomllib, then TOML configuration files can
only be read if coverage.py is installed with the ``[toml]`` extra.
Coverage.py will raise an error if toml support is not installed when it sees
Expand All @@ -41,6 +45,7 @@ Unreleased

.. _issue 1515: https://github.com/nedbat/coveragepy/issues/1515
.. _issue 1516: https://github.com/nedbat/coveragepy/issues/1516
.. _issue 1519: https://github.com/nedbat/coveragepy/issues/1519


.. _changes_7-0-1:
Expand Down
13 changes: 9 additions & 4 deletions coverage/files.py
Expand Up @@ -395,7 +395,11 @@ class PathAliases:
map a path through those aliases to produce a unified path.
"""
def __init__(self, debugfn:Optional[Callable[[str], None]]=None, relative:bool=False) -> None:
def __init__(
self,
debugfn: Optional[Callable[[str], None]]=None,
relative: bool=False,
) -> None:
# A list of (original_pattern, regex, result)
self.aliases: List[Tuple[str, Regex, str]] = []
self.debugfn = debugfn or (lambda msg: 0)
Expand Down Expand Up @@ -431,10 +435,11 @@ def add(self, pattern: str, result: str) -> None:
if pattern.endswith("*"):
raise ConfigError("Pattern must not end with wildcards.")

# The pattern is meant to match a filepath. Let's make it absolute
# The pattern is meant to match a file path. Let's make it absolute
# unless it already is, or is meant to match any prefix.
if not pattern.startswith('*') and not isabs_anywhere(pattern + pattern_sep):
pattern = abs_file(pattern)
if not self.relative:
if not pattern.startswith('*') and not isabs_anywhere(pattern + pattern_sep):
pattern = abs_file(pattern)
if not pattern.endswith(pattern_sep):
pattern += pattern_sep

Expand Down
14 changes: 12 additions & 2 deletions tests/test_files.py
Expand Up @@ -17,7 +17,9 @@
GlobMatcher, ModuleMatcher, PathAliases, TreeMatcher, abs_file,
actual_path, find_python_files, flat_rootname, globs_to_regex,
)

from tests.coveragetest import CoverageTest
from tests.helpers import os_sep


class FilesTest(CoverageTest):
Expand Down Expand Up @@ -415,8 +417,16 @@ def test_no_dotslash(self, rel_yn):
# The result shouldn't start with "./" if the map result didn't.
aliases = PathAliases(relative=rel_yn)
aliases.add('*/project', '.')
# Because the map result has no slash, the actual result is os-dependent.
self.assert_mapped(aliases, '/ned/home/project/src/a.py', f'src{os.sep}a.py')
self.assert_mapped(aliases, '/ned/home/project/src/a.py', os_sep('src/a.py'))

def test_relative_pattern(self):
aliases = PathAliases(relative=True)
aliases.add(".tox/*/site-packages", "src")
self.assert_mapped(
aliases,
".tox/py314/site-packages/proj/a.py",
os_sep("src/proj/a.py"),
)

def test_multiple_patterns(self, rel_yn):
# also test the debugfn...
Expand Down

0 comments on commit d08e6d0

Please sign in to comment.