xonsh/tests/test_prompt.py

266 lines
7.4 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
from xonsh.prompt.base import PromptFormatter, PROMPT_FIELDS
2016-11-14 21:09:25 +02:00
from xonsh.prompt import vc
2016-09-15 17:06:35 +03:00
from tools import 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()
@pytest.fixture
def live_fields():
return PROMPT_FIELDS
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize(
"fields", [{"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
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize(
"fields",
[
{
"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"),
("{current_job:{} | }xonsh", "sleep | xonsh"),
("{none:{} | }{a_string}{empty:!}", "cats!"),
("{none:{}}", ""),
("{{{a_string:{{{}}}}}}", "{{cats}}"),
("{{{none:{{{}}}}}}", "{}"),
],
)
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):
2018-08-30 09:18:49 -05: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'
2018-08-30 09:18:49 -05:00
for p in ("{{user}", "{{user"):
assert "user" in formatter(p)
2016-09-15 17:06:35 +03:00
2018-08-30 09:18:49 -05: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'
2018-08-30 09:18:49 -05: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):
2018-09-13 14:03:35 -04: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
2018-08-30 09:18:49 -05: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
2018-08-30 09:18:49 -05:00
def test_format_prompt_with_func_that_raises(formatter, capsys, xonsh_builtins):
2018-09-13 14:03:35 -04:00
xonsh_builtins.__xonsh__.env = Env()
2018-08-30 09:18:49 -05:00
template = "tt {zerodiv} tt"
exp = "tt {BACKGROUND_RED}{ERROR:zerodiv}{NO_COLOR} tt"
2018-08-30 09:18:49 -05:00
fields = {"zerodiv": lambda: 1 / 0}
2016-11-02 15:35:59 +02:00
obs = formatter(template, fields)
2016-09-15 17:31:41 +03:00
assert exp == obs
out, err = capsys.readouterr()
2018-08-30 09:18:49 -05:00
assert "prompt: error" in err
2016-11-02 15:06:17 +02:00
def test_format_prompt_with_no_env(formatter, xonsh_builtins, live_fields):
xonsh_builtins.__xonsh__.shell.prompt_formatter = formatter
env = Env()
2019-02-13 18:49:39 -05:00
env.pop("VIRTUAL_ENV", None) # For virtualenv
env.pop("CONDA_DEFAULT_ENV", None) # For conda/CircleCI
xonsh_builtins.__xonsh__.env = env
2019-02-13 18:49:39 -05:00
assert formatter("{env_name}", fields=live_fields) == ""
2019-02-13 18:49:39 -05:00
@pytest.mark.parametrize("envname", ["env", "foo", "bar"])
def test_format_prompt_with_various_envs(
formatter, xonsh_builtins, live_fields, envname
):
xonsh_builtins.__xonsh__.shell.prompt_formatter = formatter
env = Env(VIRTUAL_ENV=envname)
xonsh_builtins.__xonsh__.env = env
2019-02-13 18:49:39 -05:00
exp = live_fields["env_prefix"] + envname + live_fields["env_postfix"]
assert formatter("{env_name}", fields=live_fields) == exp
2019-02-13 18:49:39 -05:00
@pytest.mark.parametrize("pre", ["(", "[[", "", " "])
@pytest.mark.parametrize("post", [")", "]]", "", " "])
def test_format_prompt_with_various_prepost(
2019-02-13 18:49:39 -05:00
formatter, xonsh_builtins, live_fields, pre, post
):
xonsh_builtins.__xonsh__.shell.prompt_formatter = formatter
2019-02-13 18:49:39 -05:00
env = Env(VIRTUAL_ENV="env")
xonsh_builtins.__xonsh__.env = env
2019-02-13 18:49:39 -05:00
live_fields.update({"env_prefix": pre, "env_postfix": post})
2019-02-13 18:49:39 -05:00
exp = pre + "env" + post
assert formatter("{env_name}", fields=live_fields) == exp
def test_noenv_with_disable_set(formatter, xonsh_builtins, live_fields):
xonsh_builtins.__xonsh__.shell.prompt_formatter = formatter
2019-02-13 18:49:39 -05:00
env = Env(VIRTUAL_ENV="env", VIRTUAL_ENV_DISABLE_PROMPT=1)
xonsh_builtins.__xonsh__.env = env
2019-02-13 18:49:39 -05:00
exp = ""
assert formatter("{env_name}", fields=live_fields) == exp
2019-02-13 18:49:39 -05:00
@pytest.mark.parametrize("disable", [0, 1])
def test_custom_env_overrides_default(formatter, xonsh_builtins, live_fields, disable):
xonsh_builtins.__xonsh__.shell.prompt_formatter = formatter
2019-02-13 18:49:39 -05:00
prompt = "!venv active! "
env = Env(
2019-02-13 18:49:39 -05:00
VIRTUAL_ENV="env", VIRTUAL_ENV_PROMPT=prompt, VIRTUAL_ENV_DISABLE_PROMPT=disable
)
xonsh_builtins.__xonsh__.env = env
2019-02-13 18:49:39 -05:00
exp = "" if disable else prompt
assert formatter("{env_name}", fields=live_fields) == exp
2016-11-02 15:06:17 +02:00
def test_promptformatter_cache(formatter):
spam = Mock()
2018-08-30 09:18:49 -05:00
template = "{spam} and {spam}"
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()
2018-08-30 09:18:49 -05:00
template = "{spam} and {spam}"
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.
2018-08-30 09:18:49 -05:00
VC_BRANCH = {"git": "master", "hg": "default"}
2016-11-14 21:09:25 +02:00
2018-08-30 09:18:49 -05: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:
2018-08-30 09:18:49 -05:00
sp.call([vc, "init"])
except FileNotFoundError:
2018-08-30 09:18:49 -05:00
pytest.skip("cannot find {} executable".format(vc))
# git needs at least one commit
2018-08-30 09:18:49 -05:00
if vc == "git":
with open("test-file", "w"):
pass
2018-08-30 09:18:49 -05:00
sp.call(["git", "add", "test-file"])
sp.call(["git", "commit", "--no-gpg-sign", "-m", "test commit"])
2018-08-30 09:18:49 -05:00
return {"name": vc, "dir": temp_dir}
2016-11-17 21:37:45 +02:00
def test_test_repo(test_repo):
2018-08-30 09:18:49 -05:00
dotdir = os.path.isdir(
os.path.join(test_repo["dir"], ".{}".format(test_repo["name"]))
)
2016-11-18 14:42:54 +02:00
assert dotdir
2018-08-30 09:18:49 -05:00
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
2018-08-30 09:18:49 -05:00
temp_dir = tempfile.mkdtemp()
2018-09-13 14:03:35 -04:00
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):
2018-09-13 14:03:35 -04:00
xonsh_builtins.__xonsh__.env = Env(VC_BRANCH_TIMEOUT=2)
2016-11-14 21:09:25 +02:00
# get corresponding function from vc module
2018-08-30 09:18:49 -05:00
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:
2018-08-30 09:18:49 -05:00
assert obs == VC_BRANCH[test_repo["name"]]
def test_current_branch_calls_locate_binary_for_empty_cmds_cache(xonsh_builtins):
2018-09-13 14:03:35 -04:00
cache = xonsh_builtins.__xonsh__.commands_cache
xonsh_builtins.__xonsh__.env = DummyEnv(VC_BRANCH_TIMEOUT=1)
cache.is_empty = Mock(return_value=True)
2018-08-30 09:18:49 -05:00
cache.locate_binary = Mock(return_value="")
vc.current_branch()
assert cache.locate_binary.called
2016-11-18 14:42:54 +02:00
2018-08-30 09:18:49 -05:00
def test_current_branch_does_not_call_locate_binary_for_non_empty_cmds_cache(
xonsh_builtins
):
2018-09-13 14:03:35 -04:00
cache = xonsh_builtins.__xonsh__.commands_cache
xonsh_builtins.__xonsh__.env = DummyEnv(VC_BRANCH_TIMEOUT=1)
cache.is_empty = Mock(return_value=False)
2018-08-30 09:18:49 -05:00
cache.locate_binary = Mock(return_value="")
# make lazy locate return nothing to avoid running vc binaries
2018-08-30 09:18:49 -05:00
cache.lazy_locate_binary = Mock(return_value="")
vc.current_branch()
assert not cache.locate_binary.called