Use newer pytest tmp_path API

This commit is contained in:
James R. Barlow
2019-06-01 01:55:51 -07:00
parent ba41ccae1b
commit fb933edc0f
8 changed files with 50 additions and 47 deletions
+13 -13
View File
@@ -91,7 +91,7 @@ OCRMYPDF = [sys.executable, '-m', 'ocrmypdf']
@pytest.helpers.register
def spoof(tmpdir_factory, **kwargs):
def spoof(tmp_path_factory, **kwargs):
"""Modify PATH to override subprocess executables
spoof(program1='replacement', ...)
@@ -101,8 +101,8 @@ def spoof(tmpdir_factory, **kwargs):
"""
env = os.environ.copy()
slug = '-'.join(v.replace('.py', '') for v in sorted(kwargs.values()))
spoofer_base = Path(str(tmpdir_factory.mktemp('spoofers')))
tmpdir = spoofer_base / slug
spoofer_base = tmp_path_factory.mktemp('spoofers')
tmpdir = Path(spoofer_base / slug)
tmpdir.mkdir(parents=True)
for replace_program, with_spoof in kwargs.items():
@@ -141,15 +141,15 @@ def os_environ(new_env):
@pytest.fixture(scope='session')
def spoof_tesseract_noop(tmpdir_factory):
return spoof(tmpdir_factory, tesseract='tesseract_noop.py')
def spoof_tesseract_noop(tmp_path_factory):
return spoof(tmp_path_factory, tesseract='tesseract_noop.py')
@pytest.fixture(scope='session')
def spoof_tesseract_cache(tmpdir_factory):
def spoof_tesseract_cache(tmp_path_factory):
if running_in_docker():
return os.environ.copy()
return spoof(tmpdir_factory, tesseract="tesseract_cache.py")
return spoof(tmp_path_factory, tesseract="tesseract_cache.py")
@pytest.fixture
@@ -163,22 +163,22 @@ def ocrmypdf_exec():
@pytest.fixture(scope="function")
def outdir(tmpdir):
return Path(str(tmpdir))
def outdir(tmp_path):
return tmp_path
@pytest.fixture(scope="function")
def outpdf(tmpdir):
return str(Path(str(tmpdir)) / 'out.pdf')
def outpdf(tmp_path):
return tmp_path / 'out.pdf'
@pytest.fixture(scope="function")
def no_outpdf(tmpdir):
def no_outpdf(tmp_path):
"""This just documents the fact that a test is not expected to produce
output. Unfortunately an assertion failure inside a test fixture produces
an error rather than a test failure, so no testing is done. It's up to
the test to confirm that no output file was created."""
return str(Path(str(tmpdir)) / 'no_output.pdf')
return tmp_path / 'no_output.pdf'
@pytest.helpers.register
+2 -2
View File
@@ -28,8 +28,8 @@ from ocrmypdf.exec.tesseract import HOCR_TEMPLATE
@pytest.fixture
def blank_hocr(tmpdir):
filename = Path(str(tmpdir)) / "blank.hocr"
def blank_hocr(tmp_path):
filename = tmp_path / "blank.hocr"
filename.write_text(HOCR_TEMPLATE) # pylint: disable=E1101
return filename
+2 -2
View File
@@ -80,13 +80,13 @@ def test_pickle(crom_pix):
assert pix.mode == pix2.mode
def test_leptonica_compile(tmpdir):
def test_leptonica_compile(tmp_path):
from ocrmypdf.lib.compile_leptonica import ffibuilder
# Compile the library but build it somewhere that won't interfere with
# existing compiled library. Also compile in API mode so that we test
# the interfaces, even though we use it ABI mode.
ffibuilder.compile(tmpdir=fspath(tmpdir), target=fspath(tmpdir / 'lepttest.*'))
ffibuilder.compile(tmpdir=fspath(tmp_path), target=fspath(tmp_path / 'lepttest.*'))
def test_with_stderr(capsys):
+23 -20
View File
@@ -47,44 +47,46 @@ RENDERERS = ['hocr', 'sandwich']
@pytest.fixture(scope='session')
def spoof_tesseract_crash(tmpdir_factory):
return spoof(tmpdir_factory, tesseract='tesseract_crash.py')
def spoof_tesseract_crash(tmp_path_factory):
return spoof(tmp_path_factory, tesseract='tesseract_crash.py')
@pytest.fixture(scope='session')
def spoof_tesseract_big_image_error(tmpdir_factory):
return spoof(tmpdir_factory, tesseract='tesseract_big_image_error.py')
def spoof_tesseract_big_image_error(tmp_path_factory):
return spoof(tmp_path_factory, tesseract='tesseract_big_image_error.py')
@pytest.fixture(scope='session')
def spoof_no_tess_no_pdfa(tmpdir_factory):
return spoof(tmpdir_factory, tesseract='tesseract_noop.py', gs='gs_pdfa_failure.py')
@pytest.fixture(scope='session')
def spoof_no_tess_pdfa_warning(tmpdir_factory):
def spoof_no_tess_no_pdfa(tmp_path_factory):
return spoof(
tmpdir_factory, tesseract='tesseract_noop.py', gs='gs_feature_elision.py'
tmp_path_factory, tesseract='tesseract_noop.py', gs='gs_pdfa_failure.py'
)
@pytest.fixture(scope='session')
def spoof_no_tess_gs_render_fail(tmpdir_factory):
def spoof_no_tess_pdfa_warning(tmp_path_factory):
return spoof(
tmpdir_factory, tesseract='tesseract_noop.py', gs='gs_render_failure.py'
tmp_path_factory, tesseract='tesseract_noop.py', gs='gs_feature_elision.py'
)
@pytest.fixture(scope='session')
def spoof_no_tess_gs_raster_fail(tmpdir_factory):
def spoof_no_tess_gs_render_fail(tmp_path_factory):
return spoof(
tmpdir_factory, tesseract='tesseract_noop.py', gs='gs_raster_failure.py'
tmp_path_factory, tesseract='tesseract_noop.py', gs='gs_render_failure.py'
)
@pytest.fixture(scope='session')
def spoof_tess_bad_utf8(tmpdir_factory):
return spoof(tmpdir_factory, tesseract='tesseract_badutf8.py')
def spoof_no_tess_gs_raster_fail(tmp_path_factory):
return spoof(
tmp_path_factory, tesseract='tesseract_noop.py', gs='gs_raster_failure.py'
)
@pytest.fixture(scope='session')
def spoof_tess_bad_utf8(tmp_path_factory):
return spoof(tmp_path_factory, tesseract='tesseract_badutf8.py')
def test_quick(spoof_tesseract_cache, resources, outpdf):
@@ -225,7 +227,8 @@ def test_skip_ocr(spoof_tesseract_cache, resources, outpdf):
def test_redo_ocr(spoof_tesseract_cache, resources, outpdf):
in_ = resources / 'graph_ocred.pdf'
before = PdfInfo(in_, detailed_page_analysis=True)
out = check_ocrmypdf(in_, outpdf, '--redo-ocr', env=spoof_tesseract_cache)
out = outpdf
out = check_ocrmypdf(in_, out, '--redo-ocr')
after = PdfInfo(out, detailed_page_analysis=True)
assert before[0].has_text and after[0].has_text
assert (
@@ -936,7 +939,7 @@ def test_compression_changed(
def test_sidecar_pagecount(spoof_tesseract_cache, resources, outpdf):
sidecar = outpdf + '.txt'
sidecar = outpdf.with_suffix('.txt')
check_ocrmypdf(
resources / 'multipage.pdf',
outpdf,
@@ -960,7 +963,7 @@ def test_sidecar_pagecount(spoof_tesseract_cache, resources, outpdf):
def test_sidecar_nonempty(spoof_tesseract_cache, resources, outpdf):
sidecar = outpdf + '.txt'
sidecar = outpdf.with_suffix('.txt')
check_ocrmypdf(
resources / 'ccitt.pdf', outpdf, '--sidecar', sidecar, env=spoof_tesseract_cache
)
+2 -2
View File
@@ -261,10 +261,10 @@ def test_xml_metadata_preserved(spoof_tesseract_noop, output_type, resources, ou
)
def test_srgb_in_unicode_path(tmpdir):
def test_srgb_in_unicode_path(tmp_path):
"""Test that we can produce pdfmark when install path is not ASCII"""
dstdir = Path(fspath(tmpdir)) / b'\xe4\x80\x80'.decode('utf-8')
dstdir = tmp_path / b'\xe4\x80\x80'.decode('utf-8')
dstdir.mkdir()
dst = dstdir / 'sRGB.icc'
+3 -3
View File
@@ -263,12 +263,12 @@ def test_rotate_page_level(image_angle, page_angle, resources, outdir):
assert check_monochrome_correlation(outdir, reference, 1, out, 1) > 0.2
def test_tesseract_orientation(resources, tmpdir):
def test_tesseract_orientation(resources, tmp_path):
pix = leptonica.Pix.open(resources / 'crom.png')
pix_rotated = pix.rotate_orth(2) # 180 degrees clockwise
pix_rotated.write_implied_format(tmpdir / '000001.png')
pix_rotated.write_implied_format(tmp_path / '000001.png')
log = logging.getLogger()
tesseract.get_orientation( # Test results of this are unreliable
tmpdir / '000001.png', engine_mode='3', timeout=10, log=log
tmp_path / '000001.png', engine_mode='3', timeout=10, log=log
)
+3 -3
View File
@@ -174,10 +174,10 @@ def test_content_preservation(ensure_tess4, resources, outpdf):
assert len(page.images) > 1, "masks were rasterized"
def test_no_languages(ensure_tess4, tmpdir):
def test_no_languages(ensure_tess4, tmp_path):
env = ensure_tess4
(tmpdir / 'tessdata').mkdir()
env['TESSDATA_PREFIX'] = fspath(tmpdir)
(tmp_path / 'tessdata').mkdir()
env['TESSDATA_PREFIX'] = fspath(tmp_path)
with modified_os_environ(env):
with pytest.raises(MissingDependencyError):
+2 -2
View File
@@ -44,8 +44,8 @@ def have_unpaper():
@pytest.fixture(scope="session")
def spoof_unpaper_oldversion(tmpdir_factory):
return spoof(tmpdir_factory, unpaper="unpaper_oldversion.py")
def spoof_unpaper_oldversion(tmp_path_factory):
return spoof(tmp_path_factory, unpaper="unpaper_oldversion.py")
def test_no_unpaper(resources, no_outpdf):