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

add fixture to kill all terminals after each test #221

Merged
merged 3 commits into from
May 6, 2020
Merged
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
42 changes: 28 additions & 14 deletions tests/test_terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@
pytest.skip("Terminal API tests time out on Windows.", allow_module_level=True)


# Kill all running terminals after each test to avoid cross-test issues
# with still running terminals.
@pytest.fixture
def kill_all(serverapp):
async def _():
await serverapp.web_app.settings["terminal_manager"].kill_all()
return _


@pytest.fixture
def terminal_path(tmp_path):
subdir = tmp_path.joinpath('terminal_path')
Expand All @@ -21,7 +30,7 @@ def terminal_path(tmp_path):
shutil.rmtree(str(subdir), ignore_errors=True)


async def test_terminal_create(fetch):
async def test_terminal_create(fetch, kill_all):
await fetch(
'api', 'terminals',
method='POST',
Expand All @@ -37,9 +46,10 @@ async def test_terminal_create(fetch):
data = json.loads(resp_list.body.decode())

assert len(data) == 1
await kill_all()


async def test_terminal_create_with_kwargs(fetch, ws_fetch, terminal_path):
async def test_terminal_create_with_kwargs(fetch, ws_fetch, terminal_path, kill_all):
resp_create = await fetch(
'api', 'terminals',
method='POST',
Expand All @@ -59,9 +69,15 @@ async def test_terminal_create_with_kwargs(fetch, ws_fetch, terminal_path):
data = json.loads(resp_get.body.decode())

assert data['name'] == term_name
await kill_all()


async def test_terminal_create_with_cwd(fetch, ws_fetch, terminal_path):
async def test_terminal_create_with_cwd(
fetch,
ws_fetch,
terminal_path,
kill_all
):
resp = await fetch(
'api', 'terminals',
method='POST',
Expand All @@ -75,21 +91,19 @@ async def test_terminal_create_with_cwd(fetch, ws_fetch, terminal_path):
ws = await ws_fetch(
'terminals', 'websocket', term_name
)
await ws.write_message(json.dumps(['stdin', 'pwd\r']))

ws.write_message(json.dumps(['stdin', 'pwd\r\n']))

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

message = json.loads(message)
return messages

if message[0] == 'stdout':
message_stdout += message[1]
response = json.loads(response)
if response[0] == "stdout":
messages += response[1]

ws.close()

assert str(terminal_path) in message_stdout
assert str(terminal_path) in messages
await kill_all()