diff --git a/tests/test_ghostscript.py b/tests/test_ghostscript.py index 8756e8a8..c659fd68 100644 --- a/tests/test_ghostscript.py +++ b/tests/test_ghostscript.py @@ -18,12 +18,47 @@ import logging from decimal import Decimal + import pikepdf import pytest from PIL import Image +from ocrmypdf.exceptions import ExitCode from ocrmypdf.exec.ghostscript import rasterize_pdf +check_ocrmypdf = pytest.helpers.check_ocrmypdf +run_ocrmypdf = pytest.helpers.run_ocrmypdf +run_ocrmypdf_api = pytest.helpers.run_ocrmypdf_api +spoof = pytest.helpers.spoof + + +@pytest.fixture(scope='session') +def spoof_no_tess_gs_render_fail(tmp_path_factory): + return spoof( + tmp_path_factory, tesseract='tesseract_noop.py', gs='gs_render_failure.py' + ) + + +@pytest.fixture(scope='session') +def spoof_no_tess_gs_raster_fail(tmp_path_factory): + return spoof( + tmp_path_factory, tesseract='tesseract_noop.py', gs='gs_raster_failure.py' + ) + + +@pytest.fixture(scope='session') +def spoof_no_tess_no_pdfa(tmp_path_factory): + return spoof( + tmp_path_factory, tesseract='tesseract_noop.py', gs='gs_pdfa_failure.py' + ) + + +@pytest.fixture(scope='session') +def spoof_no_tess_pdfa_warning(tmp_path_factory): + return spoof( + tmp_path_factory, tesseract='tesseract_noop.py', gs='gs_feature_elision.py' + ) + @pytest.fixture def linn(resources): @@ -79,3 +114,32 @@ def test_rasterize_rotated(linn, outdir, caplog): with Image.open(outdir / 'out.png') as im: assert im.size == (target_size[1], target_size[0]) assert im.info['dpi'] == (target_dpi[1], target_dpi[0]) + + +def test_gs_render_failure(spoof_no_tess_gs_render_fail, resources, outpdf): + p, out, err = run_ocrmypdf( + resources / 'blank.pdf', outpdf, env=spoof_no_tess_gs_render_fail + ) + print(err) + assert p.returncode == ExitCode.child_process_error + + +def test_gs_raster_failure(spoof_no_tess_gs_raster_fail, resources, outpdf): + p, out, err = run_ocrmypdf( + resources / 'ccitt.pdf', outpdf, env=spoof_no_tess_gs_raster_fail + ) + print(err) + assert p.returncode == ExitCode.child_process_error + + +def test_ghostscript_pdfa_failure(spoof_no_tess_no_pdfa, resources, outpdf): + p, out, err = run_ocrmypdf( + resources / 'ccitt.pdf', outpdf, env=spoof_no_tess_no_pdfa + ) + assert ( + p.returncode == ExitCode.pdfa_conversion_failed + ), "Unexpected return when PDF/A fails" + + +def test_ghostscript_feature_elision(spoof_no_tess_pdfa_warning, resources, outpdf): + check_ocrmypdf(resources / 'ccitt.pdf', outpdf, env=spoof_no_tess_pdfa_warning) diff --git a/tests/test_main.py b/tests/test_main.py index 17946965..73f91e5e 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -56,34 +56,6 @@ def spoof_tesseract_big_image_error(tmp_path_factory): return spoof(tmp_path_factory, tesseract='tesseract_big_image_error.py') -@pytest.fixture(scope='session') -def spoof_no_tess_no_pdfa(tmp_path_factory): - return spoof( - tmp_path_factory, tesseract='tesseract_noop.py', gs='gs_pdfa_failure.py' - ) - - -@pytest.fixture(scope='session') -def spoof_no_tess_pdfa_warning(tmp_path_factory): - return spoof( - tmp_path_factory, tesseract='tesseract_noop.py', gs='gs_feature_elision.py' - ) - - -@pytest.fixture(scope='session') -def spoof_no_tess_gs_render_fail(tmp_path_factory): - return spoof( - tmp_path_factory, tesseract='tesseract_noop.py', gs='gs_render_failure.py' - ) - - -@pytest.fixture(scope='session') -def spoof_no_tess_gs_raster_fail(tmp_path_factory): - return spoof( - tmp_path_factory, tesseract='tesseract_noop.py', gs='gs_raster_failure.py' - ) - - def test_quick(spoof_tesseract_cache, resources, outpdf): check_ocrmypdf(resources / 'ccitt.pdf', outpdf, env=spoof_tesseract_cache) @@ -557,19 +529,6 @@ def test_linearized_pdf_and_indirect_object(spoof_tesseract_noop, resources, out check_ocrmypdf(resources / 'epson.pdf', outpdf, env=spoof_tesseract_noop) -def test_ghostscript_pdfa_failure(spoof_no_tess_no_pdfa, resources, outpdf): - p, out, err = run_ocrmypdf( - resources / 'ccitt.pdf', outpdf, env=spoof_no_tess_no_pdfa - ) - assert ( - p.returncode == ExitCode.pdfa_conversion_failed - ), "Unexpected return when PDF/A fails" - - -def test_ghostscript_feature_elision(spoof_no_tess_pdfa_warning, resources, outpdf): - check_ocrmypdf(resources / 'ccitt.pdf', outpdf, env=spoof_no_tess_pdfa_warning) - - def test_very_high_dpi(spoof_tesseract_cache, resources, outpdf): "Checks for a Decimal quantize error with high DPI, etc" check_ocrmypdf(resources / '2400dpi.pdf', outpdf, env=spoof_tesseract_cache) @@ -718,22 +677,6 @@ def test_skip_big_with_no_images(spoof_tesseract_noop, resources, outpdf): ) -def test_gs_render_failure(spoof_no_tess_gs_render_fail, resources, outpdf): - p, out, err = run_ocrmypdf( - resources / 'blank.pdf', outpdf, env=spoof_no_tess_gs_render_fail - ) - print(err) - assert p.returncode == ExitCode.child_process_error - - -def test_gs_raster_failure(spoof_no_tess_gs_raster_fail, resources, outpdf): - p, out, err = run_ocrmypdf( - resources / 'ccitt.pdf', outpdf, env=spoof_no_tess_gs_raster_fail - ) - print(err) - assert p.returncode == ExitCode.child_process_error - - @pytest.mark.skipif( '8.0.0' <= qpdf.version() <= '8.0.1', reason="qpdf regression on pages with no contents",