2
2
import json
3
3
from pathlib import Path
4
4
5
- __all__ = ['diff_summary' , 'assert_existence' , 'replace_baseline_hash' , ' patch_summary' ]
5
+ __all__ = ['diff_summary' , 'assert_existence' , 'patch_summary' ]
6
6
7
7
8
8
class MatchError (Exception ):
9
9
pass
10
10
11
11
12
- def diff_summary (baseline , result , hash_library = None ):
13
- """Diff a pytest-mpl summary dictionary."""
14
- if hash_library and hash_library .exists ():
12
+ def diff_summary (baseline , result , baseline_hash_library = None , result_hash_library = None ):
13
+ """Diff a pytest-mpl summary dictionary.
14
+
15
+ Parameters
16
+ ----------
17
+ baseline : dict
18
+ Baseline pytest-mpl summary.
19
+ result : dict
20
+ Generated result pytest-mpl summary.
21
+ baseline_hash_library : Path, optional, default=None
22
+ Path to the baseline hash library.
23
+ Baseline hashes in the baseline summary are updated to these values
24
+ to handle different Matplotlib versions.
25
+ result_hash_library : Path, optional, default=None
26
+ Path to the "baseline" image hash library.
27
+ Result hashes in the baseline summary are updated to these values
28
+ to handle different Matplotlib versions.
29
+ """
30
+ if baseline_hash_library and baseline_hash_library .exists ():
15
31
# Load "correct" baseline hashes
16
- with open (hash_library , 'r' ) as f :
17
- hash_library = json .load (f )
32
+ with open (baseline_hash_library , 'r' ) as f :
33
+ baseline_hash_library = json .load (f )
34
+ if result_hash_library and result_hash_library .exists ():
35
+ # Load "correct" result hashes
36
+ with open (result_hash_library , 'r' ) as f :
37
+ result_hash_library = json .load (f )
18
38
19
39
# Get test names
20
40
baseline_tests = set (baseline .keys ())
@@ -31,9 +51,14 @@ def diff_summary(baseline, result, hash_library=None):
31
51
baseline_summary = baseline [test ]
32
52
result_summary = result [test ]
33
53
34
- # Swap the baseline hashes in the summary for the baseline hashes in the hash library
35
- if hash_library :
36
- baseline_summary = replace_baseline_hash (baseline_summary , hash_library [test ])
54
+ # Swap the baseline and result hashes in the summary
55
+ # for the corresponding hashes in each hash library
56
+ if baseline_hash_library :
57
+ baseline_summary = replace_hash (baseline_summary , 'baseline_hash' ,
58
+ baseline_hash_library [test ])
59
+ if result_hash_library :
60
+ baseline_summary = replace_hash (baseline_summary , 'result_hash' ,
61
+ result_hash_library [test ])
37
62
38
63
# Get keys of recorded items
39
64
baseline_keys = set (baseline_summary .keys ())
@@ -107,31 +132,26 @@ def patch_summary(summary, patch_file):
107
132
return summary
108
133
109
134
110
- def replace_baseline_hash (summary , new_baseline ):
111
- """Replace a baseline hash in a pytest-mpl summary with a different baseline.
112
-
113
- Result hashes which match the existing baseline are also updated.
135
+ def replace_hash (summary , hash_key , new_hash ):
136
+ """Replace a hash in a pytest-mpl summary with a different hash.
114
137
115
138
Parameters
116
139
----------
117
140
summary : dict
118
141
A single test from a pytest-mpl summary.
119
- new_baseline : str
120
- The new baseline.
142
+ hash_key : str
143
+ Key of the hash. Either `baseline_hash` or `result_hash`.
144
+ new_hash : str
145
+ The new hash.
121
146
"""
122
- assert isinstance (new_baseline , str )
123
- old_baseline = summary ['baseline_hash' ]
124
- if not isinstance (old_baseline , str ) or old_baseline == new_baseline :
147
+ assert isinstance (new_hash , str )
148
+ old_hash = summary [hash_key ]
149
+ if not isinstance (old_hash , str ) or old_hash == new_hash :
125
150
return summary # Either already correct or missing
126
151
127
- # If the old result hash matches the old baseline hash, also update the result hash
128
- old_result = summary ['result_hash' ]
129
- if isinstance (old_result , str ) and old_result == old_baseline :
130
- summary ['result_hash' ] = new_baseline
131
-
132
- # Update the baseline hash
133
- summary ['baseline_hash' ] = new_baseline
134
- summary ['status_msg' ] = summary ['status_msg' ].replace (old_baseline , new_baseline )
152
+ # Update the hash
153
+ summary [hash_key ] = new_hash
154
+ summary ['status_msg' ] = summary ['status_msg' ].replace (old_hash , new_hash )
135
155
136
156
return summary
137
157
0 commit comments