Skip to content

Commit

Permalink
Migrate more os.path to pathlib in tests (ManimCommunity#2991)
Browse files Browse the repository at this point in the history
* Migrate more `os.path` to `pathlib` in tests

* Convert test fixtures to pathlib

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix mypy errors in tests

* migrate another pathlib instance

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Benjamin Hackl <devel@benjamin-hackl.at>
3 people authored Oct 26, 2022
1 parent 78a3b06 commit 206db54
Showing 20 changed files with 154 additions and 169 deletions.
3 changes: 1 addition & 2 deletions example_scenes/opengl.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
from pathlib import Path

import manim.utils.opengl as opengl
@@ -462,7 +461,7 @@ def construct(self):
# 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"
)
3 changes: 2 additions & 1 deletion manim/utils/commands.py
Original file line number Diff line number Diff line change
@@ -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",
10 changes: 5 additions & 5 deletions tests/assert_utils.py
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@
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)

10 changes: 4 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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
10 changes: 5 additions & 5 deletions tests/helpers/video_utils.py
Original file line number Diff line number Diff line change
@@ -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,17 +43,17 @@ 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
--------
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}")
10 changes: 4 additions & 6 deletions tests/interface/test_commands.py
Original file line number Diff line number Diff line change
@@ -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():
4 changes: 2 additions & 2 deletions tests/module/utils/test_file_ops.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion tests/opengl/test_ipython_magic_opengl.py
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 1 addition & 1 deletion tests/test_ipython_magic.py
Original file line number Diff line number Diff line change
@@ -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()
17 changes: 6 additions & 11 deletions tests/test_logging/test_logging.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

import os
from pathlib import Path

from manim import capture
@@ -10,14 +9,10 @@

@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)
39 changes: 18 additions & 21 deletions tests/test_plugins/test_plugins.py
Original file line number Diff line number Diff line change
@@ -60,13 +60,12 @@ def {function_name}():

@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(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,
),
)
(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))
6 changes: 3 additions & 3 deletions tests/test_scene_rendering/conftest.py
Original file line number Diff line number Diff line change
@@ -9,12 +9,12 @@

@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
Original file line number Diff line number Diff line change
@@ -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)
Loading
Oops, something went wrong.

0 comments on commit 206db54

Please sign in to comment.