Skip to content

Commit

Permalink
More types
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea committed Jul 3, 2023
1 parent db3640b commit 67fe80e
Show file tree
Hide file tree
Showing 30 changed files with 146 additions and 86 deletions.
4 changes: 2 additions & 2 deletions src/molecule/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ class Status(NamedTuple):
driver_name: str
provisioner_name: str
scenario_name: str
created: bool
converged: bool
created: str
converged: str
39 changes: 21 additions & 18 deletions src/molecule/test/a_unit/command/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import os

import pytest
from pytest_mock import MockerFixture

from molecule import config, util
from molecule.command import base
Expand All @@ -37,12 +38,12 @@ def execute(self, action_args=None):
# config.Config._validate from executing. Thus preventing odd side-effects
# throughout patched.assert_called unit tests.
@pytest.fixture()
def _base_class(patched_config_validate, config_instance):
def _base_class(patched_config_validate, config_instance: config.Config):
return ExtendedBase


@pytest.fixture()
def _instance(_base_class, config_instance):
def _instance(_base_class, config_instance: config.Config):
return _base_class(config_instance)


Expand Down Expand Up @@ -95,7 +96,7 @@ def test_init_calls_setup(_patched_base_setup, _instance):


def test_setup(
mocker,
mocker: MockerFixture,
patched_add_or_update_vars,
_patched_write_config,
_patched_manage_inventory,
Expand All @@ -109,7 +110,7 @@ def test_setup(


def test_execute_cmdline_scenarios(
config_instance,
config_instance: config.Config,
_patched_print_matrix,
_patched_execute_scenario,
):
Expand All @@ -119,7 +120,7 @@ def test_execute_cmdline_scenarios(
# - execute_scenario is called once, indicating the function correctly
# loops over Scenarios.
scenario_name = None
args = {}
args: dict[str, str] = {}
command_args = {"destroy": "always", "subcommand": "test"}
base.execute_cmdline_scenarios(scenario_name, args, command_args)

Expand All @@ -128,14 +129,14 @@ def test_execute_cmdline_scenarios(


def test_execute_cmdline_scenarios_prune(
config_instance,
config_instance: config.Config,
_patched_prune,
_patched_execute_subcommand,
):
# Subcommands should be executed and prune *should* run when
# destroy is 'always'
scenario_name = "default"
args = {}
args: dict[str, str] = {}
command_args = {"destroy": "always", "subcommand": "test"}

base.execute_cmdline_scenarios(scenario_name, args, command_args)
Expand All @@ -145,14 +146,14 @@ def test_execute_cmdline_scenarios_prune(


def test_execute_cmdline_scenarios_no_prune(
config_instance,
config_instance: config.Config,
_patched_prune,
_patched_execute_subcommand,
):
# Subcommands should be executed but prune *should not* run when
# destroy is 'never'
scenario_name = "default"
args = {}
args: dict[str, str] = {}
command_args = {"destroy": "never", "subcommand": "test"}

base.execute_cmdline_scenarios(scenario_name, args, command_args)
Expand All @@ -162,7 +163,7 @@ def test_execute_cmdline_scenarios_no_prune(


def test_execute_cmdline_scenarios_exit_destroy(
config_instance,
config_instance: config.Config,
_patched_execute_scenario,
_patched_prune,
_patched_execute_subcommand,
Expand All @@ -174,7 +175,7 @@ def test_execute_cmdline_scenarios_exit_destroy(
# raises SystemExit
# - scenario is pruned
scenario_name = "default"
args = {}
args: dict[str, str] = {}
command_args = {"destroy": "always", "subcommand": "test"}
_patched_execute_scenario.side_effect = SystemExit()

Expand All @@ -190,7 +191,7 @@ def test_execute_cmdline_scenarios_exit_destroy(


def test_execute_cmdline_scenarios_exit_nodestroy(
config_instance,
config_instance: config.Config,
_patched_execute_scenario,
_patched_prune,
_patched_sysexit,
Expand All @@ -201,7 +202,7 @@ def test_execute_cmdline_scenarios_exit_nodestroy(
# - scenario is not pruned
# - caught SystemExit is reraised
scenario_name = "default"
args = {}
args: dict[str, str] = {}
command_args = {"destroy": "never", "subcommand": "test"}

_patched_execute_scenario.side_effect = SystemExit()
Expand All @@ -215,11 +216,11 @@ def test_execute_cmdline_scenarios_exit_nodestroy(
assert not _patched_sysexit.called


def test_runtime_paths(config_instance, _patched_sysexit):
def test_runtime_paths(config_instance: config.Config, _patched_sysexit):
# the ansible_collections_path and ansible_roles_path from the runtime
# should be added to the provisioner's paths
scenario_name = None
args = {}
args: dict[str, str] = {}
command_args = {"destroy": "never", "subcommand": "verify"}

base.result_callback()
Expand All @@ -228,10 +229,12 @@ def test_runtime_paths(config_instance, _patched_sysexit):
home = os.path.expanduser("~")
cache_dir = config_instance.runtime.cache_dir
runtime_roles_path = config_instance.runtime.environ.get("ANSIBLE_ROLES_PATH")
assert isinstance(runtime_roles_path, str)
provisioner_roles_path = config_instance.provisioner.env.get("ANSIBLE_ROLES_PATH")
runtime_collections_path = config_instance.runtime.environ.get(
config_instance.ansible_collections_path,
)
assert isinstance(runtime_collections_path, str)
provisioner_collections_path = config_instance.provisioner.env.get(
config_instance.ansible_collections_path,
)
Expand Down Expand Up @@ -260,7 +263,7 @@ def test_execute_subcommand(config_instance: config.Config):
assert config_instance.action == "list"


def test_execute_scenario(mocker, _patched_execute_subcommand):
def test_execute_scenario(mocker: MockerFixture, _patched_execute_subcommand):
# call a spoofed scenario with a sequence that does not include destroy:
# - execute_subcommand should be called once for each sequence item
# - prune should not be called, since the sequence has no destroy step
Expand All @@ -273,7 +276,7 @@ def test_execute_scenario(mocker, _patched_execute_subcommand):
assert not scenario.prune.called


def test_execute_scenario_destroy(mocker, _patched_execute_subcommand):
def test_execute_scenario_destroy(mocker: MockerFixture, _patched_execute_subcommand):
# call a spoofed scenario with a sequence that includes destroy:
# - execute_subcommand should be called once for each sequence item
# - prune should be called, since the sequence has a destroy step
Expand Down Expand Up @@ -315,7 +318,7 @@ def test_verify_configs_raises_with_no_configs(caplog):

def test_verify_configs_raises_with_duplicate_configs(
caplog,
config_instance,
config_instance: config.Config,
):
with pytest.raises(SystemExit) as e:
configs = [config_instance, config_instance]
Expand Down
6 changes: 4 additions & 2 deletions src/molecule/test/a_unit/command/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
# DEALINGS IN THE SOFTWARE.

import pytest
from pytest_mock import MockerFixture

from molecule import config
from molecule.command import check


Expand All @@ -32,11 +34,11 @@ def _patched_ansible_check(mocker):
# config.Config._validate from executing. Thus preventing odd side-effects
# throughout patched.assert_called unit tests.
def test_execute(
mocker,
mocker: MockerFixture,
caplog,
_patched_ansible_check,
patched_config_validate,
config_instance,
config_instance: config.Config,
):
c = check.Check(config_instance)
c.execute()
Expand Down
9 changes: 5 additions & 4 deletions src/molecule/test/a_unit/command/test_cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
import os

import pytest
from pytest_mock import MockerFixture

from molecule import util
from molecule import config, util
from molecule.command import cleanup


Expand All @@ -45,11 +46,11 @@ def _patched_ansible_cleanup(mocker):
indirect=True,
)
def test_execute(
mocker,
mocker: MockerFixture,
_patched_ansible_cleanup,
caplog,
patched_config_validate,
config_instance,
config_instance: config.Config,
):
pb = os.path.join(config_instance.scenario.directory, "cleanup.yml")
util.write_file(pb, "")
Expand All @@ -65,7 +66,7 @@ def test_execute(
def test_execute_skips_when_playbook_not_configured(
caplog,
_patched_ansible_cleanup,
config_instance,
config_instance: config.Config,
):
cu = cleanup.Cleanup(config_instance)
cu.execute()
Expand Down
8 changes: 5 additions & 3 deletions src/molecule/test/a_unit/command/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@


import pytest
from pytest_mock import MockerFixture

from molecule import config
from molecule.command import create


Expand All @@ -34,11 +36,11 @@ def _patched_create_setup(mocker):
# throughout patched.assert_called unit tests.
@pytest.mark.skip(reason="create not running for delegated")
def test_execute(
mocker,
mocker: MockerFixture,
caplog,
command_patched_ansible_create,
patched_config_validate,
config_instance,
config_instance: config.Config,
):
c = create.Create(config_instance)
c.execute()
Expand All @@ -57,7 +59,7 @@ def test_execute(
def test_execute_skips_when_instances_already_created(
caplog,
command_patched_ansible_create,
config_instance,
config_instance: config.Config,
):
config_instance.state.change_state("created", True)
c = create.Create(config_instance)
Expand Down
4 changes: 3 additions & 1 deletion src/molecule/test/a_unit/command/test_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

from pytest_mock import MockerFixture

from molecule import config
from molecule.command import dependency

Expand All @@ -26,7 +28,7 @@
# config.Config._validate from executing. Thus preventing odd side-effects
# throughout patched.assert_called unit tests.
def test_execute(
mocker,
mocker: MockerFixture,
caplog,
patched_ansible_galaxy,
patched_config_validate,
Expand Down
8 changes: 5 additions & 3 deletions src/molecule/test/a_unit/command/test_destroy.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@


import pytest
from pytest_mock import MockerFixture

from molecule import config
from molecule.command import destroy


Expand All @@ -39,11 +41,11 @@ def _patched_destroy_setup(mocker):
# throughout patched.assert_called unit tests.
@pytest.mark.skip(reason="destroy not running for delegated")
def test_execute(
mocker,
mocker: MockerFixture,
caplog,
patched_config_validate,
_patched_ansible_destroy,
config_instance,
config_instance: config.Config,
):
d = destroy.Destroy(config_instance)
d.execute()
Expand All @@ -67,7 +69,7 @@ def test_execute_skips_when_destroy_strategy_is_never(
_patched_destroy_setup,
caplog,
_patched_ansible_destroy,
config_instance,
config_instance: config.Config,
):
config_instance.command_args = {"destroy": "never"}

Expand Down
8 changes: 5 additions & 3 deletions src/molecule/test/a_unit/command/test_idempotence.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
# DEALINGS IN THE SOFTWARE.

import pytest
from pytest_mock import MockerFixture

from molecule import config
from molecule.command import idempotence


Expand All @@ -32,14 +34,14 @@ def _patched_is_idempotent(mocker):
# config.Config._validate from executing. Thus preventing odd side-effects
# throughout patched.assert_called unit tests.
@pytest.fixture()
def _instance(patched_config_validate, config_instance):
def _instance(patched_config_validate, config_instance: config.Config):
config_instance.state.change_state("converged", True)

return idempotence.Idempotence(config_instance)


def test_execute(
mocker,
mocker: MockerFixture,
caplog,
patched_ansible_converge,
_patched_is_idempotent,
Expand Down Expand Up @@ -74,7 +76,7 @@ def test_execute_raises_when_not_converged(


def test_execute_raises_when_fails_idempotence(
mocker,
mocker: MockerFixture,
caplog,
patched_ansible_converge,
_patched_is_idempotent,
Expand Down
3 changes: 2 additions & 1 deletion src/molecule/test/a_unit/command/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

from molecule import config
from molecule.command import list
from molecule.driver import base


def test_execute(capsys, config_instance):
def test_execute(capsys, config_instance: config.Config):
l = list.List(config_instance)
x = [
base.Status(
Expand Down
3 changes: 2 additions & 1 deletion src/molecule/test/a_unit/command/test_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@


import pytest
from pytest_mock import MockerFixture

from molecule import config
from molecule.command import login
Expand All @@ -32,7 +33,7 @@ def _instance(config_instance: config.Config):
return login.Login(config_instance)


def test_execute(mocker, _instance):
def test_execute(mocker: MockerFixture, _instance):
_instance._config.command_args = {"host": "instance-1"}
m = mocker.patch("molecule.command.login.Login._get_login")
_instance.execute()
Expand Down
Loading

0 comments on commit 67fe80e

Please sign in to comment.