def test_webm_format_output(tmp_path, manim_cfg_file, simple_scenes_path): """Test only webm created when --format webm is set""" scene_name = "SquareToCircle" command = [ sys.executable, "-m", "manim", "--renderer", "opengl", "-ql", "--media_dir", str(tmp_path), "--format", "webm", simple_scenes_path, scene_name, ] out, err, exit_code = capture(command) assert exit_code == 0, err unexpected_mp4_path = (tmp_path / "videos" / "simple_scenes" / "480p15" / "SquareToCircle.mp4") assert not unexpected_mp4_path.exists( ), "unexpected mp4 file found at " + str(unexpected_mp4_path, ) expected_webm_path = (tmp_path / "videos" / "simple_scenes" / "480p15" / "SquareToCircle.webm") assert expected_webm_path.exists( ), "expected webm file not found at " + str(expected_webm_path, )
def test_plugin_with_all(tmp_path, create_plugin, python_version, simple_scenes_path): create_plugin = create_plugin( "{plugin_name}", "WithAll", "import_all", all_dec="__all__=['WithAll']", ) plugin_name = create_plugin["plugin_name"] cfg_file = cfg_file_create( cfg_file_contents.format(plugin_name=plugin_name), tmp_path, ) scene_name = "WithAllTest" command = [ python_version, "-m", "manim", "-ql", "--media_dir", str(cfg_file.parent), "--config_file", str(cfg_file), simple_scenes_path, scene_name, ] out, err, exit_code = capture(command, cwd=str(cfg_file.parent)) print(out) print(err) assert exit_code == 0, err
def test_default_format_output_for_transparent_flag( tmp_path, manim_cfg_file, simple_scenes_path, ): """Test .mov is created by default when transparent flag is set""" scene_name = "SquareToCircle" command = [ sys.executable, "-m", "manim", "--renderer", "opengl", "-ql", "--write_to_movie", "--media_dir", str(tmp_path), "-t", simple_scenes_path, scene_name, ] out, err, exit_code = capture(command) assert exit_code == 0, err unexpected_webm_path = (tmp_path / "videos" / "simple_scenes" / "480p15" / "SquareToCircle.webm") assert not unexpected_webm_path.exists( ), "unexpected webm file found at " + str(unexpected_webm_path, ) expected_mov_path = (tmp_path / "videos" / "simple_scenes" / "480p15" / "SquareToCircle.mov") assert expected_mov_path.exists( ), "expected .mov file not found at " + str(expected_mov_path, )
def test_plugin_function_like( tmp_path, create_plugin, python_version, simple_scenes_path, ): function_like_plugin = create_plugin( "{plugin_name}.__init__:import_all", "FunctionLike", "import_all", ) cfg_file = cfg_file_create( cfg_file_contents.format( plugin_name=function_like_plugin["plugin_name"]), tmp_path, ) scene_name = "FunctionLikeTest" command = [ python_version, "-m", "manim", "-ql", "--media_dir", str(cfg_file.parent), "--config_file", str(cfg_file), simple_scenes_path, scene_name, ] out, err, exit_code = capture(command, cwd=str(cfg_file.parent)) print(out) print(err) assert exit_code == 0, err
def test_images_are_zero_padded_when_zero_pad_set( tmp_path, manim_cfg_file, simple_scenes_path, ): """Test images are zero padded when --format png and --zero_pad n are set""" scene_name = "SquareToCircle" command = [ sys.executable, "-m", "manim", "--renderer", "opengl", "-ql", "--media_dir", str(tmp_path), "--format", "png", "--zero_pad", "3", simple_scenes_path, scene_name, ] out, err, exit_code = capture(command) assert exit_code == 0, err unexpected_png_path = tmp_path / "images" / "simple_scenes" / "SquareToCircle0.png" assert not unexpected_png_path.exists( ), "non zero padded png file found at " + str(unexpected_png_path, ) expected_png_path = tmp_path / "images" / "simple_scenes" / "SquareToCircle000.png" assert expected_png_path.exists( ), "png file not found at " + str(expected_png_path)
def test_mp4_format_output(tmp_path, manim_cfg_file, simple_scenes_path): """Test only mp4 created without manim version in file name when --format mp4 is set""" scene_name = "SquareToCircle" command = [ sys.executable, "-m", "manim", "-ql", "--media_dir", str(tmp_path), "--format", "mp4", simple_scenes_path, scene_name, ] out, err, exit_code = capture(command) assert exit_code == 0, err unexpected_gif_path = (tmp_path / "videos" / "simple_scenes" / "480p15" / add_version_before_extension("SquareToCircle.gif")) assert not unexpected_gif_path.exists( ), "unexpected gif file found at " + str(unexpected_gif_path, ) expected_mp4_path = (tmp_path / "videos" / "simple_scenes" / "480p15" / "SquareToCircle.mp4") assert expected_mp4_path.exists(), "expected mp4 file not found at " + str( expected_mp4_path, )
def test_a_flag(tmp_path, manim_cfg_file, infallible_scenes_path): command = [ sys.executable, "-m", "manim", "--renderer", "opengl", "--write_to_movie", "-ql", "--media_dir", str(tmp_path), "-a", infallible_scenes_path, ] out, err, exit_code = capture(command) assert exit_code == 0, err one_is_not_empty = (tmp_path / "videos" / "infallible_scenes" / "480p15" / "Wait1.mp4").is_file() assert one_is_not_empty, "running manim with -a flag did not render the first scene" two_is_not_empty = (tmp_path / "videos" / "infallible_scenes" / "480p15" / "Wait2.mp4").is_file() assert (two_is_not_empty ), "running manim with -a flag did not render the second scene"
def test_no_default_image_output_with_non_static_scene(tmp_path, manim_cfg_file, simple_scenes_path): scene_name = "SceneWithNonStaticWait" command = [ sys.executable, "-m", "manim", "--renderer", "opengl", "-ql", "--media_dir", str(tmp_path), simple_scenes_path, scene_name, ] out, err, exit_code = capture(command) assert exit_code == 0, err exists = (tmp_path / "videos").exists() assert not exists, "running manim with static scene rendered a video" is_empty = len(os.listdir(tmp_path / "images" / "simple_scenes")) == 0 assert ( is_empty ), "running manim static scene with interactive embed rendered an image"
def test_mov_can_be_set_as_output_format(tmp_path, manim_cfg_file, simple_scenes_path): """Test .mov is created by when set using --format mov arg""" scene_name = "SquareToCircle" command = [ sys.executable, "-m", "manim", "--renderer", "opengl", "-ql", "--media_dir", str(tmp_path), "--format", "mov", simple_scenes_path, scene_name, ] out, err, exit_code = capture(command) assert exit_code == 0, err unexpected_webm_path = (tmp_path / "videos" / "simple_scenes" / "480p15" / "SquareToCircle.webm") assert not unexpected_webm_path.exists( ), "unexpected webm file found at " + str(unexpected_webm_path, ) expected_mov_path = (tmp_path / "videos" / "simple_scenes" / "480p15" / "SquareToCircle.mov") assert expected_mov_path.exists( ), "expected .mov file not found at " + str(expected_mov_path, )
def test_custom_folders(tmp_path, manim_cfg_file, simple_scenes_path): scene_name = "SquareToCircle" command = [ sys.executable, "-m", "manim", "--renderer", "opengl", "-ql", "-s", "--media_dir", str(tmp_path), "--custom_folders", simple_scenes_path, scene_name, ] out, err, exit_code = capture(command) assert exit_code == 0, err exists = (tmp_path / "videos").exists() assert not exists, "--custom_folders produced a 'videos/' dir" exists = add_version_before_extension(tmp_path / "SquareToCircle.png").exists() assert exists, "--custom_folders did not produce the output file"
def test_no_image_output_with_interactive_embed(tmp_path, manim_cfg_file, simple_scenes_path): """Check no image is output for a static scene when interactive embed is called""" scene_name = "InteractiveStaticScene" command = [ sys.executable, "-m", "manim", "--renderer", "opengl", "-ql", "--media_dir", str(tmp_path), simple_scenes_path, scene_name, ] out, err, exit_code = capture(command) assert exit_code == 0, err exists = (tmp_path / "videos").exists() assert not exists, "running manim with static scene rendered a video" is_empty = len(os.listdir(tmp_path / "images" / "simple_scenes")) == 0 assert ( is_empty ), "running manim static scene with interactive embed rendered an image"
def test_resolution_flag(tmp_path, manim_cfg_file, simple_scenes_path): scene_name = "NoAnimations" # test different separators resolutions = [ (720, 480, ";"), (1280, 720, ","), (1920, 1080, "-"), (2560, 1440, ";"), # (3840, 2160, ","), # (640, 480, "-"), # (800, 600, ";"), ] for (width, height, separator) in resolutions: command = [ sys.executable, "-m", "manim", "--media_dir", str(tmp_path), "--resolution", f"{width}{separator}{height}", str(simple_scenes_path), scene_name, ] _, err, exit_code = capture(command) assert exit_code == 0, err path = (tmp_path / "videos" / "simple_scenes" / f"{height}p60" / f"{scene_name}.mp4") meta = get_video_metadata(path) assert (width, height) == (meta["width"], meta["height"])
def _create_plugin(entry_point, class_name, function_name, all_dec=""): entry_point = entry_point.format(plugin_name=plugin_name) module_dir = plugin_dir / plugin_name module_dir.mkdir(parents=True) with open(module_dir / "__init__.py", "w") as f: f.write( plugin_init_template.format( class_name=class_name, function_name=function_name, all_dec=all_dec, ), ) with open(plugin_dir / "pyproject.toml", "w") as f: f.write( plugin_pyproject_template.format( plugin_name=plugin_name, plugin_entrypoint=entry_point, ), ) command = [ python_version, "-m", "pip", "install", str(plugin_dir.absolute()), ] out, err, exit_code = capture(command, cwd=str(plugin_dir)) print(out) assert exit_code == 0, err return { "module_dir": module_dir, "plugin_name": plugin_name, }
def test_videos_not_created_when_png_format_set( tmp_path, manim_cfg_file, simple_scenes_path, ): """Test mp4 and gifs are not created when --format png is set""" scene_name = "SquareToCircle" command = [ sys.executable, "-m", "manim", "--renderer", "opengl", "-ql", "--media_dir", str(tmp_path), "--format", "png", simple_scenes_path, scene_name, ] out, err, exit_code = capture(command) assert exit_code == 0, err unexpected_gif_path = (tmp_path / "videos" / "simple_scenes" / "480p15" / add_version_before_extension("SquareToCircle.gif")) assert not unexpected_gif_path.exists( ), "unexpected gif file found at " + str(unexpected_gif_path, ) unexpected_mp4_path = (tmp_path / "videos" / "simple_scenes" / "480p15" / "SquareToCircle.mp4") assert not unexpected_mp4_path.exists( ), "expected mp4 file not found at " + str(unexpected_mp4_path, )
def test_r_flag(tmp_path, manim_cfg_file, simple_scenes_path): scene_name = "SquareToCircle" command = [ sys.executable, "-m", "manim", "--renderer", "opengl", "-ql", "-s", "--media_dir", str(tmp_path), "-r", "200,100", simple_scenes_path, scene_name, ] out, err, exit_code = capture(command) assert exit_code == 0, err is_not_empty = any((tmp_path / "images").iterdir()) assert is_not_empty, "running manim with -s, -r flag did not render a file" filename = add_version_before_extension( tmp_path / "images" / "simple_scenes" / "SquareToCircle.png", ) assert np.asarray(Image.open(filename)).shape == (100, 200, 4)
def test_images_are_created_when_png_format_set( tmp_path, manim_cfg_file, simple_scenes_path, ): """Test images are created in media directory when --format png is set""" scene_name = "SquareToCircle" command = [ sys.executable, "-m", "manim", "--renderer", "opengl", "-ql", "--media_dir", str(tmp_path), "--format", "png", simple_scenes_path, scene_name, ] out, err, exit_code = capture(command) assert exit_code == 0, err expected_png_path = tmp_path / "images" / "simple_scenes" / "SquareToCircle0000.png" assert expected_png_path.exists( ), "png file not found at " + str(expected_png_path)
def test_manim_version(): command = [ sys.executable, "-m", "manim", "--version", ] out, err, exit_code = capture(command) assert exit_code == 0, err assert __version__ in out
def test_basic_scene_l_flag(tmp_path, manim_cfg_file, simple_scenes_path): scene_name = "SquareToCircle" command = [ sys.executable, "-m", "manim", "-ql", "--media_dir", str(tmp_path), simple_scenes_path, scene_name, ] out, err, exit_code = capture(command) assert exit_code == 0, err
def test_n_flag(tmp_path, simple_scenes_path): scene_name = "SceneWithMultipleCalls" command = [ sys.executable, "-m", "manim", "-n 3,6", "--media_dir", str(tmp_path), simple_scenes_path, scene_name, ] _, err, exit_code = capture(command) assert exit_code == 0, err
def test_many_sections(tmp_path, manim_cfg_file, simple_scenes_path): scene_name = "SceneWithSections" command = [ sys.executable, "-m", "manim", "-ql", "--save_sections", "--media_dir", str(tmp_path), simple_scenes_path, scene_name, ] _, err, exit_code = capture(command) assert exit_code == 0, err
def test_error_logging(tmp_path, python_version): path_error_scene = Path("tests/test_logging/basic_scenes_error.py") command = [ python_version, "-m", "manim", "-ql", "--media_dir", str(tmp_path), str(path_error_scene), ] _, err, exitcode = capture(command) assert exitcode != 0 and len(err) > 0
def test_play_skip(tmp_path, manim_cfg_file, simple_scenes_path): # Intended to test if play calls are correctly skipped. scene_name = "SceneWithMultipleCalls" command = [ sys.executable, "-m", "manim", "-ql", "--media_dir", str(tmp_path), "-n", "3", simple_scenes_path, scene_name, ] out, err, exit_code = capture(command) assert exit_code == 0, err
def test_tex_error_logs(tmp_path, python_version): bad_tex_scene = os.path.join("tests", "test_logging", "bad_tex_scene.py") command = [ python_version, "-m", "manim", "-ql", "--log_to_file", "-v", "INFO", "--media_dir", str(tmp_path), bad_tex_scene, "BadTex", ] _, err, exitcode = capture(command) assert exitcode != 0 and len(err) > 0
def test_basic_scene_with_default_values(tmp_path, manim_cfg_file, simple_scenes_path): scene_name = "SquareToCircle" command = [ sys.executable, "-m", "manim", "--renderer", "opengl", "--write_to_movie", "--media_dir", str(tmp_path), simple_scenes_path, scene_name, ] out, err, exit_code = capture(command) assert exit_code == 0, err
def test_no_sections(tmp_path, manim_cfg_file, simple_scenes_path): scene_name = "SquareToCircle" command = [ sys.executable, "-m", "manim", "-ql", "--media_dir", str(tmp_path), simple_scenes_path, scene_name, ] _, err, exit_code = capture(command) assert exit_code == 0, err scene_dir = os.path.join(tmp_path, "videos", "simple_scenes", "480p15") assert_dir_exists(scene_dir) assert_dir_not_exists(os.path.join(scene_dir, "sections"))
def test_plugin_no_all(tmp_path, create_plugin, python_version): create_plugin = create_plugin("{plugin_name}", "NoAll", "import_all") plugin_name = create_plugin["plugin_name"] cfg_file = cfg_file_create( cfg_file_contents.format(plugin_name=plugin_name), tmp_path, ) test_class = textwrap.dedent( f"""\ from manim import * class NoAllTest(Scene): def construct(self): assert "{plugin_name}" in globals() a = {plugin_name}.NoAll() self.play(FadeIn(a)) """, ) with tempfile.NamedTemporaryFile( mode="w", encoding="utf-8", suffix=".py", delete=False, ) as tmpfile: tmpfile.write(test_class) scene_name = "NoAllTest" command = [ python_version, "-m", "manim", "-ql", "--media_dir", str(cfg_file.parent), "--config_file", str(cfg_file), tmpfile.name, scene_name, ] out, err, exit_code = capture(command, cwd=str(cfg_file.parent)) print(out) print(err) assert exit_code == 0, err Path(tmpfile.name).unlink()
def test_input_file_via_cfg(tmp_path, manim_cfg_file, simple_scenes_path): scene_name = "SquareToCircle" with open(os.path.join(tmp_path, "manim.cfg"), "w") as file: file.write(f""" [CLI] input_file = {simple_scenes_path} """) command = [ sys.executable, "-m", "manim", "-ql", "--media_dir", ".", str(tmp_path), scene_name, ] out, err, exit_code = capture(command, cwd=tmp_path) assert exit_code == 0, err
def test_wait_skip(tmp_path, manim_cfg_file, simple_scenes_path): # Test for PR #468. Intended to test if wait calls are correctly skipped. scene_name = "SceneWithMultipleWaitCalls" command = [ sys.executable, "-m", "manim", "--renderer", "opengl", "--write_to_movie", "-ql", "--media_dir", str(tmp_path), "-n", "3", simple_scenes_path, scene_name, ] out, err, exit_code = capture(command) assert exit_code == 0, err
def test_logging_when_scene_is_not_specified(tmp_path, python_version): path_basic_scene = os.path.join( "tests", "test_logging", "basic_scenes_square_to_circle.py", ) command = [ python_version, "-m", "manim", "-ql", "-v", "DEBUG", "--log_to_file", "--media_dir", str(tmp_path), path_basic_scene, ] _, err, exitcode = capture(command) assert exitcode == 0, err
def test_s_flag(tmp_path, manim_cfg_file, simple_scenes_path): scene_name = "SquareToCircle" command = [ sys.executable, "-m", "manim", "-ql", "-s", "--media_dir", str(tmp_path), simple_scenes_path, scene_name, ] out, err, exit_code = capture(command) assert exit_code == 0, err exists = (tmp_path / "videos").exists() assert not exists, "running manim with -s flag rendered a video" is_empty = not any((tmp_path / "images" / "simple_scenes").iterdir()) assert not is_empty, "running manim with -s flag did not render an image"