@@ -127,10 +127,6 @@ def _raise_on_image_difference(expected, actual, tol):
127
127
__tracebackhide__ = True
128
128
129
129
err = compare_images (expected , actual , tol , in_decorator = True )
130
-
131
- if not os .path .exists (expected ):
132
- raise ImageComparisonFailure ('image does not exist: %s' % expected )
133
-
134
130
if err :
135
131
for key in ["actual" , "expected" ]:
136
132
err [key ] = os .path .relpath (err [key ])
@@ -172,34 +168,28 @@ class _ImageComparisonBase:
172
168
This class provides *just* the comparison-related functionality and avoids
173
169
any code that would be specific to any testing framework.
174
170
"""
175
- def __init__ (self , tol , remove_text , savefig_kwargs ):
176
- self .func = self .baseline_dir = self .result_dir = None
177
- self .tol = tol
178
- self .remove_text = remove_text
179
- self .savefig_kwargs = savefig_kwargs
180
171
181
- def delayed_init (self , func ):
182
- assert self .func is None , "it looks like same decorator used twice"
172
+ def __init__ (self , func , tol , remove_text , savefig_kwargs ):
183
173
self .func = func
184
174
self .baseline_dir , self .result_dir = _image_directories (func )
175
+ self .tol = tol
176
+ self .remove_text = remove_text
177
+ self .savefig_kwargs = savefig_kwargs
185
178
186
179
def copy_baseline (self , baseline , extension ):
187
- baseline_path = os .path .join (self .baseline_dir , baseline )
188
- orig_expected_fname = baseline_path + '.' + extension
189
- if extension == 'eps' and not os .path .exists (orig_expected_fname ):
190
- orig_expected_fname = baseline_path + '.pdf'
191
- expected_fname = make_test_filename (
192
- os .path .join (self .result_dir ,
193
- os .path .basename (orig_expected_fname )),
194
- 'expected' )
195
- if os .path .exists (orig_expected_fname ):
196
- shutil .copyfile (orig_expected_fname , expected_fname )
197
- else :
198
- reason = ("Do not have baseline image {} because this "
199
- "file does not exist: {}" .format (expected_fname ,
200
- orig_expected_fname ))
201
- raise ImageComparisonFailure (reason )
202
- return expected_fname
180
+ baseline_path = self .baseline_dir / baseline
181
+ orig_expected_path = baseline_path .with_suffix (f'.{ extension } ' )
182
+ if extension == 'eps' and not orig_expected_path .exists ():
183
+ orig_expected_path = baseline_path .with_suffix ('.pdf' )
184
+ expected_path = make_test_filename (
185
+ self .result_dir / orig_expected_path .name , 'expected' )
186
+ try :
187
+ shutil .copyfile (orig_expected_path , expected_path )
188
+ except IOError as exc :
189
+ raise ImageComparisonFailure (
190
+ f"Failed to copy baseline image { orig_expected_path } to "
191
+ f"{ expected_path } " ) from exc
192
+ return expected_path
203
193
204
194
def compare (self , idx , baseline , extension ):
205
195
__tracebackhide__ = True
@@ -209,17 +199,16 @@ def compare(self, idx, baseline, extension):
209
199
if self .remove_text :
210
200
remove_ticks_and_titles (fig )
211
201
212
- actual_fname = (
213
- os .path .join (self .result_dir , baseline ) + '.' + extension )
202
+ actual_path = (self .result_dir / baseline ).with_suffix (f'.{ extension } ' )
214
203
kwargs = self .savefig_kwargs .copy ()
215
204
if extension == 'pdf' :
216
205
kwargs .setdefault ('metadata' ,
217
206
{'Creator' : None , 'Producer' : None ,
218
207
'CreationDate' : None })
219
- fig .savefig (actual_fname , ** kwargs )
208
+ fig .savefig (actual_path , ** kwargs )
220
209
221
- expected_fname = self .copy_baseline (baseline , extension )
222
- _raise_on_image_difference (expected_fname , actual_fname , self .tol )
210
+ expected_path = self .copy_baseline (baseline , extension )
211
+ _raise_on_image_difference (expected_path , actual_path , self .tol )
223
212
224
213
225
214
def _pytest_image_comparison (baseline_images , extensions , tol ,
@@ -249,9 +238,8 @@ def decorator(func):
249
238
@functools .wraps (func )
250
239
def wrapper (* args , ** kwargs ):
251
240
__tracebackhide__ = True
252
- img = _ImageComparisonBase (tol = tol , remove_text = remove_text ,
241
+ img = _ImageComparisonBase (func , tol = tol , remove_text = remove_text ,
253
242
savefig_kwargs = savefig_kwargs )
254
- img .delayed_init (func )
255
243
matplotlib .testing .set_font_settings_for_testing ()
256
244
func (* args , ** kwargs )
257
245
@@ -390,7 +378,7 @@ def test_plot(fig_test, fig_ref):
390
378
def decorator (func ):
391
379
import pytest
392
380
393
- _ , result_dir = map ( Path , _image_directories (func ) )
381
+ _ , result_dir = _image_directories (func )
394
382
395
383
if len (inspect .signature (func ).parameters ) == 2 :
396
384
# Free-standing function.
@@ -399,9 +387,8 @@ def wrapper(ext):
399
387
fig_test = plt .figure ("test" )
400
388
fig_ref = plt .figure ("reference" )
401
389
func (fig_test , fig_ref )
402
- test_image_path = str (
403
- result_dir / (func .__name__ + "." + ext ))
404
- ref_image_path = str (
390
+ test_image_path = result_dir / (func .__name__ + "." + ext )
391
+ ref_image_path = (
405
392
result_dir / (func .__name__ + "-expected." + ext ))
406
393
fig_test .savefig (test_image_path )
407
394
fig_ref .savefig (ref_image_path )
@@ -415,9 +402,8 @@ def wrapper(self, ext):
415
402
fig_test = plt .figure ("test" )
416
403
fig_ref = plt .figure ("reference" )
417
404
func (self , fig_test , fig_ref )
418
- test_image_path = str (
419
- result_dir / (func .__name__ + "." + ext ))
420
- ref_image_path = str (
405
+ test_image_path = result_dir / (func .__name__ + "." + ext )
406
+ ref_image_path = (
421
407
result_dir / (func .__name__ + "-expected." + ext ))
422
408
fig_test .savefig (test_image_path )
423
409
fig_ref .savefig (ref_image_path )
@@ -442,7 +428,7 @@ def _image_directories(func):
442
428
baseline_dir = module_path .parent / "baseline_images" / module_path .stem
443
429
result_dir = Path ().resolve () / "result_images" / module_path .stem
444
430
result_dir .mkdir (parents = True , exist_ok = True )
445
- return str ( baseline_dir ), str ( result_dir )
431
+ return baseline_dir , result_dir
446
432
447
433
448
434
@cbook .deprecated ("3.1" , alternative = "pytest.mark.backend" )
0 commit comments