From 3b87f5807051338a8d980298b288c094f32a6383 Mon Sep 17 00:00:00 2001 From: Alexei Znamensky <103110+russoz@users.noreply.github.com> Date: Tue, 24 Oct 2023 10:04:18 +1300 Subject: [PATCH] Test helper more (#7421) * facter: add option to return facts instead of output variables * Update plugins/modules/facter.py Co-authored-by: Felix Fontein * add output doc * add changelog frag * reversed facter.py, created facter_facts.py * remove chglog fragment for new module * test helper: add Helper.from_module() * change case in variable name --------- Co-authored-by: Felix Fontein --- tests/unit/plugins/modules/helper.py | 11 +++++++++-- tests/unit/plugins/modules/test_cpanm.py | 2 +- tests/unit/plugins/modules/test_facter_facts.py | 2 +- tests/unit/plugins/modules/test_gconftool2.py | 2 +- tests/unit/plugins/modules/test_gconftool2_info.py | 2 +- tests/unit/plugins/modules/test_gio_mime.py | 2 +- tests/unit/plugins/modules/test_opkg.py | 2 +- tests/unit/plugins/modules/test_puppet.py | 2 +- tests/unit/plugins/modules/test_xfconf.py | 2 +- tests/unit/plugins/modules/test_xfconf_info.py | 2 +- 10 files changed, 18 insertions(+), 11 deletions(-) diff --git a/tests/unit/plugins/modules/helper.py b/tests/unit/plugins/modules/helper.py index db9ebe3981..18991cc30e 100644 --- a/tests/unit/plugins/modules/helper.py +++ b/tests/unit/plugins/modules/helper.py @@ -103,10 +103,17 @@ class Helper(object): @staticmethod def from_file(module_main, filename): - with open(filename, "r") as TEST_CASES: - helper = Helper(module_main, test_cases=TEST_CASES) + with open(filename, "r") as test_cases: + helper = Helper(module_main, test_cases=test_cases) return helper + @staticmethod + def from_module(module): + basename = module.__name__.split(".")[-1] + test_spec = "tests/unit/plugins/modules/test_{0}.yaml".format(basename) + helper = Helper.from_file(module.main, test_spec) + return helper + def __init__(self, module_main, test_cases): self.module_main = module_main self._test_cases = test_cases diff --git a/tests/unit/plugins/modules/test_cpanm.py b/tests/unit/plugins/modules/test_cpanm.py index 7f8c8660a2..3102f7682f 100644 --- a/tests/unit/plugins/modules/test_cpanm.py +++ b/tests/unit/plugins/modules/test_cpanm.py @@ -17,6 +17,6 @@ from ansible_collections.community.general.plugins.modules import cpanm from .helper import Helper -helper = Helper.from_file(cpanm.main, "tests/unit/plugins/modules/test_cpanm.yaml") +helper = Helper.from_module(cpanm) patch_bin = helper.cmd_fixture test_module = helper.test_module diff --git a/tests/unit/plugins/modules/test_facter_facts.py b/tests/unit/plugins/modules/test_facter_facts.py index 311030465f..8afb887e11 100644 --- a/tests/unit/plugins/modules/test_facter_facts.py +++ b/tests/unit/plugins/modules/test_facter_facts.py @@ -11,6 +11,6 @@ from ansible_collections.community.general.plugins.modules import facter_facts from .helper import Helper -helper = Helper.from_file(facter_facts.main, "tests/unit/plugins/modules/test_facter_facts.yaml") +helper = Helper.from_module(facter_facts) patch_bin = helper.cmd_fixture test_module = helper.test_module diff --git a/tests/unit/plugins/modules/test_gconftool2.py b/tests/unit/plugins/modules/test_gconftool2.py index 11e6255a0a..5ba95c0e22 100644 --- a/tests/unit/plugins/modules/test_gconftool2.py +++ b/tests/unit/plugins/modules/test_gconftool2.py @@ -11,6 +11,6 @@ from ansible_collections.community.general.plugins.modules import gconftool2 from .helper import Helper -helper = Helper.from_file(gconftool2.main, "tests/unit/plugins/modules/test_gconftool2.yaml") +helper = Helper.from_module(gconftool2) patch_bin = helper.cmd_fixture test_module = helper.test_module diff --git a/tests/unit/plugins/modules/test_gconftool2_info.py b/tests/unit/plugins/modules/test_gconftool2_info.py index d7e0670673..9e79ee8bad 100644 --- a/tests/unit/plugins/modules/test_gconftool2_info.py +++ b/tests/unit/plugins/modules/test_gconftool2_info.py @@ -11,6 +11,6 @@ from ansible_collections.community.general.plugins.modules import gconftool2_inf from .helper import Helper -helper = Helper.from_file(gconftool2_info.main, "tests/unit/plugins/modules/test_gconftool2_info.yaml") +helper = Helper.from_module(gconftool2_info) patch_bin = helper.cmd_fixture test_module = helper.test_module diff --git a/tests/unit/plugins/modules/test_gio_mime.py b/tests/unit/plugins/modules/test_gio_mime.py index 664a4290c8..c81e540df4 100644 --- a/tests/unit/plugins/modules/test_gio_mime.py +++ b/tests/unit/plugins/modules/test_gio_mime.py @@ -11,6 +11,6 @@ from ansible_collections.community.general.plugins.modules import gio_mime from .helper import Helper -helper = Helper.from_file(gio_mime.main, "tests/unit/plugins/modules/test_gio_mime.yaml") +helper = Helper.from_module(gio_mime) patch_bin = helper.cmd_fixture test_module = helper.test_module diff --git a/tests/unit/plugins/modules/test_opkg.py b/tests/unit/plugins/modules/test_opkg.py index 03c160e00d..ecdb6fdc35 100644 --- a/tests/unit/plugins/modules/test_opkg.py +++ b/tests/unit/plugins/modules/test_opkg.py @@ -11,6 +11,6 @@ from ansible_collections.community.general.plugins.modules import opkg from .helper import Helper -helper = Helper.from_file(opkg.main, "tests/unit/plugins/modules/test_opkg.yaml") +helper = Helper.from_module(opkg) patch_bin = helper.cmd_fixture test_module = helper.test_module diff --git a/tests/unit/plugins/modules/test_puppet.py b/tests/unit/plugins/modules/test_puppet.py index ff8b894e89..728b2a31e0 100644 --- a/tests/unit/plugins/modules/test_puppet.py +++ b/tests/unit/plugins/modules/test_puppet.py @@ -17,6 +17,6 @@ from ansible_collections.community.general.plugins.modules import puppet from .helper import Helper -helper = Helper.from_file(puppet.main, "tests/unit/plugins/modules/test_puppet.yaml") +helper = Helper.from_module(puppet) patch_bin = helper.cmd_fixture test_module = helper.test_module diff --git a/tests/unit/plugins/modules/test_xfconf.py b/tests/unit/plugins/modules/test_xfconf.py index 5a082fccc3..350b411baf 100644 --- a/tests/unit/plugins/modules/test_xfconf.py +++ b/tests/unit/plugins/modules/test_xfconf.py @@ -17,6 +17,6 @@ from ansible_collections.community.general.plugins.modules import xfconf from .helper import Helper -helper = Helper.from_file(xfconf.main, "tests/unit/plugins/modules/test_xfconf.yaml") +helper = Helper.from_module(xfconf) patch_bin = helper.cmd_fixture test_module = helper.test_module diff --git a/tests/unit/plugins/modules/test_xfconf_info.py b/tests/unit/plugins/modules/test_xfconf_info.py index d59da8fa08..42c911c77f 100644 --- a/tests/unit/plugins/modules/test_xfconf_info.py +++ b/tests/unit/plugins/modules/test_xfconf_info.py @@ -10,6 +10,6 @@ from ansible_collections.community.general.plugins.modules import xfconf_info from .helper import Helper -helper = Helper.from_file(xfconf_info.main, "tests/unit/plugins/modules/test_xfconf_info.yaml") +helper = Helper.from_module(xfconf_info) patch_bin = helper.cmd_fixture test_module = helper.test_module