From 9bb6ab04b916f0858e2783b14e7f426db5a3ac11 Mon Sep 17 00:00:00 2001 From: gouyu-hou Date: Sat, 28 Nov 2026 15:27:13 +0800 Subject: [PATCH] fix(excel): probe soffice without a POSIX which binary find_soffice() shelled out to ["which", "soffice"] after the known install paths. A Windows host without Git has no which executable, so subprocess.run raised FileNotFoundError, which escaped normalize_excel_bytes() and replaced the documented ValueError ("the file may be corrupt, encrypted, or not a spreadsheet") with a bare WinError 2 for every unrecognized spreadsheet. Use shutil.which(), which is portable and also honours PATHEXT. --- docreader/parser/excel_convert.py | 10 ++++--- docreader/tests/test_excel_soffice_probe.py | 33 +++++++++++++++++++++ 2 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 docreader/tests/test_excel_soffice_probe.py diff --git a/docreader/parser/excel_convert.py b/docreader/parser/excel_convert.py index b9d4006d417..d270ba06d3b 100644 --- a/docreader/parser/excel_convert.py +++ b/docreader/parser/excel_convert.py @@ -5,6 +5,7 @@ import io import logging import os +import shutil import subprocess import tempfile import time @@ -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") diff --git a/docreader/tests/test_excel_soffice_probe.py b/docreader/tests/test_excel_soffice_probe.py new file mode 100644 index 00000000000..9e4a9e50cdb --- /dev/null +++ b/docreader/tests/test_excel_soffice_probe.py @@ -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()