22import json
33from pathlib import Path
44
5- __all__ = ['diff_summary' , 'assert_existence' , 'replace_baseline_hash' , ' patch_summary' ]
5+ __all__ = ['diff_summary' , 'assert_existence' , 'patch_summary' ]
66
77
88class MatchError (Exception ):
99 pass
1010
1111
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 ():
1531 # 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 )
1838
1939 # Get test names
2040 baseline_tests = set (baseline .keys ())
@@ -31,9 +51,14 @@ def diff_summary(baseline, result, hash_library=None):
3151 baseline_summary = baseline [test ]
3252 result_summary = result [test ]
3353
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 ])
3762
3863 # Get keys of recorded items
3964 baseline_keys = set (baseline_summary .keys ())
@@ -107,31 +132,26 @@ def patch_summary(summary, patch_file):
107132 return summary
108133
109134
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.
114137
115138 Parameters
116139 ----------
117140 summary : dict
118141 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.
121146 """
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 :
125150 return summary # Either already correct or missing
126151
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 )
135155
136156 return summary
137157
0 commit comments