From 136ce0781b01e0529d146f8076fc42534099f781 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Tue, 17 Apr 2018 21:25:42 -0700 Subject: [PATCH] py3fication of some tests. --- .../2018-02-15-AL-deprecations.rst | 6 ++- .../2018-02-16-AL-deprecations.rst | 5 --- lib/matplotlib/testing/compare.py | 1 + lib/matplotlib/testing/decorators.py | 40 +++++-------------- lib/matplotlib/tests/test_compare_images.py | 10 +---- lib/matplotlib/tests/test_dviread.py | 34 +++++++--------- 6 files changed, 31 insertions(+), 65 deletions(-) delete mode 100644 doc/api/next_api_changes/2018-02-16-AL-deprecations.rst diff --git a/doc/api/next_api_changes/2018-02-15-AL-deprecations.rst b/doc/api/next_api_changes/2018-02-15-AL-deprecations.rst index 3c0fedba810d..da7b25099835 100644 --- a/doc/api/next_api_changes/2018-02-15-AL-deprecations.rst +++ b/doc/api/next_api_changes/2018-02-15-AL-deprecations.rst @@ -19,7 +19,11 @@ The following classes, methods, functions, and attributes are deprecated: - ``dates.DateFormatter.strftime_pre_1900``, ``dates.DateFormatter.strftime``, - ``font_manager.TempCache``, - ``mathtext.unichr_safe`` (use ``chr`` instead), -- ``testing.ImageComparisonTest``, +- ``testing.compare.ImageComparisonTest``, ``testing.compare.compare_float``, +- ``testing.decorators.skip_if_command_unavailable``. +- ``FigureCanvasQT.keyAutoRepeat`` (directly check + ``event.guiEvent.isAutoRepeat()`` in the event handler to decide whether to + handle autorepeated key presses). - ``FigureCanvasWx.macros``, - ``_ImageBase.iterpnames``, use the ``interpolation_names`` property instead. (this affects classes that inherit from ``_ImageBase`` including diff --git a/doc/api/next_api_changes/2018-02-16-AL-deprecations.rst b/doc/api/next_api_changes/2018-02-16-AL-deprecations.rst deleted file mode 100644 index b605be7bdd24..000000000000 --- a/doc/api/next_api_changes/2018-02-16-AL-deprecations.rst +++ /dev/null @@ -1,5 +0,0 @@ -Deprecations -```````````` -The `~.FigureCanvasQT.keyAutoRepeat` property is deprecated. Directly check -``event.guiEvent.isAutoRepeat()`` in the event handler to decide whether to -handle autorepeated key presses. diff --git a/lib/matplotlib/testing/compare.py b/lib/matplotlib/testing/compare.py index f80de10184d5..6154fdc4e888 100644 --- a/lib/matplotlib/testing/compare.py +++ b/lib/matplotlib/testing/compare.py @@ -35,6 +35,7 @@ def make_test_filename(fname, purpose): return '%s-%s%s' % (base, purpose, ext) +@cbook.deprecated("3.0") def compare_float(expected, actual, relTol=None, absTol=None): """ Fail if the floating point values are not close enough, with diff --git a/lib/matplotlib/testing/decorators.py b/lib/matplotlib/testing/decorators.py index faba2e24718f..4bf5d081d9cd 100644 --- a/lib/matplotlib/testing/decorators.py +++ b/lib/matplotlib/testing/decorators.py @@ -434,39 +434,16 @@ def image_comparison(baseline_images, extensions=None, tol=0, def _image_directories(func): """ Compute the baseline and result image directories for testing *func*. - Create the result directory if it doesn't exist. + + For test module ``foo.bar.test_baz``, the baseline directory is at + ``foo/bar/baseline_images/test_baz`` and the result directory at + ``$(pwd)/result_images/test_baz``. The result directory is created if it + doesn't exist. """ - module_name = func.__module__ - if module_name == '__main__': - # FIXME: this won't work for nested packages in matplotlib.tests - warnings.warn( - 'Test module run as script. Guessing baseline image locations.') - module_path = Path(sys.argv[0]).resolve() - subdir = module_path.stem - else: - module_path = Path(sys.modules[func.__module__].__file__) - mods = module_name.split('.') - if len(mods) >= 3: - mods.pop(0) - # mods[0] will be the name of the package being tested (in - # most cases "matplotlib") However if this is a - # namespace package pip installed and run via the nose - # multiprocess plugin or as a specific test this may be - # missing. See https://github.com/matplotlib/matplotlib/issues/3314 - if mods.pop(0) != 'tests': - warnings.warn( - "Module {!r} does not live in a parent module named 'tests'. " - "This is probably ok, but we may not be able to guess the " - "correct subdirectory containing the baseline images. If " - "things go wrong please make sure that there is a parent " - "directory named 'tests' and that it contains a __init__.py " - "file (can be empty).".format(module_name)) - subdir = os.path.join(*mods) - - baseline_dir = module_path.parent / 'baseline_images' / subdir - result_dir = Path().resolve() / 'result_images' / subdir + module_path = Path(sys.modules[func.__module__].__file__) + baseline_dir = module_path.parent / "baseline_images" / module_path.stem + result_dir = Path().resolve() / "result_images" / module_path.stem result_dir.mkdir(parents=True, exist_ok=True) - return str(baseline_dir), str(result_dir) @@ -489,6 +466,7 @@ def backend_switcher(*args, **kwargs): return switch_backend_decorator +@cbook.deprecated("3.0") def skip_if_command_unavailable(cmd): """ skips a test if a command is unavailable. diff --git a/lib/matplotlib/tests/test_compare_images.py b/lib/matplotlib/tests/test_compare_images.py index 4114f14b9815..162c5a1932aa 100644 --- a/lib/matplotlib/tests/test_compare_images.py +++ b/lib/matplotlib/tests/test_compare_images.py @@ -1,19 +1,11 @@ -from __future__ import absolute_import, division, print_function - -import six - -import io import os import shutil -import warnings -from numpy.testing import assert_almost_equal import pytest from pytest import approx from matplotlib.testing.compare import compare_images -from matplotlib.testing.decorators import _image_directories, image_comparison -from matplotlib.testing.exceptions import ImageComparisonFailure +from matplotlib.testing.decorators import _image_directories baseline_dir, result_dir = _image_directories(lambda: 'dummy func') diff --git a/lib/matplotlib/tests/test_dviread.py b/lib/matplotlib/tests/test_dviread.py index 6b005fd34170..9514c0f50e86 100644 --- a/lib/matplotlib/tests/test_dviread.py +++ b/lib/matplotlib/tests/test_dviread.py @@ -1,34 +1,32 @@ -from matplotlib.testing.decorators import skip_if_command_unavailable +import json +from pathlib import Path +import shutil import matplotlib.dviread as dr -import os.path -import json import pytest def test_PsfontsMap(monkeypatch): monkeypatch.setattr(dr, 'find_tex_file', lambda x: x) - filename = os.path.join( - os.path.dirname(__file__), - 'baseline_images', 'dviread', 'test.map') + filename = str(Path(__file__).parent / 'baseline_images/dviread/test.map') fontmap = dr.PsfontsMap(filename) # Check all properties of a few fonts for n in [1, 2, 3, 4, 5]: - key = ('TeXfont%d' % n).encode('ascii') + key = b'TeXfont%d' % n entry = fontmap[key] assert entry.texname == key - assert entry.psname == ('PSfont%d' % n).encode('ascii') + assert entry.psname == b'PSfont%d' % n if n not in [3, 5]: - assert entry.encoding == ('font%d.enc' % n).encode('ascii') + assert entry.encoding == b'font%d.enc' % n elif n == 3: assert entry.encoding == b'enc3.foo' # We don't care about the encoding of TeXfont5, which specifies # multiple encodings. if n not in [1, 5]: - assert entry.filename == ('font%d.pfa' % n).encode('ascii') + assert entry.filename == b'font%d.pfa' % n else: - assert entry.filename == ('font%d.pfb' % n).encode('ascii') + assert entry.filename == b'font%d.pfb' % n if n == 4: assert entry.effects == {'slant': -0.1, 'extend': 2.2} else: @@ -51,18 +49,16 @@ def test_PsfontsMap(monkeypatch): assert 'no-such-font' in str(exc.value) -@skip_if_command_unavailable(["kpsewhich", "-version"]) +@pytest.mark.skipif(shutil.which("kpsewhich") is None, + reason="kpsewhich is not available") def test_dviread(): - dir = os.path.join(os.path.dirname(__file__), 'baseline_images', 'dviread') - with open(os.path.join(dir, 'test.json')) as f: + dirpath = Path(__file__).parent / 'baseline_images/dviread' + with (dirpath / 'test.json').open() as f: correct = json.load(f) - for entry in correct: - entry['text'] = [[a, b, c, d.encode('ascii'), e] - for [a, b, c, d, e] in entry['text']] - with dr.Dvi(os.path.join(dir, 'test.dvi'), None) as dvi: + with dr.Dvi(str(dirpath / 'test.dvi'), None) as dvi: data = [{'text': [[t.x, t.y, chr(t.glyph), - t.font.texname, + t.font.texname.decode('ascii'), round(t.font.size, 2)] for t in page.text], 'boxes': [[b.x, b.y, b.height, b.width] for b in page.boxes]}