diff options
author | holger krekel <holger@merlinux.eu> | 2014-05-10 12:08:50 +0200 |
---|---|---|
committer | holger krekel <holger@merlinux.eu> | 2014-05-10 12:08:50 +0200 |
commit | 500730da2ac6b63e887c82d0f6a0d99670b82df9 (patch) | |
tree | 3b13b8088cf8611ae27b868d26f26bd8ec3f1fff | |
parent | 57aecd1c7f8923c2ed87ada9b7d5c4215181061c (diff) | |
parent | 7a61fdad07cd778602f687f085eb15e235d06198 (diff) | |
download | tox-500730da2ac6b63e887c82d0f6a0d99670b82df9.tar.gz |
Merged in morgan_fainberg/tox (pull request #86)
Support optional ENV variable substitution in tox.ini
-rw-r--r-- | doc/config.txt | 20 | ||||
-rw-r--r-- | tests/test_config.py | 16 | ||||
-rw-r--r-- | tox/_config.py | 16 |
3 files changed, 48 insertions, 4 deletions
diff --git a/doc/config.txt b/doc/config.txt index 054400d..a6ca4c8 100644 --- a/doc/config.txt +++ b/doc/config.txt @@ -284,6 +284,26 @@ then the value will be retrieved as ``os.environ['KEY']`` and raise an Error if the environment variable does not exist. + +environment variable substitutions with default values +++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +If you specify a substitution string like this:: + + {env:KEY:DEFAULTVALUE} + +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:: + + {env:KEY:} + +then the value will be retrieved as ``os.environ['KEY']`` +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 5e766cf..183654e 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -241,6 +241,22 @@ class TestIniParser: py.test.raises(tox.exception.ConfigError, 'reader.getdefault("section", "key2")') + def test_getdefault_environment_substitution_with_default(self, monkeypatch, newconfig): + monkeypatch.setenv("KEY1", "hello") + config = newconfig(""" + [section] + key1={env:KEY1:DEFAULT_VALUE} + key2={env:KEY2:DEFAULT_VALUE} + key3={env: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): config = newconfig(""" [section] diff --git a/tox/_config.py b/tox/_config.py index 57df84d..12ccfa4 100644 --- a/tox/_config.py +++ b/tox/_config.py @@ -595,17 +595,25 @@ class IniReader: return x def _replace_env(self, match): - envkey = match.group('substitution_value') - if not envkey: + match_value = match.group('substitution_value') + if not match_value: raise tox.exception.ConfigError( 'env: requires an environment variable name') - if not envkey in os.environ: + default = None + envkey_split = match_value.split(':', 1) + + if len(envkey_split) is 2: + envkey, default = envkey_split + else: + envkey = match_value + + 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[envkey] + return os.environ.get(envkey, default) def _substitute_from_other_section(self, key): if key.startswith("[") and "]" in key: |