xonsh/tests/test_prompt.py

191 lines
5.3 KiB
Python
Raw Normal View History

2016-11-14 21:09:25 +02:00
import os
import subprocess as sp
2016-11-17 21:37:45 +02:00
import tempfile
2016-11-02 15:06:17 +02:00
from unittest.mock import Mock
2016-09-15 17:06:35 +03:00
import pytest
from xonsh.environ import Env
2016-11-01 12:38:54 +02:00
from xonsh.prompt.base import PromptFormatter
2016-11-14 21:09:25 +02:00
from xonsh.prompt import vc
2016-09-15 17:06:35 +03:00
2016-11-14 21:09:25 +02:00
from tools import skip_if_py34, DummyEnv
2016-11-14 19:28:32 +02:00
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):
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
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
2016-11-17 21:37:45 +02:00
# Xonsh interaction with version control systems.
VC_BRANCH = {'git': 'master',
'hg': 'default'}
2016-11-14 21:09:25 +02:00
2016-11-18 14:42:54 +02:00
@pytest.fixture(scope='module', params=VC_BRANCH.keys())
2016-11-17 21:37:45 +02:00
def test_repo(request):
2016-11-18 14:42:54 +02:00
"""Return a dict with vc and a temporary dir
that is a repository for testing.
"""
2016-11-17 21:37:45 +02:00
vc = request.param
2016-11-18 14:42:54 +02:00
temp_dir = tempfile.mkdtemp()
os.chdir(temp_dir)
try:
2016-11-17 21:37:45 +02:00
sp.call([vc, 'init'])
except FileNotFoundError:
pytest.skip('cannot find {} executable'.format(vc))
# git needs at least one commit
if vc == 'git':
with open('test-file', 'w'):
pass
sp.call(['git', 'add', 'test-file'])
sp.call(['git', 'commit', '-m', 'test commit'])
2016-11-18 14:42:54 +02:00
return {'name': vc, 'dir': temp_dir}
2016-11-17 21:37:45 +02:00
def test_test_repo(test_repo):
2016-11-18 14:42:54 +02:00
dotdir = os.path.isdir(os.path.join(test_repo['dir'],
'.{}'.format(test_repo['name'])))
assert dotdir
if test_repo['name'] == 'git':
assert os.path.isfile(os.path.join(test_repo['dir'], 'test-file'))
2016-11-17 21:37:45 +02:00
def test_no_repo(xonsh_builtins):
import queue
temp_dir = tempfile.mkdtemp()
xonsh_builtins.__xonsh_env__ = Env(VC_BRANCH_TIMEOUT=2, PWD=temp_dir)
q = queue.Queue()
try:
vc._get_hg_root(q)
except AttributeError:
assert False
2016-11-17 21:37:45 +02:00
def test_vc_get_branch(test_repo, xonsh_builtins):
xonsh_builtins.__xonsh_env__ = Env(VC_BRANCH_TIMEOUT=2)
2016-11-14 21:09:25 +02:00
# get corresponding function from vc module
fun = 'get_{}_branch'.format(test_repo['name'])
2016-11-17 21:37:45 +02:00
obs = getattr(vc, fun)()
2016-11-22 10:41:37 -05:00
if obs is not None:
assert obs == VC_BRANCH[test_repo['name']]
def test_current_branch_calls_locate_binary_for_empty_cmds_cache(xonsh_builtins):
cache = xonsh_builtins.__xonsh_commands_cache__
xonsh_builtins.__xonsh_env__ = DummyEnv(VC_BRANCH_TIMEOUT=1)
cache.is_empty = Mock(return_value=True)
cache.locate_binary = Mock(return_value='')
vc.current_branch()
assert cache.locate_binary.called
2016-11-18 14:42:54 +02:00
def test_current_branch_does_not_call_locate_binary_for_non_empty_cmds_cache(xonsh_builtins):
cache = xonsh_builtins.__xonsh_commands_cache__
xonsh_builtins.__xonsh_env__ = DummyEnv(VC_BRANCH_TIMEOUT=1)
cache.is_empty = Mock(return_value=False)
cache.locate_binary = Mock(return_value='')
# make lazy locate return nothing to avoid running vc binaries
cache.lazy_locate_binary = Mock(return_value='')
vc.current_branch()
assert not cache.locate_binary.called