"""Tests for template engine.""" import tempfile from pathlib import Path import pytest from project_scaffold_cli.template_engine import TemplateEngine class TestTemplateEngine: """Test TemplateEngine class.""" def test_engine_initialization(self): """Test engine can be initialized.""" engine = TemplateEngine() assert engine is not None assert engine.SUPPORTED_LANGUAGES == ["python", "nodejs", "go", "rust"] def test_render_language_template_python(self): """Test rendering Python template.""" engine = TemplateEngine() context = { "project_name": "test-project", "project_slug": "test-project", "author": "Test Author", "email": "test@example.com", "description": "A test project", "license": "MIT", "year": "2024", "language": "python", } with tempfile.TemporaryDirectory() as tmpdir: output_dir = Path(tmpdir) / "test-project" output_dir.mkdir() engine.render_language_template("python", context, output_dir) assert (output_dir / "setup.py").exists() assert (output_dir / "README.md").exists() def test_render_language_template_go(self): """Test rendering Go template.""" engine = TemplateEngine() context = { "project_name": "test-go-project", "project_slug": "test-go-project", "author": "Test Author", "email": "test@example.com", "description": "A test Go project", "license": "MIT", "year": "2024", "language": "go", } with tempfile.TemporaryDirectory() as tmpdir: output_dir = Path(tmpdir) / "test-go-project" output_dir.mkdir() engine.render_language_template("go", context, output_dir) assert (output_dir / "go.mod").exists() assert (output_dir / "main.go").exists() assert (output_dir / "README.md").exists() def test_render_language_template_rust(self): """Test rendering Rust template.""" engine = TemplateEngine() context = { "project_name": "test-rust-project", "project_slug": "test-rust-project", "author": "Test Author", "email": "test@example.com", "description": "A test Rust project", "license": "MIT", "year": "2024", "language": "rust", } with tempfile.TemporaryDirectory() as tmpdir: output_dir = Path(tmpdir) / "test-rust-project" output_dir.mkdir() engine.render_language_template("rust", context, output_dir) assert (output_dir / "Cargo.toml").exists() assert (output_dir / "src" / "main.rs").exists() assert (output_dir / "README.md").exists() def test_render_language_template_unsupported(self): """Test rendering unsupported language.""" engine = TemplateEngine() context = {"project_name": "test"} with tempfile.TemporaryDirectory() as tmpdir: with pytest.raises(ValueError) as exc_info: engine.render_language_template( "unsupported", context, Path(tmpdir) ) assert "Unsupported language" in str(exc_info.value) def test_render_ci_template_github(self): """Test rendering GitHub Actions CI template.""" engine = TemplateEngine() context = { "project_name": "test-project", "project_slug": "test-project", } with tempfile.TemporaryDirectory() as tmpdir: output_dir = Path(tmpdir) engine.render_ci_template("github", context, output_dir) workflow_path = ( output_dir / ".github" / "workflows" / "ci.yml" ) assert workflow_path.exists() content = workflow_path.read_text() assert "CI" in content def test_render_ci_template_gitlab(self): """Test rendering GitLab CI template.""" engine = TemplateEngine() context = { "project_name": "test-project", "project_slug": "test-project", } with tempfile.TemporaryDirectory() as tmpdir: output_dir = Path(tmpdir) engine.render_ci_template("gitlab", context, output_dir) assert (output_dir / ".gitlab-ci.yml").exists() def test_validate_context_missing_required(self): """Test context validation with missing required fields.""" engine = TemplateEngine() missing = engine.validate_context({}) assert "project_name" in missing assert "author" in missing def test_validate_context_complete(self): """Test context validation with all required fields.""" engine = TemplateEngine() context = {"project_name": "test", "author": "Test Author"} missing = engine.validate_context(context) assert len(missing) == 0