-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
251 lines (163 loc) · 4.74 KB
/
conftest.py
File metadata and controls
251 lines (163 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
"""
Root-level pytest conftest - installs ``bpy`` and ``mathutils`` mocks
into ``sys.modules`` *before* any ``node_runner`` code is imported.
Because this file lives **outside** the ``node_runner`` package, pytest
processes it before discovering or importing anything from the package.
"""
import sys
import types
from unittest.mock import MagicMock
# mathutils mocks
class MockVector(list):
"""Minimal mathutils.Vector substitute."""
def __init__(self, *args):
if len(args) == 1 and hasattr(args[0], "__iter__"):
super().__init__(args[0])
else:
super().__init__(args)
def __add__(self, other):
return MockVector([a + b for a, b in zip(self, other)])
def __sub__(self, other):
return MockVector([a - b for a, b in zip(self, other)])
@property
def x(self):
return self[0]
@property
def y(self):
return self[1]
class MockColor(list):
"""Minimal mathutils.Color substitute."""
def __init__(self, *args):
if len(args) == 1 and hasattr(args[0], "__iter__"):
super().__init__(args[0])
else:
super().__init__(args)
class MockEuler(list):
"""Minimal mathutils.Euler substitute."""
def __init__(self, *args):
if len(args) == 1 and hasattr(args[0], "__iter__"):
super().__init__(args[0])
else:
super().__init__(args)
mathutils_mod = types.ModuleType("mathutils")
mathutils_mod.Vector = MockVector
mathutils_mod.Color = MockColor
mathutils_mod.Euler = MockEuler
sys.modules["mathutils"] = mathutils_mod
# bpy.types stub classes
class _TypeBase:
pass
class ColorRamp(_TypeBase):
pass
class ShaderNodeTree(_TypeBase):
pass
class ColorMapping(_TypeBase):
pass
class TexMapping(_TypeBase):
pass
class CurveMapping(_TypeBase):
pass
class CurveMap(_TypeBase):
pass
class CurveMapPoint(_TypeBase):
pass
class Image(_TypeBase):
name = ""
class ImageUser(_TypeBase):
pass
class NodeFrame(_TypeBase):
pass
class Text(_TypeBase):
pass
class TextLine(_TypeBase):
pass
class Object(_TypeBase):
pass
class NodeSocketStandard(_TypeBase):
pass
class NodeGroupInput(_TypeBase):
pass
class NodeGroupOutput(_TypeBase):
pass
class NodeSocketVirtual(_TypeBase):
pass
class NodeTreeInterfaceSocket(_TypeBase):
identifier = ""
class bpy_prop_collection(list):
def values(self):
return list(self)
class bpy_prop_array(list):
pass
class Operator(_TypeBase):
bl_idname = ""
bl_label = ""
bl_options = set()
class AddonPreferences(_TypeBase):
bl_idname = ""
class Menu(_TypeBase):
bl_idname = ""
bl_label = ""
class NodeLink(_TypeBase):
pass
class NodeTree(_TypeBase):
pass
class NODE_MT_context_menu:
_entries: list = []
@classmethod
def append(cls, func):
cls._entries.append(func)
@classmethod
def remove(cls, func):
cls._entries.remove(func)
# Assemble bpy module
bpy_mod = types.ModuleType("bpy")
bpy_types = types.ModuleType("bpy.types")
for _name, _cls in [
("ColorRamp", ColorRamp),
("ShaderNodeTree", ShaderNodeTree),
("ColorMapping", ColorMapping),
("TexMapping", TexMapping),
("CurveMapping", CurveMapping),
("CurveMap", CurveMap),
("CurveMapPoint", CurveMapPoint),
("Image", Image),
("ImageUser", ImageUser),
("NodeFrame", NodeFrame),
("Text", Text),
("TextLine", TextLine),
("Object", Object),
("NodeSocketStandard", NodeSocketStandard),
("NodeGroupInput", NodeGroupInput),
("NodeGroupOutput", NodeGroupOutput),
("NodeSocketVirtual", NodeSocketVirtual),
("NodeTreeInterfaceSocket", NodeTreeInterfaceSocket),
("bpy_prop_collection", bpy_prop_collection),
("bpy_prop_array", bpy_prop_array),
("Operator", Operator),
("AddonPreferences", AddonPreferences),
("Menu", Menu),
("NodeLink", NodeLink),
("NodeTree", NodeTree),
("NODE_MT_context_menu", NODE_MT_context_menu),
]:
setattr(bpy_types, _name, _cls)
bpy_mod.types = bpy_types
bpy_props = types.ModuleType("bpy.props")
bpy_props.StringProperty = lambda **kw: ""
bpy_props.BoolProperty = lambda **kw: False
bpy_props.EnumProperty = lambda **kw: ""
bpy_mod.props = bpy_props
bpy_mod.data = MagicMock()
bpy_mod.context = MagicMock()
bpy_path = types.ModuleType("bpy.path")
bpy_path.abspath = lambda p: p # identity in tests
bpy_mod.path = bpy_path
bpy_utils = types.ModuleType("bpy.utils")
bpy_utils.register_class = lambda cls: None
bpy_utils.unregister_class = lambda cls: None
bpy_mod.utils = bpy_utils
bpy_mod.ops = MagicMock()
sys.modules["bpy"] = bpy_mod
sys.modules["bpy.types"] = bpy_types
sys.modules["bpy.props"] = bpy_props
sys.modules["bpy.utils"] = bpy_utils