2019-08-09 01:23:49 -07:00
|
|
|
# © 2019 James R. Barlow: github.com/jbarlow83
|
|
|
|
#
|
2020-08-05 00:44:42 -07:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2019-08-09 01:23:49 -07:00
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
from pathlib import Path
|
2020-06-09 15:27:14 -07:00
|
|
|
from subprocess import DEVNULL, PIPE, Popen, run
|
2019-08-09 01:23:49 -07:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from ocrmypdf.exceptions import ExitCode
|
2020-04-26 05:33:26 -07:00
|
|
|
from ocrmypdf.helpers import check_pdf
|
2019-08-09 01:23:49 -07:00
|
|
|
|
|
|
|
# pytest.helpers is dynamic
|
|
|
|
# pylint: disable=no-member,redefined-outer-name
|
|
|
|
|
|
|
|
run_ocrmypdf = pytest.helpers.run_ocrmypdf
|
2019-11-28 14:03:18 -08:00
|
|
|
run_ocrmypdf_api = pytest.helpers.run_ocrmypdf
|
2019-08-09 01:23:49 -07:00
|
|
|
|
|
|
|
|
2020-06-01 03:06:40 -07:00
|
|
|
def test_stdin(ocrmypdf_exec, resources, outpdf):
|
2019-08-09 01:23:49 -07:00
|
|
|
input_file = str(resources / 'francais.pdf')
|
|
|
|
output_file = str(outpdf)
|
|
|
|
|
|
|
|
# Runs: ocrmypdf - output.pdf < testfile.pdf
|
|
|
|
with open(input_file, 'rb') as input_stream:
|
2020-06-01 03:06:40 -07:00
|
|
|
p_args = ocrmypdf_exec + [
|
|
|
|
'-',
|
|
|
|
output_file,
|
|
|
|
'--plugin',
|
|
|
|
'tests/plugins/tesseract_noop.py',
|
|
|
|
]
|
|
|
|
p = run(p_args, stdout=PIPE, stderr=PIPE, stdin=input_stream)
|
2019-08-09 01:23:49 -07:00
|
|
|
assert p.returncode == ExitCode.ok
|
|
|
|
|
|
|
|
|
2020-06-01 03:06:40 -07:00
|
|
|
def test_stdout(ocrmypdf_exec, resources, outpdf):
|
|
|
|
if 'COV_CORE_DATAFILE' in os.environ:
|
2019-12-31 17:10:51 -08:00
|
|
|
pytest.skip(msg="Coverage uses stdout")
|
|
|
|
|
2019-08-09 01:23:49 -07:00
|
|
|
input_file = str(resources / 'francais.pdf')
|
|
|
|
output_file = str(outpdf)
|
|
|
|
|
|
|
|
# Runs: ocrmypdf francais.pdf - > test_stdout.pdf
|
|
|
|
with open(output_file, 'wb') as output_stream:
|
2020-06-01 03:06:40 -07:00
|
|
|
p_args = ocrmypdf_exec + [
|
|
|
|
input_file,
|
|
|
|
'-',
|
|
|
|
'--plugin',
|
|
|
|
'tests/plugins/tesseract_noop.py',
|
|
|
|
]
|
|
|
|
p = run(p_args, stdout=output_stream, stderr=PIPE, stdin=DEVNULL)
|
2019-08-09 01:23:49 -07:00
|
|
|
assert p.returncode == ExitCode.ok
|
|
|
|
|
2020-04-26 05:33:26 -07:00
|
|
|
assert check_pdf(output_file)
|
2019-08-09 01:23:49 -07:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
sys.version_info[0:3] >= (3, 6, 4), reason="issue fixed in Python 3.6.4"
|
|
|
|
)
|
2019-11-28 14:45:03 -08:00
|
|
|
@pytest.mark.skipif(os.name == 'nt', reason="POSIX problem")
|
2020-06-01 03:06:40 -07:00
|
|
|
def test_closed_streams(ocrmypdf_exec, resources, outpdf):
|
2019-08-09 01:23:49 -07:00
|
|
|
input_file = str(resources / 'francais.pdf')
|
|
|
|
output_file = str(outpdf)
|
|
|
|
|
|
|
|
def evil_closer():
|
|
|
|
os.close(0)
|
|
|
|
os.close(1)
|
|
|
|
|
2020-06-01 03:06:40 -07:00
|
|
|
p_args = ocrmypdf_exec + [
|
|
|
|
input_file,
|
|
|
|
output_file,
|
|
|
|
'--plugin',
|
|
|
|
'tests/plugins/tesseract_noop.py',
|
|
|
|
]
|
2019-08-09 01:23:49 -07:00
|
|
|
p = Popen( # pylint: disable=subprocess-popen-preexec-fn
|
|
|
|
p_args,
|
|
|
|
close_fds=True,
|
|
|
|
stdout=None,
|
|
|
|
stderr=PIPE,
|
|
|
|
stdin=None,
|
|
|
|
preexec_fn=evil_closer,
|
|
|
|
)
|
2020-06-09 15:27:14 -07:00
|
|
|
_out, err = p.communicate()
|
2019-08-09 01:23:49 -07:00
|
|
|
print(err.decode())
|
|
|
|
assert p.returncode == ExitCode.ok
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(sys.version_info >= (3, 7, 0), reason='better utf-8')
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
Path('/etc/alpine-release').exists(), reason="invalid test on alpine"
|
|
|
|
)
|
2019-12-07 13:09:25 -08:00
|
|
|
@pytest.mark.skipif(os.name == 'nt', reason="invalid test on Windows")
|
2020-06-09 00:30:13 -07:00
|
|
|
def test_bad_locale(monkeypatch):
|
|
|
|
monkeypatch.setenv('LC_ALL', 'C')
|
|
|
|
p, out, err = run_ocrmypdf('a', 'b')
|
2019-08-09 01:23:49 -07:00
|
|
|
assert out == '', "stdout not clean"
|
|
|
|
assert p.returncode != 0
|
|
|
|
assert 'configured to use ASCII as encoding' in err, "should whine"
|
|
|
|
|
|
|
|
|
2019-12-07 13:09:25 -08:00
|
|
|
@pytest.mark.xfail(
|
|
|
|
os.name == 'nt' and sys.version_info < (3, 8),
|
|
|
|
reason="Windows does not like this; not sure how to fix",
|
|
|
|
)
|
2020-06-01 03:06:40 -07:00
|
|
|
def test_dev_null(resources):
|
|
|
|
if 'COV_CORE_DATAFILE' in os.environ:
|
2019-12-31 17:10:51 -08:00
|
|
|
pytest.skip(msg="Coverage uses stdout")
|
|
|
|
|
2020-06-09 15:27:14 -07:00
|
|
|
p, out, _err = run_ocrmypdf(
|
2020-06-01 03:06:40 -07:00
|
|
|
resources / 'trivial.pdf',
|
|
|
|
os.devnull,
|
|
|
|
'--force-ocr',
|
|
|
|
'--plugin',
|
|
|
|
'tests/plugins/tesseract_noop.py',
|
2019-08-09 01:23:49 -07:00
|
|
|
)
|
|
|
|
assert p.returncode == 0, "could not send output to /dev/null"
|
|
|
|
assert len(out) == 0, "wrote to stdout"
|