Skip to content
Draft
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
26 changes: 16 additions & 10 deletions src/openenv/cli/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,16 +298,8 @@ def _copy_and_template_file(
) from e


def _copy_template_directory(
template_pkg: str,
template_dir: str,
dest_dir: Path,
replacements: Dict[str, str],
env_name: str,
) -> List[Path]:
"""Recursively copy template directory and apply replacements."""
created_files: List[Path] = []

def _resolve_template_path(template_pkg: str, template_dir: str) -> Path:
"""Resolve the template package directory used by `openenv init`."""
# Get the package path using importlib.resources but avoid importing the template package
# We'll use the package's __file__ to get the directory path
import importlib
Expand Down Expand Up @@ -341,6 +333,20 @@ def _copy_template_directory(
f"Template directory not found: {template_pkg}.{template_dir}"
)

return template_path


def _copy_template_directory(
template_pkg: str,
template_dir: str,
dest_dir: Path,
replacements: Dict[str, str],
env_name: str,
) -> List[Path]:
"""Recursively copy template directory and apply replacements."""
created_files: List[Path] = []
template_path = _resolve_template_path(template_pkg, template_dir)

# Walk through all files in template directory using Path
for item in template_path.rglob("*"):
if item.is_file():
Expand Down
19 changes: 19 additions & 0 deletions tests/test_cli/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from pathlib import Path

from openenv.cli.__main__ import app
from openenv.cli.commands.init import _resolve_template_path
from typer.testing import CliRunner


Expand Down Expand Up @@ -229,6 +230,24 @@ def test_init_with_output_dir(tmp_path: Path) -> None:
assert (env_dir / "models.py").exists()


def test_resolve_template_path_root() -> None:
"""Test resolving the packaged init template root."""
template_path = _resolve_template_path("openenv.cli.templates.openenv_env", "")

assert template_path.is_dir()
assert (template_path / "models.py").exists()


def test_resolve_template_path_nested_directory() -> None:
"""Test resolving a nested init template directory."""
template_path = _resolve_template_path(
"openenv.cli.templates.openenv_env", "server"
)

assert template_path.is_dir()
assert (template_path / "Dockerfile").exists()


def test_init_filename_templating(tmp_path: Path) -> None:
"""Test that filenames with placeholders are renamed correctly."""
env_name = "test_env"
Expand Down