From 14bc13ba3c27f828c105cc57af705634de58d646 Mon Sep 17 00:00:00 2001 From: Alexei Znamensky <103110+russoz@users.noreply.github.com> Date: Sat, 2 Sep 2023 14:26:39 +1200 Subject: [PATCH] further improvements (#7192) * further improvements * some renaming --- .../plugins/modules/cmd_runner_test_utils.py | 20 +++++++++++++++---- tests/unit/plugins/modules/test_opkg.py | 16 ++++----------- tests/unit/plugins/modules/test_snap.py | 14 +++---------- tests/unit/plugins/modules/test_xfconf.py | 16 ++++----------- .../unit/plugins/modules/test_xfconf_info.py | 16 ++++----------- 5 files changed, 31 insertions(+), 51 deletions(-) diff --git a/tests/unit/plugins/modules/cmd_runner_test_utils.py b/tests/unit/plugins/modules/cmd_runner_test_utils.py index 1d097b8c7f..83644a699f 100644 --- a/tests/unit/plugins/modules/cmd_runner_test_utils.py +++ b/tests/unit/plugins/modules/cmd_runner_test_utils.py @@ -6,6 +6,7 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type +import json from collections import namedtuple from itertools import chain, repeat @@ -18,7 +19,8 @@ RunCmdCall = namedtuple("RunCmdCall", ["command", "environ", "rc", "out", "err"] class CmdRunnerTestHelper(object): - def __init__(self, test_cases): + def __init__(self, module_main, test_cases): + self.module_main = module_main self._test_cases = test_cases if isinstance(test_cases, (list, tuple)): self.testcases = test_cases @@ -54,15 +56,16 @@ class CmdRunnerTestHelper(object): def testcases_ids(self): return [item.id for item in self.testcases] - def __call__(self, testcase, mocker): - return _Context(self, testcase, mocker) + def __call__(self, *args, **kwargs): + return _Context(self, *args, **kwargs) class _Context(object): - def __init__(self, helper, testcase, mocker): + def __init__(self, helper, testcase, mocker, capfd): self.helper = helper self.testcase = testcase self.mocker = mocker + self.capfd = capfd self.run_cmd_calls = self.testcase.run_command_calls self.mock_run_cmd = self._make_mock_run_cmd() @@ -82,6 +85,15 @@ class _Context(object): def __exit__(self, exc_type, exc_val, exc_tb): return False + def run(self): + with pytest.raises(SystemExit): + self.helper.module_main() + + out, err = self.capfd.readouterr() + results = json.loads(out) + + self.check_results(results) + def check_results(self, results): print("testcase =\n%s" % str(self.testcase)) print("results =\n%s" % results) diff --git a/tests/unit/plugins/modules/test_opkg.py b/tests/unit/plugins/modules/test_opkg.py index b33c8f97c0..9683de0254 100644 --- a/tests/unit/plugins/modules/test_opkg.py +++ b/tests/unit/plugins/modules/test_opkg.py @@ -6,16 +6,15 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json -from ansible_collections.community.general.plugins.modules import opkg as module import pytest +from ansible_collections.community.general.plugins.modules import opkg as module from .cmd_runner_test_utils import CmdRunnerTestHelper with open("tests/unit/plugins/modules/test_opkg.yaml", "r") as TEST_CASES: - helper = CmdRunnerTestHelper(test_cases=TEST_CASES) + helper = CmdRunnerTestHelper(module.main, test_cases=TEST_CASES) patch_bin = helper.cmd_fixture @@ -28,12 +27,5 @@ def test_module(mocker, capfd, patch_bin, testcase): Run unit tests for test cases listed in TEST_CASES """ - with helper(testcase, mocker) as ctx: - # Try to run test case - with pytest.raises(SystemExit): - module.main() - - out, err = capfd.readouterr() - results = json.loads(out) - - ctx.check_results(results) + with helper(testcase, mocker, capfd) as testcase_context: + testcase_context.run() diff --git a/tests/unit/plugins/modules/test_snap.py b/tests/unit/plugins/modules/test_snap.py index b01732b300..b32214b6a4 100644 --- a/tests/unit/plugins/modules/test_snap.py +++ b/tests/unit/plugins/modules/test_snap.py @@ -6,7 +6,6 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json import pytest from .cmd_runner_test_utils import CmdRunnerTestHelper, ModuleTestCase, RunCmdCall @@ -444,7 +443,7 @@ TEST_CASES = [ ] -helper = CmdRunnerTestHelper(test_cases=TEST_CASES) +helper = CmdRunnerTestHelper(module.main, test_cases=TEST_CASES) patch_bin = helper.cmd_fixture @@ -457,12 +456,5 @@ def test_module(mocker, capfd, patch_bin, testcase): Run unit tests for test cases listed in TEST_CASES """ - with helper(testcase, mocker) as ctx: - # Try to run test case - with pytest.raises(SystemExit): - module.main() - - out, err = capfd.readouterr() - results = json.loads(out) - - ctx.check_results(results) + with helper(testcase, mocker, capfd) as testcase_context: + testcase_context.run() diff --git a/tests/unit/plugins/modules/test_xfconf.py b/tests/unit/plugins/modules/test_xfconf.py index 3abd93077d..9d8785c580 100644 --- a/tests/unit/plugins/modules/test_xfconf.py +++ b/tests/unit/plugins/modules/test_xfconf.py @@ -12,16 +12,15 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json -from ansible_collections.community.general.plugins.modules import xfconf as module import pytest +from ansible_collections.community.general.plugins.modules import xfconf as module from .cmd_runner_test_utils import CmdRunnerTestHelper with open("tests/unit/plugins/modules/test_xfconf.yaml", "r") as TEST_CASES: - helper = CmdRunnerTestHelper(test_cases=TEST_CASES) + helper = CmdRunnerTestHelper(module.main, test_cases=TEST_CASES) patch_bin = helper.cmd_fixture @@ -34,12 +33,5 @@ def test_module(mocker, capfd, patch_bin, testcase): Run unit tests for test cases listed in TEST_CASES """ - with helper(testcase, mocker) as ctx: - # Try to run test case - with pytest.raises(SystemExit): - module.main() - - out, err = capfd.readouterr() - results = json.loads(out) - - ctx.check_results(results) + with helper(testcase, mocker, capfd) as testcase_context: + testcase_context.run() diff --git a/tests/unit/plugins/modules/test_xfconf_info.py b/tests/unit/plugins/modules/test_xfconf_info.py index e9c227c54a..77bbfebe20 100644 --- a/tests/unit/plugins/modules/test_xfconf_info.py +++ b/tests/unit/plugins/modules/test_xfconf_info.py @@ -5,16 +5,15 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json -from ansible_collections.community.general.plugins.modules import xfconf_info as module import pytest +from ansible_collections.community.general.plugins.modules import xfconf_info as module from .cmd_runner_test_utils import CmdRunnerTestHelper with open("tests/unit/plugins/modules/test_xfconf_info.yaml", "r") as TEST_CASES: - helper = CmdRunnerTestHelper(test_cases=TEST_CASES) + helper = CmdRunnerTestHelper(module.main, test_cases=TEST_CASES) patch_bin = helper.cmd_fixture @@ -27,12 +26,5 @@ def test_module(mocker, capfd, patch_bin, testcase): Run unit tests for test cases listed in TEST_CASES """ - with helper(testcase, mocker) as ctx: - # Try to run test case - with pytest.raises(SystemExit): - module.main() - - out, err = capfd.readouterr() - results = json.loads(out) - - ctx.check_results(results) + with helper(testcase, mocker, capfd) as testcase_context: + testcase_context.run()