Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Migrate more `os.path` to `pathlib` in tests (#2991)

NotWearingPants206db54af53a
diff --git a/example_scenes/opengl.py b/example_scenes/opengl.py
index 729f5a0eb..b30c5ace2 100644
--- a/example_scenes/opengl.py
+++ b/example_scenes/opengl.py
@@ -1,4 +1,3 @@
-import os
from pathlib import Path
import manim.utils.opengl as opengl
@@ -462,7 +461,7 @@ class SurfaceExample(Scene):
# in whatever you've set as the image directory in
# the custom_config.yml file
- script_location = Path(os.path.realpath(__file__)).parent
+ script_location = Path(__file__).resolve().parent
day_texture = (
script_location / "assets" / "1280px-Whole_world_-_land_and_oceans.jpg"
)
diff --git a/manim/utils/commands.py b/manim/utils/commands.py
index 51e272909..2ec9a776d 100644
--- a/manim/utils/commands.py
+++ b/manim/utils/commands.py
@@ -1,6 +1,7 @@
from __future__ import annotations
import json
+import os
from pathlib import Path
from subprocess import run
from typing import Generator
@@ -18,7 +19,7 @@ def capture(command, cwd=None, command_input=None):
return out, err, p.returncode
-def get_video_metadata(path_to_video: str) -> dict[str]:
+def get_video_metadata(path_to_video: str | os.PathLike) -> dict[str]:
command = [
"ffprobe",
"-v",
diff --git a/tests/assert_utils.py b/tests/assert_utils.py
index 4a08e6365..0d7d2417f 100644
--- a/tests/assert_utils.py
+++ b/tests/assert_utils.py
@@ -8,7 +8,7 @@ from pprint import pformat
def assert_file_exists(filepath: str | os.PathLike) -> None:
"""Assert that filepath points to an existing file. Print all the elements (files and dir) of the parent dir of the given filepath.
- This is mostly to have better assert message than using a raw assert os.path.isfile(filepath).
+ This is mostly to have better assert message than using a raw assert filepath.is_file().
Parameters
----------
@@ -22,7 +22,7 @@ def assert_file_exists(filepath: str | os.PathLike) -> None:
"""
path = Path(filepath)
if not path.is_file():
- elems = pformat([path.name for path in list(path.parent.iterdir())])
+ elems = pformat([path.name for path in path.parent.iterdir()])
message = f"{path.absolute()} is not a file. Other elements in the parent directory are \n{elems}"
raise AssertionError(message)
@@ -60,14 +60,14 @@ def assert_dir_filled(dirpath: str | os.PathLike) -> None:
AssertionError
If dirpath does not point to a directory (if the file does exist or it's a file) or the directory is empty.
"""
- if len(os.listdir(dirpath)) == 0:
+ if not any(Path(dirpath).iterdir()):
raise AssertionError(f"{dirpath} is an empty directory.")
def assert_file_not_exists(filepath: str | os.PathLike) -> None:
"""Assert that filepath does not point to an existing file. Print all the elements (files and dir) of the parent dir of the given filepath.
- This is mostly to have better assert message than using a raw assert os.path.isfile(filepath).
+ This is mostly to have better assert message than using a raw assert filepath.is_file().
Parameters
----------
@@ -81,7 +81,7 @@ def assert_file_not_exists(filepath: str | os.PathLike) -> None:
"""
path = Path(filepath)
if path.is_file():
- elems = pformat([path.name for path in list(path.parent.iterdir())])
+ elems = pformat([path.name for path in path.parent.iterdir()])
message = f"{path.absolute()} is a file. Other elements in the parent directory are \n{elems}"
raise AssertionError(message)
diff --git a/tests/conftest.py b/tests/conftest.py
index 05a4e0dcd..5b9edb656 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -1,7 +1,7 @@
from __future__ import annotations
-import os
import sys
+from pathlib import Path
import pytest
@@ -55,12 +55,10 @@ def python_version():
@pytest.fixture
def reset_cfg_file():
- cfgfilepath = os.path.join(os.path.dirname(__file__), "test_cli", "manim.cfg")
- with open(cfgfilepath) as cfgfile:
- original = cfgfile.read()
+ cfgfilepath = Path(__file__).parent / "test_cli" / "manim.cfg"
+ original = cfgfilepath.read_text()
yield
- with open(cfgfilepath, "w") as cfgfile:
- cfgfile.write(original)
+ cfgfilepath.write_text(original)
@pytest.fixture
diff --git a/tests/helpers/video_utils.py b/tests/helpers/video_utils.py
index 15bcd3590..cc96e9acf 100644
--- a/tests/helpers/video_utils.py
+++ b/tests/helpers/video_utils.py
@@ -31,7 +31,7 @@ def get_section_index(metapath: Path) -> list[dict[str, Any]]:
return index
-def save_control_data_from_video(path_to_video: str, name: str) -> None:
+def save_control_data_from_video(path_to_video: Path, name: str) -> None:
"""Helper used to set up a new test that will compare videos.
This will create a new ``.json`` file in ``control_data/videos_data`` that contains:
@@ -43,9 +43,9 @@ def save_control_data_from_video(path_to_video: str, name: str) -> None:
Parameters
----------
- path_to_video : :class:`str`
+ path_to_video
Path to the video to extract information from.
- name : :class:`str`
+ name
Name of the test. The .json file will be named with it.
See Also
@@ -53,7 +53,7 @@ def save_control_data_from_video(path_to_video: str, name: str) -> None:
tests/utils/video_tester.py : read control data and compare with output of test
"""
- orig_path_to_sections = Path(path_to_video)
+ orig_path_to_sections = path_to_video
path_to_sections = orig_path_to_sections.parent.absolute() / "sections"
tests_directory = Path(__file__).absolute().parent.parent
path_control_data = Path(tests_directory) / "control_data" / "videos_data"
@@ -71,6 +71,6 @@ def save_control_data_from_video(path_to_video: str, name: str) -> None:
"section_index": section_index,
}
path_saved = Path(path_control_data) / f"{name}.json"
- with open(path_saved, "w") as f:
+ with path_saved.open("w") as f:
json.dump(data, f, indent=4)
logger.info(f"Data for {name} saved in {path_saved}")
diff --git a/tests/interface/test_commands.py b/tests/interface/test_commands.py
index 9f4dbfe0a..5f6f0dcb7 100644
--- a/tests/interface/test_commands.py
+++ b/tests/interface/test_commands.py
@@ -103,16 +103,14 @@ def test_manim_init_scene(tmp_path):
)
assert not result.exception
assert (Path(tmp_dir) / "my_awesome_file.py").exists()
- with open(Path(tmp_dir) / "my_awesome_file.py") as f:
- file_content = f.read()
- assert "NamedFileTestScene(Scene):" in file_content
+ file_content = (Path(tmp_dir) / "my_awesome_file.py").read_text()
+ assert "NamedFileTestScene(Scene):" in file_content
result = runner.invoke(
main, command_unnamed, prog_name="manim", input="Default\n"
)
assert (Path(tmp_dir) / "main.py").exists()
- with open(Path(tmp_dir) / "main.py") as f:
- file_content = f.read()
- assert "DefaultFileTestScene(Scene):" in file_content
+ file_content = (Path(tmp_dir) / "main.py").read_text()
+ assert "DefaultFileTestScene(Scene):" in file_content
def test_manim_new_command():
diff --git a/tests/module/utils/test_file_ops.py b/tests/module/utils/test_file_ops.py
index 943a70f75..b56684eb0 100644
--- a/tests/module/utils/test_file_ops.py
+++ b/tests/module/utils/test_file_ops.py
@@ -12,7 +12,7 @@ def test_guarantee_existence(tmp_path: Path):
guarantee_existence(test_dir)
# test if file dir got created
assert_dir_exists(test_dir)
- with open(test_dir / "test.txt", "x") as f:
+ with (test_dir / "test.txt").open("x") as f:
pass
# test if file didn't get deleted
guarantee_existence(test_dir)
@@ -21,7 +21,7 @@ def test_guarantee_existence(tmp_path: Path):
def test_guarantee_empty_existence(tmp_path: Path):
test_dir = tmp_path / "test"
test_dir.mkdir()
- with open(test_dir / "test.txt", "x"):
+ with (test_dir / "test.txt").open("x"):
pass
guarantee_empty_existence(test_dir)
diff --git a/tests/opengl/test_ipython_magic_opengl.py b/tests/opengl/test_ipython_magic_opengl.py
index ade725cbe..ad1c61a6b 100644
--- a/tests/opengl/test_ipython_magic_opengl.py
+++ b/tests/opengl/test_ipython_magic_opengl.py
@@ -29,7 +29,7 @@ def test_jupyter_file_output(tmp_path):
with tempconfig({"scene_names": [scene_name], "renderer": "opengl"}):
file_name = _generate_file_name()
actual_path = tmp_path.with_name(file_name)
- with open(actual_path, "w") as outfile:
+ with actual_path.open("w") as outfile:
outfile.write("")
assert actual_path.exists()
assert actual_path.is_file()
diff --git a/tests/test_ipython_magic.py b/tests/test_ipython_magic.py
index b3bff2450..91d1fba8c 100644
--- a/tests/test_ipython_magic.py
+++ b/tests/test_ipython_magic.py
@@ -25,7 +25,7 @@ def test_jupyter_file_output(tmp_path):
with tempconfig({"scene_names": [scene_name]}):
file_name = _generate_file_name()
actual_path = tmp_path.with_name(file_name)
- with open(actual_path, "w") as outfile:
+ with actual_path.open("w") as outfile:
outfile.write("")
assert actual_path.exists()
assert actual_path.is_file()
diff --git a/tests/test_logging/test_logging.py b/tests/test_logging/test_logging.py
index 2c8b94c59..1bbc59de0 100644
--- a/tests/test_logging/test_logging.py
+++ b/tests/test_logging/test_logging.py
@@ -1,6 +1,5 @@
from __future__ import annotations
-import os
from pathlib import Path
from manim import capture
@@ -10,14 +9,10 @@ from ..utils.logging_tester import *
@logs_comparison(
"BasicSceneLoggingTest.txt",
- os.path.join("logs", "basic_scenes_square_to_circle_SquareToCircle.log"),
+ "logs/basic_scenes_square_to_circle_SquareToCircle.log",
)
def test_logging_to_file(tmp_path, python_version):
- path_basic_scene = os.path.join(
- "tests",
- "test_logging",
- "basic_scenes_square_to_circle.py",
- )
+ path_basic_scene = Path("tests/test_logging/basic_scenes_square_to_circle.py")
command = [
python_version,
"-m",
@@ -28,7 +23,7 @@ def test_logging_to_file(tmp_path, python_version):
"--log_to_file",
"--media_dir",
str(tmp_path),
- path_basic_scene,
+ str(path_basic_scene),
"SquareToCircle",
]
_, err, exitcode = capture(command)
@@ -54,10 +49,10 @@ def test_error_logging(tmp_path, python_version):
@logs_comparison(
"bad_tex_scene_BadTex.txt",
- Path("logs/bad_tex_scene_BadTex.log"),
+ "logs/bad_tex_scene_BadTex.log",
)
def test_tex_error_logs(tmp_path, python_version):
- bad_tex_scene = os.path.join("tests", "test_logging", "bad_tex_scene.py")
+ bad_tex_scene = Path("tests/test_logging/bad_tex_scene.py")
command = [
python_version,
"-m",
@@ -68,7 +63,7 @@ def test_tex_error_logs(tmp_path, python_version):
"INFO",
"--media_dir",
str(tmp_path),
- bad_tex_scene,
+ str(bad_tex_scene),
"BadTex",
]
_, err, exitcode = capture(command)
diff --git a/tests/test_plugins/test_plugins.py b/tests/test_plugins/test_plugins.py
index 149d70541..b73a5d6a1 100644
--- a/tests/test_plugins/test_plugins.py
+++ b/tests/test_plugins/test_plugins.py
@@ -60,13 +60,12 @@ cfg_file_contents = textwrap.dedent(
@pytest.fixture
def simple_scenes_path():
- yield str(Path(__file__).parent / "simple_scenes.py")
+ yield Path(__file__).parent / "simple_scenes.py"
def cfg_file_create(cfg_file_contents, path):
file_loc = (path / "manim.cfg").absolute()
- with open(file_loc, "w") as f:
- f.write(cfg_file_contents)
+ file_loc.write_text(cfg_file_contents)
return file_loc
@@ -93,7 +92,7 @@ def test_plugin_warning(tmp_path, python_version, simple_scenes_path):
str(cfg_file.parent),
"--config_file",
str(cfg_file),
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
out, err, exit_code = capture(command, cwd=str(cfg_file.parent))
@@ -110,21 +109,19 @@ def create_plugin(tmp_path, python_version, random_string):
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,
- ),
- )
+ (module_dir / "__init__.py").write_text(
+ plugin_init_template.format(
+ class_name=class_name,
+ function_name=function_name,
+ all_dec=all_dec,
+ ),
+ )
+ (plugin_dir / "pyproject.toml").write_text(
+ plugin_pyproject_template.format(
+ plugin_name=plugin_name,
+ plugin_entrypoint=entry_point,
+ ),
+ )
command = [
python_version,
"-m",
@@ -173,7 +170,7 @@ def test_plugin_function_like(
str(cfg_file.parent),
"--config_file",
str(cfg_file),
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
out, err, exit_code = capture(command, cwd=str(cfg_file.parent))
@@ -251,7 +248,7 @@ def test_plugin_with_all(tmp_path, create_plugin, python_version, simple_scenes_
str(cfg_file.parent),
"--config_file",
str(cfg_file),
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
out, err, exit_code = capture(command, cwd=str(cfg_file.parent))
diff --git a/tests/test_scene_rendering/conftest.py b/tests/test_scene_rendering/conftest.py
index 9748edab0..d7ae20852 100644
--- a/tests/test_scene_rendering/conftest.py
+++ b/tests/test_scene_rendering/conftest.py
@@ -9,12 +9,12 @@ from manim import config, tempconfig
@pytest.fixture
def manim_cfg_file():
- return str(Path(__file__).parent / "manim.cfg")
+ return Path(__file__).parent / "manim.cfg"
@pytest.fixture
def simple_scenes_path():
- return str(Path(__file__).parent / "simple_scenes.py")
+ return Path(__file__).parent / "simple_scenes.py"
@pytest.fixture
@@ -46,7 +46,7 @@ def disabling_caching():
@pytest.fixture
def infallible_scenes_path():
- return str(Path(__file__).parent / "infallible_scenes.py")
+ return Path(__file__).parent / "infallible_scenes.py"
@pytest.fixture
diff --git a/tests/test_scene_rendering/opengl/test_caching_related_opengl.py b/tests/test_scene_rendering/opengl/test_caching_related_opengl.py
index 3bc517957..c9c82a449 100644
--- a/tests/test_scene_rendering/opengl/test_caching_related_opengl.py
+++ b/tests/test_scene_rendering/opengl/test_caching_related_opengl.py
@@ -29,7 +29,7 @@ def test_wait_skip(tmp_path, manim_cfg_file, simple_scenes_path):
str(tmp_path),
"-n",
"3",
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
out, err, exit_code = capture(command)
@@ -56,7 +56,7 @@ def test_play_skip(tmp_path, manim_cfg_file, simple_scenes_path):
str(tmp_path),
"-n",
"3",
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
out, err, exit_code = capture(command)
diff --git a/tests/test_scene_rendering/opengl/test_cli_flags_opengl.py b/tests/test_scene_rendering/opengl/test_cli_flags_opengl.py
index 876b011bd..8f36200dd 100644
--- a/tests/test_scene_rendering/opengl/test_cli_flags_opengl.py
+++ b/tests/test_scene_rendering/opengl/test_cli_flags_opengl.py
@@ -1,6 +1,5 @@
from __future__ import annotations
-import os
import sys
import numpy as np
@@ -30,7 +29,7 @@ def test_basic_scene_with_default_values(tmp_path, manim_cfg_file, simple_scenes
"--write_to_movie",
"--media_dir",
str(tmp_path),
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
out, err, exit_code = capture(command)
@@ -91,7 +90,7 @@ def test_basic_scene_l_flag(tmp_path, manim_cfg_file, simple_scenes_path):
"--write_to_movie",
"--media_dir",
str(tmp_path),
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
out, err, exit_code = capture(command)
@@ -116,7 +115,7 @@ def test_n_flag(tmp_path, simple_scenes_path):
"-n 3,6",
"--media_dir",
str(tmp_path),
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
_, err, exit_code = capture(command)
@@ -136,7 +135,7 @@ def test_s_flag_no_animations(tmp_path, manim_cfg_file, simple_scenes_path):
"-s",
"--media_dir",
str(tmp_path),
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
out, err, exit_code = capture(command)
@@ -161,7 +160,7 @@ def test_image_output_for_static_scene(tmp_path, manim_cfg_file, simple_scenes_p
"-ql",
"--media_dir",
str(tmp_path),
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
out, err, exit_code = capture(command)
@@ -189,7 +188,7 @@ def test_no_image_output_with_interactive_embed(
"-ql",
"--media_dir",
str(tmp_path),
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
out, err, exit_code = capture(command)
@@ -198,7 +197,7 @@ def test_no_image_output_with_interactive_embed(
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
+ is_empty = not any((tmp_path / "images" / "simple_scenes").iterdir())
assert (
is_empty
), "running manim static scene with interactive embed rendered an image"
@@ -218,7 +217,7 @@ def test_no_default_image_output_with_non_static_scene(
"-ql",
"--media_dir",
str(tmp_path),
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
out, err, exit_code = capture(command)
@@ -227,7 +226,7 @@ def test_no_default_image_output_with_non_static_scene(
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
+ is_empty = not any((tmp_path / "images" / "simple_scenes").iterdir())
assert (
is_empty
), "running manim static scene with interactive embed rendered an image"
@@ -248,14 +247,14 @@ def test_image_output_for_static_scene_with_write_to_movie(
"-ql",
"--media_dir",
str(tmp_path),
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
out, err, exit_code = capture(command)
assert exit_code == 0, err
- exists = len(os.listdir(tmp_path / "videos")) == 0
- assert not exists, "running manim with static scene rendered a video"
+ is_empty = not any((tmp_path / "videos").iterdir())
+ assert not is_empty, "running manim with static scene rendered a video"
is_empty = not any((tmp_path / "images" / "simple_scenes").iterdir())
assert not is_empty, "running manim without animations did not render an image"
@@ -274,7 +273,7 @@ def test_s_flag(tmp_path, manim_cfg_file, simple_scenes_path):
"-s",
"--media_dir",
str(tmp_path),
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
out, err, exit_code = capture(command)
@@ -302,7 +301,7 @@ def test_r_flag(tmp_path, manim_cfg_file, simple_scenes_path):
str(tmp_path),
"-r",
"200,100",
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
out, err, exit_code = capture(command)
@@ -330,7 +329,7 @@ def test_a_flag(tmp_path, manim_cfg_file, infallible_scenes_path):
"--media_dir",
str(tmp_path),
"-a",
- infallible_scenes_path,
+ str(infallible_scenes_path),
]
out, err, exit_code = capture(command)
assert exit_code == 0, err
@@ -369,7 +368,7 @@ def test_custom_folders(tmp_path, manim_cfg_file, simple_scenes_path):
"--media_dir",
str(tmp_path),
"--custom_folders",
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
out, err, exit_code = capture(command)
@@ -423,7 +422,7 @@ def test_gif_format_output(tmp_path, manim_cfg_file, simple_scenes_path):
str(tmp_path),
"--format",
"gif",
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
out, err, exit_code = capture(command)
@@ -461,7 +460,7 @@ def test_mp4_format_output(tmp_path, manim_cfg_file, simple_scenes_path):
str(tmp_path),
"--format",
"mp4",
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
out, err, exit_code = capture(command)
@@ -505,7 +504,7 @@ def test_videos_not_created_when_png_format_set(
str(tmp_path),
"--format",
"png",
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
out, err, exit_code = capture(command)
@@ -549,7 +548,7 @@ def test_images_are_created_when_png_format_set(
str(tmp_path),
"--format",
"png",
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
out, err, exit_code = capture(command)
@@ -580,7 +579,7 @@ def test_images_are_zero_padded_when_zero_pad_set(
"png",
"--zero_pad",
"3",
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
out, err, exit_code = capture(command)
@@ -610,7 +609,7 @@ def test_webm_format_output(tmp_path, manim_cfg_file, simple_scenes_path):
str(tmp_path),
"--format",
"webm",
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
out, err, exit_code = capture(command)
@@ -650,7 +649,7 @@ def test_default_format_output_for_transparent_flag(
"--media_dir",
str(tmp_path),
"-t",
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
out, err, exit_code = capture(command)
@@ -686,7 +685,7 @@ def test_mov_can_be_set_as_output_format(tmp_path, manim_cfg_file, simple_scenes
str(tmp_path),
"--format",
"mov",
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
out, err, exit_code = capture(command)
diff --git a/tests/test_scene_rendering/test_caching_related.py b/tests/test_scene_rendering/test_caching_related.py
index ae5b9a5c1..af2bb1ff9 100644
--- a/tests/test_scene_rendering/test_caching_related.py
+++ b/tests/test_scene_rendering/test_caching_related.py
@@ -26,7 +26,7 @@ def test_wait_skip(tmp_path, manim_cfg_file, simple_scenes_path):
str(tmp_path),
"-n",
"3",
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
out, err, exit_code = capture(command)
@@ -50,7 +50,7 @@ def test_play_skip(tmp_path, manim_cfg_file, simple_scenes_path):
str(tmp_path),
"-n",
"3",
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
out, err, exit_code = capture(command)
diff --git a/tests/test_scene_rendering/test_cli_flags.py b/tests/test_scene_rendering/test_cli_flags.py
index d48f5aac3..20bdf4fab 100644
--- a/tests/test_scene_rendering/test_cli_flags.py
+++ b/tests/test_scene_rendering/test_cli_flags.py
@@ -1,6 +1,5 @@
from __future__ import annotations
-import os
import sys
import numpy as np
@@ -28,7 +27,7 @@ def test_basic_scene_with_default_values(tmp_path, manim_cfg_file, simple_scenes
"manim",
"--media_dir",
str(tmp_path),
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
out, err, exit_code = capture(command)
@@ -86,7 +85,7 @@ def test_basic_scene_l_flag(tmp_path, manim_cfg_file, simple_scenes_path):
"-ql",
"--media_dir",
str(tmp_path),
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
out, err, exit_code = capture(command)
@@ -108,7 +107,7 @@ def test_n_flag(tmp_path, simple_scenes_path):
"-n 3,6",
"--media_dir",
str(tmp_path),
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
_, err, exit_code = capture(command)
@@ -126,7 +125,7 @@ def test_s_flag_no_animations(tmp_path, manim_cfg_file, simple_scenes_path):
"-s",
"--media_dir",
str(tmp_path),
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
out, err, exit_code = capture(command)
@@ -150,7 +149,7 @@ def test_s_flag(tmp_path, manim_cfg_file, simple_scenes_path):
"-s",
"--media_dir",
str(tmp_path),
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
out, err, exit_code = capture(command)
@@ -176,7 +175,7 @@ def test_s_flag_opengl_renderer(tmp_path, manim_cfg_file, simple_scenes_path):
"opengl",
"--media_dir",
str(tmp_path),
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
out, err, exit_code = capture(command)
@@ -202,7 +201,7 @@ def test_r_flag(tmp_path, manim_cfg_file, simple_scenes_path):
str(tmp_path),
"-r",
"200,100",
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
out, err, exit_code = capture(command)
@@ -227,7 +226,7 @@ def test_a_flag(tmp_path, manim_cfg_file, infallible_scenes_path):
"--media_dir",
str(tmp_path),
"-a",
- infallible_scenes_path,
+ str(infallible_scenes_path),
]
_, err, exit_code = capture(command)
assert exit_code == 0, err
@@ -264,7 +263,7 @@ def test_custom_folders(tmp_path, manim_cfg_file, simple_scenes_path):
"--media_dir",
str(tmp_path),
"--custom_folders",
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
out, err, exit_code = capture(command)
@@ -291,7 +290,7 @@ def test_custom_output_name_gif(tmp_path, simple_scenes_path):
"--format=gif",
"-o",
custom_name,
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
out, err, exit_code = capture(command)
@@ -337,7 +336,7 @@ def test_custom_output_name_mp4(tmp_path, simple_scenes_path):
str(tmp_path),
"-o",
custom_name,
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
out, err, exit_code = capture(command)
@@ -406,7 +405,7 @@ def test_gif_format_output(tmp_path, manim_cfg_file, simple_scenes_path):
str(tmp_path),
"--format",
"gif",
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
out, err, exit_code = capture(command)
@@ -442,7 +441,7 @@ def test_mp4_format_output(tmp_path, manim_cfg_file, simple_scenes_path):
str(tmp_path),
"--format",
"mp4",
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
out, err, exit_code = capture(command)
@@ -484,7 +483,7 @@ def test_videos_not_created_when_png_format_set(
str(tmp_path),
"--format",
"png",
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
out, err, exit_code = capture(command)
@@ -526,7 +525,7 @@ def test_images_are_created_when_png_format_set(
str(tmp_path),
"--format",
"png",
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
out, err, exit_code = capture(command)
@@ -555,7 +554,7 @@ def test_images_are_created_when_png_format_set_for_opengl(
str(tmp_path),
"--format",
"png",
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
out, err, exit_code = capture(command)
@@ -584,7 +583,7 @@ def test_images_are_zero_padded_when_zero_pad_set(
"png",
"--zero_pad",
"3",
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
out, err, exit_code = capture(command)
@@ -620,7 +619,7 @@ def test_images_are_zero_padded_when_zero_pad_set_for_opengl(
"png",
"--zero_pad",
"3",
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
out, err, exit_code = capture(command)
@@ -648,7 +647,7 @@ def test_webm_format_output(tmp_path, manim_cfg_file, simple_scenes_path):
str(tmp_path),
"--format",
"webm",
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
out, err, exit_code = capture(command)
@@ -685,7 +684,7 @@ def test_default_format_output_for_transparent_flag(
"--media_dir",
str(tmp_path),
"-t",
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
out, err, exit_code = capture(command)
@@ -719,7 +718,7 @@ def test_mov_can_be_set_as_output_format(tmp_path, manim_cfg_file, simple_scenes
str(tmp_path),
"--format",
"mov",
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
out, err, exit_code = capture(command)
@@ -747,13 +746,12 @@ def test_mov_can_be_set_as_output_format(tmp_path, manim_cfg_file, simple_scenes
)
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"""
+ (tmp_path / "manim.cfg").write_text(
+ f"""
[CLI]
input_file = {simple_scenes_path}
- """
- )
+ """
+ )
command = [
sys.executable,
diff --git a/tests/test_scene_rendering/test_file_writer.py b/tests/test_scene_rendering/test_file_writer.py
index f7ead0d48..8067dcee4 100644
--- a/tests/test_scene_rendering/test_file_writer.py
+++ b/tests/test_scene_rendering/test_file_writer.py
@@ -18,7 +18,7 @@ def test_unicode_partial_movie(tmpdir, simple_scenes_path):
"manim",
"--media_dir",
str(tmpdir / unicode_str),
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
diff --git a/tests/test_scene_rendering/test_sections.py b/tests/test_scene_rendering/test_sections.py
index 4291154df..049802f3c 100644
--- a/tests/test_scene_rendering/test_sections.py
+++ b/tests/test_scene_rendering/test_sections.py
@@ -1,6 +1,5 @@
from __future__ import annotations
-import os
import sys
import pytest
@@ -25,15 +24,15 @@ def test_no_sections(tmp_path, manim_cfg_file, simple_scenes_path):
"-ql",
"--media_dir",
str(tmp_path),
- simple_scenes_path,
+ str(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")
+ scene_dir = tmp_path / "videos" / "simple_scenes" / "480p15"
assert_dir_exists(scene_dir)
- assert_dir_not_exists(os.path.join(scene_dir, "sections"))
+ assert_dir_not_exists(scene_dir / "sections")
@pytest.mark.slow
@@ -51,15 +50,15 @@ def test_sections(tmp_path, manim_cfg_file, simple_scenes_path):
"--save_sections",
"--media_dir",
str(tmp_path),
- simple_scenes_path,
+ str(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")
+ scene_dir = tmp_path / "videos" / "simple_scenes" / "480p15"
assert_dir_exists(scene_dir)
- assert_dir_exists(os.path.join(scene_dir, "sections"))
+ assert_dir_exists(scene_dir / "sections")
@pytest.mark.slow
@@ -77,7 +76,7 @@ def test_many_sections(tmp_path, manim_cfg_file, simple_scenes_path):
"--save_sections",
"--media_dir",
str(tmp_path),
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
_, err, exit_code = capture(command)
@@ -99,7 +98,7 @@ def test_skip_animations(tmp_path, manim_cfg_file, simple_scenes_path):
"--save_sections",
"--media_dir",
str(tmp_path),
- simple_scenes_path,
+ str(simple_scenes_path),
scene_name,
]
_, err, exit_code = capture(command)
diff --git a/tests/utils/logging_tester.py b/tests/utils/logging_tester.py
index 5e40f4219..d2501c8c2 100644
--- a/tests/utils/logging_tester.py
+++ b/tests/utils/logging_tester.py
@@ -4,16 +4,16 @@ import itertools
import json
import os
from functools import wraps
+from pathlib import Path
import pytest
-def _check_logs(reference_logfile, generated_logfile):
- with open(reference_logfile) as reference_logs, open(
- generated_logfile,
- ) as generated_logs:
- reference_logs = reference_logs.readlines()
- generated_logs = generated_logs.readlines()
+def _check_logs(reference_logfile_path: Path, generated_logfile_path: Path) -> None:
+ with reference_logfile_path.open() as reference_logfile:
+ reference_logs = reference_logfile.readlines()
+ with generated_logfile_path.open() as generated_logfile:
+ generated_logs = generated_logfile.readlines()
diff = abs(len(reference_logs) - len(generated_logs))
if len(reference_logs) != len(generated_logs):
msg_assert = ""
@@ -26,7 +26,7 @@ def _check_logs(reference_logfile, generated_logfile):
for log in generated_logs[len(reference_logs) :]:
msg_assert += log
msg_assert += f"\nPath of reference log: {reference_logfile}\nPath of generated logs: {generated_logfile}"
- pytest.fail(msg_assert + reference_logfile + " " + generated_logfile)
+ pytest.fail(msg_assert)
for index, ref, gen in zip(itertools.count(), reference_logs, generated_logs):
# As they are string, we only need to check if they are equal. If they are not, we then compute a more precise difference, to debug.
@@ -46,17 +46,19 @@ def _check_logs(reference_logfile, generated_logfile):
)
-def logs_comparison(control_data_file, log_path_from_media_dir):
+def logs_comparison(
+ control_data_file: str | os.PathLike, log_path_from_media_dir: str | os.PathLike
+):
"""Decorator used for any test that needs to check logs.
Parameters
----------
- control_data_file : :class:`str`
+ control_data_file
Name of the control data file, i.e. .log that will be compared to the outputted logs.
.. warning:: You don't have to pass the path here.
.. example:: "SquareToCircleWithLFlag.log"
- log_path_from_media_dir : :class:`str`
+ log_path_from_media_dir
The path of the .log generated, from the media dir. Example: /logs/Square.log.
Returns
@@ -65,31 +67,29 @@ def logs_comparison(control_data_file, log_path_from_media_dir):
The test wrapped with which we are going to make the comparison.
"""
+ control_data_file = Path(control_data_file)
+ log_path_from_media_dir = Path(log_path_from_media_dir)
+
def decorator(f):
@wraps(f)
def wrapper(*args, **kwargs):
# NOTE : Every args goes seemingly in kwargs instead of args; this is perhaps Pytest.
result = f(*args, **kwargs)
tmp_path = kwargs["tmp_path"]
- tests_directory = os.path.dirname(
- os.path.dirname(os.path.abspath(__file__)),
- )
- control_data_path = os.path.join(
- tests_directory,
- "control_data",
- "logs_data",
- control_data_file,
+ tests_directory = Path(__file__).absolute().parent.parent
+ control_data_path = (
+ tests_directory / "control_data" / "logs_data" / control_data_file
)
path_log_generated = tmp_path / log_path_from_media_dir
# The following will say precisely which subdir does not exist.
- if not os.path.exists(path_log_generated):
+ if not path_log_generated.exists():
for parent in reversed(path_log_generated.parents):
if not parent.exists():
pytest.fail(
f"'{parent.name}' does not exist in '{parent.parent}' (which exists). ",
)
break
- _check_logs(control_data_path, str(path_log_generated))
+ _check_logs(control_data_path, path_log_generated)
return result
return wrapper
diff --git a/tests/utils/video_tester.py b/tests/utils/video_tester.py
index 643a82b8f..2b74e6540 100644
--- a/tests/utils/video_tester.py
+++ b/tests/utils/video_tester.py
@@ -4,6 +4,7 @@ import json
import os
from functools import wraps
from pathlib import Path
+from typing import Any
from manim import get_video_metadata
@@ -11,12 +12,12 @@ from ..assert_utils import assert_shallow_dict_compare
from ..helpers.video_utils import get_section_dir_layout, get_section_index
-def load_control_data(path_to_data):
- with open(path_to_data) as f:
+def load_control_data(path_to_data: Path) -> Any:
+ with path_to_data.open() as f:
return json.load(f)
-def check_video_data(path_control_data, path_video_gen):
+def check_video_data(path_control_data: Path, path_video_gen: Path) -> None:
"""Compare control data with generated output.
Used abbreviations:
exp -> expected
@@ -25,7 +26,7 @@ def check_video_data(path_control_data, path_video_gen):
meta -> metadata
"""
# movie file specification
- path_sec_gen = Path(path_video_gen).parent.absolute() / "sections"
+ path_sec_gen = path_video_gen.parent.absolute() / "sections"
control_data = load_control_data(path_control_data)
movie_meta_gen = get_video_metadata(path_video_gen)
movie_meta_exp = control_data["movie_metadata"]
@@ -48,7 +49,7 @@ def check_video_data(path_control_data, path_video_gen):
raise AssertionError(f"Sections don't match:\n{mismatch}")
# sections index file
- scene_name = Path(path_video_gen).stem
+ scene_name = path_video_gen.stem
path_sec_index_gen = path_sec_gen / f"{scene_name}.json"
sec_index_gen = get_section_index(path_sec_index_gen)
sec_index_exp = control_data["section_index"]
@@ -67,7 +68,9 @@ def check_video_data(path_control_data, path_video_gen):
)
-def video_comparison(control_data_file, scene_path_from_media_dir):
+def video_comparison(
+ control_data_file: str | os.PathLike, scene_path_from_media_dir: str | os.PathLike
+):
"""Decorator used for any test that needs to check a rendered scene/video.
.. warning::
@@ -76,11 +79,11 @@ def video_comparison(control_data_file, scene_path_from_media_dir):
Parameters
----------
- control_data_file : :class:`str`
+ control_data_file
Name of the control data file, i.e. the .json containing all the pre-rendered references of the scene tested.
.. warning:: You don't have to pass the path here.
- scene_path_from_media_dir : :class:`str`
+ scene_path_from_media_dir
The path of the scene generated, from the media dir. Example: /videos/1080p60/SquareToCircle.mp4.
See Also
@@ -88,31 +91,29 @@ def video_comparison(control_data_file, scene_path_from_media_dir):
tests/helpers/video_utils.py : create control data
"""
+ control_data_file = Path(control_data_file)
+ scene_path_from_media_dir = Path(scene_path_from_media_dir)
+
def decorator(f):
@wraps(f)
def wrapper(*args, **kwargs):
# NOTE : Every args goes seemingly in kwargs instead of args; this is perhaps Pytest.
result = f(*args, **kwargs)
tmp_path = kwargs["tmp_path"]
- tests_directory = os.path.dirname(
- os.path.dirname(os.path.abspath(__file__)),
- )
- path_control_data = os.path.join(
- tests_directory,
- "control_data",
- "videos_data",
- control_data_file,
+ tests_directory = Path(__file__).absolute().parent.parent
+ path_control_data = (
+ tests_directory / "control_data" / "videos_data" / control_data_file
)
path_video_gen = tmp_path / scene_path_from_media_dir
- if not os.path.exists(path_video_gen):
+ if not path_video_gen.exists():
for parent in reversed(path_video_gen.parents):
if not parent.exists():
raise AssertionError(
f"'{parent.name}' does not exist in '{parent.parent}' (which exists). ",
)
# TODO: use when pytest --set_test option
- # save_control_data_from_video(path_video_gen, control_data_file[:-5])
- check_video_data(path_control_data, str(path_video_gen))
+ # save_control_data_from_video(path_video_gen, control_data_file.stem)
+ check_video_data(path_control_data, path_video_gen)
return result
return wrapper