Skip to content

Commit 7524344

Browse files
authored
Merge pull request #100 from Cadair/cleanup_upgrade
Remove support for mpl 1.5 and python<3.6
2 parents 8d89d7f + 9613d02 commit 7524344

12 files changed

+47
-485
lines changed

pytest_mpl/classic.mplstyle

Lines changed: 0 additions & 419 deletions
This file was deleted.

pytest_mpl/plugin.py

Lines changed: 39 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -28,31 +28,21 @@
2828
#
2929
# https://github.com/astrofrog/wcsaxes
3030

31-
from functools import wraps
32-
33-
import contextlib
3431
import io
3532
import os
36-
import sys
3733
import json
3834
import shutil
35+
import hashlib
3936
import inspect
4037
import tempfile
4138
import warnings
42-
import hashlib
43-
from distutils.version import LooseVersion
39+
import contextlib
4440
from pathlib import Path
41+
from functools import wraps
42+
from urllib.request import urlopen
4543

4644
import pytest
4745

48-
if sys.version_info[0] == 2:
49-
from urllib import urlopen
50-
string_types = basestring # noqa
51-
else:
52-
from urllib.request import urlopen
53-
string_types = str
54-
55-
5646
SHAPE_MISMATCH_ERROR = """Error: Image dimensions did not match.
5747
Expected shape: {expected_shape}
5848
{expected_path}
@@ -74,11 +64,11 @@ def _download_file(baseline, filename):
7464
else:
7565
raise Exception("Could not download baseline image from any of the "
7666
"available URLs")
77-
result_dir = tempfile.mkdtemp()
78-
filename = os.path.join(result_dir, 'downloaded')
79-
with open(filename, 'wb') as tmpfile:
67+
result_dir = Path(tempfile.mkdtemp())
68+
filename = result_dir / 'downloaded'
69+
with open(str(filename), 'wb') as tmpfile:
8070
tmpfile.write(content)
81-
return filename
71+
return Path(filename)
8272

8373

8474
def _hash_file(in_stream):
@@ -212,6 +202,10 @@ def get_marker(item, marker_name):
212202
return item.keywords.get(marker_name)
213203

214204

205+
def path_is_not_none(apath):
206+
return Path(apath) if apath is not None else apath
207+
208+
215209
class ImageComparison(object):
216210

217211
def __init__(self,
@@ -225,13 +219,13 @@ def __init__(self,
225219
):
226220
self.config = config
227221
self.baseline_dir = baseline_dir
228-
self.baseline_relative_dir = baseline_relative_dir
229-
self.generate_dir = generate_dir
230-
self.results_dir = results_dir
231-
self.hash_library = hash_library
232-
self.generate_hash_library = generate_hash_library
233-
if self.results_dir and not os.path.exists(self.results_dir):
234-
os.mkdir(self.results_dir)
222+
self.baseline_relative_dir = path_is_not_none(baseline_relative_dir)
223+
self.generate_dir = path_is_not_none(generate_dir)
224+
self.results_dir = path_is_not_none(results_dir)
225+
self.hash_library = path_is_not_none(hash_library)
226+
self.generate_hash_library = path_is_not_none(generate_hash_library)
227+
if self.results_dir and not self.results_dir.exists():
228+
self.results_dir.mkdir()
235229

236230
# We need global state to store all the hashes generated over the run
237231
self._generated_hash_library = {}
@@ -261,7 +255,7 @@ def make_results_dir(self, item):
261255
"""
262256
Generate the directory to put the results in.
263257
"""
264-
return tempfile.mkdtemp(dir=self.results_dir)
258+
return Path(tempfile.mkdtemp(dir=self.results_dir))
265259

266260
def get_baseline_directory(self, item):
267261
"""
@@ -274,21 +268,19 @@ def get_baseline_directory(self, item):
274268
baseline_dir = compare.kwargs.get('baseline_dir', None)
275269
if baseline_dir is None:
276270
if self.baseline_dir is None:
277-
baseline_dir = os.path.join(os.path.dirname(item.fspath.strpath), 'baseline')
271+
baseline_dir = Path(item.fspath).parent / 'baseline'
278272
else:
279273
if self.baseline_relative_dir:
280274
# baseline dir is relative to the current test
281-
baseline_dir = os.path.join(
282-
os.path.dirname(item.fspath.strpath),
283-
self.baseline_relative_dir
284-
)
275+
baseline_dir = Path(item.fspath).parent / self.baseline_relative_dir
285276
else:
286277
# baseline dir is relative to where pytest was run
287278
baseline_dir = self.baseline_dir
288279

289-
baseline_remote = baseline_dir.startswith(('http://', 'https://'))
280+
baseline_remote = (isinstance(baseline_dir, str) and # noqa
281+
baseline_dir.startswith(('http://', 'https://')))
290282
if not baseline_remote:
291-
return os.path.join(os.path.dirname(item.fspath.strpath), baseline_dir)
283+
return Path(item.fspath).parent / baseline_dir
292284

293285
return baseline_dir
294286

@@ -301,13 +293,14 @@ def obtain_baseline_image(self, item, target_dir):
301293
"""
302294
filename = self.generate_filename(item)
303295
baseline_dir = self.get_baseline_directory(item)
304-
baseline_remote = baseline_dir.startswith(('http://', 'https://'))
296+
baseline_remote = (isinstance(baseline_dir, str) and # noqa
297+
baseline_dir.startswith(('http://', 'https://')))
305298
if baseline_remote:
306299
# baseline_dir can be a list of URLs when remote, so we have to
307300
# pass base and filename to download
308301
baseline_image = _download_file(baseline_dir, filename)
309302
else:
310-
baseline_image = os.path.abspath(os.path.join(baseline_dir, filename))
303+
baseline_image = (baseline_dir / filename).absolute()
311304

312305
return baseline_image
313306

@@ -321,10 +314,11 @@ def generate_baseline_image(self, item, fig):
321314
if not os.path.exists(self.generate_dir):
322315
os.makedirs(self.generate_dir)
323316

324-
fig.savefig(os.path.abspath(os.path.join(self.generate_dir, self.generate_filename(item))),
317+
fig.savefig(str((self.generate_dir / self.generate_filename(item)).absolute()),
325318
**savefig_kwargs)
319+
326320
close_mpl_figure(fig)
327-
pytest.skip("Skipping test, since generating data")
321+
pytest.skip("Skipping test, since generating image")
328322

329323
def generate_hash_name(self, item):
330324
"""
@@ -363,8 +357,8 @@ def compare_image_to_baseline(self, item, fig, result_dir):
363357

364358
baseline_image_ref = self.obtain_baseline_image(item, result_dir)
365359

366-
test_image = os.path.abspath(os.path.join(result_dir, self.generate_filename(item)))
367-
fig.savefig(test_image, **savefig_kwargs)
360+
test_image = (result_dir / self.generate_filename(item)).absolute()
361+
fig.savefig(str(test_image), **savefig_kwargs)
368362

369363
if not os.path.exists(baseline_image_ref):
370364
pytest.fail("Image file not found for comparison test in: "
@@ -376,38 +370,32 @@ def compare_image_to_baseline(self, item, fig, result_dir):
376370

377371
# distutils may put the baseline images in non-accessible places,
378372
# copy to our tmpdir to be sure to keep them in case of failure
379-
baseline_image = os.path.abspath(
380-
os.path.join(result_dir,
381-
'baseline-' + self.generate_filename(item))
382-
)
373+
baseline_image = (result_dir / f"baseline-{self.generate_filename(item)}").absolute()
383374
shutil.copyfile(baseline_image_ref, baseline_image)
384375

385376
# Compare image size ourselves since the Matplotlib
386377
# exception is a bit cryptic in this case and doesn't show
387378
# the filenames
388-
expected_shape = imread(baseline_image).shape[:2]
389-
actual_shape = imread(test_image).shape[:2]
379+
expected_shape = imread(str(baseline_image)).shape[:2]
380+
actual_shape = imread(str(test_image)).shape[:2]
390381
if expected_shape != actual_shape:
391382
error = SHAPE_MISMATCH_ERROR.format(expected_path=baseline_image,
392383
expected_shape=expected_shape,
393384
actual_path=test_image,
394385
actual_shape=actual_shape)
395386
pytest.fail(error, pytrace=False)
396387

397-
return compare_images(baseline_image, test_image, tol=tolerance)
388+
return compare_images(str(baseline_image), str(test_image), tol=tolerance)
398389

399390
def load_hash_library(self, library_path):
400-
with open(library_path) as fp:
391+
with open(str(library_path)) as fp:
401392
return json.load(fp)
402393

403394
def compare_image_to_hash_library(self, item, fig, result_dir):
404395
compare = self.get_compare(item)
405396

406397
hash_library_filename = self.hash_library or compare.kwargs.get('hash_library', None)
407-
hash_library_filename = os.path.abspath(
408-
os.path.join(os.path.dirname(item.fspath.strpath),
409-
hash_library_filename)
410-
)
398+
hash_library_filename = (Path(item.fspath).parent / hash_library_filename).absolute()
411399

412400
if not Path(hash_library_filename).exists():
413401
pytest.fail(f"Can't find hash library at path {hash_library_filename}")
@@ -432,23 +420,17 @@ def pytest_runtest_setup(self, item): # noqa
432420
if compare is None:
433421
return
434422

435-
import matplotlib
436423
import matplotlib.pyplot as plt
437424
try:
438425
from matplotlib.testing.decorators import remove_ticks_and_titles
439426
except ImportError:
440427
from matplotlib.testing.decorators import ImageComparisonTest as MplImageComparisonTest
441428
remove_ticks_and_titles = MplImageComparisonTest.remove_text
442429

443-
MPL_LT_15 = LooseVersion(matplotlib.__version__) < LooseVersion('1.5')
444-
445430
style = compare.kwargs.get('style', 'classic')
446431
remove_text = compare.kwargs.get('remove_text', False)
447432
backend = compare.kwargs.get('backend', 'agg')
448433

449-
if MPL_LT_15 and style == 'classic':
450-
style = os.path.join(os.path.dirname(__file__), 'classic.mplstyle')
451-
452434
original = item.function
453435

454436
@wraps(item.function)

setup.cfg

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,10 @@ testpaths = "tests"
4242
max-line-length = 100
4343

4444
[pycodestyle]
45-
max_line_length = 100
45+
max_line_length = 100
46+
47+
[isort]
48+
balanced_wrapping = True
49+
length_sort = False
50+
length_sort_sections = stdlib
51+
line_length = 100

tests/baseline/1.5.x/test_dpi.png

-3.03 KB
Binary file not shown.
-4.39 KB
Binary file not shown.
-5.28 KB
Binary file not shown.
-7.8 KB
Binary file not shown.
-12.4 KB
Binary file not shown.
-17.2 KB
Binary file not shown.
-16.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)