feat: add get_server_config function with validation
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -20,3 +20,40 @@ def load_config(config_path: Path) -> dict:
|
|||||||
return tomllib.load(f)
|
return tomllib.load(f)
|
||||||
except tomllib.TOMLDecodeError as e:
|
except tomllib.TOMLDecodeError as e:
|
||||||
raise ConfigError(f"Invalid TOML syntax: {e}") from e
|
raise ConfigError(f"Invalid TOML syntax: {e}") from e
|
||||||
|
|
||||||
|
|
||||||
|
REQUIRED_FIELDS = ["url", "username", "password"]
|
||||||
|
|
||||||
|
|
||||||
|
def get_server_config(config: dict, server_name: str) -> tuple[str, str, str]:
|
||||||
|
"""Extract server configuration from loaded config.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
config: Loaded config dictionary
|
||||||
|
server_name: Name of server section to use
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Tuple of (url, username, password)
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
ConfigError: If server not found or missing required fields
|
||||||
|
"""
|
||||||
|
if server_name not in config:
|
||||||
|
available = ", ".join(config.keys())
|
||||||
|
raise ConfigError(
|
||||||
|
f"Server '{server_name}' not found. Available servers: {available}"
|
||||||
|
)
|
||||||
|
|
||||||
|
server_config = config[server_name]
|
||||||
|
|
||||||
|
for field in REQUIRED_FIELDS:
|
||||||
|
if field not in server_config:
|
||||||
|
raise ConfigError(
|
||||||
|
f"Missing required field '{field}' in server '{server_name}'"
|
||||||
|
)
|
||||||
|
|
||||||
|
return (
|
||||||
|
server_config["url"],
|
||||||
|
server_config["username"],
|
||||||
|
server_config["password"],
|
||||||
|
)
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import tempfile
|
|||||||
import pytest
|
import pytest
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from zzpyjenkins.config import load_config, ConfigError
|
from zzpyjenkins.config import load_config, ConfigError, get_server_config
|
||||||
|
|
||||||
|
|
||||||
def test_load_config_file_not_found():
|
def test_load_config_file_not_found():
|
||||||
@@ -27,3 +27,44 @@ def test_load_config_invalid_toml():
|
|||||||
assert "Invalid TOML syntax" in str(exc_info.value)
|
assert "Invalid TOML syntax" in str(exc_info.value)
|
||||||
finally:
|
finally:
|
||||||
temp_path.unlink()
|
temp_path.unlink()
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_server_config_success():
|
||||||
|
"""Test getting a valid server config."""
|
||||||
|
config = {
|
||||||
|
"work": {
|
||||||
|
"url": "https://jenkins.example.com",
|
||||||
|
"username": "user",
|
||||||
|
"password": "pass"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result = get_server_config(config, "work")
|
||||||
|
assert result == ("https://jenkins.example.com", "user", "pass")
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_server_config_not_found():
|
||||||
|
"""Test error when server name doesn't exist."""
|
||||||
|
config = {
|
||||||
|
"work": {
|
||||||
|
"url": "https://jenkins.example.com",
|
||||||
|
"username": "user",
|
||||||
|
"password": "pass"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
with pytest.raises(ConfigError) as exc_info:
|
||||||
|
get_server_config(config, "home")
|
||||||
|
assert "Server 'home' not found" in str(exc_info.value)
|
||||||
|
assert "work" in str(exc_info.value)
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_server_config_missing_field():
|
||||||
|
"""Test error when required field is missing."""
|
||||||
|
config = {
|
||||||
|
"incomplete": {
|
||||||
|
"url": "https://jenkins.example.com",
|
||||||
|
"username": "user"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
with pytest.raises(ConfigError) as exc_info:
|
||||||
|
get_server_config(config, "incomplete")
|
||||||
|
assert "password" in str(exc_info.value)
|
||||||
|
|||||||
Reference in New Issue
Block a user