From e73740ae9d6ea9a45a6a60afa5dea1daae51d064 Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Mon, 3 Jun 2019 01:33:24 -0700 Subject: [PATCH] test: remove test code that support tess3 or tess4 testing --- tests/test_tess4.py | 98 +++++---------------------------------------- 1 file changed, 10 insertions(+), 88 deletions(-) diff --git a/tests/test_tess4.py b/tests/test_tess4.py index 92ee8c94..bb9cc49a 100644 --- a/tests/test_tess4.py +++ b/tests/test_tess4.py @@ -27,85 +27,17 @@ from ocrmypdf.exceptions import MissingDependencyError from ocrmypdf.exec import tesseract # pylint: disable=no-member,w0621 -spoof = pytest.helpers.spoof - - -def _ensure_tess4(): - if tesseract.v4(): - # "tesseract" on $PATH is already v4 - return os.environ.copy() - - if os.environ.get('OCRMYPDF_TESS4'): - # OCRMYPDF_TESS4 is a hint environment variable that tells us to look - # somewhere special for tess4 if and only if we need it. This allows - # setting OCRMYPDF_TESS4 to test tess4 and PATH to point to tess3 - # on a system with both installed. - env = os.environ.copy() - tess4 = Path(os.environ['OCRMYPDF_TESS4']) - assert tess4.is_file() - env['PATH'] = tess4.parent + ':' + env['PATH'] - env['OCRMYPDF_TESS4'] = os.environ['OCRMYPDF_TESS4'] - return env - - raise EnvironmentError("Can't find Tesseract 4") - - -@pytest.fixture -def ensure_tess4(): - return _ensure_tess4() - - -@contextmanager -def modified_os_environ(env): - old_env = os.environ.copy() - os.environ.update(env) - yield - for key in env: - del os.environ[key] - if key in old_env: - os.environ[key] = old_env[key] - - -def tess4_available(): - """Check if a tesseract 4 binary is available, even if it's not the - official "tesseract" on PATH - - """ - try: - # _ensure_tess4 locates the tess4 binary we are going to check - env = _ensure_tess4() - with modified_os_environ(env): - # Now jump into this environment and make sure it really is Tess4 - return tesseract.v4() and tesseract.has_textonly_pdf() - except EnvironmentError: - pass - - return False - - -# Skip all tests in this file if not tesseract 4 -pytestmark = pytest.mark.skipif( - not tess4_available(), reason="tesseract 4.0 with textonly_pdf feature required" -) check_ocrmypdf = pytest.helpers.check_ocrmypdf run_ocrmypdf = pytest.helpers.run_ocrmypdf spoof = pytest.helpers.spoof -def test_textonly_pdf(ensure_tess4, resources, outdir): - check_ocrmypdf( - resources / 'linn.pdf', - outdir / 'linn_textonly.pdf', - '--pdf-renderer', - 'sandwich', - '--sidecar', - outdir / 'foo.txt', - env=ensure_tess4, - ) +def test_tesseract_v4(): + assert tesseract.v4() -def test_pagesize_consistency_tess4(ensure_tess4, resources, outpdf): +def test_pagesize_consistency_tess4(resources, outpdf): from math import isclose infile = resources / 'linn.pdf' @@ -121,7 +53,6 @@ def test_pagesize_consistency_tess4(ensure_tess4, resources, outpdf): '--deskew', '--remove-background', '--clean-final' if pytest.helpers.have_unpaper() else None, - env=ensure_tess4, ) after_dims = pytest.helpers.first_page_dimensions(outpdf) @@ -131,7 +62,7 @@ def test_pagesize_consistency_tess4(ensure_tess4, resources, outpdf): @pytest.mark.parametrize('basename', ['graph_ocred.pdf', 'cardinal.pdf']) -def test_skip_pages_does_not_replicate(ensure_tess4, resources, basename, outdir): +def test_skip_pages_does_not_replicate(resources, basename, outdir): infile = resources / basename outpdf = outdir / basename @@ -143,7 +74,6 @@ def test_skip_pages_does_not_replicate(ensure_tess4, resources, basename, outdir '--force-ocr', '--tesseract-timeout', '0', - env=ensure_tess4, ) info_in = pdfinfo.PdfInfo(infile) @@ -156,17 +86,11 @@ def test_skip_pages_does_not_replicate(ensure_tess4, resources, basename, outdir assert info[n].width_inches == info_in[n].width_inches -def test_content_preservation(ensure_tess4, resources, outpdf): +def test_content_preservation(resources, outpdf): infile = resources / 'masks.pdf' check_ocrmypdf( - infile, - outpdf, - '--pdf-renderer', - 'sandwich', - '--tesseract-timeout', - '0', - env=ensure_tess4, + infile, outpdf, '--pdf-renderer', 'sandwich', '--tesseract-timeout', '0' ) info = pdfinfo.PdfInfo(outpdf) @@ -174,12 +98,10 @@ def test_content_preservation(ensure_tess4, resources, outpdf): assert len(page.images) > 1, "masks were rasterized" -def test_no_languages(ensure_tess4, tmp_path): - env = ensure_tess4 +def test_no_languages(tmp_path): + env = os.environ.copy() (tmp_path / 'tessdata').mkdir() env['TESSDATA_PREFIX'] = fspath(tmp_path) - with modified_os_environ(env): - with pytest.raises(MissingDependencyError): - tesseract.languages.cache_clear() - tesseract.languages() + with pytest.raises(MissingDependencyError): + tesseract.languages(tesseract_env=env)