Skip to content

Commit

Permalink
debug: label each matcher with its role
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Apr 11, 2021
1 parent 0285af9 commit 16f54a9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 21 deletions.
15 changes: 9 additions & 6 deletions coverage/files.py
Expand Up @@ -215,12 +215,13 @@ class TreeMatcher(object):
somewhere in a subtree rooted at one of the directories.
"""
def __init__(self, paths):
def __init__(self, paths, name):
self.original_paths = list(paths)
self.paths = list(map(os.path.normcase, paths))
self.name = name

def __repr__(self):
return "<TreeMatcher %r>" % self.paths
return "<TreeMatcher {!r} {!r}>".format(self.name, self.original_paths)

def info(self):
"""A list of strings for displaying when dumping state."""
Expand All @@ -242,11 +243,12 @@ def match(self, fpath):

class ModuleMatcher(object):
"""A matcher for modules in a tree."""
def __init__(self, module_names):
def __init__(self, module_names, name):
self.modules = list(module_names)
self.name = name

def __repr__(self):
return "<ModuleMatcher %r>" % (self.modules)
return "<ModuleMatcher {!r} {!r}>".format(self.name, self.modules)

def info(self):
"""A list of strings for displaying when dumping state."""
Expand All @@ -270,12 +272,13 @@ def match(self, module_name):

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

def __repr__(self):
return "<FnmatchMatcher %r>" % self.pats
return "<FnmatchMatcher {!r} {!r}>".format(self.name, self.pats)

def info(self):
"""A list of strings for displaying when dumping state."""
Expand Down
14 changes: 7 additions & 7 deletions coverage/inorout.py
Expand Up @@ -258,27 +258,27 @@ 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, "source")
against.append("trees {!r}".format(self.source_match))
if self.source_pkgs:
self.source_pkgs_match = ModuleMatcher(self.source_pkgs)
self.source_pkgs_match = ModuleMatcher(self.source_pkgs, "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, "coverage")
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, "pylib")
debug("Python stdlib matching: {!r}".format(self.pylib_match))
if self.include:
self.include_match = FnmatchMatcher(self.include)
self.include_match = FnmatchMatcher(self.include, "include")
debug("Include matching: {!r}".format(self.include_match))
if self.omit:
self.omit_match = FnmatchMatcher(self.omit)
self.omit_match = FnmatchMatcher(self.omit, "omit")
debug("Omit matching: {!r}".format(self.omit_match))
if self.third_paths:
self.third_match = TreeMatcher(self.third_paths)
self.third_match = TreeMatcher(self.third_paths, "third")
debug("Third-party lib matching: {!r}".format(self.third_match))

# Check if the source we want to measure has been installed as a
Expand Down
4 changes: 2 additions & 2 deletions coverage/report.py
Expand Up @@ -57,11 +57,11 @@ def get_analysis_to_report(coverage, morfs):
config = coverage.config

if config.report_include:
matcher = FnmatchMatcher(prep_patterns(config.report_include))
matcher = FnmatchMatcher(prep_patterns(config.report_include), "report_include")
file_reporters = [fr for fr in file_reporters if matcher.match(fr.filename)]

if config.report_omit:
matcher = FnmatchMatcher(prep_patterns(config.report_omit))
matcher = FnmatchMatcher(prep_patterns(config.report_omit), "report_omit")
file_reporters = [fr for fr in file_reporters if not matcher.match(fr.filename)]

if not file_reporters:
Expand Down
12 changes: 6 additions & 6 deletions tests/test_files.py
Expand Up @@ -167,7 +167,7 @@ def test_tree_matcher(self):
files.canonical_filename("sub4/file5.py"),
files.canonical_filename("SUB5/file6.py"),
]
tm = TreeMatcher(trees)
tm = TreeMatcher(trees, "test")
assert tm.info() == trees
for filepath, matches in matches_to_try:
self.assertMatches(tm, filepath, matches)
Expand All @@ -190,7 +190,7 @@ def test_module_matcher(self):
('yourmain', False),
]
modules = ['test', 'py.test', 'mymain']
mm = ModuleMatcher(modules)
mm = ModuleMatcher(modules, "test")
assert mm.info() == modules
for modulename, matches in matches_to_try:
assert mm.match(modulename) == matches, modulename
Expand All @@ -203,23 +203,23 @@ def test_fnmatch_matcher(self):
(self.make_file("sub3/file4.py"), True),
(self.make_file("sub3/file5.c"), False),
]
fnm = FnmatchMatcher(["*.py", "*/sub2/*"])
fnm = FnmatchMatcher(["*.py", "*/sub2/*"], "test")
assert fnm.info() == ["*.py", "*/sub2/*"]
for filepath, matches in matches_to_try:
self.assertMatches(fnm, filepath, matches)

def test_fnmatch_matcher_overload(self):
fnm = FnmatchMatcher(["*x%03d*.txt" % i for i in range(500)])
fnm = FnmatchMatcher(["*x%03d*.txt" % i for i in range(500)], "test")
self.assertMatches(fnm, "x007foo.txt", True)
self.assertMatches(fnm, "x123foo.txt", True)
self.assertMatches(fnm, "x798bar.txt", False)

def test_fnmatch_windows_paths(self):
# We should be able to match Windows paths even if we are running on
# a non-Windows OS.
fnm = FnmatchMatcher(["*/foo.py"])
fnm = FnmatchMatcher(["*/foo.py"], "test")
self.assertMatches(fnm, r"dir\foo.py", True)
fnm = FnmatchMatcher([r"*\foo.py"])
fnm = FnmatchMatcher([r"*\foo.py"], "test")
self.assertMatches(fnm, r"dir\foo.py", True)


Expand Down

0 comments on commit 16f54a9

Please sign in to comment.