2015-07-22 02:59:25 -07:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2015-07-25 00:57:07 -07:00
|
|
|
from __future__ import print_function
|
2015-07-22 02:59:25 -07:00
|
|
|
from subprocess import Popen, PIPE
|
|
|
|
import os
|
2015-07-22 04:00:59 -07:00
|
|
|
import shutil
|
|
|
|
from contextlib import suppress
|
2015-07-22 11:21:33 -07:00
|
|
|
import sys
|
|
|
|
|
|
|
|
if sys.version_info.major < 3:
|
|
|
|
print("Requires Python 3.4+")
|
|
|
|
sys.exit(1)
|
2015-07-22 02:59:25 -07:00
|
|
|
|
|
|
|
TESTS_ROOT = os.path.abspath(os.path.dirname(__file__))
|
|
|
|
PROJECT_ROOT = os.path.dirname(TESTS_ROOT)
|
|
|
|
OCRMYPDF = os.path.join(PROJECT_ROOT, 'OCRmyPDF.sh')
|
|
|
|
TEST_RESOURCES = os.path.join(PROJECT_ROOT, 'tests', 'resources')
|
2015-07-22 03:16:19 -07:00
|
|
|
TEST_OUTPUT = os.path.join(PROJECT_ROOT, 'tests', 'output')
|
2015-07-22 02:59:25 -07:00
|
|
|
|
|
|
|
|
2015-07-22 03:16:19 -07:00
|
|
|
def setup_module():
|
2015-07-22 04:00:59 -07:00
|
|
|
with suppress(FileNotFoundError):
|
|
|
|
shutil.rmtree(TEST_OUTPUT)
|
|
|
|
with suppress(FileExistsError):
|
2015-07-22 03:16:19 -07:00
|
|
|
os.mkdir(TEST_OUTPUT)
|
|
|
|
|
|
|
|
|
2015-07-22 04:00:59 -07:00
|
|
|
def run_ocrmypdf(input_file, output_file, *args):
|
|
|
|
sh_args = ['sh', OCRMYPDF] + list(args) + [input_file, output_file]
|
2015-07-22 02:59:25 -07:00
|
|
|
sh = Popen(
|
|
|
|
sh_args, close_fds=True, stdout=PIPE, stderr=PIPE,
|
|
|
|
universal_newlines=True)
|
|
|
|
out, err = sh.communicate()
|
|
|
|
return sh, out, err
|
|
|
|
|
|
|
|
|
2015-07-22 04:00:59 -07:00
|
|
|
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 or input_basename)
|
|
|
|
|
|
|
|
sh, _, err = run_ocrmypdf(input_file, output_file, *args)
|
2015-07-22 02:59:25 -07:00
|
|
|
assert sh.returncode == 0, err
|
2015-07-22 04:00:59 -07:00
|
|
|
assert os.path.exists(output_file), "Output file not created"
|
|
|
|
assert os.stat(output_file).st_size > 100, "PDF too small, empty or near empty"
|
2015-07-22 02:59:25 -07:00
|
|
|
|
|
|
|
|
|
|
|
def test_quick():
|
2015-07-22 04:00:59 -07:00
|
|
|
check_ocrmypdf('c02-22.pdf', 'test_quick.pdf')
|
|
|
|
|
|
|
|
|
|
|
|
def test_deskew():
|
|
|
|
check_ocrmypdf('skew.pdf', 'test_deskew.pdf', '-d')
|
|
|
|
|
|
|
|
|
|
|
|
def test_clean():
|
|
|
|
check_ocrmypdf('skew.pdf', 'test_clean.pdf', '-c')
|