feat(config): add XDG path resolution with CWD fallback

Add _resolve_config_file() searching ~/.config/librofm-downloader/
then CWD. Wire into load_config() so None triggers search. Missing
config.yaml is non-fatal (prints notice, uses defaults); missing
secrets.yaml raises ConfigError listing searched paths. All OS
exceptions wrapped as domain errors. XDG dir auto-created.

Closes #22
This commit is contained in:
AlexKucera committed 2026-06-04 10:00:52 +02:00
1 parent 090132f39e
commit 3ba9f25903
6 files changed
+308 -14

No files matched your search

+157 -1
View File
@@ -6,6 +6,12 @@ import pytest
from librofm_downloader.config import (
load_config,
_resolve_config_file,
Config,
ConfigError,
CredentialsInConfigError,
load_config,
_resolve_config_file,
Config,
CredentialsInConfigError,
MissingFieldError,
@@ -230,4 +236,154 @@ class TestEdgeCases:
)
# config_minimal has no format, so it gets default
# We just verify it doesn't raise for valid formats
assert config.format in ("m4b_mp3_fallback", "mp3_only", "m4b_only")
assert config.format in ("m4b_mp3_fallback", "mp3_only", "m4b_only")
class TestResolveConfigFile:
"""XDG-then-CWD path resolution for config files."""
def test_returns_path_when_file_in_xdg(self, tmp_path, monkeypatch):
"""File found in XDG dir → returns resolved Path."""
xdg_dir = tmp_path / ".config" / "librofm-downloader"
xdg_dir.mkdir(parents=True)
(xdg_dir / "config.yaml").write_text("librofm:\n format: m4b_only\n")
(tmp_path / "cwd").mkdir(exist_ok=True)
monkeypatch.setenv("HOME", str(tmp_path))
monkeypatch.chdir(tmp_path / "cwd") # CWD has no file
result = _resolve_config_file("config.yaml")
assert result is not None
assert result.name == "config.yaml"
assert "librofm-downloader" in str(result)
def test_returns_path_when_file_only_in_cwd(self, tmp_path, monkeypatch):
"""File only in CWD (not XDG) → returns CWD Path."""
(tmp_path / "config.yaml").write_text("librofm:\n format: mp3_only\n")
monkeypatch.setenv("HOME", str(tmp_path))
monkeypatch.chdir(tmp_path) # CWD has the file, XDG is empty
result = _resolve_config_file("config.yaml")
assert result is not None
assert result.name == "config.yaml"
# Should be resolved from CWD, not XDG
assert ".config" not in str(result)
def test_xdg_wins_when_both_present(self, tmp_path, monkeypatch):
"""File in both XDG and CWD → XDG path takes priority."""
xdg_dir = tmp_path / ".config" / "librofm-downloader"
xdg_dir.mkdir(parents=True)
(xdg_dir / "config.yaml").write_text("xdg_version\n")
(tmp_path / "config.yaml").write_text("cwd_version\n")
monkeypatch.setenv("HOME", str(tmp_path))
monkeypatch.chdir(tmp_path)
result = _resolve_config_file("config.yaml")
assert result is not None
assert "librofm-downloader" in str(result) # XDG path
assert result.read_text() == "xdg_version\n"
def test_returns_none_when_file_absent_from_both(self, tmp_path, monkeypatch):
"""File missing from XDG and CWD → returns None."""
monkeypatch.setenv("HOME", str(tmp_path))
monkeypatch.chdir(tmp_path)
result = _resolve_config_file("config.yaml")
assert result is None
def test_xdg_directory_auto_created(self, tmp_path, monkeypatch):
"""XDG parent dir is created even when file is absent."""
xdg_parent = tmp_path / ".config" / "librofm-downloader"
assert not xdg_parent.exists()
monkeypatch.setenv("HOME", str(tmp_path))
monkeypatch.chdir(tmp_path)
_resolve_config_file("config.yaml")
assert xdg_parent.is_dir()
class TestLoadConfigWithResolution:
"""load_config wired to _resolve_config_file for None paths."""
def test_secrets_only_returns_defaulted_config(self, tmp_path, monkeypatch):
"""config.yaml missing, secrets.yaml present → defaulted Config + creds."""
xdg_dir = tmp_path / ".config" / "librofm-downloader"
xdg_dir.mkdir(parents=True)
(xdg_dir / "secrets.yaml").write_text(
"librofm:\n username: alice\n password: s3cret\n"
)
monkeypatch.setenv("HOME", str(tmp_path))
monkeypatch.chdir(tmp_path)
config = load_config(None, None)
# Credentials from secrets
assert config.username == "alice"
assert config.password == "s3cret"
# Built-in defaults (config.yaml was missing)
assert config.format == "m4b_mp3_fallback"
assert config.output_dir == "./audiobooks"
assert config.download_extras is True
assert config.download_covers is True
def test_neither_file_raises_error_with_searched_paths(self, tmp_path, monkeypatch):
"""Both files missing → ConfigError listing every path searched."""
monkeypatch.setenv("HOME", str(tmp_path))
monkeypatch.chdir(tmp_path)
with pytest.raises(ConfigError) as exc_info:
load_config(None, None)
msg = str(exc_info.value)
assert "secrets.yaml" in msg
assert "librofm-downloader" in msg or ".config" in msg
def test_explicit_config_path_bypasses_search(self, tmp_path, monkeypatch):
"""Explicit config path → used as-is; secrets still searches if None."""
# Explicit config file
explicit_config = tmp_path / "my_config.yaml"
explicit_config.write_text(
"librofm:\n format: mp3_only\n output_dir: ./custom\n"
)
# Secrets only in XDG
xdg_dir = tmp_path / ".config" / "librofm-downloader"
xdg_dir.mkdir(parents=True)
(xdg_dir / "secrets.yaml").write_text(
"librofm:\n username: bob\n password: p4ss\n"
)
monkeypatch.setenv("HOME", str(tmp_path))
monkeypatch.chdir(tmp_path)
config = load_config(explicit_config, None)
assert config.format == "mp3_only" # from explicit config
assert config.output_dir == "./custom" # from explicit config
assert config.username == "bob" # from resolved secrets
def test_missing_config_provides_detectable_signal(self, tmp_path, monkeypatch):
"""Missing config.yaml → caller can detect defaults were used."""
xdg_dir = tmp_path / ".config" / "librofm-downloader"
xdg_dir.mkdir(parents=True)
(xdg_dir / "secrets.yaml").write_text(
"librofm:\n username: carol\n password: pw\n"
)
monkeypatch.setenv("HOME", str(tmp_path))
monkeypatch.chdir(tmp_path)
config = load_config(None, None)
# Signal: _config_path is None when config.yaml was missing
assert config._config_path is None