2
2
import json
3
3
import os
4
4
import shutil
5
+ import sys
5
6
6
7
import pytest
7
8
from tornado .httpclient import HTTPClientError
@@ -18,6 +19,16 @@ def terminal_path(tmp_path):
18
19
shutil .rmtree (str (subdir ), ignore_errors = True )
19
20
20
21
22
+ @pytest .fixture
23
+ def terminal_root_dir (jp_root_dir ):
24
+ subdir = jp_root_dir .joinpath ("terminal_path" )
25
+ subdir .mkdir ()
26
+
27
+ yield subdir
28
+
29
+ shutil .rmtree (str (subdir ), ignore_errors = True )
30
+
31
+
21
32
CULL_TIMEOUT = 10
22
33
CULL_INTERVAL = 3
23
34
@@ -137,6 +148,78 @@ async def test_terminal_create_with_cwd(
137
148
await jp_cleanup_subprocesses ()
138
149
139
150
151
+ async def test_terminal_create_with_relative_cwd (
152
+ jp_fetch , jp_ws_fetch , jp_root_dir , terminal_root_dir , jp_cleanup_subprocesses
153
+ ):
154
+ resp = await jp_fetch (
155
+ "api" ,
156
+ "terminals" ,
157
+ method = "POST" ,
158
+ body = json .dumps ({"cwd" : str (terminal_root_dir .relative_to (jp_root_dir ))}),
159
+ allow_nonstandard_methods = True ,
160
+ )
161
+
162
+ data = json .loads (resp .body .decode ())
163
+ term_name = data ["name" ]
164
+
165
+ ws = await jp_ws_fetch ("terminals" , "websocket" , term_name )
166
+
167
+ ws .write_message (json .dumps (["stdin" , "pwd\r \n " ]))
168
+
169
+ message_stdout = ""
170
+ while True :
171
+ try :
172
+ message = await asyncio .wait_for (ws .read_message (), timeout = 5.0 )
173
+ except asyncio .TimeoutError :
174
+ break
175
+
176
+ message = json .loads (message )
177
+
178
+ if message [0 ] == "stdout" :
179
+ message_stdout += message [1 ]
180
+
181
+ ws .close ()
182
+
183
+ expected = terminal_root_dir .name if sys .platform == "win32" else str (terminal_root_dir )
184
+ assert expected in message_stdout
185
+ await jp_cleanup_subprocesses ()
186
+
187
+
188
+ async def test_terminal_create_with_bad_cwd (jp_fetch , jp_ws_fetch , jp_cleanup_subprocesses ):
189
+ non_existing_path = "/tmp/path/to/nowhere"
190
+ resp = await jp_fetch (
191
+ "api" ,
192
+ "terminals" ,
193
+ method = "POST" ,
194
+ body = json .dumps ({"cwd" : non_existing_path }),
195
+ allow_nonstandard_methods = True ,
196
+ )
197
+
198
+ data = json .loads (resp .body .decode ())
199
+ term_name = data ["name" ]
200
+
201
+ ws = await jp_ws_fetch ("terminals" , "websocket" , term_name )
202
+
203
+ ws .write_message (json .dumps (["stdin" , "pwd\r \n " ]))
204
+
205
+ message_stdout = ""
206
+ while True :
207
+ try :
208
+ message = await asyncio .wait_for (ws .read_message (), timeout = 5.0 )
209
+ except asyncio .TimeoutError :
210
+ break
211
+
212
+ message = json .loads (message )
213
+
214
+ if message [0 ] == "stdout" :
215
+ message_stdout += message [1 ]
216
+
217
+ ws .close ()
218
+
219
+ assert non_existing_path not in message_stdout
220
+ await jp_cleanup_subprocesses ()
221
+
222
+
140
223
async def test_culling_config (jp_server_config , jp_configurable_serverapp ):
141
224
terminal_mgr_config = jp_configurable_serverapp ().config .ServerApp .TerminalManager
142
225
assert terminal_mgr_config .cull_inactive_timeout == CULL_TIMEOUT
0 commit comments