Skip to content

Commit

Permalink
process path aliases in run
Browse files Browse the repository at this point in the history
  • Loading branch information
graingert committed Sep 1, 2020
1 parent ebb6b39 commit dfd952b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
15 changes: 6 additions & 9 deletions coverage/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,15 +678,12 @@ def combine(self, data_paths=None, strict=False):
self._post_init()
self.get_data()

aliases = None
if self.config.paths:
aliases = PathAliases()
for paths in self.config.paths.values():
result = paths[0]
for pattern in paths[1:]:
aliases.add(pattern, result)

combine_parallel_data(self._data, aliases=aliases, data_paths=data_paths, strict=strict)
combine_parallel_data(
self._data,
aliases=PathAliases.configure(config),
data_paths=data_paths,
strict=strict,
)

def get_data(self):
"""Get the collected data.
Expand Down
26 changes: 24 additions & 2 deletions coverage/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,8 @@ class TreeMatcher(object):
somewhere in a subtree rooted at one of the directories.
"""
def __init__(self, paths):
def __init__(self, paths, aliases=None):
self.aliases = aliases
self.paths = list(paths)

def __repr__(self):
Expand All @@ -227,6 +228,9 @@ def info(self):

def match(self, fpath):
"""Does `fpath` indicate a file in one of our trees?"""
if self.aliases is not None:
fpath = self.aliases.map(fpath)

for p in self.paths:
if fpath.startswith(p):
if fpath == p:
Expand Down Expand Up @@ -268,7 +272,8 @@ def match(self, module_name):

class FnmatchMatcher(object):
"""A matcher for files by file name pattern."""
def __init__(self, pats):
def __init__(self, pats, aliases=None):
self.aliases = aliases
self.pats = list(pats)
self.re = fnmatches_to_regex(self.pats, case_insensitive=env.WINDOWS)

Expand All @@ -281,6 +286,8 @@ def info(self):

def match(self, fpath):
"""Does `fpath` match one of our file name patterns?"""
if self.aliases is not None:
fpath = self.aliases.map(fpath)
return self.re.match(fpath) is not None


Expand Down Expand Up @@ -408,6 +415,21 @@ def map(self, path):
return path


@classmethod
def configure(cls, config):
paths = config.paths
if not paths:
return None

aliases = PathAliases()
for paths in paths.values():
result = paths[0]
for pattern in paths[1:]:
aliases.add(pattern, result)
return aliases



def find_python_files(dirname):
"""Yield all of the importable Python files in `dirname`, recursively.
Expand Down
13 changes: 7 additions & 6 deletions coverage/inorout.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from coverage.backward import code_object
from coverage.disposition import FileDisposition, disposition_init
from coverage.files import TreeMatcher, FnmatchMatcher, ModuleMatcher
from coverage.files import prep_patterns, find_python_files, canonical_filename
from coverage.files import prep_patterns, find_python_files, canonical_filename, PathAliases
from coverage.misc import CoverageException
from coverage.python import source_for_file, source_for_morf

Expand Down Expand Up @@ -132,6 +132,7 @@ def __init__(self, warn, debug):

def configure(self, config):
"""Apply the configuration to get ready for decision-time."""
aliases = PathAliases.configure(config)
for src in config.source or []:
if os.path.isdir(src):
self.source.append(canonical_filename(src))
Expand Down Expand Up @@ -186,24 +187,24 @@ def debug(msg):
if self.source or self.source_pkgs:
against = []
if self.source:
self.source_match = TreeMatcher(self.source)
self.source_match = TreeMatcher(self.source, aliases=aliases)
against.append("trees {!r}".format(self.source_match))
if self.source_pkgs:
self.source_pkgs_match = ModuleMatcher(self.source_pkgs)
against.append("modules {!r}".format(self.source_pkgs_match))
debug("Source matching against " + " and ".join(against))
else:
if self.cover_paths:
self.cover_match = TreeMatcher(self.cover_paths)
self.cover_match = TreeMatcher(self.cover_paths, aliases=aliases)
debug("Coverage code matching: {!r}".format(self.cover_match))
if self.pylib_paths:
self.pylib_match = TreeMatcher(self.pylib_paths)
self.pylib_match = TreeMatcher(self.pylib_paths, aliases=aliases)
debug("Python stdlib matching: {!r}".format(self.pylib_match))
if self.include:
self.include_match = FnmatchMatcher(self.include)
self.include_match = FnmatchMatcher(self.include, aliases=aliases)
debug("Include matching: {!r}".format(self.include_match))
if self.omit:
self.omit_match = FnmatchMatcher(self.omit)
self.omit_match = FnmatchMatcher(self.omit, alieases=alieases)
debug("Omit matching: {!r}".format(self.omit_match))

def should_trace(self, filename, frame=None):
Expand Down

0 comments on commit dfd952b

Please sign in to comment.