1+ import os
2+ from pathlib import Path
13import sys
24import tempfile
35
@@ -38,29 +40,35 @@ def finish(self):
3840 pass
3941
4042
41- def test_null_movie_writer ():
42- # Test running an animation with NullMovieWriter.
43-
44- fig = plt .figure ()
43+ def make_animation (** kwargs ):
44+ fig , ax = plt .subplots ()
45+ line , = ax .plot ([])
4546
4647 def init ():
4748 pass
4849
4950 def animate (i ):
50- pass
51+ line .set_data ([0 , 1 ], [0 , i ])
52+ return line ,
53+
54+ return animation .FuncAnimation (fig , animate , ** kwargs )
55+
56+
57+ def test_null_movie_writer ():
58+ # Test running an animation with NullMovieWriter.
5159
5260 num_frames = 5
61+ anim = make_animation (frames = num_frames )
62+
5363 filename = "unused.null"
5464 dpi = 50
5565 savefig_kwargs = dict (foo = 0 )
56-
57- anim = animation .FuncAnimation (fig , animate , init_func = init ,
58- frames = num_frames )
5966 writer = NullMovieWriter ()
67+
6068 anim .save (filename , dpi = dpi , writer = writer ,
6169 savefig_kwargs = savefig_kwargs )
6270
63- assert writer .fig == fig
71+ assert writer .fig == plt . figure ( 1 ) # The figure used by make_animation.
6472 assert writer .outfile == filename
6573 assert writer .dpi == dpi
6674 assert writer .args == ()
@@ -174,23 +182,8 @@ def animate(i):
174182
175183
176184def test_no_length_frames ():
177- fig , ax = plt .subplots ()
178- line , = ax .plot ([], [])
179-
180- def init ():
181- line .set_data ([], [])
182- return line ,
183-
184- def animate (i ):
185- x = np .linspace (0 , 10 , 100 )
186- y = np .sin (x + i )
187- line .set_data (x , y )
188- return line ,
189-
190- anim = animation .FuncAnimation (fig , animate , init_func = init ,
191- frames = iter (range (5 )))
192- writer = NullMovieWriter ()
193- anim .save ('unused.null' , writer = writer )
185+ (make_animation (frames = iter (range (5 )))
186+ .save ('unused.null' , writer = NullMovieWriter ()))
194187
195188
196189def test_movie_writer_registry ():
@@ -215,3 +208,55 @@ def test_movie_writer_registry():
215208 assert not animation .writers ._dirty
216209 assert animation .writers .is_available ("ffmpeg" )
217210 mpl .rcParams ['animation.ffmpeg_path' ] = ffmpeg_path
211+
212+
213+ @pytest .mark .skipif (
214+ not animation .writers .is_available (mpl .rcParams ["animation.writer" ]),
215+ reason = "animation writer not installed" )
216+ @pytest .mark .parametrize ("method_name" , ["to_html5_video" , "to_jshtml" ])
217+ def test_embed_limit (method_name , caplog ):
218+ with mpl .rc_context ({"animation.embed_limit" : 1e-6 }): # ~1 byte.
219+ getattr (make_animation (frames = 1 ), method_name )()
220+ assert len (caplog .records ) == 1
221+ record , = caplog .records
222+ assert (record .name == "matplotlib.animation"
223+ and record .levelname == "WARNING" )
224+
225+
226+ @pytest .mark .skipif (
227+ not animation .writers .is_available (mpl .rcParams ["animation.writer" ]),
228+ reason = "animation writer not installed" )
229+ @pytest .mark .parametrize (
230+ "method_name" ,
231+ ["to_html5_video" ,
232+ pytest .mark .xfail ("to_jshtml" )]) # Needs to be fixed.
233+ def test_cleanup_temporaries (method_name , tmpdir ):
234+ with tmpdir .as_cwd ():
235+ getattr (make_animation (frames = 1 ), method_name )()
236+ assert list (Path (str (tmpdir )).iterdir ()) == []
237+
238+
239+ # Currently, this fails with a ValueError after we try to communicate() twice
240+ # with the Popen.
241+ @pytest .mark .xfail
242+ @pytest .mark .skipif (os .name != "posix" , reason = "requires a POSIX OS" )
243+ def test_failing_ffmpeg (tmpdir , monkeypatch ):
244+ """
245+ Test that we correctly raise an OSError when ffmpeg fails.
246+
247+ To do so, mock ffmpeg using a simple executable shell script that
248+ succeeds when called with no arguments (so that it gets registered by
249+ `isAvailable`), but fails otherwise, and add it to the $PATH.
250+ """
251+ try :
252+ with tmpdir .as_cwd ():
253+ monkeypatch .setenv ("PATH" , ".:" + os .environ ["PATH" ])
254+ exe_path = Path (tmpdir , "ffmpeg" )
255+ exe_path .write_text ("#!/bin/sh\n "
256+ "[[ $@ -eq 0 ]]\n " )
257+ os .chmod (str (exe_path ), 0o755 )
258+ animation .writers .reset_available_writers ()
259+ with pytest .raises (OSError ):
260+ make_animation ().save ("test.mpeg" )
261+ finally :
262+ animation .writers .reset_available_writers ()
0 commit comments