Skip to content

Commit

Permalink
[#31] Accept Path objects for YAML files
Browse files Browse the repository at this point in the history
  • Loading branch information
swrichards committed Dec 19, 2024
1 parent 8f75d62 commit 80ba04d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
17 changes: 10 additions & 7 deletions django_setup_configuration/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
from dataclasses import dataclass
from functools import partial
from os import PathLike
from pathlib import Path
from typing import Any, Generator

Expand Down Expand Up @@ -42,7 +43,7 @@ class SetupConfigurationRunner:
"""

configured_steps: list[BaseConfigurationStep]
yaml_source: str | None
yaml_source: PathLike | None
object_source: dict | None

_config_source_models_for_step: dict[BaseConfigurationStep, ConfigSourceModels]
Expand All @@ -52,7 +53,7 @@ def __init__(
self,
*,
steps: list[type[BaseConfigurationStep] | str] | None = None,
yaml_source: str | None = None,
yaml_source: PathLike | str | None = None,
object_source: dict | None = None,
):
if not (configured_steps := steps or settings.SETUP_CONFIGURATION_STEPS):
Expand All @@ -66,12 +67,14 @@ def __init__(
except ImportError as exc:
raise ConfigurationException(f"Unable to import step {exc.name}")

self.yaml_source = yaml_source
if self.yaml_source:
yaml_file = Path(yaml_source).resolve()
if not yaml_file.exists():
if yaml_source:
self.yaml_source = (
Path(yaml_source) if isinstance(yaml_source, str) else yaml_source
)
self.yaml_source = self.yaml_source.resolve()
if not self.yaml_source.exists():
raise ConfigurationException(
f"YAML source is not an existing file path: {yaml_file}"
f"YAML source is not an existing file path: {self.yaml_source}"
)

self._config_source_models_for_step = {}
Expand Down
3 changes: 2 additions & 1 deletion django_setup_configuration/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from os import PathLike
from typing import Any

from django_setup_configuration.configuration import BaseConfigurationStep
Expand Down Expand Up @@ -37,7 +38,7 @@ def build_step_config_from_sources(
def execute_single_step(
step: type[BaseConfigurationStep] | str,
*,
yaml_source: str | None = None,
yaml_source: PathLike | str | None = None,
object_source: dict | None = None,
) -> StepExecutionResult:
"""
Expand Down
26 changes: 26 additions & 0 deletions tests/test_runner.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from pathlib import Path
import pytest

from django_setup_configuration.exceptions import (
Expand Down Expand Up @@ -199,3 +200,28 @@ def test_settings_can_be_overriden_with_object(
"username": "overriden username",
},
)


@pytest.mark.parametrize(
"path_factory",
(
lambda path: str(path),
lambda path: Path(path),
),
)
def test_yaml_source_can_be_string_or_path_like(
step_execute_mock, expected_step_config, path_factory, test_step_yaml_path
):
result = execute_single_step(
TestStep, yaml_source=path_factory(test_step_yaml_path)
)

assert result == StepExecutionResult(
step=result.step,
is_enabled=True,
has_run=True,
run_exception=None,
config_model=expected_step_config,
)
assert type(result.step) is TestStep
step_execute_mock.assert_called_once_with(expected_step_config)

0 comments on commit 80ba04d

Please sign in to comment.