Skip to content

Commit 7e95ae8

Browse files
committed
Hash libraries for baseline and result
1 parent 2854e03 commit 7e95ae8

File tree

2 files changed

+49
-27
lines changed

2 files changed

+49
-27
lines changed

tests/subtests/helpers.py

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,39 @@
22
import json
33
from 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

88
class 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

tests/subtests/test_subtest.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
FTV = matplotlib.ft2font.__freetype_version__.replace('.', '')
1616
VERSION_ID = f"mpl{MPL_VERSION.major}{MPL_VERSION.minor}_ft{FTV}"
1717
HASH_LIBRARY = Path(__file__).parent / 'hashes' / (VERSION_ID + ".json")
18+
RESULT_LIBRARY = Path(__file__).parent / 'result_hashes' / (VERSION_ID + ".json")
1819
HASH_LIBRARY_FLAG = rf'--mpl-hash-library={HASH_LIBRARY}'
1920

2021
TEST_FILE = Path(__file__).parent / 'subtest.py'
@@ -95,7 +96,8 @@ def run_subtest(baseline_summary_name, tmp_path, args, summaries=None, xfail=Tru
9596
# Note: version specific hashes should be handled by diff_summary instead
9697

9798
# Compare summaries
98-
diff_summary(baseline_summary, result_summary, hash_library=HASH_LIBRARY)
99+
diff_summary(baseline_summary, result_summary,
100+
baseline_hash_library=HASH_LIBRARY, result_hash_library=RESULT_LIBRARY)
99101

100102
# Ensure reported images exist
101103
assert_existence(result_summary, path=results_path)

0 commit comments

Comments
 (0)