summaryrefslogtreecommitdiff
path: root/coverage/files.py
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2013-09-26 22:24:01 -0400
committerNed Batchelder <ned@nedbatchelder.com>2013-09-26 22:24:01 -0400
commit9a32586334141664fb393e56d3e1d76545993d79 (patch)
treeb29c5d3190190d7d1dfa9a90f4da729710623868 /coverage/files.py
parente9b9419bc9a7d184070cc39bdf71924599a5e57f (diff)
downloadpython-coveragepy-git-9a32586334141664fb393e56d3e1d76545993d79.tar.gz
Allow relative paths in the [paths] aliases. #267.
Diffstat (limited to 'coverage/files.py')
-rw-r--r--coverage/files.py17
1 files changed, 15 insertions, 2 deletions
diff --git a/coverage/files.py b/coverage/files.py
index 976db4ed..464535a8 100644
--- a/coverage/files.py
+++ b/coverage/files.py
@@ -3,6 +3,7 @@
from coverage.backward import to_string
from coverage.misc import CoverageException
import fnmatch, os, os.path, re, sys
+import ntpath, posixpath
class FileLocator(object):
"""Understand how filenames work."""
@@ -110,13 +111,20 @@ else:
"""The actual path for non-Windows platforms."""
return filename
+
def abs_file(filename):
"""Return the absolute normalized form of `filename`."""
- path = os.path.abspath(os.path.realpath(filename))
+ path = os.path.expandvars(os.path.expanduser(filename))
+ path = os.path.abspath(os.path.realpath(path))
path = actual_path(path)
return path
+def isabs_anywhere(filename):
+ """Is `filename` an absolute path on any OS?"""
+ return ntpath.isabs(filename) or posixpath.isabs(filename)
+
+
def prep_patterns(patterns):
"""Prepare the file patterns for use in a `FnmatchMatcher`.
@@ -230,6 +238,11 @@ class PathAliases(object):
if pattern.endswith("*"):
raise CoverageException("Pattern must not end with wildcards.")
pattern_sep = sep(pattern)
+
+ # The pattern is meant to match a filepath. Let's make it absolute
+ # unless it already is, or is meant to match any prefix.
+ if not pattern.startswith('*') and not isabs_anywhere(pattern):
+ pattern = abs_file(pattern)
pattern += pattern_sep
# Make a regex from the pattern. fnmatch always adds a \Z or $ to
@@ -237,7 +250,7 @@ class PathAliases(object):
regex_pat = fnmatch.translate(pattern).replace(r'\Z(', '(')
if regex_pat.endswith("$"):
regex_pat = regex_pat[:-1]
- # We want */a/b.py to match on Windows to, so change slash to match
+ # We want */a/b.py to match on Windows too, so change slash to match
# either separator.
regex_pat = regex_pat.replace(r"\/", r"[\\/]")
# We want case-insensitive matching, so add that flag.