10.0.0: CmdRunner: remove deprecated feature (#8928)

* remove deprecated feature

* remove deprecated feature from tests as well

* Adjust the integration test

* add changelog frag
pull/8937/head
Alexei Znamensky 2024-09-26 21:01:36 +12:00 committed by GitHub
parent 0bc5f24863
commit 002f137134
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 16 additions and 54 deletions

View File

@ -0,0 +1,2 @@
breaking_changes:
- cmd_runner module utils - CLI arguments created directly from module parameters are no longer assigned a default formatter (https://github.com/ansible-collections/community.general/pull/8928).

View File

@ -10,7 +10,6 @@ import os
from functools import wraps from functools import wraps
from ansible.module_utils.common.collections import is_sequence from ansible.module_utils.common.collections import is_sequence
from ansible.module_utils.six import iteritems
from ansible.module_utils.common.locale import get_best_parsable_locale from ansible.module_utils.common.locale import get_best_parsable_locale
@ -167,23 +166,6 @@ class _Format(object):
default = [] default = []
return _ArgFormat(lambda value: _ensure_list(_map.get(value, default)), ignore_none=ignore_none) return _ArgFormat(lambda value: _ensure_list(_map.get(value, default)), ignore_none=ignore_none)
@staticmethod
def as_default_type(_type, arg="", ignore_none=None):
#
# DEPRECATION: This method is deprecated and will be removed in community.general 10.0.0
#
# Instead of using the implicit formats provided here, use the explicit necessary format method.
#
fmt = _Format
if _type == "dict":
return fmt.as_func(lambda d: ["--{0}={1}".format(*a) for a in iteritems(d)], ignore_none=ignore_none)
if _type == "list":
return fmt.as_func(lambda value: ["--{0}".format(x) for x in value], ignore_none=ignore_none)
if _type == "bool":
return fmt.as_bool("--{0}".format(arg))
return fmt.as_opt_val("--{0}".format(arg), ignore_none=ignore_none)
@staticmethod @staticmethod
def unpack_args(func): def unpack_args(func):
@wraps(func) @wraps(func)
@ -252,10 +234,6 @@ class CmdRunner(object):
_cmd = self.command[0] _cmd = self.command[0]
self.command[0] = _cmd if (os.path.isabs(_cmd) or '/' in _cmd) else module.get_bin_path(_cmd, opt_dirs=path_prefix, required=True) self.command[0] = _cmd if (os.path.isabs(_cmd) or '/' in _cmd) else module.get_bin_path(_cmd, opt_dirs=path_prefix, required=True)
for mod_param_name, spec in iteritems(module.argument_spec):
if mod_param_name not in self.arg_formats:
self.arg_formats[mod_param_name] = _Format.as_default_type(spec.get('type', 'str'), mod_param_name)
@property @property
def binary(self): def binary(self):
return self.command[0] return self.command[0]

View File

@ -3,7 +3,7 @@
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later # SPDX-License-Identifier: GPL-3.0-or-later
- name: create copy of /bin/echo ({{ item.name }}) - name: Create copy of /bin/echo ({{ item.name }})
ansible.builtin.copy: ansible.builtin.copy:
src: /bin/echo src: /bin/echo
dest: "{{ item.copy_to }}/echo" dest: "{{ item.copy_to }}/echo"
@ -11,7 +11,7 @@
remote_src: true remote_src: true
when: item.copy_to is defined when: item.copy_to is defined
- name: test cmd_echo module ({{ item.name }}) - name: Test cmd_echo module ({{ item.name }})
cmd_echo: cmd_echo:
cmd: "{{ item.cmd | default(omit) }}" cmd: "{{ item.cmd | default(omit) }}"
path_prefix: "{{ item.path_prefix | default(omit) }}" path_prefix: "{{ item.path_prefix | default(omit) }}"
@ -24,6 +24,10 @@
check_mode: "{{ item.check_mode | default(omit) }}" check_mode: "{{ item.check_mode | default(omit) }}"
ignore_errors: "{{ item.expect_error | default(omit) }}" ignore_errors: "{{ item.expect_error | default(omit) }}"
- name: check results ({{ item.name }}) - name: Debug test results ({{ item.name }})
ansible.builtin.debug:
var: test_result
- name: Check results ({{ item.name }})
_unsafe_assert: _unsafe_assert:
that: "{{ item.assertions }}" that: "{{ item.assertions }}"

View File

@ -38,22 +38,12 @@ cmd_echo_tests:
- test_result.out == "-- --answer=43 --bb-here\n" - test_result.out == "-- --answer=43 --bb-here\n"
- test_result.err == "" - test_result.err == ""
- name: implicit aa format
arg_formats:
bb:
func: as_bool
args: [--bb-here]
arg_order: ['aa', 'bb']
arg_values:
bb: true
aa: 1984
assertions:
- test_result.rc == 0
- test_result.out == "-- --aa 1984 --bb-here\n"
- test_result.err == ""
- name: missing bb format - name: missing bb format
arg_order: ['aa', 'bb'] arg_order: ['aa', 'bb']
arg_formats:
aa:
func: as_opt_eq_val
args: [--answer]
arg_values: arg_values:
bb: true bb: true
aa: 1984 aa: 1984
@ -69,6 +59,9 @@ cmd_echo_tests:
- name: missing bb value - name: missing bb value
arg_formats: arg_formats:
aa:
func: as_opt_eq_val
args: [--answer]
bb: bb:
func: as_bool func: as_bool
args: [--bb-here] args: [--bb-here]

View File

@ -6,7 +6,6 @@
from __future__ import (absolute_import, division, print_function) from __future__ import (absolute_import, division, print_function)
__metaclass__ = type __metaclass__ = type
from sys import version_info
from functools import partial from functools import partial
import pytest import pytest
@ -40,10 +39,6 @@ TC_FORMATS = dict(
simple_list_max_len_ok=(partial(cmd_runner_fmt.as_list, max_len=1), 42, ["42"], None), 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_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_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_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_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_none=(partial(cmd_runner_fmt.as_fixed, ["--always-here", "--forever"]), None, ["--always-here", "--forever"], None),
@ -52,16 +47,6 @@ TC_FORMATS = dict(
stack_opt_val__str=(partial(cmd_runner_fmt.stack(cmd_runner_fmt.as_opt_val), "-t"), ["potatoes", "bananas"], ["-t", "potatoes", "-t", "bananas"], None), stack_opt_val__str=(partial(cmd_runner_fmt.stack(cmd_runner_fmt.as_opt_val), "-t"), ["potatoes", "bananas"], ["-t", "potatoes", "-t", "bananas"], None),
stack_opt_eq_val__int=(partial(cmd_runner_fmt.stack(cmd_runner_fmt.as_opt_eq_val), "--answer"), [42, 17], ["--answer=42", "--answer=17"], None), stack_opt_eq_val__int=(partial(cmd_runner_fmt.stack(cmd_runner_fmt.as_opt_eq_val), "--answer"), [42, 17], ["--answer=42", "--answer=17"], 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
partial(cmd_runner_fmt.as_default_type, "dict"),
OrderedDict((('a', 1), ('b', 2))),
["--a=1", "--b=2"],
None
)
TC_FORMATS_IDS = sorted(TC_FORMATS.keys()) TC_FORMATS_IDS = sorted(TC_FORMATS.keys())