1+ import os
2+
13import pytest
24
5+ import matplotlib as mpl
36import matplotlib .pyplot as plt
4-
5-
6- pytest . importorskip ( "matplotlib.backends.backend_macosx" ,
7- reason = "These are mac only tests" )
7+ try :
8+ from matplotlib . backends import _macosx
9+ except ImportError :
10+ pytest . skip ( "These are mac only tests" , allow_module_level = True )
811
912
1013@pytest .mark .backend ('macosx' )
@@ -18,3 +21,26 @@ def test_cached_renderer():
1821 fig = plt .figure (2 )
1922 fig .draw_without_rendering ()
2023 assert fig ._cachedRenderer is not None
24+
25+
26+ @pytest .mark .backend ('macosx' )
27+ def test_savefig_rcparam (monkeypatch , tmp_path ):
28+
29+ def new_choose_save_file (title , directory , filename ):
30+ # Replacement function instead of opening a GUI window
31+ # Make a new directory for testing the update of the rcParams
32+ assert directory == str (tmp_path )
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 mpl .rc_context ({"savefig.directory" : tmp_path }):
39+ fig .canvas .toolbar .save_figure ()
40+ # Check the saved location got created
41+ save_file = f"{ tmp_path } /test/{ fig .canvas .get_default_filename ()} "
42+ assert os .path .exists (save_file )
43+
44+ # Check the savefig.directory rcParam got updated because
45+ # we added a subdirectory "test"
46+ assert mpl .rcParams ["savefig.directory" ] == f"{ tmp_path } /test"
0 commit comments