-
Notifications
You must be signed in to change notification settings - Fork 26
/
test_gallery.py
108 lines (88 loc) · 3.24 KB
/
test_gallery.py
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
"""Test that the Sphinx Gallery files run without warnings or errors.
See tox.ini for usage.
"""
import importlib.util
import logging
import os
import os.path
import sys
import traceback
import warnings
TOTAL = 0
FAILURES = 0
ERRORS = 0
def _import_from_file(script):
modname = os.path.basename(script)
spec = importlib.util.spec_from_file_location(os.path.basename(script), script)
module = importlib.util.module_from_spec(spec)
sys.modules[modname] = module
spec.loader.exec_module(module)
_numpy_warning_re = "numpy.ufunc size changed, may indicate binary incompatibility. Expected 216, got 192"
_experimental_warning_re = (
"[a-zA-Z0-9]+ is experimental -- it may be removed in the future "
"and is not guaranteed to maintain backward compatibility"
)
def run_gallery_tests():
global TOTAL, FAILURES, ERRORS
logging.info("Testing execution of Sphinx Gallery files")
# get all python file names in docs/gallery
gallery_file_names = list()
for root, _, files in os.walk(os.path.join(os.path.dirname(__file__), "docs", "gallery")):
for f in files:
if f.endswith(".py"):
gallery_file_names.append(os.path.join(root, f))
warnings.simplefilter("error")
warnings.filterwarnings(
"ignore",
category=DeprecationWarning, # these can be triggered by downstream packages. ignore for these tests
)
TOTAL += len(gallery_file_names)
for script in gallery_file_names:
logging.info("Executing %s" % script)
try:
with warnings.catch_warnings(record=True):
warnings.filterwarnings(
"ignore",
message=_experimental_warning_re,
category=UserWarning,
)
warnings.filterwarnings(
# this warning is triggered when some numpy extension code in an upstream package was compiled
# against a different version of numpy than the one installed
"ignore",
message=_numpy_warning_re,
category=RuntimeWarning,
)
_import_from_file(script)
except (ImportError, ValueError) as e:
if "linkml" in str(e):
pass # this is OK because linkml is not always installed
else:
raise e
except Exception:
print(traceback.format_exc())
FAILURES += 1
ERRORS += 1
def main():
logging_format = (
"======================================================================\n"
"%(asctime)s - %(levelname)s - %(message)s"
)
logging.basicConfig(format=logging_format, level=logging.INFO)
run_gallery_tests()
final_message = "Ran %s tests" % TOTAL
exitcode = 0
if ERRORS > 0 or FAILURES > 0:
exitcode = 1
_list = list()
if ERRORS > 0:
_list.append("errors=%d" % ERRORS)
if FAILURES > 0:
_list.append("failures=%d" % FAILURES)
final_message = "%s - FAILED (%s)" % (final_message, ",".join(_list))
else:
final_message = "%s - OK" % final_message
logging.info(final_message)
return exitcode
if __name__ == "__main__":
sys.exit(main())