diff --git a/skills/shelfmark/download_books.py b/skills/shelfmark/download_books.py index f9930855..a7df5f27 100755 --- a/skills/shelfmark/download_books.py +++ b/skills/shelfmark/download_books.py @@ -294,7 +294,13 @@ def check_calibre(title: str) -> bool: import subprocess cmd = ["calibredb", "list", "--search", calibre_title_search(title)] - result = subprocess.run(cmd, check=False, capture_output=True, text=True) + try: + result = subprocess.run(cmd, check=False, capture_output=True, text=True) + except OSError as exc: + # calibredb missing, not executable, or unloadable: the old shell call + # answered 127 and its empty output read as "not in the library". + print(f"warning: cannot run calibredb ({exc}); treating the book as absent") + return False output = result.stdout.strip() # calibredb output has header line "id title authors", count non-header lines lines = [l.strip() for l in output.split("\n") if l.strip()] diff --git a/tests/skills/test_check_calibre.py b/tests/skills/test_check_calibre.py index f07282f4..95641742 100644 --- a/tests/skills/test_check_calibre.py +++ b/tests/skills/test_check_calibre.py @@ -70,6 +70,16 @@ def test_title_is_one_argument_and_one_query_term(title): assert _every_quote_escaped(inner), inner +@pytest.mark.parametrize("error", [FileNotFoundError("calibredb"), PermissionError("calibredb")]) +def test_missing_or_unusable_calibredb_reads_as_absent(error, capsys): + """A shell answered 127 and looked like "not in the library"; a missing or + unusable binary must not raise out of the batch.""" + module = _load_module() + with patch.object(subprocess, "run", side_effect=error): + assert module.check_calibre("Dune") is False + assert "cannot run calibredb" in capsys.readouterr().out + + def _every_quote_escaped(inner: str) -> bool: i = 0 while i < len(inner):