2022-07-28 01:06:46 -07:00
|
|
|
# SPDX-FileCopyrightText: 2022 James R. Barlow
|
|
|
|
|
# SPDX-License-Identifier: MPL-2.0
|
2019-08-09 01:23:49 -07:00
|
|
|
|
2022-07-23 00:39:24 -07:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2019-08-09 01:23:49 -07:00
|
|
|
import os
|
2023-04-14 00:38:34 -07:00
|
|
|
from subprocess import DEVNULL, PIPE, run
|
2019-08-09 01:23:49 -07:00
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
2020-04-26 05:33:26 -07:00
|
|
|
from ocrmypdf.helpers import check_pdf
|
2019-08-09 01:23:49 -07:00
|
|
|
|
2021-04-07 01:56:51 -07:00
|
|
|
from .conftest import 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',
|
|
|
|
|
]
|
2022-04-03 19:11:06 -07:00
|
|
|
run(p_args, capture_output=True, stdin=input_stream, check=True)
|
2019-08-09 01:23:49 -07:00
|
|
|
|
|
|
|
|
|
2020-06-01 03:06:40 -07:00
|
|
|
def test_stdout(ocrmypdf_exec, resources, outpdf):
|
|
|
|
|
if 'COV_CORE_DATAFILE' in os.environ:
|
2022-04-14 20:15:00 -07:00
|
|
|
pytest.skip("Coverage uses stdout")
|
2019-12-31 17:10:51 -08:00
|
|
|
|
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',
|
|
|
|
|
]
|
2021-04-07 02:18:08 -07:00
|
|
|
run(p_args, stdout=output_stream, stderr=PIPE, stdin=DEVNULL, check=True)
|
2019-08-09 01:23:49 -07:00
|
|
|
|
2020-04-26 05:33:26 -07:00
|
|
|
assert check_pdf(output_file)
|
2019-08-09 01:23:49 -07:00
|
|
|
|
|
|
|
|
|
2026-06-28 12:02:34 -07:00
|
|
|
def test_stdout_protected_from_pollution(ocrmypdf_exec, resources, outpdf):
|
|
|
|
|
if 'COV_CORE_DATAFILE' in os.environ:
|
|
|
|
|
pytest.skip("Coverage uses stdout")
|
|
|
|
|
|
|
|
|
|
input_file = str(resources / 'francais.pdf')
|
|
|
|
|
output_file = str(outpdf)
|
|
|
|
|
|
|
|
|
|
# A plugin deliberately writes garbage to stdout during the run. With stdout
|
|
|
|
|
# protection active, that garbage must be diverted to stderr and never reach
|
|
|
|
|
# the PDF we are writing to stdout.
|
|
|
|
|
with open(output_file, 'wb') as output_stream:
|
|
|
|
|
p_args = ocrmypdf_exec + [
|
|
|
|
|
input_file,
|
|
|
|
|
'-',
|
|
|
|
|
'--plugin',
|
|
|
|
|
'tests/plugins/tesseract_noop.py',
|
|
|
|
|
'--plugin',
|
|
|
|
|
'tests/plugins/stdout_polluter.py',
|
|
|
|
|
]
|
|
|
|
|
p = run(p_args, stdout=output_stream, stderr=PIPE, stdin=DEVNULL, check=True)
|
|
|
|
|
|
|
|
|
|
assert check_pdf(output_file), "PDF on stdout was corrupted"
|
|
|
|
|
with open(output_file, 'rb') as f:
|
|
|
|
|
assert b'POLLUTION' not in f.read(), "pollution leaked into the PDF"
|
|
|
|
|
assert b'POLLUTION' in p.stderr, "pollution was not diverted to stderr"
|
|
|
|
|
|
|
|
|
|
|
2025-11-10 16:57:30 -08:00
|
|
|
@pytest.mark.skipif(os.name == 'nt', reason='Windows does not support /dev/null')
|
2020-06-01 03:06:40 -07:00
|
|
|
def test_dev_null(resources):
|
|
|
|
|
if 'COV_CORE_DATAFILE' in os.environ:
|
2022-04-14 20:15:00 -07:00
|
|
|
pytest.skip("Coverage uses stdout")
|
2019-12-31 17:10:51 -08:00
|
|
|
|
2021-12-06 17:00:25 -08:00
|
|
|
p = 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"
|
2021-12-06 17:00:25 -08:00
|
|
|
assert len(p.stdout) == 0, "wrote to stdout"
|