From 6d8bc9f8cd64a8452b19eea76fd8d59f3da8e3e9 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Wed, 10 Jan 2018 12:35:47 -0800 Subject: [PATCH] Use symlinks instead of copies for test result_images. --- lib/matplotlib/testing/decorators.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/matplotlib/testing/decorators.py b/lib/matplotlib/testing/decorators.py index 20e4e265b79b..a68bc65520eb 100644 --- a/lib/matplotlib/testing/decorators.py +++ b/lib/matplotlib/testing/decorators.py @@ -192,13 +192,18 @@ def copy_baseline(self, baseline, extension): os.path.join(self.result_dir, os.path.basename(orig_expected_fname)), 'expected') - if os.path.exists(orig_expected_fname): - shutil.copyfile(orig_expected_fname, expected_fname) - else: - reason = ("Do not have baseline image {} because this " - "file does not exist: {}".format(expected_fname, - orig_expected_fname)) - raise ImageComparisonFailure(reason) + try: + # os.symlink errors if the target already exists. + with contextlib.suppress(OSError): + os.remove(expected_fname) + try: + os.symlink(orig_expected_fname, expected_fname) + except OSError: # On Windows, symlink *may* be unavailable. + shutil.copyfile(orig_expected_fname, expected_fname) + except OSError: + raise ImageComparisonFailure( + f"Missing baseline image {expected_fname} because the " + f"following file cannot be accessed: {orig_expected_fname}") return expected_fname def compare(self, idx, baseline, extension):