From d9878f15c414966c7f6c4757df3659208e2e2255 Mon Sep 17 00:00:00 2001 From: burtenshaw Date: Wed, 15 Jul 2026 09:06:46 +0200 Subject: [PATCH] Refactor init template path resolution --- src/openenv/cli/commands/init.py | 26 ++++++++++++++++---------- tests/test_cli/test_init.py | 19 +++++++++++++++++++ 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/src/openenv/cli/commands/init.py b/src/openenv/cli/commands/init.py index b29f4db60..a3ef8427c 100644 --- a/src/openenv/cli/commands/init.py +++ b/src/openenv/cli/commands/init.py @@ -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 @@ -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(): diff --git a/tests/test_cli/test_init.py b/tests/test_cli/test_init.py index 5f03bfb69..34b0ead6b 100644 --- a/tests/test_cli/test_init.py +++ b/tests/test_cli/test_init.py @@ -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 @@ -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"