-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathrun_tests.py
More file actions
executable file
·294 lines (251 loc) · 8.73 KB
/
run_tests.py
File metadata and controls
executable file
·294 lines (251 loc) · 8.73 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/usr/bin/env python3
# /*##########################################################################
#
# Copyright (c) 2015-2025 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/
"""Run the tests of the project.
This script expects a suite function in <project_package>.test,
which returns a unittest.TestSuite.
Test coverage dependencies: coverage, lxml.
"""
__authors__ = ["Jérôme Kieffer", "Thomas Vincent"]
__date__ = "05/05/2025"
__license__ = "MIT"
import sys
import logging
import os
import sysconfig
from argparse import ArgumentParser
from pathlib import Path
import warnings
# Capture all default warnings
logging.captureWarnings(True)
warnings.simplefilter("default")
logger = logging.getLogger("run_tests")
logger.setLevel(logging.WARNING)
logger.info("Python %s %s", sys.version, tuple.__itemsize__ * 8)
if sys.version_info.major < 3 or sys.version_info.minor < 10:
logger.error("SILX requires at least Python3.10")
try:
import resource
except ImportError:
resource = None
logger.warning("resource module missing")
try:
import importlib
importer = importlib.import_module
except ImportError:
def importer(name):
module = __import__(name)
# returns the leaf module, instead of the root module
subnames = name.split(".")
subnames.pop(0)
for subname in subnames:
module = getattr(module, subname)
return module
try:
import numpy
except Exception as error:
logger.warning("Numpy missing: %s", error)
else:
logger.info("Numpy %s", numpy.version.version)
try:
import h5py
except Exception as error:
logger.warning("h5py missing: %s", error)
else:
logger.info("h5py %s", h5py.version.version)
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
from bootstrap import get_project_name, build_project # noqa: E402
PROJECT_NAME = get_project_name(PROJECT_DIR)
logger.info("Project name: %s", PROJECT_NAME)
def is_debug_python():
"""Returns true if the Python interpreter is in debug mode."""
if sysconfig.get_config_var("Py_DEBUG"):
return True
return hasattr(sys, "gettotalrefcount")
# Prevent importing from source directory
if os.path.dirname(os.path.abspath(__file__)) == os.path.abspath(sys.path[0]):
removed_from_sys_path = sys.path.pop(0)
logger.info("Patched sys.path, removed: '%s'", removed_from_sys_path)
def get_test_options(project_module):
"""Returns the test options if available, else None"""
module_name = project_module.__name__ + ".test.utils"
logger.info("Import %s", module_name)
try:
test_utils = importer(module_name)
except ImportError:
logger.warning("No module named '%s'. No test options available.", module_name)
return None
test_options = getattr(test_utils, "test_options", None)
return test_options
if "-i" in sys.argv or "--installed" in sys.argv:
for bad_path in (".", os.getcwd(), PROJECT_DIR):
if bad_path in sys.path:
sys.path.remove(bad_path)
try:
module = importer(PROJECT_NAME)
except Exception:
logger.error(
"Cannot run tests on installed version: %s not installed or raising error.",
PROJECT_NAME,
)
raise
else:
print("Running tests on system-wide installed project")
else:
build_dir = build_project(PROJECT_NAME, PROJECT_DIR)
sys.path.insert(0, build_dir)
logger.warning("Patched sys.path, added: '%s'", build_dir)
module = importer(PROJECT_NAME)
epilog = """Environment variables:
WITH_QT_TEST=False to disable graphical tests
SILX_OPENCL=False to disable OpenCL tests.
WITH_HIGH_MEM_TEST: set to True to enable all tests >100Mb
WITH_GL_TEST=False to disable tests using OpenGL
"""
parser = ArgumentParser(description="Run the tests.", epilog=epilog)
test_options = get_test_options(module)
"""Contains extra configuration for the tests."""
if test_options is not None:
test_options.add_parser_argument(parser)
parser.add_argument(
"test_name",
nargs="*",
default=(PROJECT_NAME,),
help=f"Test names to run (Default: {PROJECT_NAME})",
)
parser.add_argument(
"-i",
"--installed",
action="store_true",
dest="installed",
default=False,
help="Test the installed version instead of"
"building from the source and testing the development version",
)
parser.add_argument(
"--no-gui",
action="store_false",
dest="gui",
default=True,
help="Disable the test of the graphical use interface",
)
parser.add_argument(
"--no-opengl",
action="store_false",
dest="opengl",
default=True,
help="Disable tests using OpenGL",
)
parser.add_argument(
"--no-opencl",
action="store_false",
dest="opencl",
default=True,
help="Disable tests using OpenCL",
)
parser.add_argument(
"--high-mem",
action="store_true",
dest="high_mem",
default=False,
help="Enable tests requiring large amounts of data (>100Mb)",
)
parser.add_argument(
"-v",
"--verbose",
default=0,
action="count",
dest="verbose",
help="Increase verbosity. Option -v prints additional "
+ "INFO messages. Use -vv for full verbosity, "
+ "including debug messages and test help strings.",
)
parser.add_argument(
"--qt-binding",
dest="qt_binding",
default=None,
help="Force using a Qt binding, from 'PyQt5', 'PyQt6', or 'PySide6'",
)
options = parser.parse_args()
test_verbosity = 1
use_buffer = True
if options.verbose == 1:
logging.root.setLevel(logging.INFO)
logger.info("Set log level: INFO")
test_verbosity = 2
use_buffer = False
elif options.verbose > 1:
logging.root.setLevel(logging.DEBUG)
logger.info("Set log level: DEBUG")
test_verbosity = 2
use_buffer = False
if options.qt_binding:
binding = options.qt_binding.lower()
if binding == "pyqt5":
logger.info("Force using PyQt5")
import PyQt5.QtCore # noqa
elif binding == "pyqt6":
logger.info("Force using PyQt6")
import PyQt6.QtCore # noqa
elif binding == "pyside6":
logger.info("Force using PySide6")
import PySide6.QtCore # noqa
else:
raise ValueError("Qt binding '%s' is unknown" % options.qt_binding)
PROJECT_VERSION = getattr(module, "version", "")
PROJECT_PATH = module.__path__[0]
if __name__ == "__main__": # Needed for multiprocessing support on Windows
project_module = module
PROJECT_PATH = str(Path(project_module.__path__[0]).resolve())
print(f"PROJECT_PATH: {PROJECT_PATH}")
sys.path.insert(0, PROJECT_PATH)
# corresponds to options to pass back to pytest ...
pytest_options = []
if options.qt_binding:
pytest_options.append(f"--qt-binding={options.qt_binding}")
if options.gui is False:
pytest_options.append("--no-gui")
if options.opencl is False:
pytest_options.append("--no-opencl")
if options.opengl is False:
pytest_options.append("--no-opengl")
if options.high_mem is True:
pytest_options.append("--high-mem")
def path2module(option):
if option.endswith(".py"):
option = option[:-3]
option_parts = option.split(os.path.sep)
if option_parts == ["src", PROJECT_NAME]:
option_parts = [PROJECT_NAME]
elif len(option_parts) == 1:
pass
elif option_parts[:2] == ["src", PROJECT_NAME]:
option_parts = option_parts[1:]
return ".".join(i for i in option_parts if i)
modules = [path2module(p) for p in options.test_name]
test_module = importlib.import_module(f"{PROJECT_NAME}.test")
# print(modules)
# print(pytest_options)
rc = test_module.run_tests(modules=modules, args=pytest_options)
sys.exit(rc)