Skip to content

Commit 1887611

Browse files
committed
[#29] Accept Path objects for YAML files
1 parent 8f75d62 commit 1887611

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

django_setup_configuration/runner.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import logging
33
from dataclasses import dataclass
44
from functools import partial
5+
from os import PathLike
56
from pathlib import Path
67
from typing import Any, Generator
78

@@ -42,7 +43,7 @@ class SetupConfigurationRunner:
4243
"""
4344

4445
configured_steps: list[BaseConfigurationStep]
45-
yaml_source: str | None
46+
yaml_source: PathLike | None
4647
object_source: dict | None
4748

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

69-
self.yaml_source = yaml_source
70-
if self.yaml_source:
71-
yaml_file = Path(yaml_source).resolve()
72-
if not yaml_file.exists():
70+
if yaml_source:
71+
self.yaml_source = (
72+
Path(yaml_source) if isinstance(yaml_source, str) else yaml_source
73+
)
74+
self.yaml_source = self.yaml_source.resolve()
75+
if not self.yaml_source.exists():
7376
raise ConfigurationException(
74-
f"YAML source is not an existing file path: {yaml_file}"
77+
f"YAML source is not an existing file path: {self.yaml_source}"
7578
)
7679

7780
self._config_source_models_for_step = {}

django_setup_configuration/test_utils.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from os import PathLike
12
from typing import Any
23

34
from django_setup_configuration.configuration import BaseConfigurationStep
@@ -37,7 +38,7 @@ def build_step_config_from_sources(
3738
def execute_single_step(
3839
step: type[BaseConfigurationStep] | str,
3940
*,
40-
yaml_source: str | None = None,
41+
yaml_source: PathLike | str | None = None,
4142
object_source: dict | None = None,
4243
) -> StepExecutionResult:
4344
"""

tests/test_runner.py

+26
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from pathlib import Path
12
import pytest
23

34
from django_setup_configuration.exceptions import (
@@ -199,3 +200,28 @@ def test_settings_can_be_overriden_with_object(
199200
"username": "overriden username",
200201
},
201202
)
203+
204+
205+
@pytest.mark.parametrize(
206+
"path_factory",
207+
(
208+
lambda path: str(path),
209+
lambda path: Path(path),
210+
),
211+
)
212+
def test_yaml_source_can_be_string_or_path_like(
213+
step_execute_mock, expected_step_config, path_factory, test_step_yaml_path
214+
):
215+
result = execute_single_step(
216+
TestStep, yaml_source=path_factory(test_step_yaml_path)
217+
)
218+
219+
assert result == StepExecutionResult(
220+
step=result.step,
221+
is_enabled=True,
222+
has_run=True,
223+
run_exception=None,
224+
config_model=expected_step_config,
225+
)
226+
assert type(result.step) is TestStep
227+
step_execute_mock.assert_called_once_with(expected_step_config)

0 commit comments

Comments
 (0)