Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Qalthos committed Oct 31, 2024
1 parent dc992c9 commit 1cb8024
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 26 deletions.
33 changes: 20 additions & 13 deletions tests/unit/command/test_side_effect.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,18 @@


if TYPE_CHECKING:
from typing import Literal
from unittest.mock import MagicMock, Mock

from pytest_mock import MockerFixture

from molecule.types import ProvisionerData


@pytest.fixture()
def _command_provisioner_section_with_side_effect_data(): # type: ignore[no-untyped-def] # noqa: ANN202
def _command_provisioner_section_with_side_effect_data() -> (
dict[Literal["provisioner"], ProvisionerData]
):
return {
"provisioner": {
"name": "ansible",
Expand All @@ -44,7 +51,7 @@ def _command_provisioner_section_with_side_effect_data(): # type: ignore[no-unt


@pytest.fixture()
def _patched_ansible_side_effect(mocker): # type: ignore[no-untyped-def] # noqa: ANN001, ANN202
def _patched_ansible_side_effect(mocker: MockerFixture) -> MagicMock:
return mocker.patch("molecule.provisioner.ansible.Ansible.side_effect")


Expand All @@ -56,32 +63,32 @@ def _patched_ansible_side_effect(mocker): # type: ignore[no-untyped-def] # noq
["_command_provisioner_section_with_side_effect_data"], # noqa: PT007
indirect=True,
)
def test_side_effect_execute( # type: ignore[no-untyped-def] # noqa: ANN201, D103
def test_side_effect_execute( # noqa: D103
mocker: MockerFixture, # noqa: ARG001
_patched_ansible_side_effect, # noqa: ANN001, PT019
caplog, # noqa: ANN001
patched_config_validate, # noqa: ANN001, ARG001
_patched_ansible_side_effect: Mock, # noqa: PT019
caplog: pytest.LogCaptureFixture,
patched_config_validate: Mock, # noqa: ARG001
config_instance: config.Config,
):
) -> None:
pb = os.path.join(config_instance.scenario.directory, "side_effect.yml") # noqa: PTH118
util.write_file(pb, "")

se = side_effect.SideEffect(config_instance)
se.execute() # type: ignore[no-untyped-call]
se.execute()

assert "default" in caplog.text
assert "side_effect" in caplog.text

_patched_ansible_side_effect.assert_called_once_with(None)


def test_side_effect_execute_skips_when_playbook_not_configured( # type: ignore[no-untyped-def] # noqa: ANN201, D103
caplog, # noqa: ANN001
_patched_ansible_side_effect, # noqa: ANN001, PT019
def test_side_effect_execute_skips_when_playbook_not_configured( # noqa: D103
caplog: pytest.LogCaptureFixture,
_patched_ansible_side_effect: Mock, # noqa: PT019
config_instance: config.Config,
):
) -> None:
se = side_effect.SideEffect(config_instance)
se.execute() # type: ignore[no-untyped-call]
se.execute()

msg = "Skipping, side effect playbook not configured."
assert msg in caplog.text
Expand Down
16 changes: 9 additions & 7 deletions tests/unit/command/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,30 @@


if TYPE_CHECKING:
from unittest.mock import MagicMock, Mock

from pytest_mock import MockerFixture

from molecule import config


@pytest.fixture()
def _patched_ansible_syntax(mocker): # type: ignore[no-untyped-def] # noqa: ANN001, ANN202
def _patched_ansible_syntax(mocker: MockerFixture) -> MagicMock:
return mocker.patch("molecule.provisioner.ansible.Ansible.syntax")


# NOTE(retr0h): The use of the `patched_config_validate` fixture, disables
# config.Config._validate from executing. Thus preventing odd side-effects
# throughout patched.assert_called unit tests.
def test_syntax_execute( # type: ignore[no-untyped-def] # noqa: ANN201, D103
def test_syntax_execute( # noqa: D103
mocker: MockerFixture, # noqa: ARG001
caplog, # noqa: ANN001
_patched_ansible_syntax, # noqa: ANN001, PT019
patched_config_validate, # noqa: ANN001, ARG001
caplog: pytest.LogCaptureFixture,
_patched_ansible_syntax: Mock, # noqa: PT019
patched_config_validate: Mock, # noqa: ARG001
config_instance: config.Config,
):
) -> None:
s = syntax.Syntax(config_instance)
s.execute() # type: ignore[no-untyped-call]
s.execute()

assert "default" in caplog.text
assert "syntax" in caplog.text
Expand Down
16 changes: 10 additions & 6 deletions tests/unit/command/test_verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@


if TYPE_CHECKING:
from unittest.mock import Mock

import pytest

from pytest_mock import MockerFixture

from molecule import config
Expand All @@ -33,15 +37,15 @@
# NOTE(retr0h): The use of the `patched_config_validate` fixture, disables
# config.Config._validate from executing. Thus preventing odd side-effects
# throughout patched.assert_called unit tests.
def test_verify_execute( # type: ignore[no-untyped-def] # noqa: ANN201, D103
def test_verify_execute( # noqa: D103
mocker: MockerFixture, # noqa: ARG001
caplog, # noqa: ANN001
patched_default_verifier, # noqa: ANN001, ARG001
patched_config_validate, # noqa: ANN001, ARG001
caplog: pytest.LogCaptureFixture,
patched_default_verifier: Mock, # noqa: ARG001
patched_config_validate: Mock, # noqa: ARG001
config_instance: config.Config,
):
) -> None:
v = verify.Verify(config_instance)
v.execute() # type: ignore[no-untyped-call]
v.execute()

assert "default" in caplog.text
assert "verify" in caplog.text

0 comments on commit 1cb8024

Please sign in to comment.