ghostscript: improve test coverage of error cases

This commit is contained in:
James R. Barlow
2022-01-25 23:45:47 -08:00
parent fcc4c2d371
commit 3b406112d0
2 changed files with 24 additions and 6 deletions
+4 -5
View File
@@ -71,7 +71,8 @@ def jpeg_passthrough_available() -> bool:
def _gs_error_reported(stream) -> bool:
return True if re.search(r'error', stream, flags=re.IGNORECASE) else False
match = re.search(r'error', stream, flags=re.IGNORECASE)
return bool(match)
def rasterize_pdf(
@@ -160,16 +161,14 @@ class GhostscriptFollower:
if not self.progressbar_class:
return
if not self.progressbar:
m = self.re_process.match(line.strip())
if m:
if m := self.re_process.match(line.strip()):
self.count = int(m.group(1))
self.progressbar = self.progressbar_class(
total=self.count, desc="PDF/A conversion", unit='page'
)
return
else:
m = self.re_page.match(line.strip())
if m:
if self.re_page.match(line.strip()):
self.progressbar.update()
+20 -1
View File
@@ -6,11 +6,13 @@
import logging
import subprocess
from decimal import Decimal
from unittest.mock import patch
import pikepdf
import pytest
from PIL import Image
from PIL import Image, UnidentifiedImageError
from ocrmypdf._exec.ghostscript import rasterize_pdf
from ocrmypdf.exceptions import ExitCode
@@ -124,3 +126,20 @@ def test_ghostscript_feature_elision(resources, outpdf):
'--plugin',
'tests/plugins/gs_feature_elision.py',
)
def test_rasterize_pdf_errors(resources, no_outpdf, caplog):
with patch('ocrmypdf._exec.ghostscript.run') as mock:
# ghostscript can produce
mock.return_value = subprocess.CompletedProcess(
['fakegs'], returncode=0, stdout=b'', stderr=b'error this is an error'
)
with pytest.raises(UnidentifiedImageError):
rasterize_pdf(
resources / 'francais.pdf',
no_outpdf,
raster_device='pngmono',
raster_dpi=Resolution(100, 100),
)
assert "this is an error" in caplog.text
assert "invalid page image file" in caplog.text