Skip to content

Commit

Permalink
🐛 Fix initializing ADR
Browse files Browse the repository at this point in the history
  • Loading branch information
AlTosterino committed Jul 9, 2024
1 parent 766b4a9 commit 8710ead
Show file tree
Hide file tree
Showing 12 changed files with 318 additions and 310 deletions.
436 changes: 213 additions & 223 deletions poetry.lock

Large diffs are not rendered by default.

55 changes: 0 additions & 55 deletions pyproject.toml

This file was deleted.

19 changes: 12 additions & 7 deletions src/adrpy/entrypoints/cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pathlib import Path
from typing import Optional
from typing import Annotated

import typer
from adrpy.injection import lidi
Expand All @@ -13,13 +13,15 @@

@app.command()
def init(
path: Optional[Path] = typer.Argument(
path: Path = typer.Argument(
None,
help=(
"Path in where ADRs should reside. "
"If not provided Path will be extracted from pyproject.toml"
"If not provided, Path will be extracted from pyproject.toml. "
"If no pyproject.toml is found, ADRs will be initialized in the current "
"working directory."
),
)
),
) -> None:
"""
Initialize ADR directory with first ADR in given PATH
Expand All @@ -33,9 +35,12 @@ def init(

@app.command()
def new(
name: str = typer.Argument(
..., help="Name of new ADR. Longer names (with spaces) should be put in quotation marks."
),
name: Annotated[
str,
typer.Argument(
help="Name of new ADR. Longer names (with spaces) should be put in quotation marks."
),
]
) -> None:
"""
Create new ADR with given NAME
Expand Down
12 changes: 6 additions & 6 deletions src/adrpy/injection/modules.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from adrpy.repositories.adr.base import BaseADRRepository
from adrpy.repositories.adr.repository import ADRFileRepository
from adrpy.services.template.base import BaseTemplateService
from adrpy.services.template.service import MakoTemplateService
from adrpy.shared_kernel.settings import Settings
from lidipy import Lidi


def bind_modules(lidi: Lidi) -> None:
lidi.bind(BaseADRRepository, ADRFileRepository(settings=lidi.resolve(Settings)))
from adrpy.repositories.adr.base import BaseADRRepository
from adrpy.repositories.adr.repository import ADRFileRepository
from adrpy.services.template.base import BaseTemplateService
from adrpy.services.template.service import MakoTemplateService

lidi.bind(BaseADRRepository, ADRFileRepository())
lidi.bind(BaseTemplateService, MakoTemplateService())
3 changes: 2 additions & 1 deletion src/adrpy/injection/settings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from adrpy.shared_kernel.settings import Settings
from lidipy import Lidi


def bind_settings(lidi: Lidi) -> None:
from adrpy.shared_kernel.settings import Settings

lidi.bind(Settings, Settings(), singleton=True)
6 changes: 2 additions & 4 deletions src/adrpy/repositories/adr/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ def get_template(self, name: str) -> Template: # TODO: Rename to get_app_templa
...

@abstractmethod
def create(self, adr_name: str, template: RenderedTemplate) -> None:
...
def create(self, adr_name: str, template: RenderedTemplate) -> None: ...

@abstractmethod
def get_next_ordinal_number(self) -> int:
...
def get_next_ordinal_number(self) -> int: ...


# ? Possibility of custom templates?
4 changes: 2 additions & 2 deletions src/adrpy/repositories/adr/repository.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import re

from adrpy.injection import lidi
from adrpy.repositories.adr.base import BaseADRRepository
from adrpy.shared_kernel.settings import Settings
from adrpy.shared_kernel.value_objects.template import RenderedTemplate, Template


class ADRFileRepository(BaseADRRepository):
def __init__(self, settings: Settings) -> None:
self.settings = settings
settings = lidi.resolve_attr(Settings)

def get_template(self, name: str) -> Template:
template_path = self.settings.APP_TEMPLATES_DIR / name
Expand Down
3 changes: 1 addition & 2 deletions src/adrpy/services/template/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@

class BaseTemplateService(ABC):
@abstractmethod
def render(self, template_file: Template, data: dict) -> RenderedTemplate:
...
def render(self, template_file: Template, data: dict) -> RenderedTemplate: ...
18 changes: 12 additions & 6 deletions src/adrpy/shared_kernel/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,28 @@

@dataclass(frozen=True)
class Settings:
# TODO: Add DEBUG logger handler
working_directory: Path = field(default_factory=Path.cwd)
initial_adr_dir: Path | None = None
initial_adr_dir: Path | None = None # TODO: Rename to requested_adr_dir or something
APP_TEMPLATES_DIR: Path = field(init=False, default=Path(__file__).parents[1] / "templates")

@property
def adr_dir(self) -> Path | None:
# TODO: Handle missing dir
if self.initial_adr_dir:
return self.initial_adr_dir
return self.__get_adr_dir_from_pyproject()
if adr_dir_from_config := self.__get_adr_dir_from_config():
return adr_dir_from_config
return self.working_directory

def __get_adr_dir_from_pyproject(self) -> Path | None:
def __get_adr_dir_from_config(self) -> Path | None:
import tomllib

with open("pyproject.toml", "rb") as f:
data = tomllib.load(f)
try:
with open("pyproject.toml", "rb") as f:
data = tomllib.load(f)
except FileNotFoundError:
# TODO): Add debug log here
return None
tools = data.get("tool", {})
adrpy_tool = tools.get("adrpy", {})
adrpy_dir = adrpy_tool.get("dir", None)
Expand Down
6 changes: 2 additions & 4 deletions src/adrpy/use_cases/initializing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
from adrpy.repositories.adr.base import BaseADRRepository
from adrpy.services.template.base import BaseTemplateService
from adrpy.shared_kernel.dtos import InitializeADRDTO
from adrpy.shared_kernel.settings import Settings


@dataclass
class InitializingADR:
template_service = lidi.resolve(BaseTemplateService)
file_service = lidi.resolve(BaseADRRepository)
settings = lidi.resolve(Settings)
template_service = lidi.resolve_attr(BaseTemplateService)
file_service = lidi.resolve_attr(BaseADRRepository)
INITIAL_ADR_NAME: str = field(init=False, default="0001-record-architecture-decisions")

def execute(self, dto: InitializeADRDTO) -> None:
Expand Down
Empty file added tests/unit/__init__.py
Empty file.
66 changes: 66 additions & 0 deletions tests/unit/test_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from pathlib import Path
from typing import Generator

import pytest
from adrpy.shared_kernel.settings import Settings

PYPROJECT_PATH = Path("pyproject.toml")
PYPROJECT_DATA = """
[tool.adrpy]
dir = "docs/adr"
"""
PYPROJECT_WRONG_DATA = """
[tool.adrpython]
dir = "docs/adr"
"""


@pytest.fixture(scope="module")
def correct_pyproject_toml() -> Generator[None, None, None]:
with open(PYPROJECT_PATH, "w") as pyproject:
pyproject.write(PYPROJECT_DATA)
yield
Path(PYPROJECT_PATH).unlink(missing_ok=True)


@pytest.fixture(scope="module")
def wrong_pyproject_toml() -> Generator[None, None, None]:
with open(PYPROJECT_PATH, "w") as pyproject:
pyproject.write(PYPROJECT_WRONG_DATA)
yield
Path(PYPROJECT_PATH).unlink(missing_ok=True)


def test_should_get_adr_dir_from_settings_when_no_initial_dir_set() -> None:
# Given
settings = Settings()

# When & Then
assert Path.cwd() == settings.adr_dir


def test_should_get_adr_dir_from_settings_when_initial_dir_set() -> None:
# Given
settings = Settings(initial_adr_dir=Path(__file__).parent)

# When & Then
assert settings.adr_dir == Path(__file__).parent


def test_should_get_adr_dir_from_pyproject_toml(correct_pyproject_toml: None) -> None:
# Given
settings = Settings()

# When & Then
assert settings.adr_dir == Path.cwd() / Path("docs/adr")


def test_should_fallback_wrong_adr_dir_from_pyproject_toml_to_working_directory(
wrong_pyproject_toml: None,
) -> None:
# TODO: Maybe this shouldn't fallback, but raise instead?
# Given
settings = Settings()

# When & Then
assert settings.adr_dir == Path.cwd()

0 comments on commit 8710ead

Please sign in to comment.