Files
gitignore-generator-cli/tests/test_generator.py
7000pctAUTO b64be9101b
Some checks failed
CI / test (push) Has been cancelled
CI / build (push) Has been cancelled
fix: resolve CI/CD issues - linting errors and test fixture
2026-01-30 15:43:35 +00:00

150 lines
6.1 KiB
Python

"""Tests for generator.py."""
from unittest.mock import patch
import pytest
from gitignore_generator.generator import (
GitignoreGenerator,
generate_gitignore
)
class TestGitignoreGenerator:
"""Tests for GitignoreGenerator class."""
@pytest.fixture
def generator(self):
"""Create generator instance."""
return GitignoreGenerator()
@pytest.fixture
def generator_with_custom(self, temp_dir):
"""Create generator with custom patterns file."""
custom_file = temp_dir / "custom.txt"
custom_file.write_text("*.custom\ncustom/")
return GitignoreGenerator(custom_patterns_file=str(custom_file))
def test_combine_patterns_single_tech(self, generator):
"""Test combining patterns for single technology."""
with patch('gitignore_generator.generator.get_patterns_batch') as mock:
mock.return_value = {
'python': '# Python\n__pycache__/\n*.pyc\n'
}
result = generator.combine_patterns(['python'])
assert '__pycache__' in result
assert '*.pyc' in result
def test_combine_patterns_multiple_techs(self, generator):
"""Test combining patterns for multiple technologies."""
with patch('gitignore_generator.generator.get_patterns_batch') as mock:
mock.return_value = {
'node': '# Node\nnode_modules/\n*.log\n',
'python': '# Python\n__pycache__/\n*.pyc\n'
}
result = generator.combine_patterns(['node', 'python'])
assert 'node_modules' in result
assert '__pycache__' in result
def test_combine_patterns_handles_duplicates(self, generator):
"""Test that duplicate patterns are handled."""
with patch('gitignore_generator.generator.get_patterns_batch') as mock:
mock.return_value = {
'node': '# Node\n*.log\n',
'python': '# Python\n*.log\n'
}
result = generator.combine_patterns(['node', 'python'])
lines = [line for line in result.splitlines() if line and not line.startswith('#')]
assert lines.count('*.log') == 1
def test_combine_patterns_add_patterns(self, generator):
"""Test adding custom patterns."""
with patch('gitignore_generator.generator.get_patterns_batch') as mock:
mock.return_value = {'python': '__pycache__/\n'}
result = generator.combine_patterns(['python'], add_patterns=['*.custom'])
assert '*.custom' in result
def test_generate_file_to_stdout(self, generator):
"""Test generating gitignore to stdout."""
with patch('gitignore_generator.generator.get_patterns_batch') as mock:
mock.return_value = {'python': '__pycache__/\n'}
result = generator.generate_file(['python'], preview=True)
assert '__pycache__' in result
def test_generate_file_to_path(self, generator, temp_dir):
"""Test generating gitignore to file."""
output_path = temp_dir / '.gitignore'
with patch('gitignore_generator.generator.get_patterns_batch') as mock:
mock.return_value = {'python': '__pycache__/\n'}
generator.generate_file(['python'], output_path=str(output_path))
assert output_path.exists()
content = output_path.read_text()
assert '__pycache__' in content
def test_generate_file_preview_mode(self, generator, temp_dir):
"""Test preview mode doesn't write file."""
output_path = temp_dir / '.gitignore'
with patch('gitignore_generator.generator.get_patterns_batch') as mock:
mock.return_value = {'python': '__pycache__/\n'}
generator.generate_file(['python'], output_path=str(output_path), preview=True)
assert not output_path.exists()
def test_load_custom_patterns(self, generator_with_custom):
"""Test loading custom patterns from file."""
patterns = generator_with_custom.load_custom_patterns()
assert '*.custom' in patterns
def test_generate_file_with_custom_patterns(self, generator_with_custom):
"""Test generating with custom patterns file."""
with patch('gitignore_generator.generator.get_patterns_batch') as mock:
mock.return_value = {'python': '__pycache__/\n'}
result = generator_with_custom.generate_file(['python'])
assert '__pycache__' in result
assert '*.custom' in result
def test_merge_with_existing(self, generator, temp_dir):
"""Test merging with existing .gitignore."""
existing = temp_dir / '.gitignore'
existing.write_text('# Existing\nexisting.txt\n')
with patch('gitignore_generator.generator.get_patterns_batch') as mock:
mock.return_value = {'python': '__pycache__/\n'}
result = generator.merge_with_existing(
str(existing), ['python']
)
assert 'existing.txt' in result
assert '__pycache__' in result
def test_combine_patterns_adds_comments(self, generator):
"""Test that technology comments are added."""
with patch('gitignore_generator.generator.get_patterns_batch') as mock:
mock.return_value = {'python': '__pycache__/\n'}
result = generator.combine_patterns(['python'])
assert '# --- PYTHON ---' in result
class TestGenerateGitignoreFunction:
"""Tests for generate_gitignore convenience function."""
def test_generate_gitignore(self):
"""Test generate_gitignore function."""
with patch('gitignore_generator.generator.get_patterns_batch') as mock:
mock.return_value = {'node': 'node_modules/\n'}
result = generate_gitignore(['node'])
assert 'node_modules' in result
def test_generate_gitignore_with_output(self, temp_dir):
"""Test generate_gitignore with output path."""
output_path = temp_dir / '.gitignore'
with patch('gitignore_generator.generator.get_patterns_batch') as mock:
mock.return_value = {'node': 'node_modules/\n'}
generate_gitignore(['node'], output_path=str(output_path))
assert output_path.exists()