1212from matplotlib .font_manager import (
1313 findfont , findSystemFonts , FontProperties , fontManager , json_dump ,
1414 json_load , get_font , get_fontconfig_fonts , is_opentype_cff_font ,
15- MSUserFontDirectories )
15+ MSUserFontDirectories , _call_fc_list )
1616from matplotlib import pyplot as plt , rc_context
1717
1818has_fclist = shutil .which ('fc-list' ) is not None
@@ -131,7 +131,34 @@ def test_find_ttc():
131131 fig .savefig (BytesIO (), format = "ps" )
132132
133133
134- def test_user_fonts ():
134+ @pytest .mark .skipif (sys .platform != 'linux' , reason = 'Linux only' )
135+ def test_user_fonts_linux (tmpdir , monkeypatch ):
136+ font_test_file = 'mpltest.ttf'
137+
138+ # Precondition: the test font should not be available
139+ fonts = findSystemFonts ()
140+ if any (font_test_file in font for font in fonts ):
141+ pytest .skip (f'{ font_test_file } already exists in system fonts' )
142+
143+ # Prepare a temporary user font directory
144+ user_fonts_dir = tmpdir .join ('fonts' )
145+ user_fonts_dir .ensure (dir = True )
146+ shutil .copyfile (Path (__file__ ).parent / font_test_file ,
147+ user_fonts_dir .join (font_test_file ))
148+
149+ with monkeypatch .context () as m :
150+ m .setenv ('XDG_DATA_HOME' , str (tmpdir ))
151+ _call_fc_list .cache_clear ()
152+ # Now, the font should be available
153+ fonts = findSystemFonts ()
154+ assert any (font_test_file in font for font in fonts )
155+
156+ # Make sure the temporary directory is no longer cached.
157+ _call_fc_list .cache_clear ()
158+
159+
160+ @pytest .mark .skipif (sys .platform != 'win32' , reason = 'Windows only' )
161+ def test_user_fonts_win32 ():
135162 if not os .environ .get ('APPVEYOR' , False ):
136163 pytest .xfail ('This test does only work on appveyor since user fonts '
137164 'are Windows specific and the developer\' s font '
@@ -141,7 +168,8 @@ def test_user_fonts():
141168
142169 # Precondition: the test font should not be available
143170 fonts = findSystemFonts ()
144- assert not any (font_test_file in font for font in fonts )
171+ if any (font_test_file in font for font in fonts ):
172+ pytest .skip (f'{ font_test_file } already exists in system fonts' )
145173
146174 user_fonts_dir = MSUserFontDirectories [0 ]
147175
0 commit comments