1
1
import logging
2
2
import os
3
- from importlib .machinery import SourceFileLoader
4
3
import re
4
+ from importlib .machinery import SourceFileLoader
5
5
6
6
import pytest
7
7
from jupyter_client import kernelspec
@@ -164,7 +164,7 @@ def check_cells_for_error_output(cells):
164
164
if output ["output_type" ] == "error" :
165
165
pytest .fail ("Found error output in cell: " + str (output ))
166
166
167
-
167
+
168
168
def execute_with_mocks (tb , mock_level , path_to_utils , path_to_mocks ):
169
169
# We do this check to make sure that the notebook can be executed (with mocks).
170
170
tb .inject (
@@ -178,7 +178,7 @@ def execute_with_mocks(tb, mock_level, path_to_utils, path_to_mocks):
178
178
run = False ,
179
179
before = 0 ,
180
180
)
181
-
181
+
182
182
# Uncomment all test sections in the notebook
183
183
for i , cell in enumerate (tb .cells ):
184
184
if cell .get ("cell_type" ) == "code" and "source" in cell :
@@ -187,39 +187,37 @@ def execute_with_mocks(tb, mock_level, path_to_utils, path_to_mocks):
187
187
# Uncomment the test section
188
188
modified_source = uncomment_test_section (source )
189
189
tb .cells [i ]["source" ] = modified_source
190
-
190
+
191
191
# Execute the notebook with the uncommented test sections
192
192
tb .execute ()
193
193
test_mocks = SourceFileLoader ("notebook_mocks" , path_to_mocks ).load_module ()
194
194
test_mocks .post_run (tb )
195
195
196
-
197
196
198
197
def uncomment_test_section (source ):
199
198
"""Uncomment sections marked with # UNCOMMENT_TO_RUN."""
200
- descriptive_comment = re .compile (r'^\s*##\s' )
201
- regular_comment = re .compile (r'^\s*#\s?' )
202
-
203
-
199
+ descriptive_comment = re .compile (r"^\s*##\s" )
200
+ regular_comment = re .compile (r"^\s*#\s?" )
201
+
204
202
lines = source .splitlines ()
205
203
result = []
206
204
uncomment_mode = False
207
-
205
+
208
206
for line in lines :
209
207
stripped_line = line .strip ()
210
-
208
+
211
209
if stripped_line == "# UNCOMMENT_FOR_TEST" :
212
210
uncomment_mode = True
213
211
result .append (line )
214
212
continue
215
-
213
+
216
214
if uncomment_mode :
217
215
if descriptive_comment .match (stripped_line ):
218
216
# Handle descriptive comments (##)
219
217
result .append (f"# { stripped_line [3 :].lstrip ()} " )
220
218
elif regular_comment .match (stripped_line ):
221
219
# Handle regular comments (#)
222
- result .append (regular_comment .sub ('' , line ))
220
+ result .append (regular_comment .sub ("" , line ))
223
221
elif stripped_line :
224
222
# Non-empty, non-comment line
225
223
uncomment_mode = False
@@ -229,5 +227,5 @@ def uncomment_test_section(source):
229
227
result .append (line )
230
228
else :
231
229
result .append (line )
232
-
233
- return ' \n ' .join (result )
230
+
231
+ return " \n " .join (result )
0 commit comments