Skip to content

Commit

Permalink
Merge eb61eca into f5caea6
Browse files Browse the repository at this point in the history
  • Loading branch information
mondeja committed Oct 26, 2021
2 parents f5caea6 + eb61eca commit be2d7ed
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 1.0.2
current_version = 1.1.0

[bumpversion:file:pytest_blender/__init__.py]

Expand Down
17 changes: 12 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,25 @@ jobs:
- name: Install dependencies
id: install-dependencies
run: |
python -m pip install .[test]
python -m pip install .[test] coveralls
BLENDER_EXECUTABLE_PATH="$(cat _blender-executable-path.txt | tr -d '\n')"
PYTHON_BLENDER_EXECUTABLE="$(pytest-blender --blender-executable $BLENDER_EXECUTABLE_PATH)"
$PYTHON_BLENDER_EXECUTABLE -m ensurepip
$PYTHON_BLENDER_EXECUTABLE -m pip install pytest
pytest_version="$(< setup.cfg grep pytest== | head -n 1 | cut -d'=' -f3)"
pytest_cov_version="$(< setup.cfg grep pytest-cov== | head -n 1 | cut -d'=' -f3)"
$PYTHON_BLENDER_EXECUTABLE -m pip install pytest==$pytest_version pytest-cov==$pytest_cov_version
echo "::set-output name=blender-executable::$BLENDER_EXECUTABLE_PATH"
- name: Test with Blender Python executable (no cache)
run: pytest -svv -p no:cacheprovider --blender-executable "${{ steps.install-dependencies.outputs.blender-executable }}" tests
run: pytest -svv -p no:cacheprovider --cov=pytest_blender --cov-config=setup.cfg --blender-executable "${{ steps.install-dependencies.outputs.blender-executable }}" tests
- name: Test with Blender Python executable
run: pytest -svv --blender-executable "${{ steps.install-dependencies.outputs.blender-executable }}" tests
run: pytest -svv --cov=pytest_blender --cov-config=setup.cfg --blender-executable "${{ steps.install-dependencies.outputs.blender-executable }}" tests
- name: Test with local Python executable
run: BLENDER_EXECUTABLE="${{ steps.install-dependencies.outputs.blender-executable }}" pytest -svv -p no:pytest-blender tests
run: BLENDER_EXECUTABLE="${{ steps.install-dependencies.outputs.blender-executable }}" pytest -svv -p no:pytest-blender --cov=pytest_blender --cov-config=setup.cfg tests
- name: Coveralls
run: coveralls
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COVERALLS_SERVICE_NAME: github

build-sdist:
if: startsWith(github.ref, 'refs/tags/')
Expand Down
21 changes: 13 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ Returns the version of the Python executable builtin in the Blender release of
the currently running session.

<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>)
<b>install_addons_from_dir</b>(<i>addons_dir</i>,
<i>addon_module_names=None</i>, <i>save_userpref=True</i>,
<i>default_set=True</i>, <i>persistent=True</i>, <i>\*\*kwargs</i>)`list`

Function that installs and enables a set of addons whose modules are located in
a directory. This function is designed to be executed before the pytest session
Expand All @@ -122,25 +122,30 @@ to disable them after the execution of the test suite:
```python
import os

ADDON_MODULE_NAMES = ["my_awesome_addon_module_name"]
import pytest

@pytest.fixture(scope="session", autouse=True)
def _register_addons(request, install_addons_from_dir, disable_addons):
install_addons_from_dir(os.path.abspath("src"), ADDON_MODULE_NAMES)
addon_module_names = install_addons_from_dir(os.path.abspath("src"))
yield
disable_addons(ADDON_MODULE_NAMES)
disable_addons(addon_module_names)
```
- **addons_dir** (str) Directory in which are located the modules of the
addons.
- **addon_module_names** (list) Name of the addons modules (without the
`.py` extension).
- **addon_module_names** (list) Name of the addons modules. If not defined
(default) all the python modules located in `addons_dir` whose names do not
start with `__` will be considered addons.
- **save_userpref** (bool) Save user preferences after installation.
- **default_set** (bool) Set the user-preference calling `addon_utils.enable`.
- **persistent** (bool) Ensure the addon is enabled for the entire session
(after loading new files).
- **\*\*kwargs** (dict) Subsecuent keyword arguments are passed to
[`bpy.ops.preferences.addon_install`](https://docs.blender.org/api/current/bpy.ops.preferences.html#bpy.ops.preferences.addon_install).

Returns the addon module names as a list, ready to be passed to
[`disable_addons`](https://github.com/mondeja/pytest-blender#disable_addons)
function.

<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>)
Expand Down
2 changes: 1 addition & 1 deletion pytest_blender/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.0.2"
__version__ = "1.1.0"
2 changes: 1 addition & 1 deletion pytest_blender/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def build_parser():
dest="blender_executable",
nargs=1,
default=which_blender_by_os,
help="Blender executable location.",
help="Custom Blender executable location for 'pytest-blender' plugin.",
)
return parser

Expand Down
15 changes: 13 additions & 2 deletions pytest_blender/run_pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def install_addons_from_dir(self, request):

def _install_addons_from_dir(
addons_dir,
addon_module_names,
addon_module_names=None,
save_userpref=True,
default_set=True,
persistent=True,
Expand All @@ -216,8 +216,17 @@ def _install_addons_from_dir(
import addon_utils # noqa F401
import bpy # noqa F401

if addon_module_names is None:
addon_module_names = [
fname.rstrip(".py")
for fname in os.listdir(addons_dir)
if not fname.startswith("__")
]

for addon_module_name in addon_module_names:
addon_filepath = os.path.join(addons_dir, f"{addon_module_name}.py")
addon_filepath = os.path.join(
addons_dir, f"{addon_module_name.rstrip('.py')}.py"
)
bpy.ops.preferences.addon_install(filepath=addon_filepath, **kwargs)
addon_utils.enable(
addon_module_name,
Expand All @@ -227,6 +236,8 @@ def _install_addons_from_dir(
if save_userpref:
bpy.ops.wm.save_userpref()

return addon_module_names

return _install_addons_from_dir

@pytest.fixture(scope="session")
Expand Down
9 changes: 8 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = pytest_blender
version = 1.0.2
version = 1.1.0
description = Blender Pytest plugin.
long_description = file: README.md
long_description_content_type = text/markdown
Expand Down Expand Up @@ -45,6 +45,7 @@ dev =
isort==5.9.1
pre-commit==2.13.0
pytest==6.2.5
pytest-cov==3.0.0
yamllint==1.26.1
lint =
black==20.8b1
Expand All @@ -55,6 +56,12 @@ lint =
yamllint==1.26.1
test =
pytest==6.2.5
pytest-cov==3.0.0

[coverage:report]
exclude_lines =
pragma: no cover
if __name__ == .__main__.:

[flake8]
max-line-length = 88
Expand Down
6 changes: 3 additions & 3 deletions tests/addons/pytest_blender_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
}


class ObjectMoveX(bpy.types.Operator):
class PytestBlenderObjectMoveX(bpy.types.Operator):
"""My Object Moving Script"""

bl_idname = "object.move_x"
Expand All @@ -23,11 +23,11 @@ def execute(self, context):


def register():
bpy.utils.register_class(ObjectMoveX)
bpy.utils.register_class(PytestBlenderObjectMoveX)


def unregister():
bpy.utils.unregister_class(ObjectMoveX)
bpy.utils.unregister_class(PytestBlenderObjectMoveX)


if __name__ == "__main__":
Expand Down
8 changes: 3 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ def _register_addons(request, install_addons_from_dir, disable_addons):
os.path.abspath(os.path.dirname(__file__)),
"addons",
)
addon_module_names = ["pytest_blender_basic"]

f = io.StringIO()
with redirect_stdout(f):
install_addons_from_dir(addons_dir, addon_module_names)

yield

addon_module_names = install_addons_from_dir(addons_dir)
yield
with redirect_stdout(f):
disable_addons(addon_module_names)
2 changes: 1 addition & 1 deletion tests/test_addons.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ def test_basic_addon():

_operator_class_loaded = False
for operator_cls in _bpy.types.Operator.__subclasses__():
if operator_cls.__name__ == "ObjectMoveX":
if operator_cls.__name__ == "PytestBlenderObjectMoveX":
_operator_class_loaded = True
assert _operator_class_loaded

0 comments on commit be2d7ed

Please sign in to comment.