Skip to content

Commit 9ed0a30

Browse files
committed
TST: macosx savefig respects rcParam setting
1 parent 326ea1a commit 9ed0a30

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

lib/matplotlib/tests/test_backend_macosx.py

+31-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
import os
2+
import tempfile
3+
14
import pytest
25

6+
import matplotlib as mpl
37
import matplotlib.pyplot as plt
4-
5-
6-
pytest.importorskip("matplotlib.backends.backend_macosx",
7-
reason="These are mac only tests")
8+
try:
9+
from matplotlib.backends import _macosx
10+
except ImportError:
11+
pytest.skip("These are mac only tests", allow_module_level=True)
812

913

1014
@pytest.mark.backend('macosx')
@@ -18,3 +22,26 @@ def test_cached_renderer():
1822
fig = plt.figure(2)
1923
fig.draw_without_rendering()
2024
assert fig._cachedRenderer is not None
25+
26+
27+
@pytest.mark.backend('macosx')
28+
def test_savefig_rcparam(monkeypatch):
29+
30+
def new_choose_save_file(title, directory, filename):
31+
# Replacement function instead of opening a GUI window
32+
# Make a new directory for testing the update of the rcParams
33+
os.makedirs(f"{directory}/test")
34+
return f"{directory}/test/{filename}"
35+
36+
monkeypatch.setattr(_macosx, "choose_save_file", new_choose_save_file)
37+
fig = plt.figure()
38+
with tempfile.TemporaryDirectory() as temp_dir, \
39+
mpl.rc_context({"savefig.directory": temp_dir}):
40+
fig.canvas.toolbar.save_figure()
41+
# Check the saved location got created
42+
save_file = f"{temp_dir}/test/{fig.canvas.get_default_filename()}"
43+
assert os.path.exists(save_file)
44+
45+
# Check the savefig.directory rcParam got updated because
46+
# we added a subdirectory "test"
47+
assert mpl.rcParams["savefig.directory"] == f"{temp_dir}/test"

0 commit comments

Comments
 (0)