Skip to content

Commit

Permalink
TST: Run macosx backends in a subprocess
Browse files Browse the repository at this point in the history
In the macosx backend, `event_loop_is_running` returns true after
`lazy_init` is called, which happens in the `new` constructor for _any_
class. This means it is "running" as soon as a figure is created, and
this will block any other backend tests.
  • Loading branch information
QuLogic committed Nov 16, 2024
1 parent 183b04f commit c97d30d
Showing 1 changed file with 32 additions and 13 deletions.
45 changes: 32 additions & 13 deletions lib/matplotlib/tests/test_backend_macosx.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import os
from pathlib import Path

import pytest
from unittest import mock

import matplotlib as mpl
import matplotlib.pyplot as plt
try:
from matplotlib.backends import _macosx
except ImportError:
pytest.skip("These are mac only tests", allow_module_level=True)
from matplotlib.testing import subprocess_run_helper


@pytest.mark.backend('macosx')
def test_cached_renderer():
_test_timeout = 60


def _test_cached_renderer():
# Make sure that figures have an associated renderer after
# a fig.canvas.draw() call
fig = plt.figure(1)
Expand All @@ -24,8 +24,14 @@ def test_cached_renderer():
assert fig.canvas.get_renderer()._renderer is not None


@pytest.mark.backend('macosx')
def test_savefig_rcparam(monkeypatch, tmp_path):
@pytest.mark.backend('macosx', skip_on_importerror=True)
def test_cached_renderer():
subprocess_run_helper(_test_cached_renderer, timeout=_test_timeout,
extra_env={"MPLBACKEND": "macosx"})


def _test_savefig_rcparam():
tmp_path = Path(os.environ["TEST_SAVEFIG_PATH"])

def new_choose_save_file(title, directory, filename):
# Replacement function instead of opening a GUI window
Expand All @@ -34,9 +40,10 @@ def new_choose_save_file(title, directory, filename):
os.makedirs(f"{directory}/test")
return f"{directory}/test/{filename}"

monkeypatch.setattr(_macosx, "choose_save_file", new_choose_save_file)
fig = plt.figure()
with mpl.rc_context({"savefig.directory": tmp_path}):
with (mock.patch("matplotlib.backends._macosx.choose_save_file",
new_choose_save_file),
mpl.rc_context({"savefig.directory": tmp_path})):
fig.canvas.toolbar.save_figure()
# Check the saved location got created
save_file = f"{tmp_path}/test/{fig.canvas.get_default_filename()}"
Expand All @@ -47,14 +54,20 @@ def new_choose_save_file(title, directory, filename):
assert mpl.rcParams["savefig.directory"] == f"{tmp_path}/test"


@pytest.mark.backend('macosx')
@pytest.mark.backend('macosx', skip_on_importerror=True)
def test_savefig_rcparam(tmp_path):
subprocess_run_helper(
_test_savefig_rcparam, timeout=_test_timeout,
extra_env={"MPLBACKEND": "macosx", "TEST_SAVEFIG_PATH": tmp_path})


@pytest.mark.backend('macosx', skip_on_importerror=True)
def test_ipython():
from matplotlib.testing import ipython_in_subprocess
ipython_in_subprocess("osx", {(8, 24): "macosx", (7, 0): "MacOSX"})


@pytest.mark.backend('macosx')
def test_save_figure_return():
def _test_save_figure_return():
fig, ax = plt.subplots()
ax.imshow([[1]])
prop = "matplotlib.backends._macosx.choose_save_file"
Expand All @@ -65,3 +78,9 @@ def test_save_figure_return():
with mock.patch(prop, return_value=None):
fname = fig.canvas.manager.toolbar.save_figure()
assert fname is None


@pytest.mark.backend('macosx', skip_on_importerror=True)
def test_save_figure_return():
subprocess_run_helper(_test_save_figure_return, timeout=_test_timeout,
extra_env={"MPLBACKEND": "macosx"})

0 comments on commit c97d30d

Please sign in to comment.