Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#29] Accept Path objects for YAML files #41

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
27 changes: 27 additions & 0 deletions tests/test_runner.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from pathlib import Path

import pytest

from django_setup_configuration.exceptions import (
Expand Down Expand Up @@ -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)
Loading