[Pytest-commit] commit/tox: hpk42: Merged in morgan_fainberg/tox (pull request #86)

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Sat May 10 12:08:54 CEST 2014


1 new commit in tox:

https://bitbucket.org/hpk42/tox/commits/0f763647663b/
Changeset:   0f763647663b
User:        hpk42
Date:        2014-05-10 12:08:50
Summary:     Merged in morgan_fainberg/tox (pull request #86)

Support optional ENV variable substitution in tox.ini
Affected #:  3 files

diff -r c07badfc0f02b5816403d3b5dc324c146708efb5 -r 0f763647663b72f85174897ac1fdbfd5026e97d2 doc/config.txt
--- a/doc/config.txt
+++ b/doc/config.txt
@@ -284,6 +284,26 @@
 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 -r c07badfc0f02b5816403d3b5dc324c146708efb5 -r 0f763647663b72f85174897ac1fdbfd5026e97d2 tests/test_config.py
--- a/tests/test_config.py
+++ b/tests/test_config.py
@@ -241,6 +241,22 @@
         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 -r c07badfc0f02b5816403d3b5dc324c146708efb5 -r 0f763647663b72f85174897ac1fdbfd5026e97d2 tox/_config.py
--- a/tox/_config.py
+++ b/tox/_config.py
@@ -595,17 +595,25 @@
         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:

Repository URL: https://bitbucket.org/hpk42/tox/

--

This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.


More information about the pytest-commit mailing list