Skip to content

Commit

Permalink
Merge 61078bb into 505018d
Browse files Browse the repository at this point in the history
  • Loading branch information
ptomulik committed Nov 13, 2018
2 parents 505018d + 61078bb commit c852bf9
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/engine/SCons/Platform/virtualenv.py
Expand Up @@ -37,11 +37,11 @@


def _enable_virtualenv_default():
return SCons.Util.get_bool_envvar('SCONS_ENABLE_VIRTUALENV', virtualenv_enabled_by_default)
return SCons.Util.get_os_env_bool('SCONS_ENABLE_VIRTUALENV', virtualenv_enabled_by_default)


def _ignore_virtualenv_default():
return SCons.Util.get_bool_envvar('SCONS_IGNORE_VIRTUALENV', False)
return SCons.Util.get_os_env_bool('SCONS_IGNORE_VIRTUALENV', False)


enable_virtualenv = _enable_virtualenv_default()
Expand Down
27 changes: 16 additions & 11 deletions src/engine/SCons/Util.py
Expand Up @@ -1597,20 +1597,20 @@ def cmp(a, b):
return (a > b) - (a < b)


def get_bool_envvar(name, default=False):
"""
Get a value of OS environment variable converting it to boolean.
- FOO=1, FOO=123, FOO=true, FOO=yes, FOO=y, FOO=on are examples of ``True``
values.
- FOO=0, FOO=false, FOO=no, FOO=n, FOO=off are examples of ``False``
values.
def get_env_bool(env, name, default=False):
"""Get a value of env[name] converted to boolean. The value of env[name] is
interpreted as follows: 'true', 'yes', 'y', 'on' (case insensitive) and
anything convertible to int that yields non-zero integer are True values;
'0', 'false', 'no', 'n' and 'off' (case insensitive) are False values. For
all other cases, default value is returned.
If a variable can't be converted to a boolean, default value is returned
(``False`` by default)
:Parameters:
- `env` - dict or dict-like object, a convainer with variables
- `name` - name of the variable in env to be returned
- `default` - returned when env[name] does not exist or can't be converted to bool
"""
try:
var = os.environ[name]
var = env[name]
except KeyError:
return default
try:
Expand All @@ -1623,6 +1623,11 @@ def get_bool_envvar(name, default=False):
else:
return default


def get_os_env_bool(name, default=False):
"""Same as get_env_bool(os.environ, name, default)."""
return get_env_bool(os.environ, name, default)

# Local Variables:
# tab-width:4
# indent-tabs-mode:nil
Expand Down
51 changes: 44 additions & 7 deletions src/engine/SCons/UtilTests.py
Expand Up @@ -905,13 +905,50 @@ def __exit__(self, *args):
self.stop()


class get_bool_envvarTestCase(unittest.TestCase):
class get_env_boolTestCase(unittest.TestCase):
def test_missing(self):
env = dict()
var = get_env_bool(env, 'FOO')
assert var is False, "var should be False, not %s" % repr(var)
env = {'FOO': '1'}
var = get_env_bool(env, 'BAR')
assert var is False, "var should be False, not %s" % repr(var)

def test_true(self):
for foo in [ 'TRUE', 'True', 'true',
'YES', 'Yes', 'yes',
'Y', 'y',
'ON', 'On', 'on',
'1', '20', '-1']:
env = {'FOO': foo}
var = get_env_bool(env, 'FOO')
assert var is True, 'var should be True, not %s' % repr(var)

def test_false(self):
for foo in [ 'FALSE', 'False', 'false',
'NO', 'No', 'no',
'N', 'n',
'OFF', 'Off', 'off',
'0']:
env = {'FOO': foo}
var = get_env_bool(env, 'FOO', True)
assert var is False, 'var should be True, not %s' % repr(var)

def test_default(self):
env = {'FOO': 'other'}
var = get_env_bool(env, 'FOO', True)
assert var is True, 'var should be True, not %s' % repr(var)
var = get_env_bool(env, 'FOO', False)
assert var is False, 'var should be False, not %s' % repr(var)


class get_os_env_boolTestCase(unittest.TestCase):
def test_missing(self):
with OsEnviron(dict()):
var = get_bool_envvar('FOO')
var = get_os_env_bool('FOO')
assert var is False, "var should be False, not %s" % repr(var)
with OsEnviron({'FOO': '1'}):
var = get_bool_envvar('BAR')
var = get_os_env_bool('BAR')
assert var is False, "var should be False, not %s" % repr(var)

def test_true(self):
Expand All @@ -921,7 +958,7 @@ def test_true(self):
'ON', 'On', 'on',
'1', '20', '-1']:
with OsEnviron({'FOO': foo}):
var = get_bool_envvar('FOO')
var = get_os_env_bool('FOO')
assert var is True, 'var should be True, not %s' % repr(var)

def test_false(self):
Expand All @@ -931,14 +968,14 @@ def test_false(self):
'OFF', 'Off', 'off',
'0']:
with OsEnviron({'FOO': foo}):
var = get_bool_envvar('FOO', True)
var = get_os_env_bool('FOO', True)
assert var is False, 'var should be True, not %s' % repr(var)

def test_default(self):
with OsEnviron({'FOO': 'other'}):
var = get_bool_envvar('FOO', True)
var = get_os_env_bool('FOO', True)
assert var is True, 'var should be True, not %s' % repr(var)
var = get_bool_envvar('FOO', False)
var = get_os_env_bool('FOO', False)
assert var is False, 'var should be False, not %s' % repr(var)


Expand Down

0 comments on commit c852bf9

Please sign in to comment.