77import subprocess
88import sys
99
10+ COVERAGE_COMMAND_ENV_VAR = "COVERAGE_COMMAND"
11+
1012
1113def eprint (* args , ** kwargs ):
1214 """Print to stderr."""
1315 print (* args , file = sys .stderr , ** kwargs )
1416
1517
16- def run_test_notebook (notebook_path ) -> bool :
18+ def run_test_notebook (notebook_path : str , coverage_args : list [ str ] ) -> bool :
1719 """Run test_notebook.py for the given notebook."""
1820
1921 pytest_bin = shutil .which ("pytest" )
@@ -22,7 +24,7 @@ def run_test_notebook(notebook_path) -> bool:
2224 raise RuntimeError ("Python 3 not found" )
2325
2426 result = subprocess .run (
25- [pytest_bin , "--ignore=test" , "--cov" , "--cov-append" , "test_notebook.py" ],
27+ [pytest_bin , "--ignore=test" , * coverage_args , "test_notebook.py" ],
2628 env = {
2729 ** os .environ ,
2830 "INPUT_FILE" : notebook_path ,
@@ -40,6 +42,14 @@ def run_test_notebook(notebook_path) -> bool:
4042 return False
4143
4244
45+ def parse_coverage_command () -> list [str ]:
46+ """Get coverage command from environment variable."""
47+ coverage_cmd = os .getenv (COVERAGE_COMMAND_ENV_VAR )
48+ if not coverage_cmd :
49+ return []
50+ return [* coverage_cmd .split (), "--cov-append" ]
51+
52+
4353def main () -> int :
4454 """Run all Jupyter Notebooks and check for errors."""
4555
@@ -49,13 +59,19 @@ def main() -> int:
4959 eprint (f"The folder { example_folder } does not exist." )
5060 return - 1
5161
62+ coverage_args = parse_coverage_command ()
63+ if coverage_args :
64+ eprint (f"Using coverage args: { ' ' .join (coverage_args )} " )
65+ else :
66+ eprint (f"No coverage args in env { COVERAGE_COMMAND_ENV_VAR } provided." )
67+
5268 for root , _dirs , files in os .walk (example_folder ):
5369 for file in files :
5470 if not file .endswith (".ipynb" ):
5571 eprint (f"Skipping non-notebook file { file } " )
5672 continue
5773 notebook_path = os .path .join (root , file )
58- if not run_test_notebook (notebook_path ):
74+ if not run_test_notebook (notebook_path , coverage_args ):
5975 return - 1
6076
6177 break # skip subdirectories
0 commit comments