4
4
5
5
import importlib
6
6
import inspect
7
- import os
8
7
import string
8
+ from pathlib import Path
9
9
10
10
from pygmt .exceptions import GMTImageComparisonFailure
11
11
from pygmt .io import load_dataarray
@@ -39,6 +39,7 @@ def check_figures_equal(*, extensions=("png",), tol=0.0, result_dir="result_imag
39
39
>>> import pytest
40
40
>>> import shutil
41
41
>>> from pygmt import Figure
42
+ >>> from pathlib import Path
42
43
43
44
>>> @check_figures_equal(result_dir="tmp_result_images")
44
45
... def test_check_figures_equal():
@@ -50,7 +51,7 @@ def check_figures_equal(*, extensions=("png",), tol=0.0, result_dir="result_imag
50
51
... )
51
52
... return fig_ref, fig_test
52
53
>>> test_check_figures_equal()
53
- >>> assert len(os.listdir( "tmp_result_images")) == 0
54
+ >>> assert len(list(Path( "tmp_result_images").iterdir() )) == 0
54
55
>>> shutil.rmtree(path="tmp_result_images") # cleanup folder if tests pass
55
56
56
57
>>> @check_figures_equal(result_dir="tmp_result_images")
@@ -63,12 +64,9 @@ def check_figures_equal(*, extensions=("png",), tol=0.0, result_dir="result_imag
63
64
>>> with pytest.raises(GMTImageComparisonFailure):
64
65
... test_check_figures_unequal()
65
66
>>> for suffix in ["", "-expected", "-failed-diff"]:
66
- ... assert os.path.exists(
67
- ... os.path.join(
68
- ... "tmp_result_images",
69
- ... f"test_check_figures_unequal{suffix}.png",
70
- ... )
71
- ... )
67
+ ... assert (
68
+ ... Path("tmp_result_images") / f"test_check_figures_unequal{suffix}.png"
69
+ ... ).exists()
72
70
>>> shutil.rmtree(path="tmp_result_images") # cleanup folder if tests pass
73
71
"""
74
72
allowed_chars = set (string .digits + string .ascii_letters + "_-[]()" )
@@ -78,7 +76,7 @@ def decorator(func):
78
76
import pytest
79
77
from matplotlib .testing .compare import compare_images
80
78
81
- os . makedirs (result_dir , exist_ok = True )
79
+ Path (result_dir ). mkdir ( parents = True , exist_ok = True )
82
80
old_sig = inspect .signature (func )
83
81
84
82
@pytest .mark .parametrize ("ext" , extensions )
@@ -93,8 +91,8 @@ def wrapper(*args, ext="png", request=None, **kwargs):
93
91
file_name = func .__name__
94
92
try :
95
93
fig_ref , fig_test = func (* args , ** kwargs )
96
- ref_image_path = os . path . join (result_dir , f"{ file_name } -expected.{ ext } " )
97
- test_image_path = os . path . join (result_dir , f"{ file_name } .{ ext } " )
94
+ ref_image_path = Path (result_dir ) / f"{ file_name } -expected.{ ext } "
95
+ test_image_path = Path (result_dir ) / f"{ file_name } .{ ext } "
98
96
fig_ref .savefig (ref_image_path )
99
97
fig_test .savefig (test_image_path )
100
98
@@ -107,11 +105,11 @@ def wrapper(*args, ext="png", request=None, **kwargs):
107
105
in_decorator = True ,
108
106
)
109
107
if err is None : # Images are the same
110
- os . remove ( ref_image_path )
111
- os . remove ( test_image_path )
108
+ ref_image_path . unlink ( )
109
+ test_image_path . unlink ( )
112
110
else : # Images are not the same
113
111
for key in ["actual" , "expected" , "diff" ]:
114
- err [key ] = os . path . relpath (err [key ])
112
+ err [key ] = Path (err [key ]). relative_to ( "." )
115
113
raise GMTImageComparisonFailure (
116
114
f"images not close (RMS { err ['rms' ]:.3f} ):\n "
117
115
f"\t { err ['actual' ]} \n "
0 commit comments