Skip to content

Commit

Permalink
Drop use of pytest-helpers-namespace (#3195)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea committed Jul 19, 2021
1 parent 646cd29 commit 769658d
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 68 deletions.
2 changes: 0 additions & 2 deletions conftest.py
Expand Up @@ -6,8 +6,6 @@

import pytest

pytest_plugins = ["helpers_namespace"]


@pytest.fixture(scope="session", autouse=True)
def environ():
Expand Down
1 change: 0 additions & 1 deletion setup.cfg
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/molecule/config.py
Expand Up @@ -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)

Expand Down
18 changes: 6 additions & 12 deletions src/molecule/test/conftest.py
Expand Up @@ -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"

Expand Down
36 changes: 13 additions & 23 deletions src/molecule/test/functional/conftest.py
Expand Up @@ -23,14 +23,15 @@
import shutil
import subprocess
from subprocess import PIPE
from typing import Optional

import pexpect
import pkg_resources
import pytest

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

Expand Down Expand Up @@ -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
Expand All @@ -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",
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -236,17 +227,16 @@ 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")


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)
Expand Down
34 changes: 24 additions & 10 deletions src/molecule/test/functional/test_command.py
Expand Up @@ -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


Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
23 changes: 14 additions & 9 deletions src/molecule/test/unit/conftest.py
Expand Up @@ -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,)
Expand Down Expand Up @@ -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)

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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"}

Expand Down

0 comments on commit 769658d

Please sign in to comment.