diff --git a/src/zzpyjenkins/config.py b/src/zzpyjenkins/config.py new file mode 100644 index 0000000..3cf7bab --- /dev/null +++ b/src/zzpyjenkins/config.py @@ -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 diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..d4839a6 --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1 @@ +# Tests package diff --git a/tests/test_config.py b/tests/test_config.py new file mode 100644 index 0000000..f54ac22 --- /dev/null +++ b/tests/test_config.py @@ -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()