mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
parent
c2d3302fc4
commit
14bc13ba3c
5 changed files with 31 additions and 51 deletions
|
@ -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)
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in a new issue