Skip to content

Commit

Permalink
Minor test improvements (#4363)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored Jan 21, 2025
1 parent f7e98b6 commit 9e526b0
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 30 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,6 @@ node_modules/
# There is a test for this, otherwise unknown
# tests/b_functional/test_command.py test_w_gitignore
tests/fixtures/resources/.extensions/

# docs output
_readthedocs/
2 changes: 1 addition & 1 deletion .readthedocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ build:
python: "3.12"
commands:
- pip install --user tox
- python3 -m tox -e docs -- --strict --site-dir=_readthedocs/html/
- python3 -m tox -e docs
python:
install:
- method: pip
Expand Down
1 change: 1 addition & 0 deletions molecule
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ line-length = 100
[tool.coverage.report]
exclude_also = ["if TYPE_CHECKING:", "pragma: no cover"]
fail_under = 90
ignore_errors = true
ignore_errors = false # avoid spamming the console if there are no errors
show_missing = true
skip_covered = true
skip_empty = true
Expand Down
8 changes: 8 additions & 0 deletions src/molecule/scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ def __init__(self, config: Config) -> None:
self.config = config
self._setup()

def __repr__(self) -> str:
"""Return a representation of the instance.
Returns:
A string.
"""
return f"<Scenario {self.name} from {self.config.project_directory}>"

def _remove_scenario_state_directory(self) -> None:
"""Remove scenario cached disk stored state."""
directory = str(Path(self.ephemeral_directory).parent)
Expand Down
56 changes: 29 additions & 27 deletions tests/unit/test_scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@

import pytest

from molecule import config, scenario, util
from molecule import config, util
from molecule.scenario import Scenario, ephemeral_directory


if TYPE_CHECKING:
Expand All @@ -41,11 +42,11 @@
def _instance(
patched_config_validate: Mock,
config_instance: config.Config,
) -> scenario.Scenario:
return scenario.Scenario(config_instance)
) -> Scenario:
return Scenario(config_instance)


def test_prune(_instance: scenario.Scenario) -> None: # noqa: PT019, D103
def test_prune(_instance: Scenario) -> None: # noqa: PT019, D103
e_dir = Path(_instance.ephemeral_directory)
# prune data also includes files in the scenario inventory dir,
# which is "<e_dir>/inventory" by default.
Expand Down Expand Up @@ -86,40 +87,41 @@ def test_prune(_instance: scenario.Scenario) -> None: # noqa: PT019, D103
assert not (e_dir / pruned_dir).is_dir()


def test_config_member(_instance: scenario.Scenario) -> None: # noqa: PT019, D103
def test_config_member(_instance: Scenario) -> None: # noqa: PT019, D103
assert isinstance(_instance.config, config.Config)


def test_scenario_init_calls_setup( # noqa: D103
patched_scenario_setup: Mock,
_instance: scenario.Scenario, # noqa: PT019
_instance: Scenario, # noqa: PT019
) -> None:
patched_scenario_setup.assert_called_once_with()


def test_scenario_name_property( # noqa: D103
_instance: scenario.Scenario, # noqa: PT019
_instance: Scenario, # noqa: PT019
) -> None:
assert _instance.name == "default"


def test_ephemeral_directory_property( # noqa: D103
_instance: scenario.Scenario, # noqa: PT019
_instance: Scenario, # noqa: PT019
) -> None:
assert os.access(_instance.ephemeral_directory, os.W_OK)
# assert that scenario path is included in in repr (useful for debugging)
assert _instance.ephemeral_directory in repr(_instance)


def test_scenario_inventory_directory_property( # noqa: D103
_instance: scenario.Scenario, # noqa: PT019
_instance: Scenario, # noqa: PT019
) -> None:
ephemeral_directory = Path(_instance.config.scenario.ephemeral_directory)
e_dir = ephemeral_directory / "inventory"
e_dir = Path(_instance.config.scenario.ephemeral_directory) / "inventory"

assert str(e_dir) == _instance.inventory_directory


def test_check_sequence_property( # noqa: D103
_instance: scenario.Scenario, # noqa: PT019
_instance: Scenario, # noqa: PT019
) -> None:
sequence = [
"dependency",
Expand All @@ -137,59 +139,59 @@ def test_check_sequence_property( # noqa: D103


def test_converge_sequence_property( # noqa: D103
_instance: scenario.Scenario, # noqa: PT019
_instance: Scenario, # noqa: PT019
) -> None:
sequence = ["dependency", "create", "prepare", "converge"]

assert sequence == _instance.converge_sequence


def test_create_sequence_property( # noqa: D103
_instance: scenario.Scenario, # noqa: PT019
_instance: Scenario, # noqa: PT019
) -> None:
sequence = ["dependency", "create", "prepare"]

assert sequence == _instance.create_sequence


def test_dependency_sequence_property( # noqa: D103
_instance: scenario.Scenario, # noqa: PT019
_instance: Scenario, # noqa: PT019
) -> None:
assert _instance.dependency_sequence == ["dependency"]


def test_destroy_sequence_property( # noqa: D103
_instance: scenario.Scenario, # noqa: PT019
_instance: Scenario, # noqa: PT019
) -> None:
assert _instance.destroy_sequence == ["dependency", "cleanup", "destroy"]


def test_idempotence_sequence_property( # noqa: D103
_instance: scenario.Scenario, # noqa: PT019
_instance: Scenario, # noqa: PT019
) -> None:
assert _instance.idempotence_sequence == ["idempotence"]


def test_prepare_sequence_property( # noqa: D103
_instance: scenario.Scenario, # noqa: PT019
_instance: Scenario, # noqa: PT019
) -> None:
assert _instance.prepare_sequence == ["prepare"]


def test_side_effect_sequence_property( # noqa: D103
_instance: scenario.Scenario, # noqa: PT019
_instance: Scenario, # noqa: PT019
) -> None:
assert _instance.side_effect_sequence == ["side_effect"]


def test_syntax_sequence_property( # noqa: D103
_instance: scenario.Scenario, # noqa: PT019
_instance: Scenario, # noqa: PT019
) -> None:
assert _instance.syntax_sequence == ["syntax"]


def test_test_sequence_property( # noqa: D103
_instance: scenario.Scenario, # noqa: PT019
_instance: Scenario, # noqa: PT019
) -> None:
sequence = [
"dependency",
Expand All @@ -210,21 +212,21 @@ def test_test_sequence_property( # noqa: D103


def test_verify_sequence_property( # noqa: D103
_instance: scenario.Scenario, # noqa: PT019
_instance: Scenario, # noqa: PT019
) -> None:
assert _instance.verify_sequence == ["verify"]


def test_sequence_property_with_invalid_subcommand( # noqa: D103
_instance: scenario.Scenario, # noqa: PT019
_instance: Scenario, # noqa: PT019
) -> None:
_instance.config.command_args = {"subcommand": "invalid"}

assert _instance.sequence == []


def test_setup_creates_ephemeral_and_inventory_directories( # noqa: D103
_instance: scenario.Scenario, # noqa: PT019
_instance: Scenario, # noqa: PT019
) -> None:
ephemeral_dir = _instance.config.scenario.ephemeral_directory
inventory_dir = _instance.config.scenario.inventory_directory
Expand All @@ -238,7 +240,7 @@ def test_setup_creates_ephemeral_and_inventory_directories( # noqa: D103
def test_ephemeral_directory() -> None: # noqa: D103
# assure we can write to ephemeral directory
path = Path("foo/bar")
assert os.access(scenario.ephemeral_directory(path), os.W_OK)
assert os.access(ephemeral_directory(path), os.W_OK)


def test_ephemeral_directory_overridden_via_env_var(
Expand All @@ -255,7 +257,7 @@ def test_ephemeral_directory_overridden_via_env_var(
monkeypatch.setenv("MOLECULE_EPHEMERAL_DIRECTORY", "foo/bar")

path = Path("foo/bar")
assert os.access(scenario.ephemeral_directory(path), os.W_OK)
assert os.access(ephemeral_directory(path), os.W_OK)


def test_ephemeral_directory_overridden_via_env_var_uses_absolute_path(
Expand All @@ -271,4 +273,4 @@ def test_ephemeral_directory_overridden_via_env_var_uses_absolute_path(
monkeypatch.chdir(tmp_path)
monkeypatch.setenv("MOLECULE_EPHEMERAL_DIRECTORY", "foo/bar")

assert Path(scenario.ephemeral_directory()).is_absolute()
assert Path(ephemeral_directory()).is_absolute()
3 changes: 2 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,11 @@ skip_install = false
extras =
docs
set_env =
DYLD_FALLBACK_LIBRARY_PATH = /opt/homebrew/lib:{env:LD_LIBRARY_PATH}
NO_COLOR = 1
TERM = dump
commands =
mkdocs build {posargs:}
mkdocs build {posargs:--strict --site-dir=_readthedocs/html/}

[testenv:lint]
description = Enforce quality standards under {basepython}
Expand Down

0 comments on commit 9e526b0

Please sign in to comment.