From 769658d1abdac8c41bf1aea0ef09e395f3f73a78 Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Mon, 19 Jul 2021 07:45:18 +0100 Subject: [PATCH] Drop use of pytest-helpers-namespace (#3195) Related: https://github.com/saltstack/pytest-helpers-namespace/issues/10 --- conftest.py | 2 -- setup.cfg | 1 - src/molecule/config.py | 4 +-- src/molecule/test/conftest.py | 18 ++++------ src/molecule/test/functional/conftest.py | 36 +++++++------------ src/molecule/test/functional/test_command.py | 34 ++++++++++++------ src/molecule/test/unit/conftest.py | 23 +++++++----- .../test/unit/provisioner/test_ansible.py | 7 ++-- .../provisioner/test_ansible_playbooks.py | 5 +-- src/molecule/test/unit/test_util.py | 9 ++--- 10 files changed, 71 insertions(+), 68 deletions(-) diff --git a/conftest.py b/conftest.py index f9fbb69754..01e0408b6f 100644 --- a/conftest.py +++ b/conftest.py @@ -6,8 +6,6 @@ import pytest -pytest_plugins = ["helpers_namespace"] - @pytest.fixture(scope="session", autouse=True) def environ(): diff --git a/setup.cfg b/setup.cfg index 423d6fa378..78656954aa 100644 --- a/setup.cfg +++ b/setup.cfg @@ -112,7 +112,6 @@ test = pexpect >= 4.8.0, < 5 pytest-cov >= 2.10.1 - pytest-helpers-namespace >= 2019.1.8 pytest-html >= 3.0.0 pytest-mock >= 3.3.1 pytest-plus >= 0.2 diff --git a/src/molecule/config.py b/src/molecule/config.py index d7e1682f55..0c1bfc41b7 100644 --- a/src/molecule/config.py +++ b/src/molecule/config.py @@ -433,12 +433,12 @@ def _validate(self): util.sysexit_with_message(msg) -def molecule_directory(path): +def molecule_directory(path: str) -> str: """Return directory of the current scenario.""" return os.path.join(path, MOLECULE_DIRECTORY) -def molecule_file(path): +def molecule_file(path: str) -> str: """Return file path of current scenario.""" return os.path.join(path, MOLECULE_FILE) diff --git a/src/molecule/test/conftest.py b/src/molecule/test/conftest.py index cdfa00f871..5ec60d42a6 100644 --- a/src/molecule/test/conftest.py +++ b/src/molecule/test/conftest.py @@ -74,33 +74,27 @@ def resources_folder_path(): return resources_folder_path -@pytest.helpers.register -def molecule_project_directory(): +def molecule_project_directory() -> str: return os.getcwd() -@pytest.helpers.register -def molecule_directory(): +def molecule_directory() -> str: return config.molecule_directory(molecule_project_directory()) -@pytest.helpers.register -def molecule_scenario_directory(): +def molecule_scenario_directory() -> str: return os.path.join(molecule_directory(), "default") -@pytest.helpers.register -def molecule_file(): +def molecule_file() -> str: return get_molecule_file(molecule_scenario_directory()) -@pytest.helpers.register -def get_molecule_file(path): +def get_molecule_file(path: str) -> str: return config.molecule_file(path) -@pytest.helpers.register -def molecule_ephemeral_directory(_fixture_uuid): +def molecule_ephemeral_directory(_fixture_uuid) -> str: project_directory = "test-project-{}".format(_fixture_uuid) scenario_name = "test-instance" diff --git a/src/molecule/test/functional/conftest.py b/src/molecule/test/functional/conftest.py index 4f405d5621..46c5003131 100644 --- a/src/molecule/test/functional/conftest.py +++ b/src/molecule/test/functional/conftest.py @@ -23,6 +23,7 @@ import shutil import subprocess from subprocess import PIPE +from typing import Optional import pexpect import pkg_resources @@ -30,7 +31,7 @@ from molecule import logger, util from molecule.config import ansible_version -from molecule.test.conftest import change_dir_to +from molecule.test.conftest import change_dir_to, molecule_directory from molecule.text import strip_ansi_color from molecule.util import run_command @@ -92,7 +93,6 @@ def skip_test(request, driver_name): pass -@pytest.helpers.register def idempotence(scenario_name): cmd = ["molecule", "create", "--scenario-name", scenario_name] assert run_command(cmd).returncode == 0 @@ -104,31 +104,29 @@ def idempotence(scenario_name): assert run_command(cmd).returncode == 0 -@pytest.helpers.register def init_role(temp_dir, driver_name): role_directory = os.path.join(temp_dir.strpath, "myorg.myrole") cmd = ["molecule", "init", "role", "myorg.myrole", "--driver-name", driver_name] assert run_command(cmd).returncode == 0 - pytest.helpers.metadata_lint_update(role_directory) + metadata_lint_update(role_directory) with change_dir_to(role_directory): cmd = ["molecule", "test", "--all"] assert run_command(cmd).returncode == 0 -@pytest.helpers.register def init_scenario(temp_dir, driver_name): # Create role role_directory = os.path.join(temp_dir.strpath, "test-init") cmd = ["molecule", "init", "role", "test-init", "--driver-name", driver_name] assert run_command(cmd).returncode == 0 - pytest.helpers.metadata_lint_update(role_directory) + metadata_lint_update(role_directory) with change_dir_to(role_directory): # Create scenario - molecule_directory = pytest.helpers.molecule_directory() - scenario_directory = os.path.join(molecule_directory, "test-scenario") + molecule_dir = molecule_directory() + scenario_directory = os.path.join(molecule_dir, "test-scenario") cmd = [ "molecule", @@ -148,17 +146,15 @@ def init_scenario(temp_dir, driver_name): assert run_command(cmd).returncode == 0 -@pytest.helpers.register -def metadata_lint_update(role_directory): +def metadata_lint_update(role_directory: str) -> None: # By default, ansible-lint will fail on newly-created roles because the # fields in this file have not been changed from their defaults. This is # good because molecule should create this file using the defaults, and # users should receive feedback to change these defaults. However, this # blocks the testing of 'molecule init' itself, so ansible-lint should # be configured to ignore these metadata lint errors. - ansible_lint_src = os.path.join( - os.path.dirname(util.abs_path(__file__)), ".ansible-lint" - ) + dirname = os.path.dirname(os.path.abspath(__file__)) + ansible_lint_src = os.path.join(dirname, ".ansible-lint") shutil.copy(ansible_lint_src, role_directory) # Explicitly lint here to catch any unexpected lint errors before @@ -170,8 +166,7 @@ def metadata_lint_update(role_directory): assert run_command(cmd).returncode == 0 -@pytest.helpers.register -def list(x): +def list_cmd(x): cmd = ["molecule", "list"] result = run_command(cmd) assert result.returncode == 0 @@ -181,7 +176,6 @@ def list(x): assert l in out -@pytest.helpers.register def list_with_format_plain(x): cmd = ["molecule", "list", "--format", "plain"] result = util.run_command(cmd) @@ -191,7 +185,6 @@ def list_with_format_plain(x): assert l in out -@pytest.helpers.register def login(login_args, scenario_name="default"): cmd = ["molecule", "destroy", "--scenario-name", scenario_name] assert run_command(cmd).returncode == 0 @@ -212,8 +205,7 @@ def login(login_args, scenario_name="default"): child.sendline("exit") -@pytest.helpers.register -def test(driver_name, scenario_name="default", parallel=False): +def run_test(driver_name, scenario_name="default", parallel=False): cmd = ["molecule", "test", "--scenario-name", scenario_name] if driver_name != "delegated": if scenario_name is None: @@ -224,7 +216,6 @@ def test(driver_name, scenario_name="default", parallel=False): assert run_command(cmd).returncode == 0 -@pytest.helpers.register def verify(scenario_name="default"): cmd = ["molecule", "create", "--scenario-name", scenario_name] assert run_command(cmd).returncode == 0 @@ -236,7 +227,7 @@ def verify(scenario_name="default"): assert run_command(cmd).returncode == 0 -def get_docker_executable(): +def get_docker_executable() -> Optional[str]: return shutil.which("docker") @@ -244,9 +235,8 @@ def get_virtualbox_executable(): return shutil.which("VBoxManage") -@pytest.helpers.register @util.lru_cache() -def supports_docker(): +def supports_docker() -> bool: docker = get_docker_executable() if docker: result = subprocess.run([docker, "info"], stdout=PIPE, universal_newlines=True) diff --git a/src/molecule/test/functional/test_command.py b/src/molecule/test/functional/test_command.py index baf5b695ac..f1ac808c3d 100644 --- a/src/molecule/test/functional/test_command.py +++ b/src/molecule/test/functional/test_command.py @@ -18,8 +18,19 @@ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # DEALINGS IN THE SOFTWARE. -import pytest +from typing import Optional +import pytest +from pytest import FixtureRequest + +from molecule.test.functional.conftest import ( + idempotence, + init_role, + init_scenario, + list_with_format_plain, + run_test, + verify, +) from molecule.util import run_command @@ -37,8 +48,11 @@ def scenario_name(request): @pytest.fixture -def driver_name(request): - return request.param +def driver_name(request: FixtureRequest) -> Optional[str]: + try: + return request.param + except AttributeError: + return None @pytest.mark.extensive @@ -146,19 +160,19 @@ def test_command_destroy(scenario_to_test, with_scenario, scenario_name): indirect=["scenario_to_test", "driver_name", "scenario_name"], ) def test_command_idempotence(scenario_to_test, with_scenario, scenario_name): - pytest.helpers.idempotence(scenario_name) + idempotence(scenario_name) @pytest.mark.parametrize("driver_name", [("delegated")], indirect=["driver_name"]) @pytest.mark.xfail(reason="https://github.com/ansible-community/molecule/issues/3171") def test_command_init_role(temp_dir, driver_name, skip_test): - pytest.helpers.init_role(temp_dir, driver_name) + init_role(temp_dir, driver_name) @pytest.mark.parametrize("driver_name", [("delegated")], indirect=["driver_name"]) @pytest.mark.xfail(reason="https://github.com/ansible-community/molecule/issues/3171") def test_command_init_scenario(temp_dir, driver_name, skip_test): - pytest.helpers.init_scenario(temp_dir, driver_name) + init_scenario(temp_dir, driver_name) @pytest.mark.extensive @@ -186,7 +200,7 @@ def test_command_lint(scenario_to_test, with_scenario, scenario_name): indirect=["scenario_to_test", "driver_name"], ) def test_command_list_with_format_plain(scenario_to_test, with_scenario, expected): - pytest.helpers.list_with_format_plain(expected) + list_with_format_plain(expected) # @pytest.mark.parametrize( @@ -202,7 +216,7 @@ def test_command_list_with_format_plain(scenario_to_test, with_scenario, expecte # indirect=["scenario_to_test", "driver_name", "scenario_name"], # ) # def test_command_login(scenario_to_test, with_scenario, login_args, scenario_name): -# pytest.helpers.login(login_args, scenario_name) +# login(login_args, scenario_name) @pytest.mark.extensive @@ -255,7 +269,7 @@ def test_command_syntax(scenario_to_test, with_scenario, scenario_name): indirect=["scenario_to_test", "driver_name", "scenario_name"], ) def test_command_test(scenario_to_test, with_scenario, scenario_name, driver_name): - pytest.helpers.test(driver_name, scenario_name) + run_test(driver_name, scenario_name) @pytest.mark.extensive @@ -267,4 +281,4 @@ def test_command_test(scenario_to_test, with_scenario, scenario_name, driver_nam indirect=["scenario_to_test", "driver_name", "scenario_name"], ) def test_command_verify(scenario_to_test, with_scenario, scenario_name): - pytest.helpers.verify(scenario_name) + verify(scenario_name) diff --git a/src/molecule/test/unit/conftest.py b/src/molecule/test/unit/conftest.py index 4daca0edf4..90a128979f 100644 --- a/src/molecule/test/unit/conftest.py +++ b/src/molecule/test/unit/conftest.py @@ -24,20 +24,25 @@ import shutil from pathlib import Path from subprocess import CompletedProcess +from typing import Any, Tuple from uuid import uuid4 import pytest from molecule import config, util +from molecule.test.conftest import ( + molecule_directory, + molecule_ephemeral_directory, + molecule_file, + molecule_scenario_directory, +) -@pytest.helpers.register -def write_molecule_file(filename, data): +def write_molecule_file(filename: str, data: Any) -> None: util.write_file(filename, util.safe_dump(data)) -@pytest.helpers.register -def os_split(s): +def os_split(s: str) -> Tuple[str, ...]: rest, tail = os.path.split(s) if rest in ("", os.path.sep): return (tail,) @@ -109,12 +114,12 @@ def molecule_data( @pytest.fixture def molecule_directory_fixture(temp_dir): - return pytest.helpers.molecule_directory() + return molecule_directory() @pytest.fixture def molecule_scenario_directory_fixture(molecule_directory_fixture): - path = pytest.helpers.molecule_scenario_directory() + path = molecule_scenario_directory() if not os.path.isdir(path): os.makedirs(path) @@ -123,7 +128,7 @@ def molecule_scenario_directory_fixture(molecule_directory_fixture): @pytest.fixture def molecule_ephemeral_directory_fixture(molecule_scenario_directory_fixture): - path = pytest.helpers.molecule_ephemeral_directory(str(uuid4())) + path = molecule_ephemeral_directory(str(uuid4())) if not os.path.isdir(path): os.makedirs(path) yield @@ -134,7 +139,7 @@ def molecule_ephemeral_directory_fixture(molecule_scenario_directory_fixture): def molecule_file_fixture( molecule_scenario_directory_fixture, molecule_ephemeral_directory_fixture ): - return pytest.helpers.molecule_file() + return molecule_file() @pytest.fixture @@ -144,7 +149,7 @@ def config_instance( mdc = copy.deepcopy(molecule_data) if hasattr(request, "param"): mdc = util.merge_dicts(mdc, request.getfixturevalue(request.param)) - pytest.helpers.write_molecule_file(molecule_file_fixture, mdc) + write_molecule_file(molecule_file_fixture, mdc) c = config.Config(molecule_file_fixture) c.command_args = {"subcommand": "test"} diff --git a/src/molecule/test/unit/provisioner/test_ansible.py b/src/molecule/test/unit/provisioner/test_ansible.py index e0b68f81fd..0bdde7d13d 100644 --- a/src/molecule/test/unit/provisioner/test_ansible.py +++ b/src/molecule/test/unit/provisioner/test_ansible.py @@ -26,6 +26,7 @@ from molecule import config, util from molecule.provisioner import ansible, ansible_playbooks +from molecule.test.unit.conftest import os_split @pytest.fixture @@ -319,7 +320,7 @@ def test_playbooks_property(_instance): def test_directory_property(_instance): result = _instance.directory - parts = pytest.helpers.os_split(result) + parts = os_split(result) assert ("molecule", "provisioner", "ansible") == parts[-3:] @@ -680,7 +681,7 @@ def test_default_to_regular(_instance): def test_get_plugin_directory(_instance): result = _instance._get_plugin_directory() - parts = pytest.helpers.os_split(result) + parts = os_split(result) assert ("molecule", "provisioner", "ansible", "plugins") == parts[-4:] @@ -719,7 +720,7 @@ def test_get_modules_directories_multi_ansible_library(_instance, monkeypatch): def test_get_filter_plugin_directory(_instance): result = _instance._get_filter_plugin_directory() - parts = pytest.helpers.os_split(result) + parts = os_split(result) x = ("molecule", "provisioner", "ansible", "plugins", "filter") assert x == parts[-5:] diff --git a/src/molecule/test/unit/provisioner/test_ansible_playbooks.py b/src/molecule/test/unit/provisioner/test_ansible_playbooks.py index 8fbfdfd5e3..aa8b8ecadb 100644 --- a/src/molecule/test/unit/provisioner/test_ansible_playbooks.py +++ b/src/molecule/test/unit/provisioner/test_ansible_playbooks.py @@ -24,6 +24,7 @@ from molecule import util from molecule.provisioner import ansible_playbooks +from molecule.test.unit.conftest import os_split @pytest.fixture @@ -74,7 +75,7 @@ def test_verify_property(_instance): def test_get_playbook_directory(_instance): result = _instance._get_playbook_directory() - parts = pytest.helpers.os_split(result) + parts = os_split(result) x = ("molecule", "provisioner", "ansible", "playbooks") assert x == parts[-4:] @@ -136,7 +137,7 @@ def test_get_ansible_playbook_with_driver_key_when_playbook_key_missing( def test_get_bundled_driver_playbook(_instance): result = _instance._get_bundled_driver_playbook("create") - parts = pytest.helpers.os_split(result) + parts = os_split(result) x = ("molecule", "driver", "playbooks", "create.yml") assert x == parts[-4:] diff --git a/src/molecule/test/unit/test_util.py b/src/molecule/test/unit/test_util.py index 98ad857321..69d85628f1 100644 --- a/src/molecule/test/unit/test_util.py +++ b/src/molecule/test/unit/test_util.py @@ -29,6 +29,7 @@ from molecule import util from molecule.console import console from molecule.constants import MOLECULE_HEADER +from molecule.test.conftest import get_molecule_file, molecule_directory from molecule.text import strip_ansi_escape @@ -164,14 +165,14 @@ def test_run_command_with_debug_handles_no_env(mocker, patched_print_debug): def test_os_walk(temp_dir): scenarios = ["scenario1", "scenario2", "scenario3"] - molecule_directory = pytest.helpers.molecule_directory() + mol_dir = molecule_directory() for scenario in scenarios: - scenario_directory = os.path.join(molecule_directory, scenario) - molecule_file = pytest.helpers.get_molecule_file(scenario_directory) + scenario_directory = os.path.join(mol_dir, scenario) + molecule_file = get_molecule_file(scenario_directory) os.makedirs(scenario_directory) util.write_file(molecule_file, "") - result = [f for f in util.os_walk(molecule_directory, "molecule.yml")] + result = [f for f in util.os_walk(mol_dir, "molecule.yml")] assert 3 == len(result)