From 5da26e4c9ce89396b586ca48c8a44b0d2a72ee78 Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Tue, 5 Mar 2019 22:25:22 -0800 Subject: [PATCH] Convert most uses of subprocess.Popen to subprocess.run in test suite --- docs/batch.rst | 6 +++--- src/ocrmypdf/_unicodefun.py | 4 ++-- tests/conftest.py | 23 ++++++++++------------- tests/test_main.py | 27 ++++++++++----------------- 4 files changed, 25 insertions(+), 35 deletions(-) diff --git a/docs/batch.rst b/docs/batch.rst index fe1ec0db..d905752f 100644 --- a/docs/batch.rst +++ b/docs/batch.rst @@ -80,9 +80,9 @@ This user contributed script also provides an example of batch processing. print(full_path) cmd = ["ocrmypdf", "--deskew", filename, filename] logging.info(cmd) - proc = subprocess.Popen( + proc = subprocess.run( cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) - result = proc.stdout.read() + result = proc.stdout if proc.returncode == 6: print("Skipped document because it already contained text") elif proc.returncode == 0: @@ -151,7 +151,7 @@ This is only possible for x86-based Synology products. Some Synology products us # the script is processed as root user via chron cmd = ['docker', 'run', '--rm', '-v', docker_mount, '-u=1030:65538', 'jbarlow83/ocrmypdf', , '--deskew' , filename, filename_OCR] logging.info(cmd) - proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + proc = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) result = proc.stdout.read() logging.info(result) full_path_OCR = dir_name + '/' + filename_OCR diff --git a/src/ocrmypdf/_unicodefun.py b/src/ocrmypdf/_unicodefun.py index 79f1b829..fbef01cb 100644 --- a/src/ocrmypdf/_unicodefun.py +++ b/src/ocrmypdf/_unicodefun.py @@ -58,9 +58,9 @@ def verify_python3_env(): # pragma: no cover if os.name == 'posix': import subprocess - rv = subprocess.Popen( + rv = subprocess.run( ['locale', '-a'], stdout=subprocess.PIPE, stderr=subprocess.PIPE - ).communicate()[0] + ).stdout good_locales = set() has_c_utf8 = False diff --git a/tests/conftest.py b/tests/conftest.py index d68a6b23..19191b6c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -19,7 +19,7 @@ import os import platform import sys from pathlib import Path -from subprocess import PIPE, Popen +from subprocess import PIPE, run import pytest @@ -171,19 +171,16 @@ def run_ocrmypdf(input_file, output_file, *args, env=None, universal_newlines=Tr if env is None: env = os.environ - p_args = OCRMYPDF + [str(arg) for arg in args] + [str(input_file), str(output_file)] - p = Popen( - p_args, - close_fds=True, - stdout=PIPE, - stderr=PIPE, - universal_newlines=universal_newlines, - env=env, + p_args = ( + OCRMYPDF + + [str(arg) for arg in args if arg is not None] + + [str(input_file), str(output_file)] ) - out, err = p.communicate() - # print(err) - - return p, out, err + p = run( + p_args, stdout=PIPE, stderr=PIPE, universal_newlines=universal_newlines, env=env + ) + # print(p.stderr) + return p, p.stdout, p.stderr @pytest.helpers.register diff --git a/tests/test_main.py b/tests/test_main.py index 06e56e57..a34968d2 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -21,7 +21,7 @@ import shutil import sys from math import isclose from pathlib import Path -from subprocess import DEVNULL, PIPE, Popen +from subprocess import DEVNULL, PIPE, run, Popen import PIL import pytest @@ -554,16 +554,13 @@ def test_stdin(spoof_tesseract_noop, ocrmypdf_exec, resources, outpdf): # Runs: ocrmypdf - output.pdf < testfile.pdf with open(input_file, 'rb') as input_stream: p_args = ocrmypdf_exec + ['-', output_file] - p = Popen( + p = run( p_args, - close_fds=True, stdout=PIPE, stderr=PIPE, stdin=input_stream, env=spoof_tesseract_noop, ) - out, err = p.communicate() - assert p.returncode == ExitCode.ok @@ -574,9 +571,8 @@ def test_stdout(spoof_tesseract_noop, ocrmypdf_exec, resources, outpdf): # Runs: ocrmypdf francais.pdf - > test_stdout.pdf with open(output_file, 'wb') as output_stream: p_args = ocrmypdf_exec + [input_file, '-'] - p = Popen( + p = run( p_args, - close_fds=True, stdout=output_stream, stderr=PIPE, stdin=DEVNULL, @@ -852,22 +848,21 @@ def test_compression_preserved( '-', output_file, ] - p = Popen( + p = run( p_args, - close_fds=True, stdout=PIPE, stderr=PIPE, stdin=input_stream, + universal_newlines=True, env=spoof_tesseract_noop, ) - out, err = p.communicate() if im.mode in ('RGBA', 'LA'): # If alpha image is input, expect an error - assert p.returncode != ExitCode.ok and b'alpha' in err + assert p.returncode != ExitCode.ok and b'alpha' in p.stderr return - assert p.returncode == ExitCode.ok, err.decode('utf-8') + assert p.returncode == ExitCode.ok, p.stderr pdfinfo = PdfInfo(output_file) @@ -913,17 +908,15 @@ def test_compression_changed( '-', output_file, ] - p = Popen( + p = run( p_args, - close_fds=True, stdout=PIPE, stderr=PIPE, stdin=input_stream, + universal_newlines=True, env=spoof_tesseract_noop, ) - out, err = p.communicate() - - assert p.returncode == ExitCode.ok, err + assert p.returncode == ExitCode.ok, p.stderr pdfinfo = PdfInfo(output_file)