Meshroom/tests/utils.py
Candice Bentéjac 3c57afb4d0 [tests] Simplify registration/unregistration of nodes in tests
Add two methods that are local to tests, `registerNodeDesc` and
`unregisterNodeDesc`, which handle the registration and unregistration
of `NodePlugins` from a node description. This reduces the amount of
code in tests whenever `NodePlugin` need to be instantiated prior
to their registration and so on.
2025-06-05 14:40:14 +02:00

43 lines
1.4 KiB
Python

from contextlib import contextmanager
from unittest.mock import patch
import meshroom
from meshroom.core import desc, pluginManager
from meshroom.core.plugins import NodePlugin, NodePluginStatus
@contextmanager
def registeredNodeTypes(nodeTypes: list[desc.Node]):
nodePluginsList = {}
for nodeType in nodeTypes:
nodePlugin = NodePlugin(nodeType)
pluginManager.registerNode(nodePlugin)
nodePluginsList[nodeType] = nodePlugin
yield
for nodeType in nodeTypes:
pluginManager.unregisterNode(nodePluginsList[nodeType])
@contextmanager
def overrideNodeTypeVersion(nodeType: desc.Node, version: str):
""" Helper context manager to override the version of a given node type. """
unpatchedFunc = meshroom.core.nodeVersion
with patch.object(
meshroom.core,
"nodeVersion",
side_effect=lambda type: version if type is nodeType else unpatchedFunc(type),
):
yield
def registerNodeDesc(nodeDesc: desc.Node):
name = nodeDesc.__name__
if not pluginManager.isRegistered(name):
pluginManager._nodePlugins[name] = NodePlugin(nodeDesc)
pluginManager._nodePlugins[name].status = NodePluginStatus.LOADED
def unregisterNodeDesc(nodeDesc: desc.Node):
name = nodeDesc.__name__
if pluginManager.isRegistered(name):
plugin = pluginManager.getRegisteredNodePlugin(name)
plugin.status = NodePluginStatus.NOT_LOADED
del pluginManager._nodePlugins[name]