Skip to content

Commit 647451d

Browse files
committed
TST: macosx savefig respects rcParam setting
1 parent d2ebb93 commit 647451d

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

lib/matplotlib/tests/test_backend_macosx.py

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

6+
import matplotlib as mpl
7+
from matplotlib.backends import _macosx
38
import matplotlib.pyplot as plt
49

510

@@ -18,3 +23,32 @@ def test_cached_renderer():
1823
fig = plt.figure(2)
1924
fig.draw_without_rendering()
2025
assert fig._cachedRenderer is not None
26+
27+
28+
@pytest.mark.backend('macosx')
29+
def test_savefig_rcparam():
30+
# Store the save file so we can overwrite it and
31+
# then put back the real one after this
32+
old_save_file_func = _macosx.choose_save_file
33+
34+
def new_choose_save_file(title, directory, filename):
35+
# Replacement function instead of opening a GUI window
36+
# Make a new directory for testing the update of the rcParams
37+
os.makedirs(f"{directory}/test")
38+
return f"{directory}/test/{filename}"
39+
40+
_macosx.choose_save_file = new_choose_save_file
41+
fig = plt.figure()
42+
with tempfile.TemporaryDirectory() as temp_dir, \
43+
mpl.rc_context({"savefig.directory": temp_dir}):
44+
fig.canvas.toolbar.save_figure()
45+
# Check the saved location got created
46+
save_file = f"{temp_dir}/test/{fig.canvas.get_default_filename()}"
47+
assert os.path.exists(save_file)
48+
49+
# Check the savefig.directory rcParam got updated because
50+
# we added a subdirectory "test"
51+
assert mpl.rcParams["savefig.directory"] == f"{temp_dir}/test"
52+
53+
# Replace the original function
54+
_macosx.choose_save_file = old_save_file_func

0 commit comments

Comments
 (0)