20
20
21
21
22
22
REQUEST_REGEX = re .compile (r"^/api/kernels/\w+-\w+-\w+-\w+-\w+/requests/\w+-\w+-\w+-\w+-\w+$" )
23
+ ANSI_REGEX = re .compile ("\x1b \\ [(.*?)([@-~])" )
24
+
25
+
26
+ def strip_ansi (text : str ):
27
+ return ANSI_REGEX .sub ("" , text )
23
28
24
29
25
30
async def _wait_request (fetch , endpoint : str ):
@@ -122,7 +127,17 @@ async def test_post_execute(jp_fetch, pending_kernel_is_ready, snippet, output):
122
127
(
123
128
(
124
129
"1 / 0" ,
125
- '{"output_type": "error", "ename": "ZeroDivisionError", "evalue": "division by zero", "traceback": ["\\ u001b[0;31m---------------------------------------------------------------------------\\ u001b[0m", "\\ u001b[0;31mZeroDivisionError\\ u001b[0m Traceback (most recent call last)", "Cell \\ u001b[0;32mIn[1], line 1\\ u001b[0m\\ n\\ u001b[0;32m----> 1\\ u001b[0m \\ u001b[38;5;241;43m1\\ u001b[39;49m\\ u001b[43m \\ u001b[49m\\ u001b[38;5;241;43m/\\ u001b[39;49m\\ u001b[43m \\ u001b[49m\\ u001b[38;5;241;43m0\\ u001b[39;49m\\ n", "\\ u001b[0;31mZeroDivisionError\\ u001b[0m: division by zero"]}' , # noqa: E501
130
+ {
131
+ "output_type" : "error" ,
132
+ "ename" : "ZeroDivisionError" ,
133
+ "evalue" : "division by zero" ,
134
+ "traceback" : [
135
+ "---------------------------------------------------------------------------" ,
136
+ "ZeroDivisionError Traceback (most recent call last)" ,
137
+ "Cell In[1], line 1\n ----> 1 1 / 0\n " ,
138
+ "ZeroDivisionError: division by zero"
139
+ ]
140
+ },
126
141
),
127
142
),
128
143
)
@@ -146,17 +161,27 @@ async def test_post_erroneous_execute(jp_fetch, pending_kernel_is_ready, snippet
146
161
147
162
assert response .code == 200
148
163
payload = json .loads (response .body )
164
+ outputs = payload ["outputs" ]
165
+ del payload ["outputs" ]
149
166
assert payload == {
150
167
"status" : "error" ,
151
- "execution_count" : 1 ,
152
- "outputs" : f"[{ output } ]" ,
168
+ "execution_count" : 1
153
169
}
170
+ outputs = json .loads (outputs )
171
+ for output in outputs :
172
+ output ["traceback" ] = [
173
+ strip_ansi (line )
174
+ for line in output ["traceback" ]
175
+ ]
176
+
177
+ assert outputs == [output ]
154
178
155
179
response2 = await jp_fetch ("api" , "kernels" , kernel ["id" ], method = "DELETE" )
156
180
assert response2 .code == 204
157
181
158
182
await asyncio .sleep (1 )
159
183
184
+
160
185
@pytest .mark .asyncio
161
186
async def test_kernel_worker_reports_error (monkeypatch ):
162
187
# Patch _execute_snippet to raise an error
@@ -196,6 +221,7 @@ async def fake_execute(client, ydoc, snippet, metadata, stdin_hook):
196
221
assert "error" in results [uid ], f"Expected an 'error' key in results[{ uid !r} ]"
197
222
assert "simulated failure" in results [uid ]["error" ]
198
223
224
+
199
225
@pytest .mark .timeout (TEST_TIMEOUT )
200
226
async def test_execution_timing_metadata (jp_root_dir , jp_fetch , pending_kernel_is_ready , rtc_create_notebook , jp_serverapp ):
201
227
snippet = "a = 1"
0 commit comments