3
3
"""
4
4
5
5
from contextlib import contextmanager
6
- from dataclasses import dataclass , field
7
6
import fnmatch
8
- import logging
9
7
import os
10
8
from pathlib import Path
11
9
import sys
21
19
matchmarker = pytest .StashKey ()
22
20
marked = pytest .StashKey ()
23
21
22
+ # Environment triggers for use either locally
23
+ # or in GitHub Action
24
+ _ENV_TRUE = {"1" , "true" , "yes" , "on" }
25
+ RUN_NOTEBOOKS = str (os .getenv ("EXAMPLES_RUN_NOTEBOOKS" , "1" )).lower () in _ENV_TRUE
26
+ RUN_PYFILES = str (os .getenv ("EXAMPLES_RUN_PYFILES" , "1" )).lower () in _ENV_TRUE
27
+
24
28
25
29
def _matches_pattern (item : pytest .Item , pattern : str ) -> bool :
26
30
to_match = os .fspath (item .path )
@@ -44,16 +48,31 @@ def pytest_sessionstart(session: pytest.Session):
44
48
45
49
46
50
def pytest_ignore_collect (collection_path : Path , config : pytest .Config ):
51
+ """Control what gets collected. By default, notebooks and tests; ignore other files."""
47
52
if "_dev" in collection_path .parts :
48
53
return True
49
54
if not collection_path .is_file ():
50
55
return
51
- if collection_path .suffix == ".py" :
52
- # specifically ignore python files
53
- return True
54
- if not collection_path .match ("**/*_test.ipynb" ):
56
+
57
+ suffix = collection_path .suffix
58
+ # Notebook tests (only *_test.ipynb)
59
+ if suffix == ".ipynb" :
60
+ if not RUN_NOTEBOOKS :
61
+ return True
62
+ return not collection_path .match ("**/*_test.ipynb" )
63
+
64
+ # Python tests (only test_*.py or *_test.py)
65
+ if suffix == ".py" :
66
+ if not RUN_PYFILES :
67
+ return True
68
+ name = collection_path .name
69
+ if fnmatch .fnmatch (name , "test_*.py" ) or fnmatch .fnmatch (name , "*_test.py" ):
70
+ return False
55
71
return True
56
72
73
+ # Ignore everything else
74
+ return True
75
+
57
76
58
77
def pytest_collection_modifyitems (config : pytest .Config , items ):
59
78
for item in items :
@@ -102,7 +121,7 @@ def run_pytest(
102
121
empty_file_for_ignoring .write_text ("" )
103
122
args += ["-c" , empty_file_for_ignoring ]
104
123
105
- with _temp_cwd (rootdir ) as p :
124
+ with _temp_cwd (rootdir ):
106
125
res = pytest .main (
107
126
args ,
108
127
** kwargs ,
@@ -114,6 +133,7 @@ def run_pytest(
114
133
def main (args ):
115
134
rootdir = Path (idaes_examples .__path__ [0 ])
116
135
136
+ # Always include --nbmake, but notebook collection can be disabled by env
117
137
res = run_pytest (
118
138
rootdir ,
119
139
[
0 commit comments