From 4046766ca5cc1afb2ed783358c31bf54e2d31aaa Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Fri, 2 Mar 2018 16:57:46 -0800 Subject: [PATCH] Fix Python 3.5 test suite failure on symlinks Did not account for API difference in pathlib --- ocrmypdf/helpers.py | 15 +++++++++++++-- tests/test_main.py | 2 +- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/ocrmypdf/helpers.py b/ocrmypdf/helpers.py index db4fde65..3310a034 100644 --- a/ocrmypdf/helpers.py +++ b/ocrmypdf/helpers.py @@ -64,9 +64,20 @@ def is_file_writable(test_file): the location is writable. """ p = Path(test_file) + if p.is_symlink(): - p = p.resolve() - if p.is_file(): + # Python 3.5 does not accept parameters for Path.resolve() and behaves + # as if strict=True (throws an exception on failure). Python 3.6 + # defaults to strict=False. This implements strict=False like behavior + # for Python 3.5. + if sys.version_info[0:2] <= (3, 5): + resolve = lambda: Path(os.path.realpath(str(p))) + else: + resolve = lambda: p.resolve(strict=False) + p = resolve() + + # p.is_file() throws an exception in some cases + if p.exists() and p.is_file(): return os.access( str(p), os.W_OK, effective_ids=(os.access in os.supports_effective_ids)) diff --git a/tests/test_main.py b/tests/test_main.py index dbee93bb..fc4c91d9 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -1134,6 +1134,6 @@ def test_output_is_symlink(spoof_tesseract_noop, resources, outdir): '--force-ocr', env=spoof_tesseract_noop ) - assert p.returncode == ExitCode.ok + assert p.returncode == ExitCode.ok, err assert (outdir / 'out.pdf').stat().st_size > 0, 'target file not created' \ No newline at end of file