From 4fe7c61916e9599c459e231245dccc92db2affa8 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Sat, 8 Sep 2018 16:42:44 -0400 Subject: Strict variable substitution is now an option --- coverage/misc.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'coverage/misc.py') 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\g\g") 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\w+) | # a plain word, - {(?P\w+)} | # or a {-wrapped word, + { # or a {-wrapped word, + (?P\w+) + (?P\??) # with maybe a strict marker + } | (?P[$]) # or a dollar sign. ) """ -- cgit v1.2.1