Skip to content

Commit 0916785

Browse files
committed
Add 'install_addons_from_dir' and 'disable_addons' fixtures.
1 parent f541420 commit 0916785

File tree

6 files changed

+107
-17
lines changed

6 files changed

+107
-17
lines changed

.bumpversion.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 0.0.4
2+
current_version = 0.0.5
33

44
[bumpversion:file:pytest_blender/__init__.py]
55

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,46 @@ currently running session.
102102
Returns the version of the Python executable builtin in the Blender release of
103103
the currently running session.
104104

105+
<a name="install_addons_from_dir" href="#install_addons_from_dir">#</a> <b>install_addons_from_dir</b>(<i>addons_dir</i>, <i>addon_module_names</i>, <i>save_userpref=True</i>, <i>default_set=True</i>, <i>persistent=True</i>, <i>\*\*kwargs</i>)
106+
107+
Returns a function that installs and enables a set of addons whose modules are
108+
located in a directory. This function is designed to be executed before the
109+
pytest session to install the addons that you want to test, using the other
110+
fixture [`disable_addons`](https://github.com/mondeja/pytest-blender#disable_addons)
111+
to disable them after the execution of the suite:
112+
113+
```python
114+
import os
115+
116+
ADDON_MODULE_NAMES = ["my_awesome_addon_module_name"]
117+
118+
@pytest.fixture(scope="session", autouse=True)
119+
def _register_addons(request, install_addons_from_dir, disable_addons):
120+
install_addons_from_dir(os.path.abspath("src"), ADDON_MODULE_NAMES)
121+
yield
122+
disable_addons(ADDON_MODULE_NAMES)
123+
```
124+
- **addons_dir** (str) Directory in which are located the modules of the addons.
125+
- **addon_module_names** (list) Name of the addons modules (without the `.py` extension).
126+
- **save_userpref** (bool) Save user preferences after installation.
127+
- **default_set** (bool) Set the user-preference calling `addon_utils.enable`.
128+
- **persistent** (bool) Ensure the addon is enabled for the entire session
129+
(after loading new files).
130+
- **\*\*kwargs** (dict) Subsecuent keyword arguments are passed to
131+
[`bpy.ops.preferences.addon_install`](https://docs.blender.org/api/current/bpy.ops.preferences.html#bpy.ops.preferences.addon_install).
132+
133+
<a name="disable_addons" href="#disable_addons">#</a> <b>disable_addons</b>(<i>addon_module_names</i>, <i>save_userpref=True</i>, <i>default_set=True</i>, <i>\*\*kwargs</i>)
134+
135+
Returns a function that disables a set of addons by module name. Is designed to
136+
disables your addons after a pytest suite execution (check
137+
[`install_addons_from_dir`](https://github.com/mondeja/pytest-blender#install_addons_from_dir)
138+
for an example).
139+
140+
- **addon_module_names** (list) Name of the addons modules (without the `.py` extension).
141+
- **save_userpref** (bool) Save user preferences after installation.
142+
- **default_set** (bool) Set the user-preference calling `addon_utils.disable`.
143+
- **\*\*kwargs** (dict) Subsecuent keyword arguments are passed to `addon_utils.disable`.
144+
105145

106146
[pypi-link]: https://pypi.org/project/pytest-blender
107147
[pypi-version-badge-link]: https://img.shields.io/pypi/v/pytest-blender

pytest_blender/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.0.4"
1+
__version__ = "0.0.5"

pytest_blender/run_pytest.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,66 @@ def blender_python_version(self, request):
129129
)
130130
return blender_python_version
131131

132+
@pytest.fixture(scope="session")
133+
def install_addons_from_dir(self, request):
134+
def _install_addons_from_dir(
135+
addons_dir,
136+
addon_module_names,
137+
save_userpref=True,
138+
default_set=True,
139+
persistent=True,
140+
**kwargs,
141+
):
142+
import addon_utils # noqa F401
143+
import bpy # noqa F401
144+
145+
for addon_module_name in addon_module_names:
146+
addon_filepath = os.path.join(addons_dir, f"{addon_module_name}.py")
147+
bpy.ops.preferences.addon_install(filepath=addon_filepath, **kwargs)
148+
addon_utils.enable(
149+
addon_module_name,
150+
default_set=default_set,
151+
persistent=persistent,
152+
)
153+
if save_userpref:
154+
bpy.ops.wm.save_userpref()
155+
156+
return _install_addons_from_dir
157+
158+
@pytest.fixture(scope="session")
159+
def disable_addons(self, request):
160+
def _disable_addons(
161+
addon_module_names,
162+
save_userpref=True,
163+
default_set=True,
164+
**kwargs,
165+
):
166+
"""Disables a set of addons by module name.
167+
168+
Parameters
169+
----------
170+
171+
addon_module_names : list
172+
Name of the addons modules (without the ``.py`` extension).
173+
174+
save_userpref : bool
175+
Save user preferences after disable.
176+
"""
177+
import addon_utils # noqa F401
178+
179+
for addon_module_name in addon_module_names:
180+
addon_utils.disable(
181+
addon_module_name,
182+
default_set=default_set,
183+
**kwargs,
184+
)
185+
if save_userpref:
186+
import bpy # noqa F401
187+
188+
bpy.ops.wm.save_userpref()
189+
190+
return _disable_addons
191+
132192
return pytest.main(argv, plugins=[PytestBlenderPlugin()])
133193

134194

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = pytest_blender
3-
version = 0.0.4
3+
version = 0.0.5
44
description = Blender Pytest plugin.
55
long_description = file: README.md
66
long_description_content_type = text/markdown

tests/conftest.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,17 @@
88

99

1010
@pytest.fixture(scope="session", autouse=True)
11-
def _register_addon(request):
12-
import addon_utils
13-
import bpy
14-
11+
def _register_addons(request, install_addons_from_dir, disable_addons):
1512
addons_dir = os.path.join(
1613
os.path.abspath(os.path.dirname(__file__)),
1714
"addons",
1815
)
19-
20-
addons_module_names = ["pytest_blender_basic"]
16+
addon_module_names = ["pytest_blender_basic"]
2117

2218
f = io.StringIO()
2319
with redirect_stdout(f):
24-
for addon_module_name in addons_module_names:
25-
addon_filepath = os.path.join(addons_dir, f"{addon_module_name}.py")
26-
bpy.ops.preferences.addon_install(filepath=addon_filepath, overwrite=True)
27-
addon_utils.enable(addon_module_name, default_set=True, persistent=True)
28-
bpy.ops.wm.save_userpref()
20+
install_addons_from_dir(addons_dir, addon_module_names)
2921

3022
yield
3123

32-
for addon_module_name in addons_module_names:
33-
addon_utils.disable(addon_module_name, default_set=True)
34-
bpy.ops.wm.save_userpref()
24+
disable_addons(addon_module_names)

0 commit comments

Comments
 (0)