summaryrefslogtreecommitdiff
path: root/coverage/files.py
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2022-11-28 10:12:56 -0500
committerNed Batchelder <ned@nedbatchelder.com>2022-11-29 20:08:48 -0500
commit7e7c44b4f5c484105559690b1efccd84839bc640 (patch)
treeaa91b46a3a986ba9ee1b6375e13e1c780f851714 /coverage/files.py
parente955f106134029e2b8991a3ad1299377b73a0e55 (diff)
downloadpython-coveragepy-git-7e7c44b4f5c484105559690b1efccd84839bc640.tar.gz
feat: file paths are only remapped if the result exists
Diffstat (limited to 'coverage/files.py')
-rw-r--r--coverage/files.py9
1 files changed, 7 insertions, 2 deletions
diff --git a/coverage/files.py b/coverage/files.py
index f016a32e..14d696b6 100644
--- a/coverage/files.py
+++ b/coverage/files.py
@@ -408,7 +408,7 @@ class PathAliases:
result = result.rstrip(r"\/") + result_sep
self.aliases.append((original_pattern, regex, result))
- def map(self, path):
+ def map(self, path, exists=os.path.exists):
"""Map `path` through the aliases.
`path` is checked against all of the patterns. The first pattern to
@@ -419,6 +419,9 @@ class PathAliases:
The separator style in the result is made to match that of the result
in the alias.
+ `exists` is a function to determine if the resulting path actually
+ exists.
+
Returns the mapped path. If a mapping has happened, this is a
canonical path. If no mapping has happened, it is the original value
of `path` unchanged.
@@ -438,6 +441,8 @@ class PathAliases:
dot_start = result.startswith(("./", ".\\")) and len(result) > 2
if new.startswith(("./", ".\\")) and not dot_start:
new = new[2:]
+ if not exists(new):
+ continue
self.debugfn(
f"Matched path {path!r} to rule {original_pattern!r} -> {result!r}, " +
f"producing {new!r}"
@@ -455,7 +460,7 @@ class PathAliases:
result = f"{dir1}{os.sep}"
self.debugfn(f"Generating rule: {pattern!r} -> {result!r} using regex {regex!r}")
self.aliases.append((pattern, re.compile(regex), result))
- return self.map(path)
+ return self.map(path, exists=exists)
self.debugfn(f"No rules match, path {path!r} is unchanged")
return path