Skip to content

Commit edbb54a

Browse files
committed
fix: */foo matches "foo/x.py", to help with combining relative file names. #991
1 parent 5be6aca commit edbb54a

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

coverage/files.py

+2
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,8 @@ def fnmatches_to_regex(patterns, case_insensitive=False, partial=False):
296296
297297
"""
298298
regexes = (fnmatch.translate(pattern) for pattern in patterns)
299+
# */ at the start should also match nothing.
300+
regexes = (re.sub(r"^\(\?s:\.\*(\\\\|/)", r"(?s:^(.*\1)?", regex) for regex in regexes)
299301
# Be agnostic: / can mean backslash or slash.
300302
regexes = (re.sub(r"/", r"[\\\\/]", regex) for regex in regexes)
301303

coverage/misc.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def bool_or_none(b):
181181

182182

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

187187

tests/test_files.py

+30
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ def test_flat_rootname(original, flat):
144144
["abc/foo/hi.py", "ABC/foo/bar/hi.py", r"ABC\foo/bar/hi.py"],
145145
["abcd/foo.py", "xabc/hi.py"],
146146
),
147+
(
148+
["*/foo"], False, True,
149+
["abc/foo/hi.py", "foo/hi.py"],
150+
["abc/xfoo/hi.py"],
151+
),
152+
147153
])
148154
def test_fnmatches_to_regex(patterns, case_insensitive, partial, matches, nomatches):
149155
regex = fnmatches_to_regex(patterns, case_insensitive=case_insensitive, partial=partial)
@@ -386,6 +392,30 @@ def test_linux_on_windows(self, paths, rel_yn):
386392
"project\\module\\tests\\file.py",
387393
)
388394

395+
@pytest.mark.parametrize("paths", lin_win_paths)
396+
def test_relative_windows_on_linux(self, paths):
397+
# https://github.com/nedbat/coveragepy/issues/991
398+
aliases = PathAliases(relative=True)
399+
for path in paths:
400+
aliases.add(path, "project/module")
401+
self.assert_mapped(
402+
aliases,
403+
r"project\module\tests\file.py",
404+
r"project/module/tests/file.py",
405+
)
406+
407+
@pytest.mark.parametrize("paths", lin_win_paths)
408+
def test_relative_linux_on_windows(self, paths):
409+
# https://github.com/nedbat/coveragepy/issues/991
410+
aliases = PathAliases(relative=True)
411+
for path in paths:
412+
aliases.add(path, r"project\module")
413+
self.assert_mapped(
414+
aliases,
415+
r"project/module/tests/file.py",
416+
r"project\module\tests\file.py",
417+
)
418+
389419
def test_multiple_wildcard(self, rel_yn):
390420
aliases = PathAliases(relative=rel_yn)
391421
aliases.add('/home/jenkins/*/a/*/b/*/django', './django')

0 commit comments

Comments
 (0)