@@ -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,38 +168,33 @@ 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'
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 = orig_expected_path . with_suffix ( '.pdf' )
191
184
expected_fname = make_test_filename (
192
- os .path .join (self .result_dir ,
193
- os .path .basename (orig_expected_fname )),
194
- 'expected' )
185
+ self .result_dir / orig_expected_path .name , 'expected' )
195
186
try :
196
187
# os.symlink errors if the target already exists.
197
188
with contextlib .suppress (OSError ):
198
189
os .remove (expected_fname )
199
190
try :
200
- os .symlink (orig_expected_fname , expected_fname )
191
+ os .symlink (orig_expected_path , expected_fname )
201
192
except OSError : # On Windows, symlink *may* be unavailable.
202
- shutil .copyfile (orig_expected_fname , expected_fname )
193
+ shutil .copyfile (orig_expected_path , expected_fname )
203
194
except OSError :
204
195
raise ImageComparisonFailure (
205
196
f"Missing baseline image { expected_fname } because the "
206
- f"following file cannot be accessed: { orig_expected_fname } " )
197
+ f"following file cannot be accessed: { orig_expected_path } " )
207
198
return expected_fname
208
199
209
200
def compare (self , idx , baseline , extension ):
@@ -214,17 +205,16 @@ def compare(self, idx, baseline, extension):
214
205
if self .remove_text :
215
206
remove_ticks_and_titles (fig )
216
207
217
- actual_fname = (
218
- os .path .join (self .result_dir , baseline ) + '.' + extension )
208
+ actual_path = (self .result_dir / baseline ).with_suffix (f'.{ extension } ' )
219
209
kwargs = self .savefig_kwargs .copy ()
220
210
if extension == 'pdf' :
221
211
kwargs .setdefault ('metadata' ,
222
212
{'Creator' : None , 'Producer' : None ,
223
213
'CreationDate' : None })
224
- fig .savefig (actual_fname , ** kwargs )
214
+ fig .savefig (actual_path , ** kwargs )
225
215
226
- expected_fname = self .copy_baseline (baseline , extension )
227
- _raise_on_image_difference (expected_fname , actual_fname , self .tol )
216
+ expected_path = self .copy_baseline (baseline , extension )
217
+ _raise_on_image_difference (expected_path , actual_path , self .tol )
228
218
229
219
230
220
def _pytest_image_comparison (baseline_images , extensions , tol ,
@@ -254,9 +244,8 @@ def decorator(func):
254
244
@functools .wraps (func )
255
245
def wrapper (* args , ** kwargs ):
256
246
__tracebackhide__ = True
257
- img = _ImageComparisonBase (tol = tol , remove_text = remove_text ,
247
+ img = _ImageComparisonBase (func , tol = tol , remove_text = remove_text ,
258
248
savefig_kwargs = savefig_kwargs )
259
- img .delayed_init (func )
260
249
matplotlib .testing .set_font_settings_for_testing ()
261
250
func (* args , ** kwargs )
262
251
@@ -395,7 +384,7 @@ def test_plot(fig_test, fig_ref):
395
384
def decorator (func ):
396
385
import pytest
397
386
398
- _ , result_dir = map ( Path , _image_directories (func ) )
387
+ _ , result_dir = _image_directories (func )
399
388
400
389
if len (inspect .signature (func ).parameters ) == 2 :
401
390
# Free-standing function.
@@ -404,9 +393,8 @@ def wrapper(ext):
404
393
fig_test = plt .figure ("test" )
405
394
fig_ref = plt .figure ("reference" )
406
395
func (fig_test , fig_ref )
407
- test_image_path = str (
408
- result_dir / (func .__name__ + "." + ext ))
409
- ref_image_path = str (
396
+ test_image_path = result_dir / (func .__name__ + "." + ext )
397
+ ref_image_path = (
410
398
result_dir / (func .__name__ + "-expected." + ext ))
411
399
fig_test .savefig (test_image_path )
412
400
fig_ref .savefig (ref_image_path )
@@ -420,9 +408,8 @@ def wrapper(self, ext):
420
408
fig_test = plt .figure ("test" )
421
409
fig_ref = plt .figure ("reference" )
422
410
func (self , fig_test , fig_ref )
423
- test_image_path = str (
424
- result_dir / (func .__name__ + "." + ext ))
425
- ref_image_path = str (
411
+ test_image_path = result_dir / (func .__name__ + "." + ext )
412
+ ref_image_path = (
426
413
result_dir / (func .__name__ + "-expected." + ext ))
427
414
fig_test .savefig (test_image_path )
428
415
fig_ref .savefig (ref_image_path )
@@ -447,7 +434,7 @@ def _image_directories(func):
447
434
baseline_dir = module_path .parent / "baseline_images" / module_path .stem
448
435
result_dir = Path ().resolve () / "result_images" / module_path .stem
449
436
result_dir .mkdir (parents = True , exist_ok = True )
450
- return str ( baseline_dir ), str ( result_dir )
437
+ return baseline_dir , result_dir
451
438
452
439
453
440
@cbook .deprecated ("3.1" , alternative = "pytest.mark.backend" )
0 commit comments