From c1258bda3e5032c73feb7df8bd28762bc11b2fc8 Mon Sep 17 00:00:00 2001 From: tech Date: Wed, 18 Mar 2026 13:31:30 +0800 Subject: [PATCH] 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 --- src/zzpyjenkins/config.py | 22 ++++++++++++++++++++++ tests/__init__.py | 1 + tests/test_config.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 src/zzpyjenkins/config.py create mode 100644 tests/__init__.py create mode 100644 tests/test_config.py 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()