diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2018-09-08 16:42:44 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2018-09-08 16:42:44 -0400 |
commit | 4fe7c61916e9599c459e231245dccc92db2affa8 (patch) | |
tree | 48ee5c9d5f7ac9af8d77ac7f03225082293bc5de /coverage | |
parent | dde0a3ef3b88eb79bff8a36943cf934452eb8c26 (diff) | |
download | python-coveragepy-git-4fe7c61916e9599c459e231245dccc92db2affa8.tar.gz |
Strict variable substitution is now an option
Diffstat (limited to 'coverage')
-rw-r--r-- | coverage/misc.py | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/coverage/misc.py b/coverage/misc.py index e2031852..c6a7c8cf 100644 --- a/coverage/misc.py +++ b/coverage/misc.py @@ -258,6 +258,7 @@ def substitute_variables(text, variables=os.environ): $VAR ${VAR} + ${VAR?} strict: an error if VAR isn't defined. A dollar can be inserted with ``$$``. @@ -270,16 +271,24 @@ def substitute_variables(text, variables=os.environ): def dollar_replace(m): """Called for each $replacement.""" # Only one of the groups will have matched, just get its text. - word = next(w for w in m.groups() if w is not None) # pragma: part covered + word = m.expand(r"\g<v1>\g<v2>\g<char>") if word == "$": return "$" else: + strict = bool(m.group('strict')) + if strict: + if word not in variables: + msg = "Variable {} is undefined: {}".format(word, text) + raise CoverageException(msg) return variables.get(word, '') dollar_pattern = r"""(?x) # Use extended regex syntax \$(?: # A dollar sign, then (?P<v1>\w+) | # a plain word, - {(?P<v2>\w+)} | # or a {-wrapped word, + { # or a {-wrapped word, + (?P<v2>\w+) + (?P<strict>\??) # with maybe a strict marker + } | (?P<char>[$]) # or a dollar sign. ) """ |