diff options
author | Morgan Fainberg <m@metacloud.com> | 2014-03-10 13:39:48 -0700 |
---|---|---|
committer | Morgan Fainberg <m@metacloud.com> | 2014-03-10 13:39:48 -0700 |
commit | 7e202871837f63b0642073d5b659205da566e569 (patch) | |
tree | 31fcb838918951bc68bf90f674ac3de64ae7ef4b | |
parent | 5f34f7e67420d51cc13566e2132462308cd263ca (diff) | |
download | tox-7e202871837f63b0642073d5b659205da566e569.tar.gz |
Support optional ENV variable substitution in tox.ini
Add in support for optional ENV variable substitutions where instead
of raising an error if the environmental variable does not exist in
os.environ, the value is substituted with an empty string.
-rw-r--r-- | doc/config.txt | 12 | ||||
-rw-r--r-- | tests/test_config.py | 13 | ||||
-rw-r--r-- | tox/_config.py | 18 |
3 files changed, 38 insertions, 5 deletions
diff --git a/doc/config.txt b/doc/config.txt index 8eb8f3a..971ca68 100644 --- a/doc/config.txt +++ b/doc/config.txt @@ -292,6 +292,18 @@ then the value will be retrieved as ``os.environ['KEY']`` and raise an Error if the environment variable does not exist. + +optional environment variable substitutions +++++++++++++++++++++++++++++++++++ + +If you specify a substitution string like this:: + + {optionalenv:KEY} + +then the value will be retrieved as ``os.environ['KEY']`` +and replace with '' 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 b8d0aa3..419d021 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -242,6 +242,19 @@ class TestIniParser: py.test.raises(tox.exception.ConfigError, 'reader.getdefault("section", "key2")') + def test_getdefault_environment_optional_sub(self, monkeypatch, newconfig): + monkeypatch.setenv("KEY1", "hello") + config = newconfig(""" + [section] + key1={optionalenv:KEY1} + key2={optionalenv:KEY2} + """) + reader = IniReader(config._cfg) + x = reader.getdefault("section", "key1") + assert x == "hello" + x = reader.getdefault("section", "key2") + assert x == "" + def test_getdefault_other_section_substitution(self, newconfig): config = newconfig(""" [section] diff --git a/tox/_config.py b/tox/_config.py index 3644d8b..4f6fa33 100644 --- a/tox/_config.py +++ b/tox/_config.py @@ -595,18 +595,25 @@ class IniReader: #print "getdefault", section, name, "returned", repr(x) return x - def _replace_env(self, match): + def _do_replace_env(self, match, error=True): envkey = match.group('substitution_value') if not envkey: raise tox.exception.ConfigError( 'env: requires an environment variable name') if not envkey in os.environ: - raise tox.exception.ConfigError( - "substitution env:%r: unkown environment variable %r" % - (envkey, envkey)) + if error: + raise tox.exception.ConfigError( + "substitution env:%r: unkown environment variable %r" % + (envkey, envkey)) + + return os.environ.get(envkey, '') + + def _replace_env(self, match): + return self._do_replace_env(match) - return os.environ[envkey] + def _replace_env_no_error(self, match): + return self._do_replace_env(match, error=False) def _substitute_from_other_section(self, key): if key.startswith("[") and "]" in key: @@ -647,6 +654,7 @@ class IniReader: handlers = { 'env' : self._replace_env, + 'optionalenv' : self._replace_env_no_error, None : self._replace_substitution, } try: |