2016-11-02 15:06:17 +02:00
|
|
|
from unittest.mock import Mock
|
|
|
|
|
2016-09-15 17:06:35 +03:00
|
|
|
import pytest
|
|
|
|
|
2016-09-15 17:19:39 +03:00
|
|
|
from xonsh.environ import Env
|
2016-11-01 12:38:54 +02:00
|
|
|
from xonsh.prompt.base import PromptFormatter
|
2016-09-15 17:06:35 +03:00
|
|
|
|
|
|
|
|
2016-11-01 12:38:54 +02:00
|
|
|
@pytest.fixture
|
2016-11-01 14:38:36 +02:00
|
|
|
def formatter(xonsh_builtins):
|
2016-11-01 12:38:54 +02:00
|
|
|
return PromptFormatter()
|
|
|
|
|
|
|
|
|
2016-11-02 15:35:59 +02:00
|
|
|
@pytest.mark.parametrize('fields', [{
|
2016-09-15 17:06:35 +03:00
|
|
|
'a_string': 'cat',
|
|
|
|
'none': (lambda: None),
|
|
|
|
'f': (lambda: 'wakka'),
|
|
|
|
}])
|
|
|
|
@pytest.mark.parametrize('inp, exp', [
|
|
|
|
('my {a_string}', 'my cat'),
|
|
|
|
('my {none}{a_string}', 'my cat'),
|
|
|
|
('{f} jawaka', 'wakka jawaka'),
|
|
|
|
])
|
2016-11-02 16:18:17 +02:00
|
|
|
def test_format_prompt(inp, exp, fields, formatter):
|
2016-11-02 15:35:59 +02:00
|
|
|
obs = formatter(template=inp, fields=fields)
|
2016-09-15 17:06:35 +03:00
|
|
|
assert exp == obs
|
|
|
|
|
|
|
|
|
2016-11-02 15:35:59 +02:00
|
|
|
@pytest.mark.parametrize('fields', [{
|
2016-09-15 17:06:35 +03:00
|
|
|
'a_string': 'cats',
|
|
|
|
'a_number': 7,
|
|
|
|
'empty': '',
|
|
|
|
'current_job': (lambda: 'sleep'),
|
|
|
|
'none': (lambda: None),
|
|
|
|
}])
|
|
|
|
@pytest.mark.parametrize('inp, exp', [
|
|
|
|
('{a_number:{0:^3}}cats', ' 7 cats'),
|
2016-11-01 12:38:54 +02:00
|
|
|
('{current_job:{} | }xonsh', 'sleep | xonsh'),
|
|
|
|
('{none:{} | }{a_string}{empty:!}', 'cats!'),
|
|
|
|
('{none:{}}', ''),
|
|
|
|
('{{{a_string:{{{}}}}}}', '{{cats}}'),
|
|
|
|
('{{{none:{{{}}}}}}', '{}'),
|
2016-09-15 17:06:35 +03:00
|
|
|
])
|
2016-11-02 16:18:17 +02:00
|
|
|
def test_format_prompt_with_format_spec(inp, exp, fields, formatter):
|
2016-11-02 15:35:59 +02:00
|
|
|
obs = formatter(template=inp, fields=fields)
|
2016-09-15 17:06:35 +03:00
|
|
|
assert exp == obs
|
|
|
|
|
|
|
|
|
2016-11-02 16:18:17 +02:00
|
|
|
def test_format_prompt_with_broken_template(formatter):
|
2016-09-15 17:06:35 +03:00
|
|
|
for p in ('{user', '{user}{hostname'):
|
2016-11-01 20:12:38 +02:00
|
|
|
assert formatter(p) == p
|
2016-09-15 17:06:35 +03:00
|
|
|
|
|
|
|
# '{{user' will be parsed to '{user'
|
|
|
|
for p in ('{{user}', '{{user'):
|
2016-11-01 20:12:38 +02:00
|
|
|
assert 'user' in formatter(p)
|
2016-09-15 17:06:35 +03:00
|
|
|
|
|
|
|
|
2016-11-01 14:10:11 +02:00
|
|
|
@pytest.mark.parametrize('inp', [
|
|
|
|
'{user',
|
|
|
|
'{{user',
|
|
|
|
'{{user}',
|
|
|
|
'{user}{hostname',
|
|
|
|
])
|
2016-11-02 16:18:17 +02:00
|
|
|
def test_format_prompt_with_broken_template_in_func(inp, formatter):
|
2016-11-01 14:10:11 +02:00
|
|
|
# '{{user' will be parsed to '{user'
|
2016-11-01 20:12:38 +02:00
|
|
|
assert '{user' in formatter(lambda: inp)
|
2016-09-15 17:06:35 +03:00
|
|
|
|
|
|
|
|
2016-11-01 12:38:54 +02:00
|
|
|
def test_format_prompt_with_invalid_func(formatter, xonsh_builtins):
|
2016-09-15 17:19:39 +03:00
|
|
|
xonsh_builtins.__xonsh_env__ = Env()
|
2016-11-01 12:38:54 +02:00
|
|
|
|
2016-09-15 17:06:35 +03:00
|
|
|
def p():
|
2016-11-01 14:10:11 +02:00
|
|
|
foo = bar # raises exception # noqa
|
2016-09-15 17:06:35 +03:00
|
|
|
return '{user}'
|
2016-11-01 12:38:54 +02:00
|
|
|
|
2016-11-01 20:12:38 +02:00
|
|
|
assert isinstance(formatter(p), str)
|
2016-09-15 17:31:41 +03:00
|
|
|
|
|
|
|
|
2016-11-01 12:38:54 +02:00
|
|
|
def test_format_prompt_with_func_that_raises(formatter, capsys, xonsh_builtins):
|
2016-09-15 18:51:13 +03:00
|
|
|
xonsh_builtins.__xonsh_env__ = Env()
|
2016-09-15 17:31:41 +03:00
|
|
|
template = 'tt {zerodiv} tt'
|
|
|
|
exp = 'tt (ERROR:zerodiv) tt'
|
2016-11-02 15:35:59 +02:00
|
|
|
fields = {'zerodiv': lambda: 1/0}
|
|
|
|
obs = formatter(template, fields)
|
2016-09-15 17:31:41 +03:00
|
|
|
assert exp == obs
|
|
|
|
out, err = capsys.readouterr()
|
|
|
|
assert 'prompt: error' in err
|
2016-11-02 15:06:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_promptformatter_cache(formatter):
|
|
|
|
spam = Mock()
|
|
|
|
template = '{spam} and {spam}'
|
2016-11-02 15:35:59 +02:00
|
|
|
fields = {'spam': spam}
|
2016-11-02 15:06:17 +02:00
|
|
|
|
2016-11-02 15:35:59 +02:00
|
|
|
formatter(template, fields)
|
2016-11-02 15:06:17 +02:00
|
|
|
|
|
|
|
assert spam.call_count == 1
|
|
|
|
|
|
|
|
|
2016-11-02 16:18:17 +02:00
|
|
|
def test_promptformatter_clears_cache(formatter):
|
2016-11-02 15:06:17 +02:00
|
|
|
spam = Mock()
|
|
|
|
template = '{spam} and {spam}'
|
2016-11-02 15:35:59 +02:00
|
|
|
fields = {'spam': spam}
|
2016-11-02 15:06:17 +02:00
|
|
|
|
2016-11-02 15:35:59 +02:00
|
|
|
formatter(template, fields)
|
|
|
|
formatter(template, fields)
|
2016-11-02 15:06:17 +02:00
|
|
|
|
|
|
|
assert spam.call_count == 2
|