Skip to content

Commit 31416ae

Browse files
MrKevinWeissfjmolinas
authored andcommitted
riotctrl.shell/tests: Add tests for prompt and prompt timeout
1 parent 77f68cb commit 31416ae

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

riotctrl/tests/shell_test.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import tempfile
66

77
import pytest
8+
from pexpect.exceptions import TIMEOUT
89

910
import riotctrl.ctrl
1011
import riotctrl.shell
@@ -20,13 +21,14 @@ def fixture_app_pidfile_env():
2021
yield {'PIDFILE': tmpfile.name}
2122

2223

23-
def init_ctrl(app_pidfile_env, skip_first_prompt=False):
24+
def init_ctrl(app_pidfile_env, skip_first_prompt=False, prompt="> "):
2425
"""Initializes RIOTCtrl for ShellInteraction tests"""
2526
env = {
2627
'QUIET': '1', # pipe > in command interferes with test
2728
'BOARD': 'board',
2829
'APPLICATION': './shell.py' +
29-
(' 1' if skip_first_prompt else ''),
30+
(' 1' if skip_first_prompt else ' 0') +
31+
" --prompt=\"{}\"".format(prompt),
3032
}
3133
env.update(app_pidfile_env)
3234

@@ -57,6 +59,31 @@ def test_shell_interaction_cmd_first_prompt_missing(app_pidfile_env):
5759
assert 'snafoo' in res
5860

5961

62+
def test_shell_interaction_cmd_different_prompt(app_pidfile_env):
63+
"""Test basic functionalities with the 'shell' application when the prompt
64+
is not default."""
65+
prompt = "my_prompt "
66+
ctrl = init_ctrl(app_pidfile_env, prompt=prompt)
67+
with ctrl.run_term(logfile=sys.stdout, reset=False):
68+
shell = riotctrl.shell.ShellInteraction(ctrl, prompt=prompt)
69+
res = shell.cmd('foobar')
70+
assert 'foobar' in res
71+
72+
73+
def test_shell_interaction_cmd_prompt_timeout(app_pidfile_env):
74+
"""Test basic functionalities with the 'shell' application when the
75+
prompt timeout is changed."""
76+
prompt = "my_prompt "
77+
ctrl = init_ctrl(app_pidfile_env, prompt=prompt)
78+
with ctrl.run_term(logfile=sys.stdout, reset=False):
79+
riotctrl.shell.ShellInteraction.PROMPT_TIMEOUT = 0
80+
shell = riotctrl.shell.ShellInteraction(ctrl)
81+
with pytest.raises(TIMEOUT):
82+
# Fast systems may occasionally reply in 0 secs
83+
for _ in range(5):
84+
shell.cmd('foobar')
85+
86+
6087
def test_shell_interaction_cmd_reset_term(app_pidfile_env):
6188
"""Test basic functionalities with the 'shell' application."""
6289
ctrl = init_ctrl(app_pidfile_env)

riotctrl/tests/utils/application/shell.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77

88
PARSER = argparse.ArgumentParser()
99
PARSER.add_argument('skip_first_prompt', type=bool, default=False, nargs='?')
10+
PARSER.add_argument('--prompt', type=str, default="> ")
1011

1112

12-
def main(skip_first_prompt=False):
13+
def main(skip_first_prompt=False, prompt="> "):
1314
"""Print some header and echo the output."""
1415
if not skip_first_prompt:
1516
print('Starting RIOT Ctrl')
@@ -18,7 +19,7 @@ def main(skip_first_prompt=False):
1819
print(input())
1920
print()
2021
while True:
21-
print(input("> "))
22+
print(input(prompt))
2223
print()
2324

2425

0 commit comments

Comments
 (0)