mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-04-21 16:21:12 +00:00
Move type checking methods out of basic.py and add unit tests (#53687)
* Move check_type_str() out of basic.py * Move check_type_list() out of basic.py * Move safe_eval() out of basic.py * Move check_type_dict() out of basic.py * Move json importing code to common location * Move check_type_bool() out of basic.py * Move _check_type_int() out of basic.py * Move _check_type_float() out of basic.py * Move _check_type_path() out of basic.py * Move _check_type_raw() out of basic.py * Move _check_type_bytes() out of basic.py * Move _check_type_bits() out of basic.py * Create text.formatters.py Move human_to_bytes, bytes_to_human, and _lenient_lowercase out of basic.py into text.formatters.py Change references in modules to point to function at new location * Move _check_type_jsonarg() out of basic.py * Rename json related functions and put them in common.text.converters Move formatters.py to common.text.formatters.py and update references in modules. * Rework check_type_str() Add allow_conversion option to make the function more self-contained. Move the messaging back to basic.py since those error messages are more relevant to using this function in the context of AnsibleModule and not when using the function in isolation. * Add unit tests for type checking functions * Change _lenient_lowercase to lenient_lowercase per feedback
This commit is contained in:
@@ -69,9 +69,10 @@ VALID_SPECS = (
|
||||
|
||||
INVALID_SPECS = (
|
||||
# Type is int; unable to convert this string
|
||||
({'arg': {'type': 'int'}}, {'arg': "bad"}, "invalid literal for int() with base 10: 'bad'"),
|
||||
({'arg': {'type': 'int'}}, {'arg': "wolf"}, "is of type {0} and we were unable to convert to int: {0} cannot be converted to an int".format(type('bad'))),
|
||||
# Type is list elements is int; unable to convert this string
|
||||
({'arg': {'type': 'list', 'elements': 'int'}}, {'arg': [1, "bad"]}, "invalid literal for int() with base 10: 'bad'"),
|
||||
({'arg': {'type': 'list', 'elements': 'int'}}, {'arg': [1, "bad"]}, "is of type {0} and we were unable to convert to int: {0} cannot be converted to "
|
||||
"an int".format(type('int'))),
|
||||
# Type is int; unable to convert float
|
||||
({'arg': {'type': 'int'}}, {'arg': 42.1}, "'float'> cannot be converted to an int"),
|
||||
# Type is list, elements is int; unable to convert float
|
||||
|
||||
@@ -64,13 +64,12 @@ class TestImports(ModuleTestCase):
|
||||
@patch.object(builtins, '__import__')
|
||||
def test_module_utils_basic_import_json(self, mock_import):
|
||||
def _mock_import(name, *args, **kwargs):
|
||||
if name == 'json':
|
||||
if name == 'ansible.module_utils.common._json_compat':
|
||||
raise ImportError
|
||||
return realimport(name, *args, **kwargs)
|
||||
|
||||
self.clear_modules(['json', 'ansible.module_utils.basic'])
|
||||
builtins.__import__('ansible.module_utils.basic')
|
||||
|
||||
self.clear_modules(['json', 'ansible.module_utils.basic'])
|
||||
mock_import.side_effect = _mock_import
|
||||
with self.assertRaises(SystemExit):
|
||||
|
||||
Reference in New Issue
Block a user