Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Fix tests (#1571)

Naveen M K6e4b02a4d125
diff --git a/conftest.py b/conftest.py
new file mode 100644
index 000000000..faebe648d
--- /dev/null
+++ b/conftest.py
@@ -0,0 +1,25 @@
+# This file is automatically picked by pytest
+# while running tests. So, that each test is
+# run on difference temporary directories and avoiding
+# errors.
+
+# If it is running Doctest the current directory
+# is changed because it also tests the config module
+# itself. If it's a normal test then it uses the
+# tempconfig to change directories.
+
+import pytest
+from _pytest.doctest import DoctestItem
+
+from manim import config, tempconfig
+
+
[email protected](autouse=True)
+def temp_media_dir(tmpdir, monkeypatch, request):
+ if isinstance(request.node, DoctestItem):
+ monkeypatch.chdir(tmpdir)
+ yield tmpdir
+ else:
+ with tempconfig({"media_dir": str(tmpdir)}):
+ assert config.media_dir == str(tmpdir)
+ yield tmpdir
diff --git a/tests/conftest.py b/tests/conftest.py
index 5092d3ace..f90a4d850 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -3,8 +3,6 @@ import sys
import pytest
-from manim import config, tempconfig
-
def pytest_addoption(parser):
parser.addoption(
@@ -53,10 +51,3 @@ def reset_cfg_file():
yield
with open(cfgfilepath, "w") as cfgfile:
cfgfile.write(original)
-
-
[email protected](autouse=True)
-def temp_media_dir(tmpdir):
- with tempconfig({"media_dir": str(tmpdir)}):
- assert config.media_dir == str(tmpdir)
- yield tmpdir
diff --git a/tests/test_plugins/simple_scenes.py b/tests/test_plugins/simple_scenes.py
index 2933687a5..187c62de9 100644
--- a/tests/test_plugins/simple_scenes.py
+++ b/tests/test_plugins/simple_scenes.py
@@ -15,13 +15,6 @@ class FunctionLikeTest(Scene):
self.play(FadeIn(a))
-class NoAllTest(Scene):
- def construct(self):
- assert "test_plugin" in globals()
- a = test_plugin.NoAll()
- self.play(FadeIn(a))
-
-
class WithAllTest(Scene):
def construct(self):
assert "WithAll" in globals()
diff --git a/tests/test_plugins/test_plugins.py b/tests/test_plugins/test_plugins.py
index 52f6c0f5d..032571ef7 100644
--- a/tests/test_plugins/test_plugins.py
+++ b/tests/test_plugins/test_plugins.py
@@ -1,4 +1,6 @@
-import shutil
+import random
+import string
+import tempfile
import textwrap
from pathlib import Path
@@ -7,28 +9,29 @@ import pytest
from ..utils.commands import capture
plugin_pyproject_template = textwrap.dedent(
+ """\
+ [tool.poetry]
+ name = "{plugin_name}"
+ authors = ["ManimCE Test Suite"]
+ version = "0.1.0"
+ description = ""
+
+ [tool.poetry.dependencies]
+ python = "^3.7"
+
+ [tool.poetry.plugins."manim.plugins"]
+ "{plugin_name}" = "{plugin_entrypoint}"
+
+ [build-system]
+ requires = ["poetry-core>=1.0.0"]
+ build-backend = "poetry.core.masonry.api"
"""
- [tool.poetry]
- name = "{plugin_name}"
- authors = ["ManimCE"]
- version = "0.1.0"
- description = ""
-
- [tool.poetry.dependencies]
- python = "^3.6"
-
- [tool.poetry.plugins."manim.plugins"]
- "{plugin_name}" = "{plugin_entrypoint}"
-
- [build-system]
- requires = ["poetry-core>=1.0.0"]
- build-backend = "poetry.core.masonry.api"
- """
)
plugin_init_template = textwrap.dedent(
- """
+ """\
from manim import *
+ {all_dec}
class {class_name}(VMobject):
def __init__(self):
super().__init__()
@@ -46,15 +49,16 @@ plugin_init_template = textwrap.dedent(
)
cfg_file_contents = textwrap.dedent(
- """
+ """\
[CLI]
- plugins = test_plugin
+ plugins = {plugin_name}
"""
)
def simple_scenes_path():
- return str(Path(__file__).parent / "simple_scenes.py")
+ yield str(Path(__file__).parent / "simple_scenes.py")
def cfg_file_create(cfg_file_contents, path):
@@ -64,14 +68,18 @@ def cfg_file_create(cfg_file_contents, path):
return file_loc
-def test_plugin_warning(tmp_path, python_version):
- cfg_file_contents = textwrap.dedent(
- """
- [CLI]
- plugins = DNEPlugin
- """
+def random_string():
+ all_letters = string.ascii_lowercase
+ a = random.Random()
+ final_letters = [a.choice(all_letters) for _ in range(8)]
+ yield "".join(final_letters)
+
+
+def test_plugin_warning(tmp_path, python_version, simple_scenes_path):
+ cfg_file = cfg_file_create(
+ cfg_file_contents.format(plugin_name="DNEplugin"), tmp_path
)
- cfg_file = cfg_file_create(cfg_file_contents, tmp_path)
scene_name = "SquareToCircle"
command = [
python_version,
@@ -82,7 +90,7 @@ def test_plugin_warning(tmp_path, python_version):
str(cfg_file.parent),
"--config_file",
str(cfg_file),
- simple_scenes_path(),
+ simple_scenes_path,
scene_name,
]
out, err, exit_code = capture(command, cwd=str(cfg_file.parent))
@@ -91,38 +99,43 @@ def test_plugin_warning(tmp_path, python_version):
@pytest.fixture
-def function_like_plugin(tmp_path, python_version):
- plugin_name = "test_plugin"
- entry_point = f"{plugin_name}.__init__:import_all"
- plugin_dir = tmp_path / "function_entrypoint"
- 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="FunctionLike",
- function_name="import_all",
+def create_plugin(tmp_path, python_version, random_string):
+ plugin_dir = tmp_path / "plugin_dir"
+ plugin_name = random_string
+
+ 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,
+ 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
- yield module_dir
- shutil.rmtree(plugin_dir)
+ 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,
+ }
+
+ yield _create_plugin
command = [python_version, "-m", "pip", "uninstall", plugin_name, "-y"]
out, err, exit_code = capture(command)
print(out)
@@ -130,8 +143,16 @@ def function_like_plugin(tmp_path, python_version):
@pytest.mark.slow
-def test_plugin_function_like(tmp_path, function_like_plugin, python_version):
- cfg_file = cfg_file_create(cfg_file_contents, tmp_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,
@@ -142,56 +163,37 @@ def test_plugin_function_like(tmp_path, function_like_plugin, python_version):
str(cfg_file.parent),
"--config_file",
str(cfg_file),
- simple_scenes_path(),
+ simple_scenes_path,
scene_name,
]
out, err, exit_code = capture(command, cwd=str(cfg_file.parent))
print(out)
- assert exit_code == 0, err
-
-
-def module_no_all_plugin(tmp_path, python_version):
- plugin_name = "test_plugin"
- entry_point = f"{plugin_name}"
- plugin_dir = tmp_path / "module_entrypoint_no_all"
- 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="NoAll",
- function_name="import_all",
- )
- )
- 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
- yield module_dir
- shutil.rmtree(plugin_dir)
- command = [python_version, "-m", "pip", "uninstall", plugin_name, "-y"]
- out, err, exit_code = capture(command)
- print(out)
+ print(err)
assert exit_code == 0, err
@pytest.mark.slow
-def test_plugin_no_all(tmp_path, module_no_all_plugin, python_version):
- cfg_file = cfg_file_create(cfg_file_contents, tmp_path)
+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,
@@ -202,58 +204,28 @@ def test_plugin_no_all(tmp_path, module_no_all_plugin, python_version):
str(cfg_file.parent),
"--config_file",
str(cfg_file),
- simple_scenes_path(),
+ tmpfile.name,
scene_name,
]
out, err, exit_code = capture(command, cwd=str(cfg_file.parent))
print(out)
print(err)
assert exit_code == 0, err
-
-
-def module_with_all_plugin(tmp_path, python_version):
- plugin_name = "test_plugin"
- entry_point = f"{plugin_name}"
- plugin_dir = tmp_path / "module_entrypoint_with_all"
- module_dir = plugin_dir / plugin_name
- module_dir.mkdir(parents=True)
- with open(module_dir / "__init__.py", "w") as f:
- f.write("__all__=['WithAll']\n")
- f.write(
- plugin_init_template.format(
- class_name="WithAll",
- function_name="import_all",
- )
- )
- 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
- yield module_dir
- shutil.rmtree(plugin_dir)
- command = [python_version, "-m", "pip", "uninstall", plugin_name, "-y"]
- out, err, exit_code = capture(command)
- print(out)
- assert exit_code == 0, err
+ Path(tmpfile.name).unlink()
@pytest.mark.slow
-def test_plugin_with_all(tmp_path, module_with_all_plugin, python_version):
- cfg_file = cfg_file_create(cfg_file_contents, tmp_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,
@@ -264,7 +236,7 @@ def test_plugin_with_all(tmp_path, module_with_all_plugin, python_version):
str(cfg_file.parent),
"--config_file",
str(cfg_file),
- simple_scenes_path(),
+ simple_scenes_path,
scene_name,
]
out, err, exit_code = capture(command, cwd=str(cfg_file.parent))
diff --git a/tests/test_scene_rendering/test_cairo_renderer.py b/tests/test_scene_rendering/test_cairo_renderer.py
index 7e1f355d9..ea4d849d9 100644
--- a/tests/test_scene_rendering/test_cairo_renderer.py
+++ b/tests/test_scene_rendering/test_cairo_renderer.py
@@ -1,6 +1,8 @@
import os
from unittest.mock import Mock, patch
+import pytest
+
from manim import *
from ..assert_utils import assert_file_exists
@@ -35,6 +37,7 @@ def test_skipping_status_with_from_to_and_up_to(using_temp_config, disabling_cac
SceneWithMultipleCalls().render()
[email protected](reason="caching issue")
def test_when_animation_is_cached(using_temp_config):
partial_movie_files = []
for _ in range(2):