Skip to content

Commit 2864ecf

Browse files
committed
Add mpl-hash-library INI option
1 parent 02eec0d commit 2864ecf

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

docs/configuration.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,15 +191,19 @@ File containing baseline hashes
191191
-------------------------------
192192
| **kwarg**: ``hash_library=<path>``
193193
| **CLI**: ``--mpl-hash-library=<path>``
194-
| **INI**: ---
194+
| **INI**: ``mpl-hash-library = <path>``
195195
| Default: *no hash comparison*
196196
197197
The file containing the baseline hashes that will be compared to the test figures.
198-
Both the kwarg option (``hash_library``) and the CLI option (``--mpl-hash-library``) are relative to the test file.
199-
In this case, the CLI option takes precedence over the kwarg option.
198+
The kwarg option (``hash_library``) is relative to the test file, while the INI option (``mpl-hash-library``) is relative to where pytest was run.
200199
The file must be a JSON file in the same format as one generated by ``--mpl-generate-hash-library``.
201200
If its directory does not exist, it will be created along with any missing parent directories.
202201

202+
.. attention::
203+
204+
For backwards compatibility, the CLI option (``--mpl-hash-library``) is relative to the test file.
205+
Also, the CLI option takes precedence over the kwarg option, but the kwarg option takes precedence over the INI option as usual.
206+
203207
Configuring this option disables baseline image comparison.
204208
If you want to enable both hash and baseline image comparison, which we call :doc:`"hybrid mode" <hybrid_mode>`, you must explicitly set the :ref:`baseline directory configuration option <baseline-dir>`.
205209

pytest_mpl/plugin.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,9 @@ def pytest_addoption(parser):
145145

146146
group.addoption("--mpl-baseline-relative", help="interpret the baseline directory as "
147147
"relative to the test location.", action="store_true")
148-
group.addoption('--mpl-hash-library',
149-
help="json library of image hashes, relative to "
150-
"location where py.test is run", action='store')
148+
mpl_hash_library_help = "json library of image hashes, relative to location where py.test is run"
149+
group.addoption('--mpl-hash-library', help=mpl_hash_library_help, action='store')
150+
parser.addini("mpl-hash-library", help=mpl_hash_library_help)
151151
group.addoption('--mpl-generate-summary', action='store',
152152
help="Generate a summary report of any failed tests"
153153
", in --mpl-results-path. The type of the report should be "
@@ -192,7 +192,8 @@ def pytest_configure(config):
192192
generate_dir = config.getoption("--mpl-generate-path")
193193
generate_hash_lib = config.getoption("--mpl-generate-hash-library")
194194
results_dir = config.getoption("--mpl-results-path") or config.getini("mpl-results-path")
195-
hash_library = config.getoption("--mpl-hash-library")
195+
hash_library = config.getoption("--mpl-hash-library") or config.getini("mpl-hash-library")
196+
_hash_library_from_cli = bool(config.getoption("--mpl-hash-library")) # for backwards compatibility
196197
generate_summary = config.getoption("--mpl-generate-summary")
197198
results_always = (config.getoption("--mpl-results-always") or
198199
config.getini("mpl-results-always"))
@@ -218,6 +219,10 @@ def pytest_configure(config):
218219
baseline_dir = os.path.abspath(generate_dir)
219220
if results_dir is not None:
220221
results_dir = os.path.abspath(results_dir)
222+
if hash_library is not None:
223+
# For backwards compatibility, don't make absolute if set via CLI option
224+
if not _hash_library_from_cli:
225+
hash_library = os.path.abspath(hash_library)
221226

222227
default_style = (config.getoption("--mpl-default-style") or
223228
config.getini("mpl-default-style") or
@@ -238,7 +243,8 @@ def pytest_configure(config):
238243
results_always=results_always,
239244
use_full_test_name=use_full_test_name,
240245
default_style=default_style,
241-
default_tolerance=default_tolerance))
246+
default_tolerance=default_tolerance,
247+
_hash_library_from_cli=_hash_library_from_cli))
242248

243249
else:
244250

@@ -297,14 +303,16 @@ def __init__(self,
297303
results_always=False,
298304
use_full_test_name=False,
299305
default_style='classic',
300-
default_tolerance=2
306+
default_tolerance=2,
307+
_hash_library_from_cli=False, # for backwards compatibility
301308
):
302309
self.config = config
303310
self.baseline_dir = baseline_dir
304311
self.baseline_relative_dir = path_is_not_none(baseline_relative_dir)
305312
self.generate_dir = path_is_not_none(generate_dir)
306313
self.results_dir = path_is_not_none(results_dir)
307314
self.hash_library = path_is_not_none(hash_library)
315+
self._hash_library_from_cli = _hash_library_from_cli # for backwards compatibility
308316
self.generate_hash_library = path_is_not_none(generate_hash_library)
309317
if generate_summary:
310318
generate_summary = {i.lower() for i in generate_summary.split(',')}
@@ -637,7 +645,10 @@ def compare_image_to_hash_library(self, item, fig, result_dir, summary=None):
637645
# Use hash library name of current test as results hash library name
638646
self.results_hash_library_name = Path(compare.kwargs.get("hash_library", "")).name
639647

640-
hash_library_filename = self.hash_library or compare.kwargs.get('hash_library', None)
648+
# Order of precedence for hash library: CLI, kwargs, INI (for backwards compatibility)
649+
hash_library_filename = compare.kwargs.get("hash_library", None) or self.hash_library
650+
if self._hash_library_from_cli: # for backwards compatibility
651+
hash_library_filename = self.hash_library
641652
hash_library_filename = (Path(item.fspath).parent / hash_library_filename).absolute()
642653

643654
if not Path(hash_library_filename).exists():

0 commit comments

Comments
 (0)