commit
0bc450e881
@ -1,5 +1,6 @@
|
||||
pbr>=0.6,!=0.7,<1.0
|
||||
cliff
|
||||
jsonschema
|
||||
PyYAML
|
||||
six
|
||||
stevedore
|
||||
|
1280
striker/common/config.py
Normal file
1280
striker/common/config.py
Normal file
File diff suppressed because it is too large
Load Diff
@ -16,6 +16,8 @@
|
||||
import os
|
||||
import time
|
||||
|
||||
import six
|
||||
|
||||
|
||||
def canonicalize_path(cwd, path):
|
||||
"""
|
||||
@ -56,3 +58,42 @@ def backoff(max_tries):
|
||||
# time
|
||||
time.sleep(sleep)
|
||||
sleep <<= 1
|
||||
|
||||
|
||||
def boolean(value, default=None):
|
||||
"""
|
||||
Convert a string value into a boolean. The values 'true', 't',
|
||||
'yes', 'y', and 'on', as well as non-zero integer values, are
|
||||
recognized as ``True``, while the values 'false', 'f', 'no', 'n',
|
||||
and 'off', as well as the integer value 0, are recognized as
|
||||
``False``. A ``ValueError`` is raised for other values unless the
|
||||
``default`` parameter is given, in which case it is returned.
|
||||
|
||||
:param value: The string value to be converted to boolean.
|
||||
:param default: If not ``None``, specifies the desired default
|
||||
value if the ``value`` is not one of the
|
||||
recognized values.
|
||||
|
||||
:returns: The boolean value derived from the string.
|
||||
"""
|
||||
|
||||
# Cover non-string case
|
||||
if not isinstance(value, six.string_types):
|
||||
return bool(value)
|
||||
|
||||
# Cover the integer case
|
||||
if value.isdigit():
|
||||
return bool(int(value))
|
||||
|
||||
# Check for recognized values
|
||||
tmp = value.lower()
|
||||
if tmp in ('true', 't', 'yes', 'y', 'on'):
|
||||
return True
|
||||
elif tmp in ('false', 'f', 'no', 'n', 'off'):
|
||||
return False
|
||||
|
||||
# Return the default value
|
||||
if default is not None:
|
||||
return default
|
||||
|
||||
raise ValueError('invalid boolean literal %r' % value)
|
||||
|
@ -16,6 +16,14 @@
|
||||
import six
|
||||
|
||||
|
||||
class TestException(Exception):
|
||||
"""
|
||||
An exception for the use of tests.
|
||||
"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
def fake_join(a, *p):
|
||||
"""
|
||||
Lifted from the POSIX implementation of os.path, for testing
|
||||
|
1472
tests/unit/common/test_config.py
Normal file
1472
tests/unit/common/test_config.py
Normal file
File diff suppressed because it is too large
Load Diff
@ -72,3 +72,52 @@ class BackoffTest(unittest.TestCase):
|
||||
mock_sleep.reset_mock()
|
||||
|
||||
self.assertEqual(i, max_tries - 1)
|
||||
|
||||
|
||||
class BooleanTest(unittest.TestCase):
|
||||
truth_table = [
|
||||
('TrUe', True),
|
||||
('t', True),
|
||||
('T', True),
|
||||
('yEs', True),
|
||||
('y', True),
|
||||
('Y', True),
|
||||
('oN', True),
|
||||
('1', True),
|
||||
('120', True),
|
||||
('FaLsE', False),
|
||||
('f', False),
|
||||
('F', False),
|
||||
('nO', False),
|
||||
('n', False),
|
||||
('N', False),
|
||||
('oFf', False),
|
||||
('0', False),
|
||||
('000', False),
|
||||
('other', None),
|
||||
(True, True),
|
||||
(False, False),
|
||||
(1, True),
|
||||
(0, False),
|
||||
]
|
||||
|
||||
def test_with_raise(self):
|
||||
for value, expected in self.truth_table:
|
||||
if expected is None:
|
||||
self.assertRaises(ValueError, utils.boolean, value)
|
||||
else:
|
||||
self.assertEqual(expected, utils.boolean(value))
|
||||
|
||||
def test_default_false(self):
|
||||
for value, expected in self.truth_table:
|
||||
if expected is None:
|
||||
expected = False
|
||||
|
||||
self.assertEqual(expected, utils.boolean(value, False))
|
||||
|
||||
def test_default_true(self):
|
||||
for value, expected in self.truth_table:
|
||||
if expected is None:
|
||||
expected = True
|
||||
|
||||
self.assertEqual(expected, utils.boolean(value, True))
|
||||
|
Loading…
x
Reference in New Issue
Block a user