diff --git a/gitignore_generator/tests/test_template_loader.py b/gitignore_generator/tests/test_template_loader.py new file mode 100644 index 0000000..167a881 --- /dev/null +++ b/gitignore_generator/tests/test_template_loader.py @@ -0,0 +1,112 @@ +"""Tests for template loader.""" + +import os +import tempfile +from pathlib import Path + +import pytest + +from gitignore_generator.template_loader import TemplateLoader + + +class TestTemplateLoader: + """Tests for TemplateLoader class.""" + + @pytest.fixture + def temp_template_dir(self): + """Create temporary template directory.""" + with tempfile.TemporaryDirectory() as tmpdir: + yield Path(tmpdir) + + @pytest.fixture + def loader_with_templates(self, temp_template_dir): + """Create loader with test templates.""" + lang_dir = temp_template_dir / "languages" + lang_dir.mkdir() + ide_dir = temp_template_dir / "ides" + ide_dir.mkdir() + + (lang_dir / "python.gitignore").write_text("# Python\n__pycache__/\n*.pyc\n") + (lang_dir / "javascript.gitignore").write_text("# JS\nnode_modules/\n") + (ide_dir / "vscode.gitignore").write_text("# VSCode\n.vscode/\n") + + index = {"languages": ["python", "javascript"], "ides": ["vscode"], "custom": []} + (temp_template_dir / "templates.json").write_text(str(index)) + + loader = TemplateLoader() + loader._templates_index = index + return loader + + def test_get_available_templates(self, loader_with_templates): + """Test getting available templates.""" + templates = loader_with_templates.get_available_templates() + assert "python" in templates + assert "javascript" in templates + assert "vscode" in templates + + def test_get_templates_by_category(self, loader_with_templates): + """Test getting templates by category.""" + by_cat = loader_with_templates.get_templates_by_category() + assert "python" in by_cat["languages"] + assert "javascript" in by_cat["languages"] + assert "vscode" in by_cat["ides"] + + def test_load_template(self, loader_with_templates): + """Test loading a template.""" + content = loader_with_templates.load_template("python") + assert content is not None + assert "__pycache__" in content + + def test_load_nonexistent_template(self, loader_with_templates): + """Test loading nonexistent template.""" + content = loader_with_templates.load_template("nonexistent") + assert content is None + + def test_template_exists(self, loader_with_templates): + """Test checking if template exists.""" + assert loader_with_templates.template_exists("python") + assert not loader_with_templates.template_exists("nonexistent") + + def test_search_templates(self, loader_with_templates): + """Test searching templates.""" + results = loader_with_templates.search_templates("python") + assert "python" in results + + def test_merge_templates(self, loader_with_templates): + """Test merging templates.""" + merged = loader_with_templates.merge_templates(["python", "javascript"]) + assert "__pycache__" in merged + assert "node_modules" in merged + + def test_save_and_delete_custom_template(self, loader_with_templates, temp_template_dir): + """Test saving and deleting custom templates.""" + loader = loader_with_templates + custom_dir = temp_template_dir / "custom" + custom_dir.mkdir() + + content = "# Custom\ncustom_file.txt\n" + result = loader.save_custom_template("my_template", content) + assert result is True + + loaded = loader.load_template("my_template") + assert loaded is not None + assert "custom_file.txt" in loaded + + delete_result = loader.delete_custom_template("my_template") + assert delete_result is True + + loaded_after = loader.load_template("my_template") + assert loaded_after is None + + def test_load_template_with_category(self, loader_with_templates): + """Test loading template with explicit category.""" + content = loader_with_templates.load_template("python", category="languages") + assert content is not None + assert "__pycache__" in content + + def test_duplicate_removal_in_merge(self, loader_with_templates): + """Test that duplicates are removed when merging.""" + merged = loader_with_templates.merge_templates(["python", "python"]) + lines = [l for l in merged.splitlines() if l.strip() and not l.strip().startswith("#")] + unique_lines = set(lines) + assert len(lines) == len(unique_lines)