Skip to content

Backport #15589 and #16693, fixes for check_figures_equal #16797

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 17, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 40 additions & 23 deletions lib/matplotlib/testing/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
from pathlib import Path
import shutil
import string
import sys
import unittest
import warnings
Expand All @@ -17,7 +18,7 @@
from matplotlib import ft2font
from matplotlib import pyplot as plt
from matplotlib import ticker
from . import is_called_from_pytest

from .compare import comparable_formats, compare_images, make_test_filename
from .exceptions import ImageComparisonFailure

Expand Down Expand Up @@ -381,34 +382,50 @@ def test_plot(fig_test, fig_ref):
fig_test.subplots().plot([1, 3, 5])
fig_ref.subplots().plot([0, 1, 2], [1, 3, 5])
"""
POSITIONAL_OR_KEYWORD = inspect.Parameter.POSITIONAL_OR_KEYWORD
ALLOWED_CHARS = set(string.digits + string.ascii_letters + '_-[]()')
KEYWORD_ONLY = inspect.Parameter.KEYWORD_ONLY
def decorator(func):
import pytest

_, result_dir = _image_directories(func)
old_sig = inspect.signature(func)

@pytest.mark.parametrize("ext", extensions)
def wrapper(*args, ext, **kwargs):
fig_test = plt.figure("test")
fig_ref = plt.figure("reference")
func(*args, fig_test=fig_test, fig_ref=fig_ref, **kwargs)
test_image_path = result_dir / (func.__name__ + "." + ext)
ref_image_path = result_dir / (
func.__name__ + "-expected." + ext
)
fig_test.savefig(test_image_path)
fig_ref.savefig(ref_image_path)
_raise_on_image_difference(
ref_image_path, test_image_path, tol=tol
)

sig = inspect.signature(func)
new_sig = sig.replace(
parameters=([param
for param in sig.parameters.values()
if param.name not in {"fig_test", "fig_ref"}]
+ [inspect.Parameter("ext", POSITIONAL_OR_KEYWORD)])
)
def wrapper(*args, **kwargs):
ext = kwargs['ext']
if 'ext' not in old_sig.parameters:
kwargs.pop('ext')
request = kwargs['request']
if 'request' not in old_sig.parameters:
kwargs.pop('request')

file_name = "".join(c for c in request.node.name
if c in ALLOWED_CHARS)
try:
fig_test = plt.figure("test")
fig_ref = plt.figure("reference")
func(*args, fig_test=fig_test, fig_ref=fig_ref, **kwargs)
test_image_path = result_dir / (file_name + "." + ext)
ref_image_path = result_dir / (file_name + "-expected." + ext)
fig_test.savefig(test_image_path)
fig_ref.savefig(ref_image_path)
_raise_on_image_difference(
ref_image_path, test_image_path, tol=tol
)
finally:
plt.close(fig_test)
plt.close(fig_ref)

parameters = [
param
for param in old_sig.parameters.values()
if param.name not in {"fig_test", "fig_ref"}
]
if 'ext' not in old_sig.parameters:
parameters += [inspect.Parameter("ext", KEYWORD_ONLY)]
if 'request' not in old_sig.parameters:
parameters += [inspect.Parameter("request", KEYWORD_ONLY)]
new_sig = old_sig.replace(parameters=parameters)
wrapper.__signature__ = new_sig

# reach a bit into pytest internals to hoist the marks from
Expand Down