summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMorgan Fainberg <m@metacloud.com>2014-03-19 00:18:30 -0700
committerMorgan Fainberg <m@metacloud.com>2014-03-19 00:18:30 -0700
commitae737eac6e792cbc6f05a527f9398876f84170c6 (patch)
tree3427ab3f9eaabd7745221a082788e3fe3a668370
parent7e202871837f63b0642073d5b659205da566e569 (diff)
downloadtox-ae737eac6e792cbc6f05a527f9398876f84170c6.tar.gz
Make optionalenv defenv to support default values
Change the 'optionalenv' substitution to 'defenv' and support providing a default substitution value. Format is "defenv:DEFAULTVALUE:KEY".
-rw-r--r--doc/config.txt18
-rw-r--r--tests/test_config.py9
-rw-r--r--tox/_config.py31
3 files changed, 38 insertions, 20 deletions
diff --git a/doc/config.txt b/doc/config.txt
index 971ca68..5a9134d 100644
--- a/doc/config.txt
+++ b/doc/config.txt
@@ -293,16 +293,24 @@ and raise an Error if the environment variable
does not exist.
-optional environment variable substitutions
-++++++++++++++++++++++++++++++++++
+environment variable substitutions with default values
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
+
+If you specify a substitution string like this::
+
+ {defenv:DEFAULTVALUE:KEY}
+
+then the value will be retrieved as ``os.environ['KEY']``
+and replace with DEFAULTVALUE if the environment variable does not
+exist.
If you specify a substitution string like this::
- {optionalenv:KEY}
+ {defenv::KEY}
then the value will be retrieved as ``os.environ['KEY']``
-and replace with '' if the environment variable does not
-exist
+and replace with and empty string if the environment variable does not
+exist.
.. _`command positional substitution`:
.. _`positional substitution`:
diff --git a/tests/test_config.py b/tests/test_config.py
index 419d021..3084001 100644
--- a/tests/test_config.py
+++ b/tests/test_config.py
@@ -242,17 +242,20 @@ class TestIniParser:
py.test.raises(tox.exception.ConfigError,
'reader.getdefault("section", "key2")')
- def test_getdefault_environment_optional_sub(self, monkeypatch, newconfig):
+ def test_getdefault_environment_substitution_with_default(self, monkeypatch, newconfig):
monkeypatch.setenv("KEY1", "hello")
config = newconfig("""
[section]
- key1={optionalenv:KEY1}
- key2={optionalenv:KEY2}
+ key1={defenv:DEFAULT_VALUE:KEY1}
+ key2={defenv:DEFAULT_VALUE:KEY2}
+ key3={defenv::KEY3}
""")
reader = IniReader(config._cfg)
x = reader.getdefault("section", "key1")
assert x == "hello"
x = reader.getdefault("section", "key2")
+ assert x == "DEFAULT_VALUE"
+ x = reader.getdefault("section", "key3")
assert x == ""
def test_getdefault_other_section_substitution(self, newconfig):
diff --git a/tox/_config.py b/tox/_config.py
index 4f6fa33..468af5f 100644
--- a/tox/_config.py
+++ b/tox/_config.py
@@ -595,25 +595,32 @@ class IniReader:
#print "getdefault", section, name, "returned", repr(x)
return x
- def _do_replace_env(self, match, error=True):
- envkey = match.group('substitution_value')
+ def _do_replace_env(self, envkey, default=None):
if not envkey:
raise tox.exception.ConfigError(
'env: requires an environment variable name')
- if not envkey in os.environ:
- if error:
- raise tox.exception.ConfigError(
- "substitution env:%r: unkown environment variable %r" %
- (envkey, envkey))
+ if not envkey in os.environ and default is None:
+ raise tox.exception.ConfigError(
+ "substitution env:%r: unkown environment variable %r" %
+ (envkey, envkey))
- return os.environ.get(envkey, '')
+ return os.environ.get(envkey, default)
def _replace_env(self, match):
- return self._do_replace_env(match)
+ envkey = match.group('substitution_value')
+ return self._do_replace_env(envkey)
+
+ def _replace_env_with_default(self, match):
+ envkey = match.group('substitution_value')
+ try:
+ default, envkey = envkey.split(':', 1)
+ except ValueError:
+ raise tox.exception.ConfigError(
+ "substitution 'defenv:%r': malformed, expected "
+ "'defenv:DEFAULTVALUE:KEY'" % match)
- def _replace_env_no_error(self, match):
- return self._do_replace_env(match, error=False)
+ return self._do_replace_env(envkey, default=default)
def _substitute_from_other_section(self, key):
if key.startswith("[") and "]" in key:
@@ -654,7 +661,7 @@ class IniReader:
handlers = {
'env' : self._replace_env,
- 'optionalenv' : self._replace_env_no_error,
+ 'defenv' : self._replace_env_with_default,
None : self._replace_substitution,
}
try: