Skip to content

Commit fa630de

Browse files
committed
review action - hash library kernel
1 parent 7c35990 commit fa630de

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

pytest_mpl/plugin.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,12 @@ def __init__(self,
351351
emsg = f'Unrecognised hashing kernel {kernel!r} not supported.'
352352
raise ValueError(emsg)
353353
kernel = requested
354+
# Flag that the kernel has been user configured.
355+
self.kernel_default = False
354356
else:
355357
kernel = DEFAULT_KERNEL
358+
# Flag that the kernel has been configured by default.
359+
self.kernel_default = True
356360
# Create the kernel.
357361
self.kernel = kernel_factory[kernel](self)
358362

@@ -631,12 +635,15 @@ def compare_image_to_hash_library(self, item, fig, result_dir, summary=None):
631635
f'in the hash library {str(hash_library_filename)!r}.')
632636
pytest.fail(emsg)
633637
if kernel_name != self.kernel.name:
634-
# TODO: we could be lenient here by raising a warning and hot-swap to
635-
# use the hash library kernel, instead of forcing a test failure?
636-
emsg = (f'Hash library {str(hash_library_filename)!r} kernel '
637-
f'{kernel_name!r} does not match configured runtime '
638-
f'kernel {self.kernel.name!r}.')
639-
pytest.fail(emsg)
638+
if self.kernel_default:
639+
# Override the default kernel with the kernel configured
640+
# within the hash library.
641+
self.kernel = kernel_factory[kernel_name](self)
642+
else:
643+
emsg = (f'Hash library {str(hash_library_filename)!r} kernel '
644+
f'{kernel_name!r} does not match configured runtime '
645+
f'kernel {self.kernel.name!r}.')
646+
pytest.fail(emsg)
640647

641648
hash_name = self.generate_test_name(item)
642649
baseline_hash = hash_library.get(hash_name, None)

tests/test_pytest_mpl.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -492,14 +492,33 @@ def test_results_always(tmpdir):
492492
assert json_res[json_image_key] is None
493493

494494

495-
def test_phash(tmpdir):
495+
@pytest.mark.parametrize("cla", ["--mpl-kernel=phash", ""])
496+
def test_phash(tmpdir, cla):
497+
test_file = tmpdir.join("test.py").strpath
498+
with open(test_file, "w") as fo:
499+
fo.write(TEST_GENERATE)
500+
501+
# Filter out empty command-line-argument (cla) from command string.
502+
command = list(filter(None,
503+
["--mpl",
504+
cla,
505+
f"--mpl-hash-library={phash_library}",
506+
test_file]
507+
)
508+
)
509+
code = call_pytest(command)
510+
assert code == 0
511+
512+
513+
def test_phash__fail(tmpdir):
496514
test_file = tmpdir.join("test.py").strpath
497515
with open(test_file, "w") as fo:
498516
fo.write(TEST_GENERATE)
499517

500518
command = ["--mpl",
501-
"--mpl-kernel=phash",
519+
"--mpl-kernel=sha256",
502520
f"--mpl-hash-library={phash_library}",
503521
test_file]
504-
code = call_pytest(command)
505-
assert code == 0
522+
523+
emsg = "'phash' does not match configured runtime kernel 'sha256'"
524+
assert_pytest_fails_with(command, emsg)

0 commit comments

Comments
 (0)