Skip to content

Commit

Permalink
Add new test, and update some types
Browse files Browse the repository at this point in the history
  • Loading branch information
Qalthos committed Sep 27, 2024
1 parent 294aa78 commit 772f02c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
4 changes: 2 additions & 2 deletions tests/unit/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from molecule import api


def test_api_drivers(): # type: ignore[no-untyped-def] # noqa: ANN201, D103
def test_api_drivers() -> None: # noqa: D103
results = api.drivers()

for result in results.values():
Expand All @@ -32,7 +32,7 @@ def test_api_drivers(): # type: ignore[no-untyped-def] # noqa: ANN201, D103
assert "default" in results


def test_api_verifiers(): # type: ignore[no-untyped-def] # noqa: ANN201, D103
def test_api_verifiers() -> None: # noqa: D103
x = ["testinfra", "ansible"]

assert all(elem in api.verifiers() for elem in x)
44 changes: 26 additions & 18 deletions tests/unit/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,34 +154,42 @@ def test_verifier_property_is_ansible(config_instance: config.Config): # type:
assert isinstance(config_instance.verifier, AnsibleVerifier)


def test_get_driver_name_from_state_file(config_instance: config.Config, mocker): # type: ignore[no-untyped-def] # noqa: ANN001, ANN201, D103
def test_verifier_property_invalid(config_instance: config.Config) -> None: # noqa: D103
config_instance.config["verifier"]["name"] = "missing"
del config_instance.verifier

with pytest.raises(RuntimeError, match="Unable to find 'missing' verifier driver."):
config_instance.verifier # noqa: B018


def test_get_driver_name_from_state_file(config_instance: config.Config, mocker) -> None: # type: ignore[no-untyped-def] # noqa: ANN001, D103
config_instance.state.change_state("driver", "state-driver")

with pytest.raises(SystemExit):
config_instance._get_driver_name() # type: ignore[no-untyped-call]
config_instance._get_driver_name()

mocker.patch("molecule.api.drivers", return_value=["state-driver"])
assert config_instance._get_driver_name() == "state-driver" # type: ignore[no-untyped-call]
assert config_instance._get_driver_name() == "state-driver"


def test_get_driver_name_from_cli(config_instance: config.Config): # type: ignore[no-untyped-def] # noqa: ANN201, D103
def test_get_driver_name_from_cli(config_instance: config.Config) -> None: # noqa: D103
config_instance.command_args = {"driver_name": "cli-driver"}

assert config_instance._get_driver_name() == "cli-driver" # type: ignore[no-untyped-call]
assert config_instance._get_driver_name() == "cli-driver"


def test_get_driver_name(config_instance: config.Config): # type: ignore[no-untyped-def] # noqa: ANN201, D103
assert config_instance._get_driver_name() == "default" # type: ignore[no-untyped-call]
def test_get_driver_name(config_instance: config.Config) -> None: # noqa: D103
assert config_instance._get_driver_name() == "default"


def test_get_driver_name_raises_when_different_driver_used( # type: ignore[no-untyped-def] # noqa: ANN201, D103
def test_get_driver_name_raises_when_different_driver_used( # noqa: D103
caplog: pytest.LogCaptureFixture,
config_instance: config.Config,
):
) -> None:
config_instance.state.change_state("driver", "foo")
config_instance.command_args = {"driver_name": "bar"}
with pytest.raises(SystemExit) as e:
config_instance._get_driver_name() # type: ignore[no-untyped-call]
config_instance._get_driver_name()

assert e.value.code == 1

Expand Down Expand Up @@ -218,8 +226,8 @@ def test_get_config_with_multiple_base_configs(config_instance: config.Config):
assert result["foo2"] == "bar2"


def test_reget_config(config_instance: config.Config): # type: ignore[no-untyped-def] # noqa: ANN201, D103
assert isinstance(config_instance._reget_config(), dict) # type: ignore[no-untyped-call]
def test_reget_config(config_instance: config.Config) -> None: # noqa: D103
assert isinstance(config_instance._reget_config(), dict)


def test_interpolate(config_instance: config.Config): # type: ignore[no-untyped-def] # noqa: ANN201, D103
Expand Down Expand Up @@ -293,31 +301,31 @@ def test_get_defaults(config_instance: config.Config, mocker): # type: ignore[n
assert defaults["scenario"]["name"] == "test_scenario_name"


def test_validate( # type: ignore[no-untyped-def] # noqa: ANN201, D103
def test_validate( # type: ignore[no-untyped-def] # noqa: D103
mocker: MockerFixture,
config_instance: config.Config,
patched_logger_debug, # noqa: ANN001
):
) -> None:
m = mocker.patch("molecule.model.schema_v3.validate")
m.return_value = None

config_instance._validate() # type: ignore[no-untyped-call]
config_instance._validate()

assert patched_logger_debug.call_count == 1

m.assert_called_with(config_instance.config)


def test_validate_exists_when_validation_fails( # type: ignore[no-untyped-def] # noqa: ANN201, D103
def test_validate_exists_when_validation_fails( # noqa: D103
mocker: MockerFixture,
caplog: pytest.LogCaptureFixture,
config_instance: config.Config,
):
) -> None:
m = mocker.patch("molecule.model.schema_v3.validate")
m.return_value = "validation errors"

with pytest.raises(SystemExit) as e:
config_instance._validate() # type: ignore[no-untyped-call]
config_instance._validate()

assert e.value.code == 1

Expand Down

0 comments on commit 772f02c

Please sign in to comment.