diff --git a/tests/qpdf_dummy_return2.py b/tests/qpdf_dummy_return2.py new file mode 100755 index 00000000..a4d999e1 --- /dev/null +++ b/tests/qpdf_dummy_return2.py @@ -0,0 +1,11 @@ +#!/usr/bin/env python3 +import sys + + +def main(): + print('qpdf dummy') + sys.exit(2) + + +if __name__ == '__main__': + main() diff --git a/tests/replace_ghostscript_nopdfa.py b/tests/replace_ghostscript_nopdfa.py deleted file mode 100755 index 918c57f5..00000000 --- a/tests/replace_ghostscript_nopdfa.py +++ /dev/null @@ -1,72 +0,0 @@ -#!/usr/bin/env python3 -import sys -import os - -''' -For testing PDF/A generation failures, this wrapper calls ghostscript -but filters out all PDF/A information so that a regular PDF will be -created instead. - -It assumes that it is called from a staged system PATH where the first -item on the PATH contains a file named 'gs' which is a symlink to this -file. It will strip out the first item on path to invoke the real -'gs'. That is, it expects this when called - -1. tests/output/[...test name...]/bin/gs is a symlink to this file -2. tests/output/bin is the first item on PATH -3. The real executable is on the path - -OCRmyPDF also calls "gs --version" and "gs --help". gs answers both -on stdout so, this wrapper prints to stderr. - -''' - - -def pdfa_param(arg): - if arg.startswith('-sPDFA'): - return True - if arg.startswith('-dPDFA'): - return True - if arg.endswith('.ps'): - return True - if 'ColorConversionStrategy' in arg: - return True - if 'ProcessColorModel' in arg: - return True - if 'OutputICCProfile' in arg: - return True - return False - - -if __name__ == '__main__': - sys_args = sys.argv[1:] - - print("Fake Ghostscript wrapper", file=sys.stderr) - - if any(pdfa_param(arg) for arg in sys_args): - # We were asked to produce a PDF/A - # Filter out PDF/A arguments - args = [arg for arg in sys.argv[1:] - if not pdfa_param(arg)] - - # Tell Ghostscript to create a PDF 1.3 instead so JHOVE won't - # think it's a PDF/A - indexof_pdfwrite = next(n for n, item in enumerate(args) - if 'pdfwrite' in item) - args.insert(indexof_pdfwrite + 1, '-dCompatibilityLevel=1.3') - print("Rewrote arguments", file=sys.stderr) - else: - args = sys_args - print("Keeping arguments", file=sys.stderr) - - exec_path = os.environ['PATH'].split(os.pathsep) - env = os.environ.copy() - env['PATH'] = os.pathsep.join(exec_path[1:]) - - print("Calling real Ghostscript with modified args and env", - file=sys.stderr) - print(args, file=sys.stderr) - print(env['PATH'], file=sys.stderr) - - sys.stderr.flush() - os.execvpe('gs', ['gs'] + args, env) diff --git a/tests/test_main.py b/tests/test_main.py index cfa51ed2..b91b04d5 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -242,53 +242,6 @@ def test_maximum_options(): yield check_maximum_options, 'tesseract' -def override_binary(binary, replacement): - '''Create a directory that contains a symlink named 'binary' that - points to replacement, another program to use in place of the - regular binary for testing. - - override_binary('gs', 'replace_gs.py') will create an environment - in which "gs" will invoke replace_gs.py. - - Not thread-safe with other test at the moment. - - Returns the os.environ["PATH"] string under which this binary will - be invoked.''' - - replacement_path = os.path.abspath(os.path.join(TESTS_ROOT, - replacement)) - subdir = os.path.splitext(os.path.basename(replacement))[0] - binary_path = os.path.abspath(os.path.join(TEST_BINARY_PATH, - subdir, - binary)) - with suppress(FileExistsError): - os.makedirs(os.path.dirname(binary_path)) - assert os.path.isdir(os.path.dirname(binary_path)) - assert not os.path.lexists(binary_path) - print("symlink %s -> %s" % (replacement_path, binary_path)) - os.symlink(replacement_path, binary_path) - - os.chmod(replacement_path, int('755', base=8)) - - return os.path.dirname(binary_path) + os.pathsep + os.environ["PATH"] - - -@pytest.fixture -def break_ghostscript_pdfa(): - return override_binary('gs', 'replace_ghostscript_nopdfa.py') - - -@pytest.mark.skipif(os.environ.get('OCRMYPDF_IN_DOCKER', False), - reason="Requires writable filesystem") -def test_ghostscript_pdfa_fails(break_ghostscript_pdfa): - env = os.environ.copy() - env['PATH'] = break_ghostscript_pdfa - - p, out, err = run_ocrmypdf_env( - 'graph_ocred.pdf', 'not_a_pdfa.pdf', '-v', '1', '--skip-text', env=env) - assert p.returncode == ExitCode.ok, err # no longer using JHOVE PDFA check - - def test_tesseract_missing_tessdata(): env = os.environ.copy() env['TESSDATA_PREFIX'] = '/tmp' @@ -354,3 +307,13 @@ def test_input_file_not_a_pdf(): assert sh.returncode == ExitCode.input_file assert (input_file in out or input_file in err) + +def test_qpdf_repair_fails(): + env = os.environ.copy() + env['OCRMYPDF_QPDF'] = os.path.abspath('./qpdf_dummy_return2.py') + p, out, err = run_ocrmypdf_env( + '-v', '1', + 'c02-22.pdf', 'wont_be_created.pdf', env=env) + print(out) + print(err) + assert p.returncode == ExitCode.input_file