feat: add config module with load_config function

Add TOML configuration file support with:
- ConfigError exception class for config-related errors
- load_config() function to parse TOML files with proper error handling
- Tests for missing file and invalid TOML syntax scenarios

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-18 13:31:30 +08:00
parent dfe91e2ce1
commit c1258bda3e
3 changed files with 52 additions and 0 deletions

22
src/zzpyjenkins/config.py Normal file
View File

@@ -0,0 +1,22 @@
"""Configuration file handling."""
from pathlib import Path
import tomllib
class ConfigError(Exception):
"""Raised when configuration is invalid or missing."""
pass
def load_config(config_path: Path) -> dict:
"""Load and parse TOML config file."""
if not config_path.exists():
raise ConfigError(f"Config file not found: {config_path}")
try:
with open(config_path, "rb") as f:
return tomllib.load(f)
except tomllib.TOMLDecodeError as e:
raise ConfigError(f"Invalid TOML syntax: {e}") from e

1
tests/__init__.py Normal file
View File

@@ -0,0 +1 @@
# Tests package

29
tests/test_config.py Normal file
View File

@@ -0,0 +1,29 @@
"""Tests for config module."""
import tempfile
import pytest
from pathlib import Path
from zzpyjenkins.config import load_config, ConfigError
def test_load_config_file_not_found():
"""Test load_config raises error when config file doesn't exist."""
with pytest.raises(ConfigError) as exc_info:
load_config(Path("/nonexistent/path/config.toml"))
assert "Config file not found" in str(exc_info.value)
def test_load_config_invalid_toml():
"""Test load_config raises error for invalid TOML syntax."""
with tempfile.NamedTemporaryFile(mode="w", suffix=".toml", delete=False) as f:
f.write("invalid toml [[[[")
temp_path = Path(f.name)
try:
with pytest.raises(ConfigError) as exc_info:
load_config(temp_path)
assert "Invalid TOML syntax" in str(exc_info.value)
finally:
temp_path.unlink()