From 1598f2f0e5f1a07f3af3c292adf0622925c8110d Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Mon, 1 Jun 2020 03:06:40 -0700 Subject: [PATCH] Abolish spoof_tesseract_noop --- src/ocrmypdf/api.py | 2 +- tests/conftest.py | 9 +-- tests/spoof/tesseract_noop.py | 134 ---------------------------------- tests/test_acroform.py | 4 +- tests/test_ghostscript.py | 58 +++++++++------ tests/test_image_input.py | 14 +++- tests/test_main.py | 131 +++++++++++++++++++++------------ tests/test_metadata.py | 51 ++++++++----- tests/test_optimize.py | 16 ++-- tests/test_preprocessing.py | 16 ++-- tests/test_stdio.py | 56 +++++++------- tests/test_unpaper.py | 25 +++++-- 12 files changed, 234 insertions(+), 282 deletions(-) delete mode 100755 tests/spoof/tesseract_noop.py diff --git a/src/ocrmypdf/api.py b/src/ocrmypdf/api.py index 68ede13a..008d284d 100644 --- a/src/ocrmypdf/api.py +++ b/src/ocrmypdf/api.py @@ -143,7 +143,7 @@ def create_options( # These arguments with special handling for which we bypass # argparse - if arg in {'tesseract_env', 'progress_bar'}: + if arg in {'tesseract_env', 'progress_bar', 'plugins'}: deferred.append((arg, val)) continue diff --git a/tests/conftest.py b/tests/conftest.py index 7be5f1f8..bc188bf9 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -169,11 +169,6 @@ def spoof(tmp_path_factory, **kwargs): return env -@pytest.fixture -def spoof_tesseract_noop(tmp_path_factory): - return spoof(tmp_path_factory, tesseract='tesseract_noop.py') - - @pytest.fixture def spoof_tesseract_cache(tmp_path_factory): if running_in_docker(): @@ -222,7 +217,7 @@ def check_ocrmypdf(input_file, output_file, *args, env=None): if env: first = env['_OCRMYPDF_TEST_PATH'].split(os.pathsep)[0] if 'tesseract_noop' in first: - options.plugins = ['tests/plugins/tesseract_noop.py'] + raise ValueError('noop') else: options.tesseract_env = env options.tesseract_env['_OCRMYPDF_TEST_INFILE'] = os.fspath(input_file) @@ -250,7 +245,7 @@ def run_ocrmypdf_api(input_file, output_file, *args, env=None): try: first = env['_OCRMYPDF_TEST_PATH'].split(os.pathsep)[0] if 'tesseract_noop' in first: - options.plugins = ['tests/plugins/tesseract_noop.py'] + raise ValueError('noop') else: options.tesseract_env = env.copy() options.tesseract_env['_OCRMYPDF_TEST_INFILE'] = os.fspath(input_file) diff --git a/tests/spoof/tesseract_noop.py b/tests/spoof/tesseract_noop.py deleted file mode 100755 index 30f97209..00000000 --- a/tests/spoof/tesseract_noop.py +++ /dev/null @@ -1,134 +0,0 @@ -#!/usr/bin/env python3 -# © 2016 James R. Barlow: github.com/jbarlow83 -# -# Permission is hereby granted, free of charge, to any person obtaining a -# copy of this software and associated documentation files (the -# "Software"), to deal in the Software without restriction, including -# without limitation the rights to use, copy, modify, merge, publish, -# distribute, sublicense, and/or sell copies of the Software, and to -# permit persons to whom the Software is furnished to do so, subject to -# the following conditions: -# -# The above copyright notice and this permission notice shall be included -# in all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -"""Tesseract no-op spoof - -To quickly run tests where getting OCR output is not necessary. - -In 'hocr' mode, create a .hocr file that specifies no text found. - -In 'pdf' mode, convert the image to PDF using another program. - -In orientation check mode, report the orientation is upright. -""" - -import sys -from pathlib import Path - -import img2pdf -import pikepdf -from PIL import Image - -VERSION_STRING = '''tesseract 4.0.0 - leptonica-1.77.0 - libjpeg 9c : libpng 1.6.35 : libtiff 4.0.10 : zlib 1.2.11 : libopenjp2 2.3.0 - Found AVX2 - Found AVX - Found SSE -SPOOFED -''' - -HOCR_TEMPLATE = ''' - - - - - - - - - -
-
-

- - -

-
-
- -''' - - -def main(): - if sys.argv[1] == '--version': - print(VERSION_STRING, file=sys.stderr) - sys.exit(0) - elif sys.argv[1] == '--list-langs': - print('List of available languages (1):\neng', file=sys.stderr) - sys.exit(0) - elif sys.argv[-2] == '--print-parameters': - print("Some parameters", file=sys.stderr) - print("textonly_pdf\t1\tSome help text") - sys.exit(0) - elif sys.argv[-2] == 'hocr': - inputf = sys.argv[-4] - output = sys.argv[-3] - with Image.open(inputf) as im, open( - output + '.hocr', 'w', encoding='utf-8' - ) as f: - w, h = im.size - f.write(HOCR_TEMPLATE.format(str(w), str(h))) - with open(output + '.txt', 'w') as f: - f.write('') - elif sys.argv[-2] == 'pdf': - if 'textonly_pdf=1' in sys.argv: - inputf = sys.argv[-4] - output = sys.argv[-3] - with Image.open(inputf) as im: - dpi = im.info['dpi'] - pagesize = im.size[0] / dpi[0], im.size[1] / dpi[1] - ptsize = pagesize[0] * 72, pagesize[1] * 72 - - pdf_out = pikepdf.new() - pdf_out.add_blank_page(page_size=ptsize) - pdf_out.save(Path(output).with_suffix('.pdf'), static_id=True) - Path(output).with_suffix('.txt').write_text('') - else: - inputf = sys.argv[-4] - output = sys.argv[-3] - pdf_bytes = img2pdf.convert([inputf], dpi=300) - with open(output + '.pdf', 'wb') as f: - f.write(pdf_bytes) - with open(output + '.txt', 'w') as f: - f.write('') - elif sys.argv[-1] == 'stdout': - inputf = sys.argv[-2] - print( - """Orientation: 0 -Orientation in degrees: 0 -Orientation confidence: 100.00 -Script: 1 -Script confidence: 100.00""", - file=sys.stderr, - ) - else: - print("Spoof doesn't understand arguments", file=sys.stderr) - print(sys.argv, file=sys.stderr) - sys.exit(1) - - sys.exit(0) - - -if __name__ == '__main__': - main() diff --git a/tests/test_acroform.py b/tests/test_acroform.py index 44de63da..4ab52406 100644 --- a/tests/test_acroform.py +++ b/tests/test_acroform.py @@ -35,8 +35,8 @@ def test_acroform_and_redo(acroform, caplog, no_outpdf): assert '--redo-ocr is not currently possible' in caplog.text -def test_acroform_message(acroform, caplog, spoof_tesseract_noop, outpdf): +def test_acroform_message(acroform, caplog, outpdf): caplog.set_level(logging.INFO) - check_ocrmypdf(acroform, outpdf, env=spoof_tesseract_noop) + check_ocrmypdf(acroform, outpdf, '--plugin', 'tests/plugins/tesseract_noop.py') assert 'fillable form' in caplog.text assert '--force-ocr' in caplog.text diff --git a/tests/test_ghostscript.py b/tests/test_ghostscript.py index da04ea84..0e6931df 100644 --- a/tests/test_ghostscript.py +++ b/tests/test_ghostscript.py @@ -33,31 +33,23 @@ spoof = pytest.helpers.spoof @pytest.fixture -def spoof_no_tess_gs_render_fail(tmp_path_factory): - return spoof( - tmp_path_factory, tesseract='tesseract_noop.py', gs='gs_render_failure.py' - ) +def spoof_gs_render_fail(tmp_path_factory): + return spoof(tmp_path_factory, gs='gs_render_failure.py') @pytest.fixture -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 spoof_gs_raster_fail(tmp_path_factory): + return spoof(tmp_path_factory, gs='gs_raster_failure.py') @pytest.fixture -def spoof_no_tess_no_pdfa(tmp_path_factory): - return spoof( - tmp_path_factory, tesseract='tesseract_noop.py', gs='gs_pdfa_failure.py' - ) +def spoof_no_pdfa(tmp_path_factory): + return spoof(tmp_path_factory, gs='gs_pdfa_failure.py') @pytest.fixture -def spoof_no_tess_pdfa_warning(tmp_path_factory): - return spoof( - tmp_path_factory, tesseract='tesseract_noop.py', gs='gs_feature_elision.py' - ) +def spoof_pdfa_warning(tmp_path_factory): + return spoof(tmp_path_factory, gs='gs_feature_elision.py') @pytest.fixture @@ -114,30 +106,48 @@ def test_rasterize_rotated(francais, outdir, caplog): assert im.info['dpi'] == (forced_dpi[1], forced_dpi[0]) -def test_gs_render_failure(spoof_no_tess_gs_render_fail, resources, outpdf): +def test_gs_render_failure(spoof_gs_render_fail, resources, outpdf): p, out, err = run_ocrmypdf( - resources / 'blank.pdf', outpdf, env=spoof_no_tess_gs_render_fail + resources / 'blank.pdf', + outpdf, + '--plugin', + 'tests/plugins/tesseract_noop.py', + env=spoof_gs_render_fail, ) assert 'Casper is not a friendly ghost' in err assert p.returncode == ExitCode.child_process_error -def test_gs_raster_failure(spoof_no_tess_gs_raster_fail, resources, outpdf): +def test_gs_raster_failure(spoof_gs_raster_fail, resources, outpdf): p, out, err = run_ocrmypdf( - resources / 'francais.pdf', outpdf, env=spoof_no_tess_gs_raster_fail + resources / 'francais.pdf', + outpdf, + '--plugin', + 'tests/plugins/tesseract_noop.py', + env=spoof_gs_raster_fail, ) assert 'Ghost story archive not found' in err assert p.returncode == ExitCode.child_process_error -def test_ghostscript_pdfa_failure(spoof_no_tess_no_pdfa, resources, outpdf): +def test_ghostscript_pdfa_failure(spoof_no_pdfa, resources, outpdf): p, out, err = run_ocrmypdf( - resources / 'francais.pdf', outpdf, env=spoof_no_tess_no_pdfa + resources / 'francais.pdf', + outpdf, + '--plugin', + 'tests/plugins/tesseract_noop.py', + env=spoof_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 / 'francais.pdf', outpdf, env=spoof_no_tess_pdfa_warning) +def test_ghostscript_feature_elision(spoof_pdfa_warning, resources, outpdf): + check_ocrmypdf( + resources / 'francais.pdf', + outpdf, + '--plugin', + 'tests/plugins/tesseract_noop.py', + env=spoof_pdfa_warning, + ) diff --git a/tests/test_image_input.py b/tests/test_image_input.py index ceb94cbe..c3636952 100644 --- a/tests/test_image_input.py +++ b/tests/test_image_input.py @@ -33,9 +33,14 @@ def baiona(resources): return Image.open(resources / 'baiona_gray.png') -def test_image_to_pdf(spoof_tesseract_noop, resources, outpdf): +def test_image_to_pdf(resources, outpdf): check_ocrmypdf( - resources / 'crom.png', outpdf, '--image-dpi', '200', env=spoof_tesseract_noop + resources / 'crom.png', + outpdf, + '--image-dpi', + '200', + '--plugin', + 'tests/plugins/tesseract_noop.py', ) @@ -77,7 +82,7 @@ def test_img2pdf_fails(resources, no_outpdf): assert rc == ocrmypdf.ExitCode.input_file -def test_jpeg_in_jpeg_out(resources, outpdf, spoof_tesseract_noop): +def test_jpeg_in_jpeg_out(resources, outpdf): check_ocrmypdf( resources / 'congress.jpg', outpdf, @@ -86,7 +91,8 @@ def test_jpeg_in_jpeg_out(resources, outpdf, spoof_tesseract_noop): '--output-type', 'pdf', # specifically check pdf because Ghostscript may convert to JPEG '--remove-background', - env=spoof_tesseract_noop, + '--plugin', + 'tests/plugins/tesseract_noop.py', ) with pikepdf.open(outpdf) as pdf: assert next(pdf.pages[0].images.values()).Filter == pikepdf.Name.DCTDecode diff --git a/tests/test_main.py b/tests/test_main.py index 8c417d76..e161730e 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -111,7 +111,7 @@ def test_redo_ocr(resources, outpdf): ), "Expected text to be different after re-OCR" -def test_argsfile(spoof_tesseract_noop, resources, outdir): +def test_argsfile(resources, outdir): path_argsfile = outdir / 'test_argsfile.txt' with open(str(path_argsfile), 'w') as argsfile: print( @@ -119,15 +119,14 @@ def test_argsfile(spoof_tesseract_noop, resources, outdir): 'ArgsFile Test', '--author', 'Test Cases', + '--plugin', + 'tests/plugins/tesseract_noop.py', sep='\n', end='\n', file=argsfile, ) check_ocrmypdf( - resources / 'graph.pdf', - path_argsfile, - '@' + str(outdir / 'test_argsfile.txt'), - env=spoof_tesseract_noop, + resources / 'graph.pdf', path_argsfile, '@' + str(outdir / 'test_argsfile.txt') ) @@ -239,23 +238,27 @@ def test_klingon(resources, outpdf): assert p.returncode == ExitCode.missing_dependency -def test_missing_docinfo(spoof_tesseract_noop, resources, outpdf): +def test_missing_docinfo(resources, outpdf): result = run_ocrmypdf_api( resources / 'missing_docinfo.pdf', outpdf, '-l', 'eng', '--skip-text', - env=spoof_tesseract_noop, + '--plugin', + 'tests/plugins/tesseract_noop.py', ) assert result == ExitCode.ok -def test_uppercase_extension(spoof_tesseract_noop, resources, outdir): +def test_uppercase_extension(resources, outdir): shutil.copy(str(resources / "skew.pdf"), str(outdir / "UPPERCASE.PDF")) check_ocrmypdf( - outdir / "UPPERCASE.PDF", outdir / "UPPERCASE_OUT.PDF", env=spoof_tesseract_noop + outdir / "UPPERCASE.PDF", + outdir / "UPPERCASE_OUT.PDF", + '--plugin', + 'tests/plugins/tesseract_noop.py', ) @@ -349,9 +352,12 @@ def test_tesseract_image_too_big( ) -def test_algo4(resources, spoof_tesseract_noop, outpdf): +def test_algo4(resources, outpdf): p, _, _ = run_ocrmypdf( - resources / 'encrypted_algo4.pdf', outpdf, env=spoof_tesseract_noop + resources / 'encrypted_algo4.pdf', + outpdf, + '--plugin', + 'tests/plugins/tesseract_noop.py', ) assert p.returncode == ExitCode.encrypted_pdf @@ -370,17 +376,19 @@ def test_jbig2_passthrough(spoof_tesseract_cache, resources, outpdf): assert out_pageinfo[0].images[0].enc == Encoding.jbig2 -def test_masks(spoof_tesseract_noop, resources, outpdf): +def test_masks(resources, outpdf): assert ( ocrmypdf.ocr( - resources / 'masks.pdf', outpdf, tesseract_env=spoof_tesseract_noop + resources / 'masks.pdf', outpdf, plugins=['tests/plugins/tesseract_noop.py'] ) == ExitCode.ok ) -def test_linearized_pdf_and_indirect_object(spoof_tesseract_noop, resources, outpdf): - check_ocrmypdf(resources / 'epson.pdf', outpdf, env=spoof_tesseract_noop) +def test_linearized_pdf_and_indirect_object(resources, outpdf): + check_ocrmypdf( + resources / 'epson.pdf', outpdf, '--plugin', 'tests/plugins/tesseract_noop.py' + ) def test_very_high_dpi(spoof_tesseract_cache, resources, outpdf): @@ -393,20 +401,27 @@ def test_very_high_dpi(spoof_tesseract_cache, resources, outpdf): assert isclose(image.dpi.x, 2400) -def test_overlay(spoof_tesseract_noop, resources, outpdf): +def test_overlay(resources, outpdf): check_ocrmypdf( - resources / 'overlay.pdf', outpdf, '--skip-text', env=spoof_tesseract_noop + resources / 'overlay.pdf', + outpdf, + '--skip-text', + '--plugin', + 'tests/plugins/tesseract_noop.py', ) -def test_destination_not_writable(spoof_tesseract_noop, resources, outdir): +def test_destination_not_writable(resources, outdir): if os.name != 'nt' and (os.getuid() == 0 or os.geteuid() == 0): pytest.xfail(reason="root can write to anything") protected_file = outdir / 'protected.pdf' protected_file.touch() protected_file.chmod(0o400) # Read-only p, _out, _err = run_ocrmypdf( - resources / 'jbig2.pdf', protected_file, env=spoof_tesseract_noop + resources / 'jbig2.pdf', + protected_file, + '--plugin', + 'tests/plugins/tesseract_noop.py', ) assert p.returncode == ExitCode.file_access_error, "Expected error" @@ -479,9 +494,13 @@ def test_user_words_ocr(resources, outdir): ) -def test_form_xobject(spoof_tesseract_noop, resources, outpdf): +def test_form_xobject(resources, outpdf): check_ocrmypdf( - resources / 'formxobject.pdf', outpdf, '--force-ocr', env=spoof_tesseract_noop + resources / 'formxobject.pdf', + outpdf, + '--force-ocr', + '--plugin', + 'tests/plugins/tesseract_noop.py', ) @@ -513,14 +532,15 @@ def test_pagesize_consistency(renderer, resources, outpdf): assert isclose(before_dims[1], after_dims[1], rel_tol=1e-4) -def test_skip_big_with_no_images(spoof_tesseract_noop, resources, outpdf): +def test_skip_big_with_no_images(resources, outpdf): check_ocrmypdf( resources / 'blank.pdf', outpdf, '--skip-big', '5', '--force-ocr', - env=spoof_tesseract_noop, + '--plugin', + 'tests/plugins/tesseract_noop.py', ) @@ -528,18 +548,20 @@ def test_skip_big_with_no_images(spoof_tesseract_noop, resources, outpdf): '8.0.0' <= pikepdf.__libqpdf_version__ <= '8.0.1', reason="libqpdf regression on pages with no contents", ) -def test_no_contents(spoof_tesseract_noop, resources, outpdf): +def test_no_contents(resources, outpdf): check_ocrmypdf( - resources / 'no_contents.pdf', outpdf, '--force-ocr', env=spoof_tesseract_noop + resources / 'no_contents.pdf', + outpdf, + '--force-ocr', + '--plugin', + 'tests/plugins/tesseract_noop.py', ) @pytest.mark.parametrize( 'image', ['baiona.png', 'baiona_gray.png', 'baiona_alpha.png', 'congress.jpg'] ) -def test_compression_preserved( - spoof_tesseract_noop, ocrmypdf_exec, resources, image, outpdf -): +def test_compression_preserved(ocrmypdf_exec, resources, image, outpdf): input_file = str(resources / image) output_file = str(outpdf) @@ -553,6 +575,8 @@ def test_compression_preserved( '150', '--output-type', 'pdf', + '--plugin', + 'tests/plugins/tesseract_noop.py', '-', output_file, ] @@ -562,7 +586,6 @@ def test_compression_preserved( stderr=PIPE, stdin=input_stream, universal_newlines=True, - env=spoof_tesseract_noop, check=False, ) @@ -596,9 +619,7 @@ def test_compression_preserved( ('congress.jpg', 'lossless'), ], ) -def test_compression_changed( - spoof_tesseract_noop, ocrmypdf_exec, resources, image, compression, outpdf -): +def test_compression_changed(ocrmypdf_exec, resources, image, compression, outpdf): input_file = str(resources / image) output_file = str(outpdf) @@ -615,6 +636,8 @@ def test_compression_changed( '0', '--pdfa-image-compression', compression, + '--plugin', + 'tests/plugins/tesseract_noop.py', '-', output_file, ] @@ -624,7 +647,6 @@ def test_compression_changed( stderr=PIPE, stdin=input_stream, universal_newlines=True, - env=spoof_tesseract_noop, check=False, ) assert p.returncode == ExitCode.ok, p.stderr @@ -717,35 +739,52 @@ def test_decompression_bomb(resources, outpdf): assert p.returncode == 0 -def test_text_curves(spoof_tesseract_noop, resources, outpdf): +def test_text_curves(resources, outpdf): with patch('ocrmypdf._pipeline.VECTOR_PAGE_DPI', 100): - check_ocrmypdf(resources / 'vector.pdf', outpdf, env=spoof_tesseract_noop) + check_ocrmypdf( + resources / 'vector.pdf', + outpdf, + '--plugin', + 'tests/plugins/tesseract_noop.py', + ) info = PdfInfo(outpdf) assert len(info.pages[0].images) == 0, "added images to the vector PDF" check_ocrmypdf( - resources / 'vector.pdf', outpdf, '--force-ocr', env=spoof_tesseract_noop + resources / 'vector.pdf', + outpdf, + '--force-ocr', + '--plugin', + 'tests/plugins/tesseract_noop.py', ) info = PdfInfo(outpdf) assert len(info.pages[0].images) != 0, "force did not rasterize" -def test_output_is_dir(spoof_tesseract_noop, resources, outdir): +def test_output_is_dir(resources, outdir): p, _out, err = run_ocrmypdf( - resources / 'trivial.pdf', outdir, '--force-ocr', env=spoof_tesseract_noop + resources / 'trivial.pdf', + outdir, + '--force-ocr', + '--plugin', + 'tests/plugins/tesseract_noop.py', ) assert p.returncode == ExitCode.file_access_error assert 'is not a writable file' in err @pytest.mark.skipif(os.name == 'nt', reason="symlink needs admin permissions") -def test_output_is_symlink(spoof_tesseract_noop, resources, outdir): +def test_output_is_symlink(resources, outdir): sym = Path(outdir / 'this_is_a_symlink') sym.symlink_to(outdir / 'out.pdf') p, _out, err = run_ocrmypdf( - resources / 'trivial.pdf', sym, '--force-ocr', env=spoof_tesseract_noop + resources / 'trivial.pdf', + sym, + '--force-ocr', + '--plugin', + 'tests/plugins/tesseract_noop.py', ) assert p.returncode == ExitCode.ok, err assert (outdir / 'out.pdf').stat().st_size > 0, 'target file not created' @@ -781,9 +820,7 @@ def test_version_check(): [0.0, 1, 'pdf', True], ], ) -def test_fast_web_view( - spoof_tesseract_noop, resources, outpdf, threshold, optimize, output_type, expected -): +def test_fast_web_view(resources, outpdf, threshold, optimize, output_type, expected): check_ocrmypdf( resources / 'trivial.pdf', outpdf, @@ -793,18 +830,20 @@ def test_fast_web_view( optimize, '--output-type', output_type, - env=spoof_tesseract_noop, + '--plugin', + 'tests/plugins/tesseract_noop.py', ) with pikepdf.open(outpdf) as pdf: assert pdf.is_linearized == expected -def test_image_dpi_not_image(caplog, spoof_tesseract_noop, resources, outpdf): +def test_image_dpi_not_image(caplog, resources, outpdf): check_ocrmypdf( resources / 'trivial.pdf', outpdf, '--image-dpi', '100', - env=spoof_tesseract_noop, + '--plugin', + 'tests/plugins/tesseract_noop.py', ) assert '--image-dpi is being ignored' in caplog.text diff --git a/tests/test_metadata.py b/tests/test_metadata.py index 795dcc43..59250eac 100644 --- a/tests/test_metadata.py +++ b/tests/test_metadata.py @@ -51,7 +51,7 @@ spoof = pytest.helpers.spoof @pytest.mark.parametrize("output_type", ['pdfa', 'pdf']) -def test_preserve_metadata(spoof_tesseract_noop, output_type, resources, outpdf): +def test_preserve_metadata(output_type, resources, outpdf): pdf_before = pikepdf.open(resources / 'graph.pdf') output = check_ocrmypdf( @@ -59,7 +59,8 @@ def test_preserve_metadata(spoof_tesseract_noop, output_type, resources, outpdf) outpdf, '--output-type', output_type, - env=spoof_tesseract_noop, + '--plugin', + 'tests/plugins/tesseract_noop.py', ) pdf_after = pikepdf.open(output) @@ -72,7 +73,7 @@ def test_preserve_metadata(spoof_tesseract_noop, output_type, resources, outpdf) @pytest.mark.parametrize("output_type", ['pdfa', 'pdf']) -def test_override_metadata(spoof_tesseract_noop, output_type, resources, outpdf): +def test_override_metadata(output_type, resources, outpdf): input_file = resources / 'c02-22.pdf' german = 'Du siehst den Wald vor lauter Bäumen nicht.' chinese = '孔子' @@ -86,7 +87,8 @@ def test_override_metadata(spoof_tesseract_noop, output_type, resources, outpdf) chinese, '--output-type', output_type, - env=spoof_tesseract_noop, + '--plugin', + 'tests/plugins/tesseract_noop.py', ) assert p.returncode == ExitCode.ok, err @@ -106,7 +108,7 @@ def test_override_metadata(spoof_tesseract_noop, output_type, resources, outpdf) assert pdfa_info['output'] == output_type -def test_high_unicode(spoof_tesseract_noop, resources, no_outpdf): +def test_high_unicode(resources, no_outpdf): # Ghostscript doesn't support high Unicode, so neither do we, to be # safe @@ -120,7 +122,8 @@ def test_high_unicode(spoof_tesseract_noop, resources, no_outpdf): high_unicode, '--output-type', 'pdfa', - env=spoof_tesseract_noop, + '--plugin', + 'tests/plugins/tesseract_noop.py', ) assert p.returncode == ExitCode.bad_args, err @@ -129,9 +132,7 @@ def test_high_unicode(spoof_tesseract_noop, resources, no_outpdf): @pytest.mark.skipif(not fitz, reason="test uses fitz") @pytest.mark.parametrize('ocr_option', ['--skip-text', '--force-ocr']) @pytest.mark.parametrize('output_type', ['pdf', 'pdfa']) -def test_bookmarks_preserved( - spoof_tesseract_noop, output_type, ocr_option, resources, outpdf -): +def test_bookmarks_preserved(output_type, ocr_option, resources, outpdf): input_file = resources / 'toc.pdf' before_toc = fitz.Document(str(input_file)).getToC() @@ -141,7 +142,8 @@ def test_bookmarks_preserved( ocr_option, '--output-type', output_type, - env=spoof_tesseract_noop, + '--plugin', + 'tests/plugins/tesseract_noop.py', ) after_toc = fitz.Document(str(outpdf)).getToC() @@ -156,13 +158,16 @@ def seconds_between_dates(date1, date2): @pytest.mark.parametrize('infile', ['trivial.pdf', 'jbig2.pdf']) @pytest.mark.parametrize('output_type', ['pdf', 'pdfa']) -def test_creation_date_preserved( - spoof_tesseract_noop, output_type, resources, infile, outpdf -): +def test_creation_date_preserved(output_type, resources, infile, outpdf): input_file = resources / infile check_ocrmypdf( - input_file, outpdf, '--output-type', output_type, env=spoof_tesseract_noop + input_file, + outpdf, + '--output-type', + output_type, + '--plugin', + 'tests/plugins/tesseract_noop.py', ) pdf_before = pikepdf.open(input_file) @@ -185,7 +190,7 @@ def test_creation_date_preserved( @pytest.mark.parametrize('output_type', ['pdf', 'pdfa']) -def test_xml_metadata_preserved(spoof_tesseract_noop, output_type, resources, outpdf): +def test_xml_metadata_preserved(output_type, resources, outpdf): input_file = resources / 'graph.pdf' try: @@ -196,7 +201,12 @@ def test_xml_metadata_preserved(spoof_tesseract_noop, output_type, resources, ou before = file_to_dict(str(input_file)) check_ocrmypdf( - input_file, outpdf, '--output-type', output_type, env=spoof_tesseract_noop + input_file, + outpdf, + '--output-type', + output_type, + '--plugin', + 'tests/plugins/tesseract_noop.py', ) after = file_to_dict(str(outpdf)) @@ -274,9 +284,14 @@ def test_srgb_in_unicode_path(tmp_path): generate_pdfa_ps(dstdir / 'out.ps') -def test_kodak_toc(resources, outpdf, spoof_tesseract_noop): +def test_kodak_toc(resources, outpdf): _output = check_ocrmypdf( - resources / 'kcs.pdf', outpdf, '--output-type', 'pdf', env=spoof_tesseract_noop + resources / 'kcs.pdf', + outpdf, + '--output-type', + 'pdf', + '--plugin', + 'tests/plugins/tesseract_noop.py', ) p = pikepdf.open(outpdf) diff --git a/tests/test_optimize.py b/tests/test_optimize.py index f9f6747c..e8cf0717 100644 --- a/tests/test_optimize.py +++ b/tests/test_optimize.py @@ -54,7 +54,7 @@ def test_mono_not_inverted(resources, outdir): @pytest.mark.skipif(not pngquant.available(), reason='need pngquant') -def test_jpg_png_params(resources, outpdf, spoof_tesseract_noop): +def test_jpg_png_params(resources, outpdf): check_ocrmypdf( resources / 'crom.png', outpdf, @@ -66,13 +66,14 @@ def test_jpg_png_params(resources, outpdf, spoof_tesseract_noop): '50', '--png-quality', '20', - env=spoof_tesseract_noop, + '--plugin', + 'tests/plugins/tesseract_noop.py', ) @pytest.mark.skipif(not jbig2enc.available(), reason='need jbig2enc') @pytest.mark.parametrize('lossy', [False, True]) -def test_jbig2_lossy(lossy, resources, outpdf, spoof_tesseract_noop): +def test_jbig2_lossy(lossy, resources, outpdf): args = [ resources / 'ccitt.pdf', outpdf, @@ -84,11 +85,13 @@ def test_jbig2_lossy(lossy, resources, outpdf, spoof_tesseract_noop): '50', '--png-quality', '20', + '--plugin', + 'tests/plugins/tesseract_noop.py', ] if lossy: args.append('--jbig2-lossy') - check_ocrmypdf(*args, env=spoof_tesseract_noop) + check_ocrmypdf(*args) pdf = pikepdf.open(outpdf) pim = pikepdf.PdfImage(next(iter(pdf.pages[0].images.values()))) @@ -104,7 +107,7 @@ def test_jbig2_lossy(lossy, resources, outpdf, spoof_tesseract_noop): not jbig2enc.available() or not pngquant.available(), reason='need jbig2enc and pngquant', ) -def test_flate_to_jbig2(resources, outdir, spoof_tesseract_noop): +def test_flate_to_jbig2(resources, outdir): # This test requires an image that pngquant is capable of converting to # to 1bpp - so use an existing 1bpp image, convert up, confirm it can # convert down @@ -122,7 +125,8 @@ def test_flate_to_jbig2(resources, outdir, spoof_tesseract_noop): '50', '--optimize', '3', - env=spoof_tesseract_noop, + '--plugin', + 'tests/plugins/tesseract_noop.py', ) pdf = pikepdf.open(outdir / 'out.pdf') diff --git a/tests/test_preprocessing.py b/tests/test_preprocessing.py index b90517eb..08e34bec 100644 --- a/tests/test_preprocessing.py +++ b/tests/test_preprocessing.py @@ -15,7 +15,6 @@ # You should have received a copy of the GNU General Public License # along with OCRmyPDF. If not, see . -import logging from math import isclose import pytest @@ -38,16 +37,18 @@ spoof = pytest.helpers.spoof RENDERERS = ['hocr', 'sandwich'] -def test_deskew(spoof_tesseract_noop, resources, outdir): +def test_deskew(resources, outdir): # Run with deskew deskewed_pdf = check_ocrmypdf( - resources / 'skew.pdf', outdir / 'skew.pdf', '-d', env=spoof_tesseract_noop + resources / 'skew.pdf', + outdir / 'skew.pdf', + '-d', + '--plugin', + 'tests/plugins/tesseract_noop.py', ) # Now render as an image again and use Leptonica to find the skew angle # to confirm that it was deskewed - log = logging.getLogger() - deskewed_png = outdir / 'deskewed.png' ghostscript.rasterize_pdf( @@ -65,7 +66,7 @@ def test_deskew(spoof_tesseract_noop, resources, outdir): assert -0.5 < skew_angle < 0.5, "Deskewing failed" -def test_remove_background(spoof_tesseract_noop, resources, outdir): +def test_remove_background(resources, outdir): # Ensure the input image does not contain pure white/black with Image.open(resources / 'congress.jpg') as im: assert im.getextrema() != ((0, 255), (0, 255), (0, 255)) @@ -76,7 +77,8 @@ def test_remove_background(spoof_tesseract_noop, resources, outdir): '--remove-background', '--image-dpi', '150', - env=spoof_tesseract_noop, + '--plugin', + 'tests/plugins/tesseract_noop.py', ) output_png = outdir / 'remove_bg.png' diff --git a/tests/test_stdio.py b/tests/test_stdio.py index e57c11f0..d78b1644 100644 --- a/tests/test_stdio.py +++ b/tests/test_stdio.py @@ -38,25 +38,24 @@ def spoof_tess_bad_utf8(tmp_path_factory): return spoof(tmp_path_factory, tesseract='tesseract_badutf8.py') -def test_stdin(spoof_tesseract_noop, ocrmypdf_exec, resources, outpdf): +def test_stdin(ocrmypdf_exec, resources, outpdf): input_file = str(resources / 'francais.pdf') output_file = str(outpdf) # Runs: ocrmypdf - output.pdf < testfile.pdf with open(input_file, 'rb') as input_stream: - p_args = ocrmypdf_exec + ['-', output_file] - p = run( - p_args, - stdout=PIPE, - stderr=PIPE, - stdin=input_stream, - env=spoof_tesseract_noop, - ) + p_args = ocrmypdf_exec + [ + '-', + output_file, + '--plugin', + 'tests/plugins/tesseract_noop.py', + ] + p = run(p_args, stdout=PIPE, stderr=PIPE, stdin=input_stream) assert p.returncode == ExitCode.ok -def test_stdout(spoof_tesseract_noop, ocrmypdf_exec, resources, outpdf): - if 'COV_CORE_DATAFILE' in spoof_tesseract_noop: +def test_stdout(ocrmypdf_exec, resources, outpdf): + if 'COV_CORE_DATAFILE' in os.environ: pytest.skip(msg="Coverage uses stdout") input_file = str(resources / 'francais.pdf') @@ -64,14 +63,13 @@ def test_stdout(spoof_tesseract_noop, ocrmypdf_exec, resources, outpdf): # Runs: ocrmypdf francais.pdf - > test_stdout.pdf with open(output_file, 'wb') as output_stream: - p_args = ocrmypdf_exec + [input_file, '-'] - p = run( - p_args, - stdout=output_stream, - stderr=PIPE, - stdin=DEVNULL, - env=spoof_tesseract_noop, - ) + p_args = ocrmypdf_exec + [ + input_file, + '-', + '--plugin', + 'tests/plugins/tesseract_noop.py', + ] + p = run(p_args, stdout=output_stream, stderr=PIPE, stdin=DEVNULL) assert p.returncode == ExitCode.ok assert check_pdf(output_file) @@ -81,7 +79,7 @@ def test_stdout(spoof_tesseract_noop, ocrmypdf_exec, resources, outpdf): sys.version_info[0:3] >= (3, 6, 4), reason="issue fixed in Python 3.6.4" ) @pytest.mark.skipif(os.name == 'nt', reason="POSIX problem") -def test_closed_streams(spoof_tesseract_noop, ocrmypdf_exec, resources, outpdf): +def test_closed_streams(ocrmypdf_exec, resources, outpdf): input_file = str(resources / 'francais.pdf') output_file = str(outpdf) @@ -89,14 +87,18 @@ def test_closed_streams(spoof_tesseract_noop, ocrmypdf_exec, resources, outpdf): os.close(0) os.close(1) - p_args = ocrmypdf_exec + [input_file, output_file] + p_args = ocrmypdf_exec + [ + input_file, + output_file, + '--plugin', + 'tests/plugins/tesseract_noop.py', + ] p = Popen( # pylint: disable=subprocess-popen-preexec-fn p_args, close_fds=True, stdout=None, stderr=PIPE, stdin=None, - env=spoof_tesseract_noop, preexec_fn=evil_closer, ) out, err = p.communicate() @@ -123,12 +125,16 @@ def test_bad_locale(): os.name == 'nt' and sys.version_info < (3, 8), reason="Windows does not like this; not sure how to fix", ) -def test_dev_null(spoof_tesseract_noop, resources): - if 'COV_CORE_DATAFILE' in spoof_tesseract_noop: +def test_dev_null(resources): + if 'COV_CORE_DATAFILE' in os.environ: pytest.skip(msg="Coverage uses stdout") p, out, err = run_ocrmypdf( - resources / 'trivial.pdf', os.devnull, '--force-ocr', env=spoof_tesseract_noop + resources / 'trivial.pdf', + os.devnull, + '--force-ocr', + '--plugin', + 'tests/plugins/tesseract_noop.py', ) assert p.returncode == 0, "could not send output to /dev/null" assert len(out) == 0, "wrote to stdout" diff --git a/tests/test_unpaper.py b/tests/test_unpaper.py index bd04da2c..5ef9fac3 100644 --- a/tests/test_unpaper.py +++ b/tests/test_unpaper.py @@ -60,45 +60,54 @@ def test_old_unpaper(spoof_unpaper_oldversion, resources, no_outpdf): @pytest.mark.skipif(not have_unpaper(), reason="requires unpaper") -def test_clean(spoof_tesseract_noop, resources, outpdf): - check_ocrmypdf(resources / "skew.pdf", outpdf, "-c", env=spoof_tesseract_noop) +def test_clean(resources, outpdf): + check_ocrmypdf( + resources / "skew.pdf", + outpdf, + "-c", + '--plugin', + 'tests/plugins/tesseract_noop.py', + ) @pytest.mark.skipif(not have_unpaper(), reason="requires unpaper") -def test_unpaper_args_valid(spoof_tesseract_noop, resources, outpdf): +def test_unpaper_args_valid(resources, outpdf): check_ocrmypdf( resources / "skew.pdf", outpdf, "-c", "--unpaper-args", "--layout double", # Spaces required here - env=spoof_tesseract_noop, + '--plugin', + 'tests/plugins/tesseract_noop.py', ) @pytest.mark.skipif(not have_unpaper(), reason="requires unpaper") -def test_unpaper_args_invalid_filename(spoof_tesseract_noop, resources, outpdf): +def test_unpaper_args_invalid_filename(resources, outpdf): p, out, err = run_ocrmypdf( resources / "skew.pdf", outpdf, "-c", "--unpaper-args", "/etc/passwd", - env=spoof_tesseract_noop, + '--plugin', + 'tests/plugins/tesseract_noop.py', ) assert "No filenames allowed" in err assert p.returncode == ExitCode.bad_args @pytest.mark.skipif(not have_unpaper(), reason="requires unpaper") -def test_unpaper_args_invalid(spoof_tesseract_noop, resources, outpdf): +def test_unpaper_args_invalid(resources, outpdf): p, out, err = run_ocrmypdf( resources / "skew.pdf", outpdf, "-c", "--unpaper-args", "unpaper is not going to like these arguments", - env=spoof_tesseract_noop, + '--plugin', + 'tests/plugins/tesseract_noop.py', ) # Can't tell difference between unpaper choking on bad arguments or some # other unpaper failure