Skip to content

Commit 967371d

Browse files
committed
Start to wizard pauses (including tests)
1 parent 2b6714b commit 967371d

2 files changed

Lines changed: 139 additions & 0 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import pytest
2+
import time
3+
from paths_cli.tests.wizard.mock_wizard import mock_wizard
4+
5+
from paths_cli.wizard.pause import *
6+
7+
8+
def test_get_pause_style():
9+
default_style = PAUSE_STYLES['default']
10+
assert get_pause_style() == default_style
11+
12+
13+
@pytest.mark.parametrize('input_type', ['string', 'tuple', 'PauseStyle'])
14+
def test_set_pause_style(input_type):
15+
# check that we have the default settings
16+
default_style = PAUSE_STYLES['default']
17+
test_style = PAUSE_STYLES['testing']
18+
input_val = {
19+
'string': 'testing',
20+
'tuple': tuple(test_style),
21+
'PauseStyle': PauseStyle(*test_style)
22+
}[input_type]
23+
assert input_val is not test_style # always a different object
24+
assert get_pause_style() == default_style
25+
set_pause_style(input_val)
26+
assert get_pause_style() != default_style
27+
assert get_pause_style() == test_style
28+
set_pause_style('default') # explicitly reset default
29+
30+
31+
def test_set_pause_bad_name():
32+
with pytest.raises(RuntimeError, match="Unknown pause style"):
33+
set_pause_style('foo')
34+
35+
# ensure we didn't break anything for later tests
36+
assert get_pause_style() == PAUSE_STYLES['default']
37+
38+
39+
def test_pause_style_context():
40+
assert get_pause_style() == PAUSE_STYLES['default']
41+
with pause_style('testing'):
42+
assert get_pause_style() == PAUSE_STYLES['testing']
43+
assert get_pause_style() == PAUSE_STYLES['default']
44+
45+
46+
def _run_pause_test(func):
47+
test_style = PAUSE_STYLES['testing']
48+
expected = getattr(test_style, func.__name__)
49+
wiz = mock_wizard([])
50+
with pause_style(test_style):
51+
start = time.time()
52+
func(wiz)
53+
duration = time.time() - start
54+
assert expected <= duration < 1.1 * duration
55+
return wiz
56+
57+
58+
def test_short():
59+
_ = _run_pause_test(short)
60+
61+
62+
def test_long():
63+
_ = _run_pause_test(long)
64+
65+
66+
def test_section():
67+
wiz = _run_pause_test(section)

‎paths_cli/wizard/pause.py‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import time
2+
import contextlib
3+
from collections import namedtuple
4+
5+
PauseStyle = namedtuple("PauseStyle", ['short', 'long', 'section'])
6+
7+
PAUSE_STYLES = {
8+
'testing': PauseStyle(0.01, 0.03, 0.05),
9+
'default': PauseStyle(0.1, 0.5, 0.5),
10+
'nopause': PauseStyle(0.0, 0.0, 0.0),
11+
}
12+
13+
_PAUSE_STYLE = PAUSE_STYLES['default']
14+
15+
16+
@contextlib.contextmanager
17+
def pause_style(style):
18+
"""Context manager for pause styles.
19+
20+
Parameters
21+
----------
22+
style : :class:`.PauseStyle`
23+
pause style to use within the context
24+
"""
25+
old_style = get_pause_style()
26+
try:
27+
set_pause_style(style)
28+
yield
29+
finally:
30+
set_pause_style(old_style)
31+
32+
33+
def get_pause_style():
34+
"""Get the current pause style"""
35+
global _PAUSE_STYLE
36+
return _PAUSE_STYLE
37+
38+
39+
def set_pause_style(style):
40+
"""Set the pause style
41+
42+
Parameters
43+
----------
44+
pause_style : :class:`.PauseStyle` or str
45+
pause style to use, can be a string if the style is registered in
46+
pause.PAUSE_STYLES
47+
"""
48+
global _PAUSE_STYLE
49+
if isinstance(style, str):
50+
try:
51+
_PAUSE_STYLE = PAUSE_STYLES[style]
52+
except KeyError as exc:
53+
raise RuntimeError(f"Unknown pause style: '{style}'") from exc
54+
else:
55+
_PAUSE_STYLE = style
56+
57+
58+
def section(wizard):
59+
"""Section break (pause and possible visual cue).
60+
"""
61+
time.sleep(_PAUSE_STYLE.section)
62+
# TODO: have the wizard draw a line?
63+
64+
65+
def long(wizard):
66+
"""Long pause from the wizard"""
67+
time.sleep(_PAUSE_STYLE.long)
68+
69+
70+
def short(wizard):
71+
"""Short pause from the wizard"""
72+
time.sleep(_PAUSE_STYLE.short)

0 commit comments

Comments
 (0)