44import subprocess
55from pathlib import Path
66
7- from helpers import assert_existence , diff_summary
7+ import matplotlib
8+ import matplotlib .ft2font
9+ from packaging .version import Version
10+
11+ from .helpers import assert_existence , diff_summary , patch_summary
12+
13+ # Handle Matplotlib and FreeType versions
14+ MPL_VERSION = Version (matplotlib .__version__ )
15+ FTV = matplotlib .ft2font .__freetype_version__ .replace ('.' , '' )
16+ VERSION_ID = f"mpl{ MPL_VERSION .major } { MPL_VERSION .minor } _ft{ FTV } "
17+ HASH_LIBRARY = Path (__file__ ).parent / 'hashes' / (VERSION_ID + ".json" )
18+ HASH_LIBRARY_FLAG = rf'--mpl-hash-library={ HASH_LIBRARY } '
819
920TEST_FILE = Path (__file__ ).parent / 'subtest.py'
1021
11- # Set to True to replace existing baseline summaries with current result
12- UPDATE_BASELINE_SUMMARIES = True
22+ # Global settings to update baselines when running pytest
23+ # Note: when updating baseline make sure you don't commit "fixes"
24+ # for tests that are expected to fail
25+ # (See also `run_subtest` argument `update_baseline` and `update_summary`.)
26+ UPDATE_BASELINE = False # baseline images and hashes
27+ UPDATE_SUMMARY = False # baseline summaries
1328
1429
15- def run_subtest (baseline_summary , tmp_path , args , summaries = None , xfail = True ):
30+ def run_subtest (baseline_summary_name , tmp_path , args , summaries = None , xfail = True ,
31+ update_baseline = UPDATE_BASELINE , update_summary = UPDATE_SUMMARY ):
1632 """ Run pytest (within pytest) and check JSON summary report.
1733
1834 Parameters
1935 ----------
20- baseline_summary : str
36+ baseline_summary_name : str
2137 String of the filename without extension for the baseline summary.
2238 tmp_path : pathlib.Path
2339 Path of a temporary directory to store results.
@@ -42,10 +58,19 @@ def run_subtest(baseline_summary, tmp_path, args, summaries=None, xfail=True):
4258 pytest_args = [sys .executable , '-m' , 'pytest' , str (TEST_FILE )]
4359 mpl_args = ['--mpl' , rf'--mpl-results-path={ results_path .as_posix ()} ' ,
4460 f'--mpl-generate-summary={ summaries } ' ]
61+ if update_baseline :
62+ mpl_args += ['--mpl-generate-path=baseline' ]
63+ if HASH_LIBRARY .exists ():
64+ mpl_args += [rf'--mpl-generate-hash-library={ HASH_LIBRARY } ' ]
4565
4666 # Run the test and record exit status
4767 status = subprocess .call (pytest_args + mpl_args + args )
4868
69+ # If updating baseline, don't check summaries
70+ if update_baseline :
71+ assert status == 0
72+ return
73+
4974 # Ensure exit status is as expected
5075 if xfail :
5176 assert status != 0
@@ -54,18 +79,23 @@ def run_subtest(baseline_summary, tmp_path, args, summaries=None, xfail=True):
5479
5580 # Load summaries
5681 baseline_path = Path (__file__ ).parent / 'summaries'
57- baseline_file = baseline_path / (baseline_summary + '.json' )
82+ baseline_file = baseline_path / (baseline_summary_name + '.json' )
83+ results_file = results_path / 'results.json'
84+ if update_summary :
85+ shutil .copy (results_file , baseline_file )
5886 with open (baseline_file , 'r' ) as f :
5987 baseline_summary = json .load (f )
60- results_file = results_path / 'results.json'
6188 with open (results_file , 'r' ) as f :
6289 result_summary = json .load (f )
6390
64- if UPDATE_BASELINE_SUMMARIES :
65- shutil .copy (results_file , baseline_file )
91+ # Apply version specific patches
92+ patch = baseline_path / (baseline_summary_name + f'_{ VERSION_ID } .patch.json' )
93+ if patch .exists ():
94+ baseline_summary = patch_summary (baseline_summary , patch )
95+ # Note: version specific hashes should be handled by diff_summary instead
6696
6797 # Compare summaries
68- diff_summary (baseline_summary , result_summary )
98+ diff_summary (baseline_summary , result_summary , hash_library = HASH_LIBRARY )
6999
70100 # Ensure reported images exist
71101 assert_existence (result_summary , path = results_path )
0 commit comments