From 09e2f772899c379ce646cf72598bd46707630182 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Sun, 29 Aug 2021 13:20:01 +0200 Subject: [PATCH] mh CmdMixin - added ArgFormat.BOOLEAN_NOT and logic (#3290) (#3294) * mh CmdMixin - added ArgFormat.BOOLEAN_NOT and logic * added changelog fragment (cherry picked from commit df8fdcda7901a40d7d48c17ec9ce35c3d43ecbfe) Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com> --- changelogs/fragments/3290-mh-cmd-boolean-not.yaml | 2 ++ plugins/module_utils/mh/mixins/cmd.py | 5 ++++- tests/unit/plugins/module_utils/test_module_helper.py | 10 ++++++++-- 3 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/3290-mh-cmd-boolean-not.yaml diff --git a/changelogs/fragments/3290-mh-cmd-boolean-not.yaml b/changelogs/fragments/3290-mh-cmd-boolean-not.yaml new file mode 100644 index 0000000000..ab34539f15 --- /dev/null +++ b/changelogs/fragments/3290-mh-cmd-boolean-not.yaml @@ -0,0 +1,2 @@ +minor_changes: + - module_helper cmd module utils - added the ``ArgFormat`` style ``BOOLEAN_NOT``, to add CLI parameters when the module argument is false-ish (https://github.com/ansible-collections/community.general/pull/3290). diff --git a/plugins/module_utils/mh/mixins/cmd.py b/plugins/module_utils/mh/mixins/cmd.py index aed4174c4f..51e5ae9873 100644 --- a/plugins/module_utils/mh/mixins/cmd.py +++ b/plugins/module_utils/mh/mixins/cmd.py @@ -16,6 +16,7 @@ class ArgFormat(object): BOOLEAN = 0 PRINTF = 1 FORMAT = 2 + BOOLEAN_NOT = 3 @staticmethod def stars_deco(num): @@ -50,12 +51,14 @@ class ArgFormat(object): _fmts = { ArgFormat.BOOLEAN: lambda _fmt, v: ([_fmt] if bool(v) else []), + ArgFormat.BOOLEAN_NOT: lambda _fmt, v: ([] if bool(v) else [_fmt]), ArgFormat.PRINTF: printf_fmt, ArgFormat.FORMAT: lambda _fmt, v: [_fmt.format(v)], } self.name = name self.stars = stars + self.style = style if fmt is None: fmt = "{0}" @@ -76,7 +79,7 @@ class ArgFormat(object): self.arg_format = (self.stars_deco(stars))(self.arg_format) def to_text(self, value): - if value is None: + if value is None and self.style != ArgFormat.BOOLEAN_NOT: return [] func = self.arg_format return [str(p) for p in func(value)] diff --git a/tests/unit/plugins/module_utils/test_module_helper.py b/tests/unit/plugins/module_utils/test_module_helper.py index f40a0f10ee..00667fcea3 100644 --- a/tests/unit/plugins/module_utils/test_module_helper.py +++ b/tests/unit/plugins/module_utils/test_module_helper.py @@ -24,6 +24,12 @@ ARG_FORMATS = dict( False, []), simple_boolean_none=("--superflag", ArgFormat.BOOLEAN, 0, None, []), + simple_boolean_not_true=("--superflag", ArgFormat.BOOLEAN_NOT, 0, + True, []), + simple_boolean_not_false=("--superflag", ArgFormat.BOOLEAN_NOT, 0, + False, ["--superflag"]), + simple_boolean_not_none=("--superflag", ArgFormat.BOOLEAN_NOT, 0, + None, ["--superflag"]), single_printf=("--param=%s", ArgFormat.PRINTF, 0, "potatoes", ["--param=potatoes"]), single_printf_no_substitution=("--param", ArgFormat.PRINTF, 0, @@ -65,7 +71,7 @@ def test_arg_format(fmt, style, stars, value, expected): af = ArgFormat('name', fmt, style, stars) actual = af.to_text(value) print("formatted string = {0}".format(actual)) - assert actual == expected + assert actual == expected, "actual = {0}".format(actual) ARG_FORMATS_FAIL = dict( @@ -218,7 +224,7 @@ CAUSE_CHG_DECO_IDS = sorted(CAUSE_CHG_DECO.keys()) @pytest.mark.parametrize(CAUSE_CHG_DECO_PARAMS, [[CAUSE_CHG_DECO[tc][param] - for param in CAUSE_CHG_DECO_PARAMS] + for param in CAUSE_CHG_DECO_PARAMS] for tc in CAUSE_CHG_DECO_IDS], ids=CAUSE_CHG_DECO_IDS) def test_cause_changes_deco(method, expect_exception, expect_changed):