From 850814131426c0e009472665c8253b625bb134f7 Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Tue, 28 Jul 2015 00:43:22 -0700 Subject: [PATCH] Drop nose, all tests working reasonably again Although the real issue was that the ruffus pipeline cannot be executed twice in the same process due to its reliance on global variables. The new OO pipeline in ruffus 2.6 would be one resolution that would allow for more comprehensive testing as opposed to farming out the execution to subprocess and inspecting the results, as is currently done. --- .gitignore | 5 ++++- ocrmypdf/test/test_pageinfo.py | 10 ++++----- ocrmypdf/unpaper.py | 4 +++- tests/test_main.py | 39 +++++++++++++--------------------- 4 files changed, 27 insertions(+), 31 deletions(-) diff --git a/.gitignore b/.gitignore index 9638ef0e..b26da2e1 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,7 @@ build/ dist/ *.egg-info/ venv/ -*/test/output \ No newline at end of file +*/test/output +bin/ +include/ +lib/ diff --git a/ocrmypdf/test/test_pageinfo.py b/ocrmypdf/test/test_pageinfo.py index 9b733e5c..fb3b474b 100644 --- a/ocrmypdf/test/test_pageinfo.py +++ b/ocrmypdf/test/test_pageinfo.py @@ -8,7 +8,7 @@ from contextlib import suppress import os import sys import shutil -from nose.tools import * +import pytest from pkg_resources import Requirement, resource_filename req = Requirement.parse('ocrmypdf') @@ -75,11 +75,10 @@ def test_single_page_image(): assert pdfimage['bpc'] == 8 # DPI in a 1"x1" is the image width - eq_(pdfimage['dpi_w'], 8) - eq_(pdfimage['dpi_h'], 8) + assert pdfimage['dpi_w'] == 8 + assert pdfimage['dpi_h'] == 8 -@raises(NotImplementedError) def test_single_page_inline_image(): filename = os.path.join(TEST_OUTPUT, 'image-mono-inline.pdf') pdf = Canvas(filename, pagesize=(8*72, 6*72)) @@ -93,7 +92,8 @@ def test_single_page_inline_image(): pdf.showPage() pdf.save() - pageinfo.pdf_get_all_pageinfo(filename) + with pytest.raises(NotImplementedError): + pageinfo.pdf_get_all_pageinfo(filename) def test_jpeg(): diff --git a/ocrmypdf/unpaper.py b/ocrmypdf/unpaper.py index f6707dc5..3b57c71f 100644 --- a/ocrmypdf/unpaper.py +++ b/ocrmypdf/unpaper.py @@ -22,8 +22,10 @@ def _version(): try: VERSION = _version() + AVAILABLE = True except FileNotFoundError: - print("Could not find 'unpaper' executable", file=sys.stderr) + VERSION = '?' + AVAILABLE = False raise try: diff --git a/tests/test_main.py b/tests/test_main.py index 66ea6564..62f72f49 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -7,8 +7,7 @@ import shutil from contextlib import suppress import sys from unittest.mock import patch, create_autospec -from nose import with_setup -from nose.tools import raises +import pytest if sys.version_info.major < 3: @@ -38,7 +37,7 @@ def run_ocrmypdf_sh(input_file, output_file, *args): return sh, out, err -def check_ocrmypdf_sh(input_basename, output_basename, *args): +def check_ocrmypdf(input_basename, output_basename, *args): input_file = os.path.join(TEST_RESOURCES, input_basename) output_file = os.path.join(TEST_OUTPUT, output_basename) @@ -46,26 +45,16 @@ def check_ocrmypdf_sh(input_basename, output_basename, *args): assert sh.returncode == 0, err assert os.path.exists(output_file), "Output file not created" assert os.stat(output_file).st_size > 100, "PDF too small or empty" - - -def run_ocrmypdf(input_basename, output_basename, *args): - input_file = os.path.join(TEST_RESOURCES, input_basename) - output_file = os.path.join(TEST_OUTPUT, output_basename) - - sys_argv = list(args) + [input_file, output_file] - with patch.object(sys, 'argv', sys_argv): - import ocrmypdf.main - ocrmypdf.main.run_pipeline() return output_file -def xtest_quick(): - check_ocrmypdf_sh('c02-22.pdf', 'test_quick.pdf') +def test_quick(): + check_ocrmypdf('c02-22.pdf', 'test_quick.pdf') -def xtest_deskew(): +def test_deskew(): # Run with deskew - deskewed_pdf = run_ocrmypdf('skew.pdf', 'test_deskew.pdf', '-d') + deskewed_pdf = check_ocrmypdf('skew.pdf', 'test_deskew.pdf', '-d') # Now render as an image again and use Leptonica to find the skew angle # to confirm that it was deskewed @@ -92,12 +81,12 @@ def xtest_deskew(): assert -0.5 < skew_angle < 0.5, "Deskewing failed" -def xtest_clean(): - check_ocrmypdf_sh('skew.pdf', 'test_clean.pdf', '-c') +def test_clean(): + check_ocrmypdf('skew.pdf', 'test_clean.pdf', '-c') def test_metadata(): - pdf = run_ocrmypdf( + pdf = check_ocrmypdf( 'c02-22.pdf', 'test_metadata.pdf', '--title', 'Du siehst den Wald vor lauter Bäumen nicht.', '--author', '孔子', @@ -117,7 +106,7 @@ def test_metadata(): def check_oversample(renderer): - oversampled_pdf = run_ocrmypdf( + oversampled_pdf = check_ocrmypdf( 'skew.pdf', 'test_oversample_%s.pdf' % renderer, '--oversample', '300', '--pdf-renderer', renderer) @@ -134,12 +123,14 @@ def test_oversample(): yield check_oversample, 'tesseract' -@raises(SystemExit) def test_repeat_ocr(): - run_ocrmypdf('graph_ocred.pdf', 'wontwork.pdf') + sh, _, _ = run_ocrmypdf_sh('graph_ocred.pdf', 'wontwork.pdf') + assert sh.returncode != 0 def test_force_ocr(): - run_ocrmypdf('graph_ocred.pdf', 'test_force.pdf') + check_ocrmypdf('graph_ocred.pdf', 'test_force.pdf', '-f') +def test_skip_ocr(): + check_ocrmypdf('graph_ocred.pdf', 'test_skip.pdf', '-s')