oscillode/tests/context.py

53 lines
1.6 KiB
Python

# oscillode
# Copyright (C) 2024 oscillode Project Contributors
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import contextlib
import json
import os
from pathlib import Path
import tomli_w
from scripts import info, pack
PATH_ADDON_ROOT = Path(__file__).resolve().parent.parent / 'oscillode'
@contextlib.contextmanager
def blender_manifest():
"""Prepares a blender_manifest.toml file temporarily in the addon root directory."""
try:
os.environ['RUNNING_BLEXT_TESTS'] = '1'
path_manifest = PATH_ADDON_ROOT / pack.BL_EXT__MANIFEST_FILENAME
path_init_settings = (
PATH_ADDON_ROOT
/ info.PROJ_SPEC['tool']['bl_ext']['packaging']['init_settings_filename']
)
with path_manifest.open('wb') as f:
tomli_w.dump(pack.BL_EXT_MANIFEST, f)
init_settings = pack.generate_init_settings_dict('test')
with path_init_settings.open('wb') as f:
tomli_w.dump(json.loads(init_settings.model_dump_json()), f)
yield
finally:
path_manifest.unlink()
path_init_settings.unlink()
del os.environ['RUNNING_BLEXT_TESTS']