Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change from tempconfig to a config fixture in tests #3853

Merged
merged 2 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import pytest

from manim import config, tempconfig
import manim


def pytest_addoption(parser):
Expand Down Expand Up @@ -45,6 +45,20 @@ def pytest_collection_modifyitems(config, items):
item.add_marker(slow_skip)


@pytest.fixture
def config():
saved = manim.config.copy()
# we need to return the actual config so that tests
# using tempconfig pass
yield manim.config
manim.config.update(saved)


@pytest.fixture
def dry_run(config):
config.dry_run = True


@pytest.fixture(scope="session")
def python_version():
# use the same python executable as it is running currently
Expand All @@ -62,10 +76,10 @@ def reset_cfg_file():


@pytest.fixture
def using_opengl_renderer():
def using_opengl_renderer(config):
"""Standard fixture for running with opengl that makes tests use a standard_config.cfg with a temp dir."""
with tempconfig({"renderer": "opengl"}):
yield
config.renderer = "opengl"
yield
# as a special case needed to manually revert back to cairo
# due to side effects of setting the renderer
config.renderer = "cairo"
6 changes: 3 additions & 3 deletions tests/helpers/graphical_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

import numpy as np

from manim import config, logger
from manim import logger
from manim.scene.scene import Scene


def set_test_scene(scene_object: type[Scene], module_name: str):
def set_test_scene(scene_object: type[Scene], module_name: str, config):
"""Function used to set up the test data for a new feature. This will basically set up a pre-rendered frame for a scene. This is meant to be used only
when setting up tests. Please refer to the wiki.
Expand Down Expand Up @@ -46,7 +46,7 @@ def set_test_scene(scene_object: type[Scene], module_name: str):

assert not np.all(
data == np.array([0, 0, 0, 255]),
), f"Control data generated for {str(scene)} only contains empty pixels."
), f"Control data generated for {scene!s} only contains empty pixels."
assert data.shape == (480, 854, 4)
tests_directory = Path(__file__).absolute().parent.parent
path_control_data = Path(tests_directory) / "control_data" / "graphical_units_data"
Expand Down
4 changes: 2 additions & 2 deletions tests/module/animation/test_animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from manim import FadeIn, Scene, config
from manim import FadeIn, Scene


@pytest.mark.parametrize(
Expand All @@ -15,7 +15,7 @@ def test_animation_forbidden_run_time(run_time):
test_scene.play(FadeIn(None, run_time=run_time))


def test_animation_run_time_shorter_than_frame_rate(caplog):
def test_animation_run_time_shorter_than_frame_rate(caplog, config):
test_scene = Scene()
test_scene.play(FadeIn(None, run_time=1 / (config.frame_rate + 1)))
assert (
Expand Down
4 changes: 2 additions & 2 deletions tests/module/animation/test_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy as np
import pytest

from manim import AddTextLetterByLetter, Text, config
from manim import AddTextLetterByLetter, Text


def test_non_empty_text_creation():
Expand All @@ -25,7 +25,7 @@ def test_whitespace_text_creation():
AddTextLetterByLetter(Text(" "))


def test_run_time_for_non_empty_text():
def test_run_time_for_non_empty_text(config):
"""Ensure the run_time is calculated correctly for non-empty text."""
s = Text("Hello")
run_time_per_char = 0.1
Expand Down
4 changes: 2 additions & 2 deletions tests/module/mobject/mobject/test_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from pathlib import Path

from manim import BraceLabel, Mobject, config
from manim import BraceLabel, Mobject


def test_mobject_copy():
Expand All @@ -18,7 +18,7 @@ def test_mobject_copy():
assert orig.submobjects[i] is not copy.submobjects[i]


def test_bracelabel_copy(tmp_path):
def test_bracelabel_copy(tmp_path, config):
"""Test that a copy is a deepcopy."""
# For this test to work, we need to tweak some folders temporarily
original_text_dir = config["text_dir"]
Expand Down
16 changes: 8 additions & 8 deletions tests/module/mobject/mobject/test_opengl_metaclass.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
from __future__ import annotations

from manim import Mobject, config, tempconfig
from manim import Mobject
from manim.mobject.opengl.opengl_compatibility import ConvertToOpenGL
from manim.mobject.opengl.opengl_mobject import OpenGLMobject


def test_metaclass_registry():
def test_metaclass_registry(config):
class SomeTestMobject(Mobject, metaclass=ConvertToOpenGL):
pass

assert SomeTestMobject in ConvertToOpenGL._converted_classes

with tempconfig({"renderer": "opengl"}):
assert OpenGLMobject in SomeTestMobject.__bases__
assert Mobject not in SomeTestMobject.__bases__
config.renderer = "opengl"
assert OpenGLMobject in SomeTestMobject.__bases__
assert Mobject not in SomeTestMobject.__bases__

config.renderer = "cairo"
assert Mobject in SomeTestMobject.__bases__
assert OpenGLMobject not in SomeTestMobject.__bases__
config.renderer = "cairo"
assert Mobject in SomeTestMobject.__bases__
assert OpenGLMobject not in SomeTestMobject.__bases__
7 changes: 1 addition & 6 deletions tests/module/mobject/mobject/test_set_attr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

import numpy as np

from manim import RendererType, config
from manim.constants import RIGHT
from manim.mobject.geometry.polygram import Square


def test_Data():
config.renderer = RendererType.OPENGL
def test_Data(using_opengl_renderer):
a = Square().move_to(RIGHT)
data_bb = a.data["bounding_box"]
np.testing.assert_array_equal(
Expand Down Expand Up @@ -39,6 +37,3 @@ def test_Data():
)

np.testing.assert_array_equal(a.bounding_box, data_bb)
config.renderer = (
RendererType.CAIRO
) # needs to be here or else the following cairo tests fail
32 changes: 16 additions & 16 deletions tests/module/mobject/text/test_texmobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
import numpy as np
import pytest

from manim import MathTex, SingleStringMathTex, Tex, TexTemplate, config, tempconfig
from manim import MathTex, SingleStringMathTex, Tex, TexTemplate, tempconfig


def test_MathTex():
def test_MathTex(config):
MathTex("a^2 + b^2 = c^2")
assert Path(config.media_dir, "Tex", "e4be163a00cf424f.svg").exists()


def test_SingleStringMathTex():
def test_SingleStringMathTex(config):
SingleStringMathTex("test")
assert Path(config.media_dir, "Tex", "8ce17c7f5013209f.svg").exists()

Expand All @@ -27,7 +27,7 @@ def test_double_braces_testing(text_input, length_sub):
assert len(t1.submobjects) == length_sub


def test_tex():
def test_tex(config):
Tex("The horse does not eat cucumber salad.")
assert Path(config.media_dir, "Tex", "c3945e23e546c95a.svg").exists()

Expand All @@ -45,7 +45,7 @@ def test_tex_temp_directory(tmpdir, monkeypatch):
assert Path("media", "Tex", "c3945e23e546c95a.svg").exists()


def test_percent_char_rendering():
def test_percent_char_rendering(config):
Tex(r"\%")
assert Path(config.media_dir, "Tex", "4a583af4d19a3adf.tex").exists()

Expand Down Expand Up @@ -194,33 +194,33 @@ def test_error_in_nested_context(capsys):
\end{align}
"""

with pytest.raises(ValueError) as err:
with pytest.raises(ValueError):
Tex(invalid_tex)

stdout = str(capsys.readouterr().out)
# validate useless context is not included
assert r"\begin{frame}" not in stdout


def test_tempconfig_resetting_tex_template():
def test_tempconfig_resetting_tex_template(config):
my_template = TexTemplate()
my_template.preamble = "Custom preamble!"
tex_template_config_value = config.tex_template
with tempconfig({"tex_template": my_template}):
assert config.tex_template.preamble == "Custom preamble!"

assert config.tex_template.preamble != "Custom preamble!"


def test_tex_garbage_collection(tmpdir, monkeypatch):
def test_tex_garbage_collection(tmpdir, monkeypatch, config):
monkeypatch.chdir(tmpdir)
Path(tmpdir, "media").mkdir()
config.media_dir = "media"

with tempconfig({"media_dir": "media"}):
tex_without_log = Tex("Hello World!") # d771330b76d29ffb.tex
assert Path("media", "Tex", "d771330b76d29ffb.tex").exists()
assert not Path("media", "Tex", "d771330b76d29ffb.log").exists()
tex_without_log = Tex("Hello World!") # d771330b76d29ffb.tex
assert Path("media", "Tex", "d771330b76d29ffb.tex").exists()
assert not Path("media", "Tex", "d771330b76d29ffb.log").exists()

config.no_latex_cleanup = True

with tempconfig({"media_dir": "media", "no_latex_cleanup": True}):
tex_with_log = Tex("Hello World, again!") # da27670a37b08799.tex
assert Path("media", "Tex", "da27670a37b08799.log").exists()
tex_with_log = Tex("Hello World, again!") # da27670a37b08799.tex
assert Path("media", "Tex", "da27670a37b08799.log").exists()
Loading
Loading