| 1 |
"""Test that the Sphinx Gallery files run without warnings or errors. |
| 2 |
|
| 3 |
See tox.ini for usage. |
| 4 |
""" |
| 5 |
|
| 6 |
import importlib.util |
| 7 |
import logging |
| 8 |
import os |
| 9 |
import os.path |
| 10 |
import sys |
| 11 |
import traceback |
| 12 |
import warnings |
| 13 |
|
| 14 |
TOTAL = 0 |
| 15 |
FAILURES = 0 |
| 16 |
ERRORS = 0 |
| 17 |
|
| 18 |
|
| 19 |
def _import_from_file(script): |
| 20 |
modname = os.path.basename(script) |
| 21 |
spec = importlib.util.spec_from_file_location(os.path.basename(script), script) |
| 22 |
module = importlib.util.module_from_spec(spec) |
| 23 |
sys.modules[modname] = module |
| 24 |
spec.loader.exec_module(module) |
| 25 |
|
| 26 |
|
| 27 |
_numpy_warning_re = "numpy.ufunc size changed, may indicate binary incompatibility. Expected 216, got 192" |
| 28 |
|
| 29 |
_experimental_warning_re = ( |
| 30 |
"[a-zA-Z0-9]+ is experimental -- it may be removed in the future " |
| 31 |
"and is not guaranteed to maintain backward compatibility" |
| 32 |
) |
| 33 |
|
| 34 |
def run_gallery_tests(): |
| 35 |
global TOTAL, FAILURES, ERRORS |
| 36 |
logging.info("Testing execution of Sphinx Gallery files") |
| 37 |
|
| 38 |
# get all python file names in docs/gallery |
| 39 |
gallery_file_names = list() |
| 40 |
for root, _, files in os.walk(os.path.join(os.path.dirname(__file__), "docs", "gallery")): |
| 41 |
for f in files: |
| 42 |
if f.endswith(".py"): |
| 43 |
gallery_file_names.append(os.path.join(root, f)) |
| 44 |
|
| 45 |
warnings.simplefilter("error") |
| 46 |
warnings.filterwarnings( |
| 47 |
"ignore", |
| 48 |
category=DeprecationWarning, # these can be triggered by downstream packages. ignore for these tests |
| 49 |
) |
| 50 |
|
| 51 |
TOTAL += len(gallery_file_names) |
| 52 |
for script in gallery_file_names: |
| 53 |
logging.info("Executing %s" % script) |
| 54 |
try: |
| 55 |
with warnings.catch_warnings(record=True): |
| 56 |
warnings.filterwarnings( |
| 57 |
"ignore", |
| 58 |
message=_experimental_warning_re, |
| 59 |
category=UserWarning, |
| 60 |
) |
| 61 |
warnings.filterwarnings( |
| 62 |
# this warning is triggered when some numpy extension code in an upstream package was compiled |
| 63 |
# against a different version of numpy than the one installed |
| 64 |
"ignore", |
| 65 |
message=_numpy_warning_re, |
| 66 |
category=RuntimeWarning, |
| 67 |
) |
| 68 |
_import_from_file(script) |
| 69 |
except (ImportError, ValueError) as e: |
| 70 |
if "linkml" in str(e): |
| 71 |
pass # this is OK because linkml is not always installed |
| 72 |
else: |
| 73 |
raise e |
| 74 |
except Exception: |
| 75 |
print(traceback.format_exc()) |
| 76 |
FAILURES += 1 |
| 77 |
ERRORS += 1 |
| 78 |
|
| 79 |
|
| 80 |
def main(): |
| 81 |
logging_format = ( |
| 82 |
"======================================================================\n" |
| 83 |
"%(asctime)s - %(levelname)s - %(message)s" |
| 84 |
) |
| 85 |
logging.basicConfig(format=logging_format, level=logging.INFO) |
| 86 |
|
| 87 |
run_gallery_tests() |
| 88 |
|
| 89 |
final_message = "Ran %s tests" % TOTAL |
| 90 |
exitcode = 0 |
| 91 |
if ERRORS > 0 or FAILURES > 0: |
| 92 |
exitcode = 1 |
| 93 |
_list = list() |
| 94 |
if ERRORS > 0: |
| 95 |
_list.append("errors=%d" % ERRORS) |
| 96 |
if FAILURES > 0: |
| 97 |
_list.append("failures=%d" % FAILURES) |
| 98 |
final_message = "%s - FAILED (%s)" % (final_message, ",".join(_list)) |
| 99 |
else: |
| 100 |
final_message = "%s - OK" % final_message |
| 101 |
|
| 102 |
logging.info(final_message) |
| 103 |
|
| 104 |
return exitcode |
| 105 |
|
| 106 |
|
| 107 |
if __name__ == "__main__": |
| 108 |
sys.exit(main()) |