-
Notifications
You must be signed in to change notification settings - Fork 50
/
test_examples.py
72 lines (58 loc) · 2.64 KB
/
test_examples.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
#!/usr/bin/env python
# ======================================================================
# MODULE DOCSTRING
# ======================================================================
"""
Test that the examples in the repo run without errors.
"""
# ======================================================================
# GLOBAL IMPORTS
# ======================================================================
import sys
import pathlib
import pytest
import subprocess
from perses.tests.utils import enter_temp_directory
ROOT_DIR_PATH = pathlib.Path(__file__).joinpath("../../../").resolve()
SLOW_EXAMPLES_KEYWORDS = ("barnase-barstar", "kinase-neq-switching")
NOT_EXAMPLES_KEYWORDS = ("analyze-benchmark", "moonshot-mainseries")
def run_script_file(file_path, cmd_args=None):
"""Run through the shell a python script."""
with enter_temp_directory():
# Make sure we grab the python executable in our env
cmd = [f"{sys.executable}", file_path]
print(cmd)
# Extend cmd list with given cmd_args
if cmd_args:
cmd.extend(cmd_args)
try:
subprocess.run(cmd, capture_output=True, check=True, shell=True)
except subprocess.CalledProcessError as error:
raise Exception(f"Example {file_path} failed. STDERR: {error.stderr.decode()}")
def find_example_scripts():
"""Find all Python scripts, excluding Jupyter notebooks, in the examples folder.
Returns
-------
example_file_paths : List[str]
List of full paths to python scripts to execute.
"""
examples_dir_path = ROOT_DIR_PATH.joinpath("examples")
example_file_paths = []
for example_file_path in examples_dir_path.glob("*/*.py"):
# TODO: find a better way to mark slow examples
example_posix_path = example_file_path.as_posix()
# mark slow examples
if any(example in example_posix_path for example in SLOW_EXAMPLES_KEYWORDS):
example_posix_path = pytest.param(example_posix_path, marks=pytest.mark.slow)
# mark non-examples
elif any(example in example_posix_path for example in NOT_EXAMPLES_KEYWORDS):
example_posix_path = pytest.param(example_posix_path, marks=pytest.mark.skip("not an example"))
example_file_paths.append(example_posix_path)
return example_file_paths
# ======================================================================
# TESTS
# ======================================================================
@pytest.mark.parametrize("example_file_path", find_example_scripts())
def test_examples(example_file_path):
"""Test that the example run without errors."""
run_script_file(example_file_path)