diff --git a/src/ocrmypdf/subprocess/_version.py b/src/ocrmypdf/subprocess/_version.py index 2ea26363..175b05c3 100644 --- a/src/ocrmypdf/subprocess/_version.py +++ b/src/ocrmypdf/subprocess/_version.py @@ -62,12 +62,18 @@ def get_version( f"Could not find program '{program}' on the PATH" ) from e - match = re.match(regex, output.strip()) - if not match: + # Some tools (e.g. veraPDF launched on a recent JDK) print warnings before + # the version line, so scan each line rather than only the start of output. + version = None + for line in output.splitlines(): + match = re.match(regex, line.strip()) + if match: + version = match.group(1) + break + if version is None: raise MissingDependencyError( f"The program '{program}' did not report its version. " f"Message was:\n{output}" ) - version = match.group(1) return version diff --git a/tests/test_main.py b/tests/test_main.py index 8428d801..193c06b5 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -100,9 +100,9 @@ def test_redo_ocr(resources, outpdf): out = check_ocrmypdf(in_, out, '--redo-ocr') after = PdfInfo(out, detailed_analysis=True) assert before[0].has_text and after[0].has_text - assert ( - before[0].get_textareas() != after[0].get_textareas() - ), "Expected text to be different after re-OCR" + assert before[0].get_textareas() != after[0].get_textareas(), ( + "Expected text to be different after re-OCR" + ) def test_argsfile(resources, outdir): @@ -768,9 +768,9 @@ def test_sidecar_pagecount(resources, outpdf): # There should a formfeed between each pair of pages, so the count of # formfeeds is the page count less one - assert ( - ocr_text.count('\f') == num_pages - 1 - ), "Sidecar page count does not match PDF page count" + assert ocr_text.count('\f') == num_pages - 1, ( + "Sidecar page count does not match PDF page count" + ) def test_sidecar_nonempty(resources, outpdf): @@ -889,6 +889,27 @@ def test_version_check(): get_version('echo') +def test_get_version_skips_leading_warning_lines(monkeypatch): + """VeraPDF 1.30.0 prints JVM warnings before its version line.""" + from subprocess import CompletedProcess + + import ocrmypdf.subprocess as sp + + output = ( + "WARNING: Final field flavour has been mutated reflectively\n" + "WARNING: Use --enable-final-field-mutation=ALL-UNNAMED to avoid this\n" + "veraPDF 1.30.0\n" + "Built: Wed Jun 03 13:29:00 PDT 2026\n" + ) + + def fake_run(args, **kwargs): + return CompletedProcess(args, 0, stdout=output, stderr="") + + monkeypatch.setattr(sp, 'run', fake_run) + version = get_version('verapdf', regex=r'veraPDF (\d+(\.\d+)*)') + assert version == '1.30.0' + + @pytest.mark.parametrize( 'threshold, optimize, output_type, expected', [