Skip to content

Commit 7562596

Browse files
Change from tempconfig to a config fixture in tests (#3853)
* Implement changes to fixtures * Change tests to use the config fixture
1 parent 67f95db commit 7562596

21 files changed

+431
-427
lines changed

tests/conftest.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import pytest
77

8-
from manim import config, tempconfig
8+
import manim
99

1010

1111
def pytest_addoption(parser):
@@ -45,6 +45,20 @@ def pytest_collection_modifyitems(config, items):
4545
item.add_marker(slow_skip)
4646

4747

48+
@pytest.fixture
49+
def config():
50+
saved = manim.config.copy()
51+
# we need to return the actual config so that tests
52+
# using tempconfig pass
53+
yield manim.config
54+
manim.config.update(saved)
55+
56+
57+
@pytest.fixture
58+
def dry_run(config):
59+
config.dry_run = True
60+
61+
4862
@pytest.fixture(scope="session")
4963
def python_version():
5064
# use the same python executable as it is running currently
@@ -62,10 +76,10 @@ def reset_cfg_file():
6276

6377

6478
@pytest.fixture
65-
def using_opengl_renderer():
79+
def using_opengl_renderer(config):
6680
"""Standard fixture for running with opengl that makes tests use a standard_config.cfg with a temp dir."""
67-
with tempconfig({"renderer": "opengl"}):
68-
yield
81+
config.renderer = "opengl"
82+
yield
6983
# as a special case needed to manually revert back to cairo
7084
# due to side effects of setting the renderer
7185
config.renderer = "cairo"

tests/helpers/graphical_units.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77

88
import numpy as np
99

10-
from manim import config, logger
10+
from manim import logger
1111
from manim.scene.scene import Scene
1212

1313

14-
def set_test_scene(scene_object: type[Scene], module_name: str):
14+
def set_test_scene(scene_object: type[Scene], module_name: str, config):
1515
"""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
1616
when setting up tests. Please refer to the wiki.
1717
@@ -46,7 +46,7 @@ def set_test_scene(scene_object: type[Scene], module_name: str):
4646

4747
assert not np.all(
4848
data == np.array([0, 0, 0, 255]),
49-
), f"Control data generated for {str(scene)} only contains empty pixels."
49+
), f"Control data generated for {scene!s} only contains empty pixels."
5050
assert data.shape == (480, 854, 4)
5151
tests_directory = Path(__file__).absolute().parent.parent
5252
path_control_data = Path(tests_directory) / "control_data" / "graphical_units_data"

tests/module/animation/test_animation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import pytest
44

5-
from manim import FadeIn, Scene, config
5+
from manim import FadeIn, Scene
66

77

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

1717

18-
def test_animation_run_time_shorter_than_frame_rate(caplog):
18+
def test_animation_run_time_shorter_than_frame_rate(caplog, config):
1919
test_scene = Scene()
2020
test_scene.play(FadeIn(None, run_time=1 / (config.frame_rate + 1)))
2121
assert (

tests/module/animation/test_creation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import numpy as np
44
import pytest
55

6-
from manim import AddTextLetterByLetter, Text, config
6+
from manim import AddTextLetterByLetter, Text
77

88

99
def test_non_empty_text_creation():
@@ -25,7 +25,7 @@ def test_whitespace_text_creation():
2525
AddTextLetterByLetter(Text(" "))
2626

2727

28-
def test_run_time_for_non_empty_text():
28+
def test_run_time_for_non_empty_text(config):
2929
"""Ensure the run_time is calculated correctly for non-empty text."""
3030
s = Text("Hello")
3131
run_time_per_char = 0.1

tests/module/mobject/mobject/test_copy.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from pathlib import Path
44

5-
from manim import BraceLabel, Mobject, config
5+
from manim import BraceLabel, Mobject
66

77

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

2020

21-
def test_bracelabel_copy(tmp_path):
21+
def test_bracelabel_copy(tmp_path, config):
2222
"""Test that a copy is a deepcopy."""
2323
# For this test to work, we need to tweak some folders temporarily
2424
original_text_dir = config["text_dir"]
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
from __future__ import annotations
22

3-
from manim import Mobject, config, tempconfig
3+
from manim import Mobject
44
from manim.mobject.opengl.opengl_compatibility import ConvertToOpenGL
55
from manim.mobject.opengl.opengl_mobject import OpenGLMobject
66

77

8-
def test_metaclass_registry():
8+
def test_metaclass_registry(config):
99
class SomeTestMobject(Mobject, metaclass=ConvertToOpenGL):
1010
pass
1111

1212
assert SomeTestMobject in ConvertToOpenGL._converted_classes
1313

14-
with tempconfig({"renderer": "opengl"}):
15-
assert OpenGLMobject in SomeTestMobject.__bases__
16-
assert Mobject not in SomeTestMobject.__bases__
14+
config.renderer = "opengl"
15+
assert OpenGLMobject in SomeTestMobject.__bases__
16+
assert Mobject not in SomeTestMobject.__bases__
1717

18-
config.renderer = "cairo"
19-
assert Mobject in SomeTestMobject.__bases__
20-
assert OpenGLMobject not in SomeTestMobject.__bases__
18+
config.renderer = "cairo"
19+
assert Mobject in SomeTestMobject.__bases__
20+
assert OpenGLMobject not in SomeTestMobject.__bases__

tests/module/mobject/mobject/test_set_attr.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22

33
import numpy as np
44

5-
from manim import RendererType, config
65
from manim.constants import RIGHT
76
from manim.mobject.geometry.polygram import Square
87

98

10-
def test_Data():
11-
config.renderer = RendererType.OPENGL
9+
def test_Data(using_opengl_renderer):
1210
a = Square().move_to(RIGHT)
1311
data_bb = a.data["bounding_box"]
1412
np.testing.assert_array_equal(
@@ -39,6 +37,3 @@ def test_Data():
3937
)
4038

4139
np.testing.assert_array_equal(a.bounding_box, data_bb)
42-
config.renderer = (
43-
RendererType.CAIRO
44-
) # needs to be here or else the following cairo tests fail

tests/module/mobject/text/test_texmobject.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
import numpy as np
66
import pytest
77

8-
from manim import MathTex, SingleStringMathTex, Tex, TexTemplate, config, tempconfig
8+
from manim import MathTex, SingleStringMathTex, Tex, TexTemplate, tempconfig
99

1010

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

1515

16-
def test_SingleStringMathTex():
16+
def test_SingleStringMathTex(config):
1717
SingleStringMathTex("test")
1818
assert Path(config.media_dir, "Tex", "8ce17c7f5013209f.svg").exists()
1919

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

2929

30-
def test_tex():
30+
def test_tex(config):
3131
Tex("The horse does not eat cucumber salad.")
3232
assert Path(config.media_dir, "Tex", "c3945e23e546c95a.svg").exists()
3333

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

4747

48-
def test_percent_char_rendering():
48+
def test_percent_char_rendering(config):
4949
Tex(r"\%")
5050
assert Path(config.media_dir, "Tex", "4a583af4d19a3adf.tex").exists()
5151

@@ -194,33 +194,33 @@ def test_error_in_nested_context(capsys):
194194
\end{align}
195195
"""
196196

197-
with pytest.raises(ValueError) as err:
197+
with pytest.raises(ValueError):
198198
Tex(invalid_tex)
199199

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

204204

205-
def test_tempconfig_resetting_tex_template():
205+
def test_tempconfig_resetting_tex_template(config):
206206
my_template = TexTemplate()
207207
my_template.preamble = "Custom preamble!"
208-
tex_template_config_value = config.tex_template
209208
with tempconfig({"tex_template": my_template}):
210209
assert config.tex_template.preamble == "Custom preamble!"
211210

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

214213

215-
def test_tex_garbage_collection(tmpdir, monkeypatch):
214+
def test_tex_garbage_collection(tmpdir, monkeypatch, config):
216215
monkeypatch.chdir(tmpdir)
217216
Path(tmpdir, "media").mkdir()
217+
config.media_dir = "media"
218218

219-
with tempconfig({"media_dir": "media"}):
220-
tex_without_log = Tex("Hello World!") # d771330b76d29ffb.tex
221-
assert Path("media", "Tex", "d771330b76d29ffb.tex").exists()
222-
assert not Path("media", "Tex", "d771330b76d29ffb.log").exists()
219+
tex_without_log = Tex("Hello World!") # d771330b76d29ffb.tex
220+
assert Path("media", "Tex", "d771330b76d29ffb.tex").exists()
221+
assert not Path("media", "Tex", "d771330b76d29ffb.log").exists()
222+
223+
config.no_latex_cleanup = True
223224

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

0 commit comments

Comments
 (0)