4
4
import subprocess
5
5
from pathlib import Path
6
6
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 } '
8
19
9
20
TEST_FILE = Path (__file__ ).parent / 'subtest.py'
10
21
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
13
28
14
29
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 ):
16
32
""" Run pytest (within pytest) and check JSON summary report.
17
33
18
34
Parameters
19
35
----------
20
- baseline_summary : str
36
+ baseline_summary_name : str
21
37
String of the filename without extension for the baseline summary.
22
38
tmp_path : pathlib.Path
23
39
Path of a temporary directory to store results.
@@ -42,10 +58,19 @@ def run_subtest(baseline_summary, tmp_path, args, summaries=None, xfail=True):
42
58
pytest_args = [sys .executable , '-m' , 'pytest' , str (TEST_FILE )]
43
59
mpl_args = ['--mpl' , rf'--mpl-results-path={ results_path .as_posix ()} ' ,
44
60
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 } ' ]
45
65
46
66
# Run the test and record exit status
47
67
status = subprocess .call (pytest_args + mpl_args + args )
48
68
69
+ # If updating baseline, don't check summaries
70
+ if update_baseline :
71
+ assert status == 0
72
+ return
73
+
49
74
# Ensure exit status is as expected
50
75
if xfail :
51
76
assert status != 0
@@ -54,18 +79,23 @@ def run_subtest(baseline_summary, tmp_path, args, summaries=None, xfail=True):
54
79
55
80
# Load summaries
56
81
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 )
58
86
with open (baseline_file , 'r' ) as f :
59
87
baseline_summary = json .load (f )
60
- results_file = results_path / 'results.json'
61
88
with open (results_file , 'r' ) as f :
62
89
result_summary = json .load (f )
63
90
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
66
96
67
97
# Compare summaries
68
- diff_summary (baseline_summary , result_summary )
98
+ diff_summary (baseline_summary , result_summary , hash_library = HASH_LIBRARY )
69
99
70
100
# Ensure reported images exist
71
101
assert_existence (result_summary , path = results_path )
0 commit comments