Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding cwd param to terminal api endpoint #160

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions jupyter_server/terminal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import terminado
from ..utils import check_version

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

from ipython_genutils.py3compat import which
from terminado import NamedTermManager
Expand Down
4 changes: 3 additions & 1 deletion jupyter_server/terminal/api_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ def get(self):
@web.authenticated
def post(self):
"""POST /terminals creates a new terminal and redirects to it"""
name, _ = self.terminal_manager.new_named_terminal()
data = self.get_json_body() or {}

name, _ = self.terminal_manager.new_named_terminal(**data)
self.finish(json.dumps({'name': name}))

# Increase the metric by one because a new terminal was created
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
'nbconvert',
'ipykernel', # bless IPython kernel for now
'Send2Trash',
'terminado>=0.8.1',
'terminado>=0.8.3',
'prometheus_client',
"pywin32>=1.0 ; sys_platform == 'win32'"
],
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pytest_plugins = ['pytest_jupyter_server']
pytest_plugins = ['pytest_jupyter_server']
90 changes: 90 additions & 0 deletions tests/test_terminal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import shutil
import pytest
import json
import asyncio
import sys


@pytest.fixture
def terminal_path(tmp_path):
subdir = tmp_path.joinpath('terminal_path')
subdir.mkdir()

yield subdir

shutil.rmtree(str(subdir), ignore_errors=True)


async def test_terminal_create(fetch):
await fetch(
'api', 'terminals',
method='POST',
allow_nonstandard_methods=True,
)

resp_list = await fetch(
'api', 'terminals',
method='GET',
allow_nonstandard_methods=True,
)

data = json.loads(resp_list.body.decode())

assert len(data) == 1


async def test_terminal_create_with_kwargs(fetch, ws_fetch, terminal_path):
resp_create = await fetch(
'api', 'terminals',
method='POST',
body=json.dumps({'cwd': str(terminal_path)}),
allow_nonstandard_methods=True,
)

data = json.loads(resp_create.body.decode())
term_name = data['name']

resp_get = await fetch(
'api', 'terminals', term_name,
method='GET',
allow_nonstandard_methods=True,
)

data = json.loads(resp_get.body.decode())

assert data['name'] == term_name


@pytest.mark.skipif(sys.platform == 'win32', reason="Test times out on Windows.")
async def test_terminal_create_with_cwd(fetch, ws_fetch, terminal_path):
resp = await fetch(
'api', 'terminals',
method='POST',
body=json.dumps({'cwd': str(terminal_path)}),
allow_nonstandard_methods=True,
)

data = json.loads(resp.body.decode())
term_name = data['name']

ws = await ws_fetch(
'terminals', 'websocket', term_name
)

ws.write_message(json.dumps(['stdin', 'pwd\r\n']))
Zsailer marked this conversation as resolved.
Show resolved Hide resolved

message_stdout = ''
while True:
try:
message = await asyncio.wait_for(ws.read_message(), timeout=1.0)
except asyncio.TimeoutError:
break

message = json.loads(message)

if message[0] == 'stdout':
message_stdout += message[1]

ws.close()

assert str(terminal_path) in message_stdout