- Fixed undefined 'tool' variable in display_history function - Changed '[tool]' markup tag usage to proper Rich syntax - All tests now pass (38/38 unit tests) - Type checking passes with mypy --strict
112 lines
3.7 KiB
Python
112 lines
3.7 KiB
Python
"""Tests for template module."""
|
|
|
|
import pytest
|
|
from pathlib import Path
|
|
|
|
|
|
class TestTemplateManagement:
|
|
"""Test cases for template management."""
|
|
|
|
def test_list_builtin_templates(self):
|
|
"""Test listing builtin templates."""
|
|
from env_pro.core.template import list_builtin_templates
|
|
|
|
templates = list_builtin_templates()
|
|
assert "fastapi" in templates
|
|
assert "django" in templates
|
|
assert "nodejs" in templates
|
|
assert "python" in templates
|
|
|
|
def test_load_builtin_template(self):
|
|
"""Test loading a builtin template."""
|
|
from env_pro.core.template import load_template
|
|
|
|
content = load_template("minimal")
|
|
assert "ENVIRONMENT" in content
|
|
|
|
def test_get_template_info(self):
|
|
"""Test getting template information."""
|
|
from env_pro.core.template import get_template_info
|
|
|
|
info = get_template_info("fastapi")
|
|
assert info["name"] == "fastapi"
|
|
assert info["type"] == "builtin"
|
|
assert "APP_NAME" in info["variables"]
|
|
|
|
def test_render_template(self):
|
|
"""Test rendering a template with variables."""
|
|
from env_pro.core.template import render_template
|
|
|
|
content = "APP_NAME=${APP_NAME}\nDEBUG=${DEBUG}"
|
|
variables = {"APP_NAME": "MyApp", "DEBUG": "true"}
|
|
rendered = render_template(content, variables)
|
|
assert "MyApp" in rendered
|
|
assert "true" in rendered
|
|
|
|
def test_render_template_missing_variable(self):
|
|
"""Test rendering template with missing variable raises error."""
|
|
from env_pro.core.template import render_template, TemplateSyntaxError
|
|
|
|
content = "APP_NAME=${APP_NAME}\nMISSING=${OTHER}"
|
|
variables = {"APP_NAME": "MyApp"}
|
|
|
|
with pytest.raises(TemplateSyntaxError):
|
|
render_template(content, variables)
|
|
|
|
def test_create_and_delete_user_template(self, temp_dir):
|
|
"""Test creating and deleting a user template."""
|
|
from env_pro.core.template import (
|
|
create_template, delete_template, load_template,
|
|
get_user_templates_dir, list_user_templates
|
|
)
|
|
import os
|
|
|
|
original_home = os.environ.get("HOME")
|
|
os.environ["HOME"] = str(temp_dir)
|
|
|
|
try:
|
|
create_template("my-template", "VAR1=value1\nVAR2=value2", "My custom template")
|
|
|
|
templates = list_user_templates()
|
|
assert "my-template" in templates
|
|
|
|
loaded = load_template("my-template")
|
|
assert "VAR1=value1" in loaded
|
|
|
|
delete_template("my-template")
|
|
templates = list_user_templates()
|
|
assert "my-template" not in templates
|
|
finally:
|
|
if original_home:
|
|
os.environ["HOME"] = original_home
|
|
else:
|
|
del os.environ["HOME"]
|
|
|
|
def test_apply_template(self, temp_dir):
|
|
"""Test applying a template to a file."""
|
|
from env_pro.core.template import apply_template
|
|
|
|
output_file = temp_dir / ".env"
|
|
apply_template("minimal", {"ENVIRONMENT": "production"}, output_file)
|
|
|
|
content = output_file.read_text()
|
|
assert "production" in content
|
|
|
|
|
|
class TestTemplateErrors:
|
|
"""Test cases for template errors."""
|
|
|
|
def test_load_nonexistent_template(self):
|
|
"""Test loading a template that doesn't exist."""
|
|
from env_pro.core.template import load_template, TemplateNotFoundError
|
|
|
|
with pytest.raises(TemplateNotFoundError):
|
|
load_template("nonexistent-template")
|
|
|
|
def test_delete_builtin_template(self):
|
|
"""Test that builtin templates cannot be deleted."""
|
|
from env_pro.core.template import delete_template, TemplateError
|
|
|
|
with pytest.raises(TemplateError):
|
|
delete_template("fastapi")
|