Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cdfbf4e

Browse files
Making both package and generation flag work simultaneously
1 parent bb028ca commit cdfbf4e

6 files changed

Lines changed: 81 additions & 25 deletions

File tree

lib/matplotlib/testing/conftest.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def pytest_configure(config):
1717
("markers", "baseline_images: Compare output against references."),
1818
("markers", "pytz: Tests that require pytz to be installed."),
1919
("markers", "network: Tests that reach out to the network."),
20-
("markers", "generate_images: Flag to generate matplotlib baseline images."),
20+
("markers", "generate_images: Flag to generate baseline images."),
2121
("filterwarnings", "error"),
2222
]:
2323
config.addinivalue_line(key, value)
@@ -152,7 +152,9 @@ def pytest_addoption(parser):
152152

153153
def pytest_collection_modifyitems(config, items):
154154
if config.getoption("--generate_images"):
155-
skip_non_matplotlib_baseline_image_generation_tests = pytest.mark.skip(reason="No need to run non image generation tests")
155+
skip_non_baseline_img_gen_tests \
156+
= pytest.mark.skip(reason="No need to run non "
157+
"image generation tests")
156158
for item in items:
157-
if "generate_images" not in item.keywords:
158-
item.add_marker(skip_non_matplotlib_baseline_image_generation_tests)
159+
if "generate_images" not in item.keywords:
160+
item.add_marker(skip_non_baseline_img_gen_tests)

lib/matplotlib/testing/decorators.py

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,9 @@
66
from pathlib import Path
77
import shutil
88
import string
9-
import subprocess
109
import sys
1110
import unittest
1211
import warnings
13-
import pytest
14-
try:
15-
from contextlib import nullcontext
16-
except ImportError:
17-
from contextlib import ExitStack as nullcontext # Py3.6.
1812

1913
import matplotlib.style
2014
import matplotlib.units
@@ -205,7 +199,6 @@ def copy_baseline(self, baseline, extension):
205199
f"{orig_expected_path}") from err
206200
return expected_fname
207201

208-
209202
def create_baseline(self, baseline, extension):
210203
src_path = self.result_dir / baseline
211204
orig_src_path = src_path.with_suffix(f'.{extension}')
@@ -224,12 +217,11 @@ def create_baseline(self, baseline, extension):
224217
raise ValueError("Failed to put the image in the right place")
225218
return dest_path
226219

227-
228-
def compare(self, idx, baseline, extension, *, _lock=False, generating=False):
220+
def compare(self, idx, baseline, extension, *, _lock=False,
221+
generating=False):
229222
__tracebackhide__ = True
230223
fignum = plt.get_fignums()[idx]
231224
fig = plt.figure(fignum)
232-
233225
if self.remove_text:
234226
remove_ticks_and_titles(fig)
235227

@@ -248,8 +240,8 @@ def compare(self, idx, baseline, extension, *, _lock=False, generating=False):
248240
self.create_baseline(baseline, extension)
249241
else:
250242
expected_path = self.copy_baseline(baseline, extension)
251-
_raise_on_image_difference(expected_path, actual_path, self.tol)
252-
243+
_raise_on_image_difference(expected_path, actual_path,
244+
self.tol)
253245

254246

255247
def _pytest_image_comparison(baseline_images, extensions, tol,
@@ -307,7 +299,8 @@ def wrapper(*args, extension, request, **kwargs):
307299
"Test generated {} images but there are {} baseline images"
308300
.format(len(plt.get_fignums()), len(our_baseline_images)))
309301
for idx, baseline in enumerate(our_baseline_images):
310-
img.compare(idx, baseline, extension, _lock=needs_lock, generating=generate_images)
302+
img.compare(idx, baseline, extension, _lock=needs_lock,
303+
generating=generate_images)
311304

312305
parameters = list(old_sig.parameters.values())
313306
if 'extension' not in old_sig.parameters:
@@ -515,9 +508,34 @@ def _image_directories(func):
515508
doesn't exist.
516509
"""
517510
module_path = Path(sys.modules[func.__module__].__file__)
518-
baseline_dir = (module_path.parent /
519-
"baseline_images" /
520-
module_path.stem)
511+
if func.__module__.startswith("matplotlib."):
512+
try:
513+
import matplotlib_baseline_images
514+
baseline_dir = (Path(matplotlib_baseline_images.__file__).parent /
515+
module_path.stem)
516+
except:
517+
if (Path(__file__).parent / 'baseline_images').exists():
518+
baseline_dir = (module_path.parent /
519+
"baseline_images" /
520+
module_path.stem)
521+
else:
522+
raise ImportError("Not able to import"
523+
"matplotlib_baseline_images")
524+
elif func.__module__.startswith("mpl_toolkits."):
525+
try:
526+
import mpl_toolkits_baseline_images
527+
baseline_file = mpl_toolkits_baseline_images.__file__
528+
baseline_dir = (Path(baseline_file).parent /
529+
module_path.stem)
530+
except:
531+
if (Path(mpl_toolkits_baseline_images.__file__).parent /
532+
module_path.stem).exists():
533+
baseline_dir = (module_path.parent /
534+
"baseline_images" /
535+
module_path.stem)
536+
else:
537+
raise ImportError("Not able to import "
538+
"mpl_toolkits_baseline_images")
521539
result_dir = Path().resolve() / "result_images" / module_path.stem
522540
result_dir.mkdir(parents=True, exist_ok=True)
523541
return baseline_dir, result_dir

lib/matplotlib/tests/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,14 @@
11
# Check for the matplotlib_baseline_images.
2+
from pathlib import Path
3+
4+
5+
try:
6+
import matplotlib_baseline_images
7+
except:
8+
if not (Path(__file__).parent / 'baseline_images').exists():
9+
raise ImportError(
10+
'The baseline image directory does not exist. '
11+
'This is most likely because the test data is not installed '
12+
'and baseline image directory is absent. You may need to'
13+
'install matplotlib_baseline_images to get the test data.'
14+
)

lib/mpl_toolkits/tests/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from pathlib import Path
2+
3+
try:
4+
import matplotlib_baseline_images
5+
except:
6+
if not (Path(__file__).parent / "baseline_images").exists():
7+
raise ImportError(
8+
'The baseline image directory does not exist. '
9+
'This is most likely because the test data is not installed '
10+
'and baseline image directory is absent. You may need to '
11+
'install matplotlib_baseline_images to get the test data.'
12+
)

lib/mpl_toolkits/tests/conftest.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,3 @@
44
pytest_unconfigure,
55
pytest_collection_modifyitems,
66
pytest_addoption)
7-

tools/triage_tests.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,22 @@
3737
# order to find the source, we need to search for a match in one of
3838
# these two places.
3939

40-
BASELINE_IMAGES = [
41-
Path('lib/matplotlib/tests/baseline_images'),
42-
Path('lib/mpl_toolkits/tests/baseline_images'),
43-
]
40+
try:
41+
import matplotlib_baseline_images
42+
import mpl_toolkits_baseline_images
43+
BASELINE_IMAGES = [
44+
(Path(matplotlib_baseline_images.__file__).parent),
45+
(Path(mpl_toolkits_baseline_images.__file__).parent),
46+
]
47+
except:
48+
if not (Path('lib/matplotlib/tests/baseline_images')).exists()\
49+
and not (Path('lib/mpl_toolkits/tests/baseline_images')).exists():
50+
raise ImportError("Not able to import baseline images packages")
51+
else:
52+
BASELINE_IMAGES = [
53+
Path('lib/matplotlib/tests/baseline_images'),
54+
Path('lib/mpl_toolkits/tests/baseline_images'),
55+
]
4456

4557
# Non-png image extensions
4658

0 commit comments

Comments
 (0)