Skip to content

Commit 37abeb6

Browse files
Zsailerqntnrbns
andauthored
Allow params to terminal api endpoint (#201)
* adding cwd param to terminal api endpoint * Adding terminal tests and new terminal kwargs * fixing fetch method in terminal tests * attempting to fix windows tests for terminal * fixing condition for windows in terminal tests * Updating terminal tests to skip windows * skip terminal tests on windows Co-authored-by: qntnrbns <[email protected]>
1 parent e74475f commit 37abeb6

File tree

5 files changed

+102
-5
lines changed

5 files changed

+102
-5
lines changed

jupyter_server/terminal/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import terminado
44
from ..utils import check_version
55

6-
if not check_version(terminado.__version__, '0.8.1'):
7-
raise ImportError("terminado >= 0.8.1 required, found %s" % terminado.__version__)
6+
if not check_version(terminado.__version__, '0.8.3'):
7+
raise ImportError("terminado >= 0.8.3 required, found %s" % terminado.__version__)
88

99
from ipython_genutils.py3compat import which
1010
from terminado import NamedTermManager

jupyter_server/terminal/api_handlers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ def get(self):
2121
@web.authenticated
2222
def post(self):
2323
"""POST /terminals creates a new terminal and redirects to it"""
24-
name, _ = self.terminal_manager.new_named_terminal()
24+
data = self.get_json_body() or {}
25+
26+
name, _ = self.terminal_manager.new_named_terminal(**data)
2527
self.finish(json.dumps({'name': name}))
2628

2729
# Increase the metric by one because a new terminal was created

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
'nbconvert',
9393
'ipykernel', # bless IPython kernel for now
9494
'Send2Trash',
95-
'terminado>=0.8.1',
95+
'terminado>=0.8.3',
9696
'prometheus_client',
9797
"pywin32>=1.0 ; sys_platform == 'win32'"
9898
],

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
pytest_plugins = ['pytest_jupyter_server']
1+
pytest_plugins = ['pytest_jupyter_server']

tests/test_terminal.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Only Run tests on MacOS and Linux
2+
import shutil
3+
import pytest
4+
import json
5+
import asyncio
6+
import sys
7+
8+
# Skip this whole module on Windows. The terminal API leads
9+
# to timeouts on Windows CI.
10+
if sys.platform.startswith('win'):
11+
pytest.skip("Terminal API tests time out on Windows.", allow_module_level=True)
12+
13+
14+
@pytest.fixture
15+
def terminal_path(tmp_path):
16+
subdir = tmp_path.joinpath('terminal_path')
17+
subdir.mkdir()
18+
19+
yield subdir
20+
21+
shutil.rmtree(str(subdir), ignore_errors=True)
22+
23+
24+
async def test_terminal_create(fetch):
25+
await fetch(
26+
'api', 'terminals',
27+
method='POST',
28+
allow_nonstandard_methods=True,
29+
)
30+
31+
resp_list = await fetch(
32+
'api', 'terminals',
33+
method='GET',
34+
allow_nonstandard_methods=True,
35+
)
36+
37+
data = json.loads(resp_list.body.decode())
38+
39+
assert len(data) == 1
40+
41+
42+
async def test_terminal_create_with_kwargs(fetch, ws_fetch, terminal_path):
43+
resp_create = await fetch(
44+
'api', 'terminals',
45+
method='POST',
46+
body=json.dumps({'cwd': str(terminal_path)}),
47+
allow_nonstandard_methods=True,
48+
)
49+
50+
data = json.loads(resp_create.body.decode())
51+
term_name = data['name']
52+
53+
resp_get = await fetch(
54+
'api', 'terminals', term_name,
55+
method='GET',
56+
allow_nonstandard_methods=True,
57+
)
58+
59+
data = json.loads(resp_get.body.decode())
60+
61+
assert data['name'] == term_name
62+
63+
64+
async def test_terminal_create_with_cwd(fetch, ws_fetch, terminal_path):
65+
resp = await fetch(
66+
'api', 'terminals',
67+
method='POST',
68+
body=json.dumps({'cwd': str(terminal_path)}),
69+
allow_nonstandard_methods=True,
70+
)
71+
72+
data = json.loads(resp.body.decode())
73+
term_name = data['name']
74+
75+
ws = await ws_fetch(
76+
'terminals', 'websocket', term_name
77+
)
78+
79+
ws.write_message(json.dumps(['stdin', 'pwd\r\n']))
80+
81+
message_stdout = ''
82+
while True:
83+
try:
84+
message = await asyncio.wait_for(ws.read_message(), timeout=1.0)
85+
except asyncio.TimeoutError:
86+
break
87+
88+
message = json.loads(message)
89+
90+
if message[0] == 'stdout':
91+
message_stdout += message[1]
92+
93+
ws.close()
94+
95+
assert str(terminal_path) in message_stdout

0 commit comments

Comments
 (0)