Add test cases for soft errors
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
# SPDX-FileCopyrightText: 2022 James R. Barlow
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
from subprocess import CalledProcessError
|
||||
from unittest.mock import patch
|
||||
|
||||
from ocrmypdf import hookimpl
|
||||
from ocrmypdf.builtin_plugins import ghostscript
|
||||
from ocrmypdf.subprocess import run
|
||||
|
||||
|
||||
def fail_if_stoponerror(args, **kwargs):
|
||||
if '-dPDFSTOPONERROR' in args:
|
||||
raise CalledProcessError(1, 'gs', output=b"", stderr=b"PDF STOP ON ERROR")
|
||||
return run(args, **kwargs)
|
||||
|
||||
|
||||
@hookimpl
|
||||
def rasterize_pdf_page(
|
||||
input_file,
|
||||
output_file,
|
||||
raster_device,
|
||||
raster_dpi,
|
||||
pageno,
|
||||
page_dpi,
|
||||
rotation,
|
||||
filter_vector,
|
||||
stop_on_soft_error,
|
||||
) -> Path:
|
||||
with patch('ocrmypdf._exec.ghostscript.run') as mock:
|
||||
mock.side_effect = fail_if_stoponerror
|
||||
ghostscript.rasterize_pdf_page(
|
||||
input_file=input_file,
|
||||
output_file=output_file,
|
||||
raster_device=raster_device,
|
||||
raster_dpi=raster_dpi,
|
||||
pageno=pageno,
|
||||
page_dpi=page_dpi,
|
||||
rotation=rotation,
|
||||
filter_vector=filter_vector,
|
||||
stop_on_soft_error=stop_on_soft_error,
|
||||
)
|
||||
mock.assert_called()
|
||||
return output_file
|
||||
@@ -0,0 +1,44 @@
|
||||
# SPDX-FileCopyrightText: 2022 James R. Barlow
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
from subprocess import CalledProcessError
|
||||
from unittest.mock import patch
|
||||
|
||||
from ocrmypdf import hookimpl
|
||||
from ocrmypdf.builtin_plugins import ghostscript
|
||||
from ocrmypdf.subprocess import run_polling_stderr
|
||||
|
||||
|
||||
def fail_if_stoponerror(args, **kwargs):
|
||||
if '-dPDFSTOPONERROR' in args:
|
||||
raise CalledProcessError(1, 'gs', output=b"", stderr=b"PDF STOP ON ERROR")
|
||||
return run_polling_stderr(args, **kwargs)
|
||||
|
||||
|
||||
@hookimpl
|
||||
def generate_pdfa(
|
||||
pdf_pages,
|
||||
pdfmark,
|
||||
output_file,
|
||||
compression,
|
||||
pdf_version,
|
||||
pdfa_part,
|
||||
stop_on_soft_error,
|
||||
):
|
||||
with patch('ocrmypdf._exec.ghostscript.run_polling_stderr') as mock:
|
||||
mock.side_effect = fail_if_stoponerror
|
||||
ghostscript.generate_pdfa(
|
||||
pdf_pages=pdf_pages,
|
||||
pdfmark=pdfmark,
|
||||
output_file=output_file,
|
||||
compression=compression,
|
||||
pdf_version=pdf_version,
|
||||
pdfa_part=pdfa_part,
|
||||
progressbar_class=None,
|
||||
stop_on_soft_error=stop_on_soft_error,
|
||||
)
|
||||
mock.assert_called()
|
||||
return output_file
|
||||
@@ -0,0 +1,58 @@
|
||||
# SPDX-FileCopyrightText: 2023 James R. Barlow
|
||||
# SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from ocrmypdf.exceptions import ExitCode
|
||||
|
||||
from .conftest import run_ocrmypdf
|
||||
|
||||
|
||||
def test_raster_continue_on_soft_error(resources, outpdf):
|
||||
p = run_ocrmypdf(
|
||||
resources / 'francais.pdf',
|
||||
outpdf,
|
||||
'--continue-on-soft-render-error',
|
||||
'--plugin',
|
||||
'tests/plugins/tesseract_noop.py',
|
||||
'--plugin',
|
||||
'tests/plugins/gs_raster_soft_error.py',
|
||||
)
|
||||
assert p.returncode == ExitCode.ok
|
||||
|
||||
|
||||
def test_raster_stop_on_soft_error(resources, outpdf):
|
||||
p = run_ocrmypdf(
|
||||
resources / 'francais.pdf',
|
||||
outpdf,
|
||||
'--plugin',
|
||||
'tests/plugins/tesseract_noop.py',
|
||||
'--plugin',
|
||||
'tests/plugins/gs_raster_soft_error.py',
|
||||
)
|
||||
assert p.returncode == ExitCode.child_process_error
|
||||
|
||||
|
||||
def test_render_continue_on_soft_error(resources, outpdf):
|
||||
p = run_ocrmypdf(
|
||||
resources / 'francais.pdf',
|
||||
outpdf,
|
||||
'--continue-on-soft-render-error',
|
||||
'--plugin',
|
||||
'tests/plugins/tesseract_noop.py',
|
||||
'--plugin',
|
||||
'tests/plugins/gs_render_soft_error.py',
|
||||
)
|
||||
assert p.returncode == ExitCode.ok
|
||||
|
||||
|
||||
def test_render_stop_on_soft_error(resources, outpdf):
|
||||
p = run_ocrmypdf(
|
||||
resources / 'francais.pdf',
|
||||
outpdf,
|
||||
'--plugin',
|
||||
'tests/plugins/tesseract_noop.py',
|
||||
'--plugin',
|
||||
'tests/plugins/gs_render_soft_error.py',
|
||||
)
|
||||
assert p.returncode == ExitCode.child_process_error
|
||||
Reference in New Issue
Block a user