133 lines
4.0 KiB
Python
133 lines
4.0 KiB
Python
"""Tests for configuration loading."""
|
|
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from src.auto_readme.config import ConfigLoader, ConfigValidator, ReadmeConfig
|
|
|
|
|
|
class TestConfigLoader:
|
|
"""Tests for ConfigLoader."""
|
|
|
|
def test_find_config_yaml(self, tmp_path):
|
|
"""Test finding YAML configuration file."""
|
|
(tmp_path / ".readmerc.yaml").write_text("project_name: test")
|
|
|
|
config_path = ConfigLoader.find_config(tmp_path)
|
|
assert config_path is not None
|
|
assert config_path.name == ".readmerc.yaml"
|
|
|
|
def test_find_config_yml(self, tmp_path):
|
|
"""Test finding YML configuration file."""
|
|
(tmp_path / ".readmerc.yml").write_text("project_name: test")
|
|
|
|
config_path = ConfigLoader.find_config(tmp_path)
|
|
assert config_path is not None
|
|
|
|
def test_find_config_toml(self, tmp_path):
|
|
"""Test finding TOML configuration file."""
|
|
(tmp_path / ".readmerc").write_text("project_name = 'test'")
|
|
|
|
config_path = ConfigLoader.find_config(tmp_path)
|
|
assert config_path is not None
|
|
|
|
def test_load_yaml_config(self, tmp_path):
|
|
"""Test loading YAML configuration file."""
|
|
config_file = tmp_path / ".readmerc.yaml"
|
|
config_file.write_text("""
|
|
project_name: "My Test Project"
|
|
description: "A test description"
|
|
template: "minimal"
|
|
interactive: true
|
|
|
|
sections:
|
|
order:
|
|
- title
|
|
- description
|
|
- overview
|
|
|
|
custom_fields:
|
|
author: "Test Author"
|
|
email: "test@example.com"
|
|
""")
|
|
|
|
config = ConfigLoader.load(config_file)
|
|
|
|
assert config.project_name == "My Test Project"
|
|
assert config.description == "A test description"
|
|
assert config.template == "minimal"
|
|
assert config.interactive is True
|
|
assert "author" in config.custom_fields
|
|
|
|
def test_load_toml_config(self, tmp_path):
|
|
"""Test loading TOML configuration file."""
|
|
config_file = tmp_path / "pyproject.toml"
|
|
config_file.write_text("""
|
|
[tool.auto-readme]
|
|
filename = "README.md"
|
|
sections = ["title", "description", "overview"]
|
|
""")
|
|
|
|
config = ConfigLoader.load(config_file)
|
|
|
|
assert config.output_filename == "README.md"
|
|
|
|
def test_load_nonexistent_file(self):
|
|
"""Test loading nonexistent configuration file."""
|
|
config = ConfigLoader.load(Path("/nonexistent/config.yaml"))
|
|
assert config.project_name is None
|
|
|
|
def test_load_invalid_yaml(self, tmp_path):
|
|
"""Test loading invalid YAML raises error."""
|
|
config_file = tmp_path / ".readmerc.yaml"
|
|
config_file.write_text("invalid: yaml: content: [")
|
|
|
|
with pytest.raises(ValueError):
|
|
ConfigLoader.load(config_file)
|
|
|
|
|
|
class TestConfigValidator:
|
|
"""Tests for ConfigValidator."""
|
|
|
|
def test_validate_valid_config(self):
|
|
"""Test validating a valid configuration."""
|
|
config = ReadmeConfig(
|
|
project_name="test",
|
|
template="base",
|
|
)
|
|
|
|
errors = ConfigValidator.validate(config)
|
|
assert len(errors) == 0
|
|
|
|
def test_validate_invalid_template(self):
|
|
"""Test validating invalid template name."""
|
|
config = ReadmeConfig(
|
|
project_name="test",
|
|
template="nonexistent",
|
|
)
|
|
|
|
errors = ConfigValidator.validate(config)
|
|
assert len(errors) == 1
|
|
assert "Invalid template" in errors[0]
|
|
|
|
def test_validate_invalid_section(self):
|
|
"""Test validating invalid section name."""
|
|
config = ReadmeConfig(
|
|
project_name="test",
|
|
sections={"order": ["invalid_section"]},
|
|
)
|
|
|
|
errors = ConfigValidator.validate(config)
|
|
assert len(errors) == 1
|
|
assert "Invalid section" in errors[0]
|
|
|
|
def test_generate_template(self):
|
|
"""Test generating configuration template."""
|
|
template = ConfigValidator.generate_template()
|
|
|
|
assert "project_name:" in template
|
|
assert "description:" in template
|
|
assert "template:" in template
|
|
assert "interactive:" in template
|