From d6ebba1aeadcde27a2e937a7b748f3ab9bd833c4 Mon Sep 17 00:00:00 2001 From: Alexei Znamensky <103110+russoz@users.noreply.github.com> Date: Fri, 1 Sep 2023 16:32:11 +1200 Subject: [PATCH] cmd tests improvement (#7181) * cmd tests improvement * fix sanity * remove commented line * fixed class init code --- tests/unit/plugins/modules/cmd_runner_test_utils.py | 13 ++++++++----- tests/unit/plugins/modules/test_opkg.py | 2 +- tests/unit/plugins/modules/test_xfconf.py | 2 +- tests/unit/plugins/modules/test_xfconf_info.py | 2 +- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/tests/unit/plugins/modules/cmd_runner_test_utils.py b/tests/unit/plugins/modules/cmd_runner_test_utils.py index 189cba730d..1d097b8c7f 100644 --- a/tests/unit/plugins/modules/cmd_runner_test_utils.py +++ b/tests/unit/plugins/modules/cmd_runner_test_utils.py @@ -6,7 +6,6 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import os from collections import namedtuple from itertools import chain, repeat @@ -19,16 +18,20 @@ RunCmdCall = namedtuple("RunCmdCall", ["command", "environ", "rc", "out", "err"] class CmdRunnerTestHelper(object): - def __init__(self, command, test_cases): - self.command = command + def __init__(self, test_cases): self._test_cases = test_cases - self.testcases = self._make_test_cases() + if isinstance(test_cases, (list, tuple)): + self.testcases = test_cases + else: + self.testcases = self._make_test_cases() @property def cmd_fixture(self): @pytest.fixture def patch_bin(mocker): - mocker.patch('ansible.module_utils.basic.AnsibleModule.get_bin_path', return_value=os.path.join('/testbin', self.command)) + def mockie(self, path, *args, **kwargs): + return "/testbin/{0}".format(path) + mocker.patch('ansible.module_utils.basic.AnsibleModule.get_bin_path', mockie) return patch_bin diff --git a/tests/unit/plugins/modules/test_opkg.py b/tests/unit/plugins/modules/test_opkg.py index a2b1fc9d74..9085a5d17f 100644 --- a/tests/unit/plugins/modules/test_opkg.py +++ b/tests/unit/plugins/modules/test_opkg.py @@ -16,7 +16,7 @@ from .cmd_runner_test_utils import CmdRunnerTestHelper TESTED_MODULE = module.__name__ with open("tests/unit/plugins/modules/test_opkg.yaml", "r") as TEST_CASES: - helper = CmdRunnerTestHelper(command="opkg", test_cases=TEST_CASES) + helper = CmdRunnerTestHelper(test_cases=TEST_CASES) patch_bin = helper.cmd_fixture diff --git a/tests/unit/plugins/modules/test_xfconf.py b/tests/unit/plugins/modules/test_xfconf.py index 0f00a3f610..be622462ef 100644 --- a/tests/unit/plugins/modules/test_xfconf.py +++ b/tests/unit/plugins/modules/test_xfconf.py @@ -22,7 +22,7 @@ from .cmd_runner_test_utils import CmdRunnerTestHelper TESTED_MODULE = module.__name__ with open("tests/unit/plugins/modules/test_xfconf.yaml", "r") as TEST_CASES: - helper = CmdRunnerTestHelper(command="xfconf-query", test_cases=TEST_CASES) + helper = CmdRunnerTestHelper(test_cases=TEST_CASES) patch_bin = helper.cmd_fixture diff --git a/tests/unit/plugins/modules/test_xfconf_info.py b/tests/unit/plugins/modules/test_xfconf_info.py index 2727d2476a..cb8b8f1da0 100644 --- a/tests/unit/plugins/modules/test_xfconf_info.py +++ b/tests/unit/plugins/modules/test_xfconf_info.py @@ -15,7 +15,7 @@ from .cmd_runner_test_utils import CmdRunnerTestHelper TESTED_MODULE = module.__name__ with open("tests/unit/plugins/modules/test_xfconf_info.yaml", "r") as TEST_CASES: - helper = CmdRunnerTestHelper(command="xfconf-query", test_cases=TEST_CASES) + helper = CmdRunnerTestHelper(test_cases=TEST_CASES) patch_bin = helper.cmd_fixture