1
+ import os
2
+ import tempfile
3
+
1
4
import pytest
2
5
6
+ import matplotlib as mpl
7
+ from matplotlib .backends import _macosx
3
8
import matplotlib .pyplot as plt
4
9
5
10
@@ -18,3 +23,32 @@ def test_cached_renderer():
18
23
fig = plt .figure (2 )
19
24
fig .draw_without_rendering ()
20
25
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