Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions docreader/parser/excel_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io
import logging
import os
import shutil
import subprocess
import tempfile
import time
Expand Down Expand Up @@ -147,7 +148,8 @@ def find_soffice() -> Optional[str]:
if path and os.path.exists(path):
return path

result = subprocess.run(["which", "soffice"], capture_output=True, text=True)
if result.returncode == 0 and result.stdout.strip():
return result.stdout.strip()
return None
# `which` is a POSIX tool: a Windows host without Git has no such executable,
# so spawning it raised FileNotFoundError and that error escaped
# normalize_excel_bytes() before it could report an unrecognized spreadsheet.
# shutil.which() is portable and also honours PATHEXT on Windows.
return shutil.which("soffice")
33 changes: 33 additions & 0 deletions docreader/tests/test_excel_soffice_probe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""The LibreOffice probe must not depend on a POSIX `which` being installed."""

import os
import sys
import unittest

from docreader.parser.excel_convert import find_soffice, normalize_excel_bytes


class SofficeProbeTests(unittest.TestCase):
def _without_posix_tools(self, func):
"""Run func() with a PATH that has no `which` executable, like a bare Windows host."""
saved = os.environ.get("PATH", "")
os.environ["PATH"] = (
os.path.dirname(sys.executable) if os.name == "nt" else "/nonexistent-bin"
)
try:
return func()
finally:
os.environ["PATH"] = saved

def test_find_soffice_returns_none_instead_of_raising(self):
self.assertIsNone(self._without_posix_tools(find_soffice))

def test_unrecognized_spreadsheet_reports_the_documented_error(self):
with self.assertRaises(ValueError):
self._without_posix_tools(
lambda: normalize_excel_bytes(b"not a spreadsheet at all")
)


if __name__ == "__main__":
unittest.main()
Loading