ENH: Use rcParams savefig.directory on macosx backend by greglucas · Pull Request #22180 · matplotlib/matplotlib · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/matplotlib/backends/backend_macosx.py
34 changes: 30 additions & 4 deletions lib/matplotlib/tests/test_backend_macosx.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import os

import pytest

import matplotlib as mpl
import matplotlib.pyplot as plt


pytest.importorskip("matplotlib.backends.backend_macosx",
reason="These are mac only tests")
try:
from matplotlib.backends import _macosx
except ImportError:
pytest.skip("These are mac only tests", allow_module_level=True)


@pytest.mark.backend('macosx')
Expand All @@ -18,3 +21,26 @@ def test_cached_renderer():
fig = plt.figure(2)
fig.draw_without_rendering()
assert fig._cachedRenderer is not None


@pytest.mark.backend('macosx')
def test_savefig_rcparam(monkeypatch, tmp_path):

def new_choose_save_file(title, directory, filename):
Comment thread
greglucas marked this conversation as resolved.
# Replacement function instead of opening a GUI window
# Make a new directory for testing the update of the rcParams
assert directory == str(tmp_path)
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}):
fig.canvas.toolbar.save_figure()
# Check the saved location got created
save_file = f"{tmp_path}/test/{fig.canvas.get_default_filename()}"
assert os.path.exists(save_file)

# Check the savefig.directory rcParam got updated because
# we added a subdirectory "test"
assert mpl.rcParams["savefig.directory"] == f"{tmp_path}/test"
25 changes: 8 additions & 17 deletions src/_macosx.m