mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
CmdRunner format as_list - check for min_len and max_len (#8288)
* CmdRunner format as_list - check for min_len and max_len * Change default min len, add chglog frag
This commit is contained in:
parent
7051fe3449
commit
85f9d89510
3 changed files with 57 additions and 38 deletions
|
@ -0,0 +1,2 @@
|
|||
minor_changes:
|
||||
- cmd_runner module_utils - add validation for minimum and maximum length in the value passed to ``cmd_runner_fmt.as_list()`` (https://github.com/ansible-collections/community.general/pull/8288).
|
|
@ -129,8 +129,15 @@ class _Format(object):
|
|||
return _ArgFormat(lambda value: ["{0}={1}".format(arg, value)], ignore_none=ignore_none)
|
||||
|
||||
@staticmethod
|
||||
def as_list(ignore_none=None):
|
||||
return _ArgFormat(_ensure_list, ignore_none=ignore_none)
|
||||
def as_list(ignore_none=None, min_len=0, max_len=None):
|
||||
def func(value):
|
||||
value = _ensure_list(value)
|
||||
if len(value) < min_len:
|
||||
raise ValueError("Parameter must have at least {0} element(s)".format(min_len))
|
||||
if max_len is not None and len(value) > max_len:
|
||||
raise ValueError("Parameter must have at most {0} element(s)".format(max_len))
|
||||
return value
|
||||
return _ArgFormat(func, ignore_none=ignore_none)
|
||||
|
||||
@staticmethod
|
||||
def as_fixed(args):
|
||||
|
|
|
@ -7,6 +7,7 @@ from __future__ import (absolute_import, division, print_function)
|
|||
__metaclass__ = type
|
||||
|
||||
from sys import version_info
|
||||
from functools import partial
|
||||
|
||||
import pytest
|
||||
|
||||
|
@ -15,55 +16,64 @@ from ansible_collections.community.general.plugins.module_utils.cmd_runner impor
|
|||
|
||||
|
||||
TC_FORMATS = dict(
|
||||
simple_boolean__true=(cmd_runner_fmt.as_bool, ("--superflag",), True, ["--superflag"]),
|
||||
simple_boolean__false=(cmd_runner_fmt.as_bool, ("--superflag",), False, []),
|
||||
simple_boolean__none=(cmd_runner_fmt.as_bool, ("--superflag",), None, []),
|
||||
simple_boolean_both__true=(cmd_runner_fmt.as_bool, ("--superflag", "--falseflag"), True, ["--superflag"]),
|
||||
simple_boolean_both__false=(cmd_runner_fmt.as_bool, ("--superflag", "--falseflag"), False, ["--falseflag"]),
|
||||
simple_boolean_both__none=(cmd_runner_fmt.as_bool, ("--superflag", "--falseflag"), None, ["--falseflag"]),
|
||||
simple_boolean_both__none_ig=(cmd_runner_fmt.as_bool, ("--superflag", "--falseflag", True), None, []),
|
||||
simple_boolean_not__true=(cmd_runner_fmt.as_bool_not, ("--superflag",), True, []),
|
||||
simple_boolean_not__false=(cmd_runner_fmt.as_bool_not, ("--superflag",), False, ["--superflag"]),
|
||||
simple_boolean_not__none=(cmd_runner_fmt.as_bool_not, ("--superflag",), None, ["--superflag"]),
|
||||
simple_optval__str=(cmd_runner_fmt.as_optval, ("-t",), "potatoes", ["-tpotatoes"]),
|
||||
simple_optval__int=(cmd_runner_fmt.as_optval, ("-t",), 42, ["-t42"]),
|
||||
simple_opt_val__str=(cmd_runner_fmt.as_opt_val, ("-t",), "potatoes", ["-t", "potatoes"]),
|
||||
simple_opt_val__int=(cmd_runner_fmt.as_opt_val, ("-t",), 42, ["-t", "42"]),
|
||||
simple_opt_eq_val__str=(cmd_runner_fmt.as_opt_eq_val, ("--food",), "potatoes", ["--food=potatoes"]),
|
||||
simple_opt_eq_val__int=(cmd_runner_fmt.as_opt_eq_val, ("--answer",), 42, ["--answer=42"]),
|
||||
simple_list_potato=(cmd_runner_fmt.as_list, (), "literal_potato", ["literal_potato"]),
|
||||
simple_list_42=(cmd_runner_fmt.as_list, (), 42, ["42"]),
|
||||
simple_map=(cmd_runner_fmt.as_map, ({'a': 1, 'b': 2, 'c': 3},), 'b', ["2"]),
|
||||
simple_default_type__list=(cmd_runner_fmt.as_default_type, ("list",), [1, 2, 3, 5, 8], ["--1", "--2", "--3", "--5", "--8"]),
|
||||
simple_default_type__bool_true=(cmd_runner_fmt.as_default_type, ("bool", "what"), True, ["--what"]),
|
||||
simple_default_type__bool_false=(cmd_runner_fmt.as_default_type, ("bool", "what"), False, []),
|
||||
simple_default_type__potato=(cmd_runner_fmt.as_default_type, ("any-other-type", "potato"), "42", ["--potato", "42"]),
|
||||
simple_fixed_true=(cmd_runner_fmt.as_fixed, [("--always-here", "--forever")], True, ["--always-here", "--forever"]),
|
||||
simple_fixed_false=(cmd_runner_fmt.as_fixed, [("--always-here", "--forever")], False, ["--always-here", "--forever"]),
|
||||
simple_fixed_none=(cmd_runner_fmt.as_fixed, [("--always-here", "--forever")], None, ["--always-here", "--forever"]),
|
||||
simple_fixed_str=(cmd_runner_fmt.as_fixed, [("--always-here", "--forever")], "something", ["--always-here", "--forever"]),
|
||||
simple_boolean__true=(partial(cmd_runner_fmt.as_bool, "--superflag"), True, ["--superflag"], None),
|
||||
simple_boolean__false=(partial(cmd_runner_fmt.as_bool, "--superflag"), False, [], None),
|
||||
simple_boolean__none=(partial(cmd_runner_fmt.as_bool, "--superflag"), None, [], None),
|
||||
simple_boolean_both__true=(partial(cmd_runner_fmt.as_bool, "--superflag", "--falseflag"), True, ["--superflag"], None),
|
||||
simple_boolean_both__false=(partial(cmd_runner_fmt.as_bool, "--superflag", "--falseflag"), False, ["--falseflag"], None),
|
||||
simple_boolean_both__none=(partial(cmd_runner_fmt.as_bool, "--superflag", "--falseflag"), None, ["--falseflag"], None),
|
||||
simple_boolean_both__none_ig=(partial(cmd_runner_fmt.as_bool, "--superflag", "--falseflag", True), None, [], None),
|
||||
simple_boolean_not__true=(partial(cmd_runner_fmt.as_bool_not, "--superflag"), True, [], None),
|
||||
simple_boolean_not__false=(partial(cmd_runner_fmt.as_bool_not, "--superflag"), False, ["--superflag"], None),
|
||||
simple_boolean_not__none=(partial(cmd_runner_fmt.as_bool_not, "--superflag"), None, ["--superflag"], None),
|
||||
simple_optval__str=(partial(cmd_runner_fmt.as_optval, "-t"), "potatoes", ["-tpotatoes"], None),
|
||||
simple_optval__int=(partial(cmd_runner_fmt.as_optval, "-t"), 42, ["-t42"], None),
|
||||
simple_opt_val__str=(partial(cmd_runner_fmt.as_opt_val, "-t"), "potatoes", ["-t", "potatoes"], None),
|
||||
simple_opt_val__int=(partial(cmd_runner_fmt.as_opt_val, "-t"), 42, ["-t", "42"], None),
|
||||
simple_opt_eq_val__str=(partial(cmd_runner_fmt.as_opt_eq_val, "--food"), "potatoes", ["--food=potatoes"], None),
|
||||
simple_opt_eq_val__int=(partial(cmd_runner_fmt.as_opt_eq_val, "--answer"), 42, ["--answer=42"], None),
|
||||
simple_list_potato=(cmd_runner_fmt.as_list, "literal_potato", ["literal_potato"], None),
|
||||
simple_list_42=(cmd_runner_fmt.as_list, 42, ["42"], None),
|
||||
simple_list_min_len_ok=(partial(cmd_runner_fmt.as_list, min_len=1), 42, ["42"], None),
|
||||
simple_list_min_len_fail=(partial(cmd_runner_fmt.as_list, min_len=10), 42, None, ValueError),
|
||||
simple_list_max_len_ok=(partial(cmd_runner_fmt.as_list, max_len=1), 42, ["42"], None),
|
||||
simple_list_max_len_fail=(partial(cmd_runner_fmt.as_list, max_len=2), [42, 42, 42], None, ValueError),
|
||||
simple_map=(partial(cmd_runner_fmt.as_map, {'a': 1, 'b': 2, 'c': 3}), 'b', ["2"], None),
|
||||
simple_default_type__list=(partial(cmd_runner_fmt.as_default_type, "list"), [1, 2, 3, 5, 8], ["--1", "--2", "--3", "--5", "--8"], None),
|
||||
simple_default_type__bool_true=(partial(cmd_runner_fmt.as_default_type, "bool", "what"), True, ["--what"], None),
|
||||
simple_default_type__bool_false=(partial(cmd_runner_fmt.as_default_type, "bool", "what"), False, [], None),
|
||||
simple_default_type__potato=(partial(cmd_runner_fmt.as_default_type, "any-other-type", "potato"), "42", ["--potato", "42"], None),
|
||||
simple_fixed_true=(partial(cmd_runner_fmt.as_fixed, ["--always-here", "--forever"]), True, ["--always-here", "--forever"], None),
|
||||
simple_fixed_false=(partial(cmd_runner_fmt.as_fixed, ["--always-here", "--forever"]), False, ["--always-here", "--forever"], None),
|
||||
simple_fixed_none=(partial(cmd_runner_fmt.as_fixed, ["--always-here", "--forever"]), None, ["--always-here", "--forever"], None),
|
||||
simple_fixed_str=(partial(cmd_runner_fmt.as_fixed, ["--always-here", "--forever"]), "something", ["--always-here", "--forever"], None),
|
||||
)
|
||||
if tuple(version_info) >= (3, 1):
|
||||
from collections import OrderedDict
|
||||
|
||||
# needs OrderedDict to provide a consistent key order
|
||||
TC_FORMATS["simple_default_type__dict"] = ( # type: ignore
|
||||
cmd_runner_fmt.as_default_type,
|
||||
("dict",),
|
||||
partial(cmd_runner_fmt.as_default_type, "dict"),
|
||||
OrderedDict((('a', 1), ('b', 2))),
|
||||
["--a=1", "--b=2"]
|
||||
["--a=1", "--b=2"],
|
||||
None
|
||||
)
|
||||
TC_FORMATS_IDS = sorted(TC_FORMATS.keys())
|
||||
|
||||
|
||||
@pytest.mark.parametrize('func, fmt_opt, value, expected',
|
||||
@pytest.mark.parametrize('func, value, expected, exception',
|
||||
(TC_FORMATS[tc] for tc in TC_FORMATS_IDS),
|
||||
ids=TC_FORMATS_IDS)
|
||||
def test_arg_format(func, fmt_opt, value, expected):
|
||||
fmt_func = func(*fmt_opt)
|
||||
actual = fmt_func(value, ctx_ignore_none=True)
|
||||
print("formatted string = {0}".format(actual))
|
||||
assert actual == expected, "actual = {0}".format(actual)
|
||||
def test_arg_format(func, value, expected, exception):
|
||||
fmt_func = func()
|
||||
try:
|
||||
actual = fmt_func(value, ctx_ignore_none=True)
|
||||
print("formatted string = {0}".format(actual))
|
||||
assert actual == expected, "actual = {0}".format(actual)
|
||||
except Exception as e:
|
||||
if exception is None:
|
||||
raise
|
||||
assert isinstance(e, exception)
|
||||
|
||||
|
||||
TC_RUNNER = dict(
|
||||
|
|
Loading…
Reference in a new issue