84 lines
2.3 KiB
Python
84 lines
2.3 KiB
Python
"""Tests for config module."""
|
|
|
|
import tempfile
|
|
|
|
import pytest
|
|
from pathlib import Path
|
|
|
|
from zzpyjenkins.config import (
|
|
load_config,
|
|
ConfigError,
|
|
get_server_config,
|
|
get_default_config_path,
|
|
)
|
|
|
|
|
|
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()
|
|
|
|
|
|
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)
|
|
|
|
|
|
def test_get_default_config_path():
|
|
"""Test default config path is correct."""
|
|
path = get_default_config_path()
|
|
assert path.name == "config.toml"
|
|
assert ".config" in str(path)
|
|
assert "zzpyjenkins" in str(path)
|