diff --git a/django_setup_configuration/runner.py b/django_setup_configuration/runner.py index 9f6e7e1..4fca58c 100644 --- a/django_setup_configuration/runner.py +++ b/django_setup_configuration/runner.py @@ -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 @@ -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] @@ -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): @@ -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 = {} diff --git a/django_setup_configuration/test_utils.py b/django_setup_configuration/test_utils.py index 761a6c5..d6410de 100644 --- a/django_setup_configuration/test_utils.py +++ b/django_setup_configuration/test_utils.py @@ -1,3 +1,4 @@ +from os import PathLike from typing import Any from django_setup_configuration.configuration import BaseConfigurationStep @@ -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: """ diff --git a/tests/test_runner.py b/tests/test_runner.py index 40eb93c..b1a221b 100644 --- a/tests/test_runner.py +++ b/tests/test_runner.py @@ -1,3 +1,5 @@ +from pathlib import Path + import pytest from django_setup_configuration.exceptions import ( @@ -199,3 +201,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)