From fe0683f5dd6230012ca38d90939e104487655fb7 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Wed, 29 Sep 2021 17:04:03 +0200 Subject: [PATCH] Parametrize/simplify test_missing_psfont. --- lib/matplotlib/tests/test_backend_pdf.py | 19 +------------------ lib/matplotlib/tests/test_backend_svg.py | 18 ------------------ lib/matplotlib/tests/test_usetex.py | 19 +++++++++++++++++++ 3 files changed, 20 insertions(+), 36 deletions(-) diff --git a/lib/matplotlib/tests/test_backend_pdf.py b/lib/matplotlib/tests/test_backend_pdf.py index 8e16eb2b7b94..7d46f6bf3c1a 100644 --- a/lib/matplotlib/tests/test_backend_pdf.py +++ b/lib/matplotlib/tests/test_backend_pdf.py @@ -9,7 +9,7 @@ import pytest import matplotlib as mpl -from matplotlib import dviread, pyplot as plt, checkdep_usetex, rcParams +from matplotlib import pyplot as plt, checkdep_usetex, rcParams from matplotlib.cbook import _get_data_path from matplotlib.ft2font import FT2Font from matplotlib.backends._backend_pdf_ps import get_glyphs_subset @@ -295,23 +295,6 @@ def test_grayscale_alpha(): ax.set_yticks([]) -# This tests tends to hit a TeX cache lock on AppVeyor. -@pytest.mark.flaky(reruns=3) -@needs_usetex -def test_missing_psfont(monkeypatch): - """An error is raised if a TeX font lacks a Type-1 equivalent""" - def psfont(*args, **kwargs): - return dviread.PsFont(texname='texfont', psname='Some Font', - effects=None, encoding=None, filename=None) - - monkeypatch.setattr(dviread.PsfontsMap, '__getitem__', psfont) - rcParams['text.usetex'] = True - fig, ax = plt.subplots() - ax.text(0.5, 0.5, 'hello') - with NamedTemporaryFile() as tmpfile, pytest.raises(ValueError): - fig.savefig(tmpfile, format='pdf') - - @mpl.style.context('default') @check_figures_equal(extensions=["pdf", "eps"]) def test_pdf_eps_savefig_when_color_is_none(fig_test, fig_ref): diff --git a/lib/matplotlib/tests/test_backend_svg.py b/lib/matplotlib/tests/test_backend_svg.py index 353603a9c093..6899f41301fb 100644 --- a/lib/matplotlib/tests/test_backend_svg.py +++ b/lib/matplotlib/tests/test_backend_svg.py @@ -1,6 +1,5 @@ import datetime from io import BytesIO -import tempfile import xml.etree.ElementTree import xml.parsers.expat @@ -8,7 +7,6 @@ import pytest import matplotlib as mpl -from matplotlib import dviread from matplotlib.figure import Figure import matplotlib.pyplot as plt from matplotlib.testing.decorators import image_comparison, check_figures_equal @@ -181,22 +179,6 @@ def count_tag(fig, tag): assert count_tag(fig5, "path") == 1 # axis patch -@needs_usetex -def test_missing_psfont(monkeypatch): - """An error is raised if a TeX font lacks a Type-1 equivalent""" - - def psfont(*args, **kwargs): - return dviread.PsFont(texname='texfont', psname='Some Font', - effects=None, encoding=None, filename=None) - - monkeypatch.setattr(dviread.PsfontsMap, '__getitem__', psfont) - mpl.rc('text', usetex=True) - fig, ax = plt.subplots() - ax.text(0.5, 0.5, 'hello') - with tempfile.TemporaryFile() as tmpfile, pytest.raises(ValueError): - fig.savefig(tmpfile, format='svg') - - # Use Computer Modern Sans Serif, not Helvetica (which has no \textwon). @mpl.style.context('default') @needs_usetex diff --git a/lib/matplotlib/tests/test_usetex.py b/lib/matplotlib/tests/test_usetex.py index 64dca0a8cd62..b726f9c26cfb 100644 --- a/lib/matplotlib/tests/test_usetex.py +++ b/lib/matplotlib/tests/test_usetex.py @@ -1,7 +1,10 @@ +from tempfile import TemporaryFile + import numpy as np import pytest import matplotlib as mpl +from matplotlib import dviread from matplotlib.testing import _has_tex_package from matplotlib.testing.decorators import check_figures_equal, image_comparison import matplotlib.pyplot as plt @@ -119,3 +122,19 @@ def test_usetex_with_underscore(): ax.legend() ax.text(0, 0, "foo_bar", usetex=True) plt.draw() + + +@pytest.mark.flaky(reruns=3) # Tends to hit a TeX cache lock on AppVeyor. +@pytest.mark.parametrize("fmt", ["pdf", "svg"]) +def test_missing_psfont(fmt, monkeypatch): + """An error is raised if a TeX font lacks a Type-1 equivalent""" + monkeypatch.setattr( + dviread.PsfontsMap, '__getitem__', + lambda self, k: dviread.PsFont( + texname='texfont', psname='Some Font', + effects=None, encoding=None, filename=None)) + mpl.rcParams['text.usetex'] = True + fig, ax = plt.subplots() + ax.text(0.5, 0.5, 'hello') + with TemporaryFile() as tmpfile, pytest.raises(ValueError): + fig.savefig(tmpfile, format=fmt)