Merge branch 'develop' into feature/parallel-downloads

This commit is contained in:
AlexKucera committed 2026-06-04 11:04:19 +02:00
commit 44fc544a3e
22 files changed
+1051 -113

No files matched your search

+223
View File
@@ -647,6 +647,229 @@ class TestAllBooksFailExitCode:
assert mock_download.call_count == 3
# ---------------------------------------------------------------------------
# Issue #23: XDG path resolution wired into CLI
# ---------------------------------------------------------------------------
class TestCLIHistoryResolution:
"""History path resolves via XDG → CWD when not explicitly provided."""
def test_history_defaults_to_xdg_location(self, tmp_path, monkeypatch):
"""run() with history_path=None → history resolves to XDG path."""
xdg_dir = tmp_path / ".config" / "librofm-downloader"
xdg_dir.mkdir(parents=True)
(xdg_dir / "download_history.json").write_text("{}")
cwd = tmp_path / "cwd"
cwd.mkdir()
monkeypatch.setenv("HOME", str(tmp_path))
monkeypatch.chdir(cwd)
with (
patch("librofm_downloader.cli.load_config") as mock_config,
patch("librofm_downloader.cli.LibroFmClient") as mock_client_cls,
patch("librofm_downloader.cli.DownloadHistory") as mock_history_cls,
):
mock_config.return_value.username = "alice"
mock_config.return_value.password = "secret"
mock_config.return_value._config_path = None
mock_config.return_value.output_dir = "./audiobooks"
mock_instance = mock_client_cls.return_value
mock_instance.authenticate.return_value = "tok"
mock_instance.fetch_library.return_value = []
exit_code = run(
config_path=None,
secrets_path=None,
history_path=None,
)
assert exit_code == 0
# Verify DownloadHistory received the XDG path
history_arg = mock_history_cls.call_args[0][0]
assert "librofm-downloader" in str(history_arg)
assert "download_history.json" in str(history_arg)
def test_history_defaults_to_xdg_when_not_found_anywhere(self, tmp_path, monkeypatch):
"""No download_history.json anywhere → defaults to XDG location."""
cwd = tmp_path / "cwd"
cwd.mkdir()
monkeypatch.setenv("HOME", str(tmp_path))
monkeypatch.chdir(cwd)
with (
patch("librofm_downloader.cli.load_config") as mock_config,
patch("librofm_downloader.cli.LibroFmClient") as mock_client_cls,
patch("librofm_downloader.cli.DownloadHistory") as mock_history_cls,
):
mock_config.return_value.username = "alice"
mock_config.return_value.password = "secret"
mock_config.return_value._config_path = None
mock_config.return_value.output_dir = "./audiobooks"
mock_instance = mock_client_cls.return_value
mock_instance.authenticate.return_value = "tok"
mock_instance.fetch_library.return_value = []
exit_code = run(history_path=None)
assert exit_code == 0
# Should default to XDG location even though file doesn't exist
history_arg = mock_history_cls.call_args[0][0]
assert "librofm-downloader" in str(history_arg)
assert ".config" in str(history_arg)
assert "download_history.json" in str(history_arg)
def test_explicit_history_path_bypasses_resolution(self, tmp_path, monkeypatch):
"""Explicit --history flag uses the exact path provided."""
with (
patch("librofm_downloader.cli.load_config") as mock_config,
patch("librofm_downloader.cli.LibroFmClient") as mock_client_cls,
patch("librofm_downloader.cli.DownloadHistory") as mock_history_cls,
):
mock_config.return_value.username = "alice"
mock_config.return_value.password = "secret"
mock_config.return_value._config_path = None
mock_config.return_value.output_dir = "./audiobooks"
mock_instance = mock_client_cls.return_value
mock_instance.authenticate.return_value = "tok"
mock_instance.fetch_library.return_value = []
exit_code = run(history_path="/custom/my_history.json")
assert exit_code == 0
history_arg = mock_history_cls.call_args[0][0]
assert str(history_arg) == "/custom/my_history.json"
class TestCLIConfigResolutionMessages:
"""CLI messages about config resolution."""
def test_missing_config_message_includes_searched_paths(self, tmp_path, monkeypatch, capsys):
"""config.yaml not found → message lists XDG and CWD paths."""
xdg_dir = tmp_path / ".config" / "librofm-downloader"
xdg_dir.mkdir(parents=True)
(xdg_dir / "secrets.yaml").write_text(
"librofm:\n username: alice\n password: secret\n"
)
cwd = tmp_path / "cwd"
cwd.mkdir()
monkeypatch.setenv("HOME", str(tmp_path))
monkeypatch.chdir(cwd)
with (
patch("librofm_downloader.cli.LibroFmClient") as mock_client_cls,
patch("librofm_downloader.cli.download_book"),
):
mock_instance = mock_client_cls.return_value
mock_instance.authenticate.return_value = "tok"
mock_instance.fetch_library.return_value = []
exit_code = run(config_path=None, secrets_path=None, history_path=None)
assert exit_code == 0
output = capsys.readouterr().out
assert "config.yaml not found" in output
assert ".config" in output or "librofm-downloader" in output
assert "Using built-in defaults" in output
def test_verbose_shows_resolved_paths(self, tmp_path, monkeypatch, capsys):
"""--verbose shows the actual resolved file paths after loading config."""
xdg_dir = tmp_path / ".config" / "librofm-downloader"
xdg_dir.mkdir(parents=True)
(xdg_dir / "secrets.yaml").write_text(
"librofm:\n username: alice\n password: secret\n"
)
(xdg_dir / "download_history.json").write_text("{}")
cwd = tmp_path / "cwd"
cwd.mkdir()
monkeypatch.setenv("HOME", str(tmp_path))
monkeypatch.chdir(cwd)
with (
patch("librofm_downloader.cli.LibroFmClient") as mock_client_cls,
patch("librofm_downloader.cli.download_book"),
):
mock_instance = mock_client_cls.return_value
mock_instance.authenticate.return_value = "tok"
mock_instance.fetch_library.return_value = []
exit_code = run(
config_path=None,
secrets_path=None,
history_path=None,
verbose=True,
)
assert exit_code == 0
output = capsys.readouterr().out.lower()
# Verbose should show the resolved XDG paths
assert "secrets" in output
assert "history" in output
assert "librofm-downloader" in output
class TestCLIMissingFilesErrors:
"""Error handling when files are missing."""
def test_empty_home_clean_secrets_error(self, tmp_path, monkeypatch, capsys):
"""No files anywhere → clean error about missing secrets, exit 1."""
cwd = tmp_path / "cwd"
cwd.mkdir()
monkeypatch.setenv("HOME", str(tmp_path))
monkeypatch.chdir(cwd)
exit_code = run(config_path=None, secrets_path=None, history_path=None)
assert exit_code == 1
output = capsys.readouterr().out
assert "secrets.yaml" in output
assert "Traceback" not in output
def test_only_secrets_in_xdg_works(self, tmp_path, monkeypatch, capsys):
"""Secrets in XDG only → defaults work, 'using built-in defaults' message visible."""
xdg_dir = tmp_path / ".config" / "librofm-downloader"
xdg_dir.mkdir(parents=True)
(xdg_dir / "secrets.yaml").write_text(
"librofm:\n username: alice\n password: secret\n"
)
cwd = tmp_path / "cwd"
cwd.mkdir()
monkeypatch.setenv("HOME", str(tmp_path))
monkeypatch.chdir(cwd)
with (
patch("librofm_downloader.cli.LibroFmClient") as mock_client_cls,
patch("librofm_downloader.cli.download_book"),
):
mock_instance = mock_client_cls.return_value
mock_instance.authenticate.return_value = "tok"
mock_instance.fetch_library.return_value = []
exit_code = run(config_path=None, secrets_path=None, history_path=None)
assert exit_code == 0
output = capsys.readouterr().out
assert "Using built-in defaults" in output
# ---------------------------------------------------------------------------
# Issue #12: workers config field + --workers CLI flag
# ---------------------------------------------------------------------------
+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,
@@ -275,4 +281,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