Skip to content

Commit

Permalink
fix: */foo matches "foo/x.py", to help with combining relative file n…
Browse files Browse the repository at this point in the history
…ames. #991
  • Loading branch information
nedbat committed Oct 15, 2022
1 parent ee4f2f5 commit 822b6ac
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
2 changes: 2 additions & 0 deletions coverage/files.py
Expand Up @@ -296,6 +296,8 @@ def fnmatches_to_regex(patterns, case_insensitive=False, partial=False):
"""
regexes = (fnmatch.translate(pattern) for pattern in patterns)
# */ at the start should also match nothing.
regexes = (re.sub(r"^\(\?s:\.\*(\\\\|/)", r"(?s:^(.*\1)?", regex) for regex in regexes)
# Be agnostic: / can mean backslash or slash.
regexes = (re.sub(r"/", r"[\\\\/]", regex) for regex in regexes)

Expand Down
2 changes: 1 addition & 1 deletion coverage/misc.py
Expand Up @@ -181,7 +181,7 @@ def bool_or_none(b):


def join_regex(regexes):
"""Combine a list of regexes into one that matches any of them."""
"""Combine a series of regexes into one that matches any of them."""
return "|".join(f"(?:{r})" for r in regexes)


Expand Down
30 changes: 30 additions & 0 deletions tests/test_files.py
Expand Up @@ -144,6 +144,12 @@ def test_flat_rootname(original, flat):
["abc/foo/hi.py", "ABC/foo/bar/hi.py", r"ABC\foo/bar/hi.py"],
["abcd/foo.py", "xabc/hi.py"],
),
(
["*/foo"], False, True,
["abc/foo/hi.py", "foo/hi.py"],
["abc/xfoo/hi.py"],
),
])
def test_fnmatches_to_regex(patterns, case_insensitive, partial, matches, nomatches):
regex = fnmatches_to_regex(patterns, case_insensitive=case_insensitive, partial=partial)
Expand Down Expand Up @@ -386,6 +392,30 @@ def test_linux_on_windows(self, paths, rel_yn):
"project\\module\\tests\\file.py",
)

@pytest.mark.parametrize("paths", lin_win_paths)
def test_relative_windows_on_linux(self, paths):
# https://github.com/nedbat/coveragepy/issues/991
aliases = PathAliases(relative=True)
for path in paths:
aliases.add(path, "project/module")
self.assert_mapped(
aliases,
r"project\module\tests\file.py",
r"project/module/tests/file.py",
)

@pytest.mark.parametrize("paths", lin_win_paths)
def test_relative_linux_on_windows(self, paths):
# https://github.com/nedbat/coveragepy/issues/991
aliases = PathAliases(relative=True)
for path in paths:
aliases.add(path, r"project\module")
self.assert_mapped(
aliases,
r"project/module/tests/file.py",
r"project\module\tests\file.py",
)

def test_multiple_wildcard(self, rel_yn):
aliases = PathAliases(relative=rel_yn)
aliases.add('/home/jenkins/*/a/*/b/*/django', './django')
Expand Down

0 comments on commit 822b6ac

Please sign in to comment.