xonsh/tests/test_builtins.py

422 lines
9.5 KiB
Python
Raw Permalink Normal View History

2015-05-14 18:18:46 -05:00
"""Tests the xonsh builtins."""
2015-02-19 23:58:25 -06:00
import os
import re
import shutil
2016-08-20 15:39:02 -04:00
import types
2022-01-31 21:26:34 +05:30
from ast import AST, Expression, Interactive, Module
from pathlib import Path
2015-02-19 23:58:25 -06:00
2016-06-22 22:50:01 +03:00
import pytest
2015-02-19 23:58:25 -06:00
2018-08-30 09:18:49 -05:00
from xonsh.built_ins import (
2022-01-31 21:26:34 +05:30
call_macro,
convert_macro_arg,
2018-08-30 09:18:49 -05:00
ensure_list_of_strs,
2022-01-31 21:26:34 +05:30
enter_macro,
2018-08-30 09:18:49 -05:00
expand_path,
2022-01-31 21:26:34 +05:30
helper,
2018-08-30 09:18:49 -05:00
in_macro_call,
2022-01-31 21:26:34 +05:30
list_of_list_of_strs_outer_product,
list_of_strs_or_callables,
pathsearch,
regexsearch,
reglob,
superhelper,
2018-08-30 09:18:49 -05:00
)
2015-05-14 18:18:46 -05:00
from xonsh.environ import Env
2022-03-24 00:46:50 +05:30
from xonsh.pytest.tools import skip_if_on_windows
2015-02-19 23:58:25 -06:00
2018-08-30 09:18:49 -05:00
HOME_PATH = os.path.expanduser("~")
2015-12-19 18:09:06 -05:00
2016-08-20 15:39:02 -04:00
@pytest.fixture(autouse=True)
def xonsh_execer_autouse(xonsh_execer):
return xonsh_execer
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize("testfile", reglob("test_.*"))
2016-06-29 18:32:42 +03:00
def test_reglob_tests(testfile):
2018-08-30 09:18:49 -05:00
assert testfile.startswith("test_")
2015-02-22 23:46:18 -06:00
2016-06-29 18:32:42 +03:00
@pytest.fixture
def home_env(xession):
2018-09-13 14:03:35 -04:00
"""Set `__xonsh__.env ` to a new Env instance on `xonsh_builtins`"""
update test xsh usage (#4581) * todo * test: remove usage of DummyEnv and setting .env attribute on xession fixture one step closer to making too much of tweaking to xession during tests * test: fix tests vox and gitstatus-prompt * docs: update test-fixture usage * fix: import flake8 error * test: remove direct access to XSH in tests * test: remove usage of XSH in test files * todo * test: use tmp-dir to create stubs * refactor: use fixture factory to mock out XonshSession * refactor: remove direct access of XSH from functions * refactor: remove direct access of XSH from functions * fix: qa checks * refactor: rename variables to match their values * test: update failing tests because it had no PATH set previously * fix: remove builtins usage from pyghooks.py * style: * refactor: update tests to use fixtures * fix: env varialbe is setup per function some tests accidentally update the env variables and that is leaking into next tests * fix: failing vox tests * test: set commands_cache per test * test: fix failing tests * fix: failing tests on linux ptk-highlight * fix: failing tests on Windows cwd-prompt * test: copy env as to not affect original object * fix: lazily evaluate cmds-cache in pyghooks * test: fix failing tests * fix: qa errors import * test: set commands-cache per test while caching path results * test: speedup test_thread_local_swap * fix: failing tests on windows * refactor: Execer doesn't control session * refactor: XSH.unload will take care of reversing builtins attrs set * test: use env.update over monkeypatch * Revert "test: use env.update over monkeypatch" This reverts commit 010a5022247a098f1741966b8af1bf758663480e.
2022-01-08 04:03:22 +05:30
xession.env["HOME"] = HOME_PATH
return xession
2016-06-25 13:06:59 +03:00
@skip_if_on_windows
2016-06-29 18:32:42 +03:00
def test_repath_backslash(home_env):
2016-06-25 13:06:59 +03:00
exp = os.listdir(HOME_PATH)
2018-08-30 09:18:49 -05:00
exp = {p for p in exp if re.match(r"\w\w.*", p)}
2016-06-25 13:06:59 +03:00
exp = {os.path.join(HOME_PATH, p) for p in exp}
2018-08-30 09:18:49 -05:00
obs = set(pathsearch(regexsearch, r"~/\w\w.*"))
assert exp == obs
2016-06-25 13:06:59 +03:00
2016-06-29 18:32:42 +03:00
2016-06-25 13:06:59 +03:00
@skip_if_on_windows
2016-06-29 18:32:42 +03:00
def test_repath_HOME_PATH_itself(home_env):
exp = HOME_PATH
2018-08-30 09:18:49 -05:00
obs = pathsearch(regexsearch, "~")
assert 1 == len(obs)
assert exp == obs[0]
2016-06-25 13:06:59 +03:00
@skip_if_on_windows
2016-06-29 18:32:42 +03:00
def test_repath_HOME_PATH_contents(home_env):
2016-06-25 13:06:59 +03:00
exp = os.listdir(HOME_PATH)
exp = {os.path.join(HOME_PATH, p) for p in exp}
2018-08-30 09:18:49 -05:00
obs = set(pathsearch(regexsearch, "~/.*"))
assert exp == obs
2016-06-25 13:06:59 +03:00
@skip_if_on_windows
2016-06-29 18:32:42 +03:00
def test_repath_HOME_PATH_var(home_env):
exp = HOME_PATH
2018-08-30 09:18:49 -05:00
obs = pathsearch(regexsearch, "$HOME")
assert 1 == len(obs)
assert exp == obs[0]
2016-06-25 13:06:59 +03:00
@skip_if_on_windows
2016-06-29 18:32:42 +03:00
def test_repath_HOME_PATH_var_brace(home_env):
exp = HOME_PATH
2016-06-25 13:06:59 +03:00
obs = pathsearch(regexsearch, '${"HOME"}')
2018-08-30 09:18:49 -05:00
assert 1 == len(obs)
assert exp == obs[0]
2016-06-25 13:06:59 +03:00
# helper
def check_repath(path, pattern):
base_testdir = Path("re_testdir")
testdir = base_testdir / path
testdir.mkdir(parents=True)
try:
obs = regexsearch(str(base_testdir / pattern))
assert [str(testdir)] == obs
finally:
shutil.rmtree(base_testdir)
@skip_if_on_windows
@pytest.mark.parametrize(
"path, pattern",
[
("test*1/model", ".*/model"),
("hello/test*1/model", "hello/.*/model"),
],
)
def test_repath_containing_asterisk(path, pattern):
check_repath(path, pattern)
@pytest.mark.parametrize(
"path, pattern",
[
("test+a/model", ".*/model"),
("hello/test+1/model", "hello/.*/model"),
],
)
def test_repath_containing_plus_sign(path, pattern):
check_repath(path, pattern)
2016-06-29 18:32:42 +03:00
def test_helper_int(home_env):
2018-08-30 09:18:49 -05:00
helper(int, "int")
2016-06-25 13:06:59 +03:00
2016-06-29 18:32:42 +03:00
def test_helper_helper(home_env):
2018-08-30 09:18:49 -05:00
helper(helper, "helper")
2016-06-25 13:06:59 +03:00
2016-06-29 18:32:42 +03:00
def test_helper_env(home_env):
2018-08-30 09:18:49 -05:00
helper(Env, "Env")
2016-06-25 13:06:59 +03:00
2016-06-29 18:32:42 +03:00
def test_superhelper_int(home_env):
2018-08-30 09:18:49 -05:00
superhelper(int, "int")
2016-06-25 13:06:59 +03:00
2016-06-29 18:32:42 +03:00
def test_superhelper_helper(home_env):
2018-08-30 09:18:49 -05:00
superhelper(helper, "helper")
2016-06-25 13:06:59 +03:00
2016-06-29 18:32:42 +03:00
def test_superhelper_env(home_env):
2018-08-30 09:18:49 -05:00
superhelper(Env, "Env")
2016-06-29 18:32:42 +03:00
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize(
"exp, inp", [(["yo"], "yo"), (["yo"], ["yo"]), (["42"], 42), (["42"], [42])]
)
2016-06-29 18:32:42 +03:00
def test_ensure_list_of_strs(exp, inp):
obs = ensure_list_of_strs(inp)
assert exp == obs
f = lambda x: 20
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize(
"exp, inp",
[
(["yo"], "yo"),
(["yo"], ["yo"]),
(["42"], 42),
(["42"], [42]),
([f], f),
([f], [f]),
],
)
2016-06-29 18:32:42 +03:00
def test_list_of_strs_or_callables(exp, inp):
obs = list_of_strs_or_callables(inp)
assert exp == obs
2016-08-20 15:39:02 -04:00
2018-09-17 18:23:20 -04:00
@pytest.mark.parametrize(
"inp, exp",
[
(["x", ["y", "z"]], ["xy", "xz"]),
(["x", ["y", "z"], ["a"]], ["xya", "xza"]),
([["y", "z"], ["a", "b"]], ["ya", "yb", "za", "zb"]),
],
)
update test xsh usage (#4581) * todo * test: remove usage of DummyEnv and setting .env attribute on xession fixture one step closer to making too much of tweaking to xession during tests * test: fix tests vox and gitstatus-prompt * docs: update test-fixture usage * fix: import flake8 error * test: remove direct access to XSH in tests * test: remove usage of XSH in test files * todo * test: use tmp-dir to create stubs * refactor: use fixture factory to mock out XonshSession * refactor: remove direct access of XSH from functions * refactor: remove direct access of XSH from functions * fix: qa checks * refactor: rename variables to match their values * test: update failing tests because it had no PATH set previously * fix: remove builtins usage from pyghooks.py * style: * refactor: update tests to use fixtures * fix: env varialbe is setup per function some tests accidentally update the env variables and that is leaking into next tests * fix: failing vox tests * test: set commands_cache per test * test: fix failing tests * fix: failing tests on linux ptk-highlight * fix: failing tests on Windows cwd-prompt * test: copy env as to not affect original object * fix: lazily evaluate cmds-cache in pyghooks * test: fix failing tests * fix: qa errors import * test: set commands-cache per test while caching path results * test: speedup test_thread_local_swap * fix: failing tests on windows * refactor: Execer doesn't control session * refactor: XSH.unload will take care of reversing builtins attrs set * test: use env.update over monkeypatch * Revert "test: use env.update over monkeypatch" This reverts commit 010a5022247a098f1741966b8af1bf758663480e.
2022-01-08 04:03:22 +05:30
def test_list_of_list_of_strs_outer_product(xession, inp, exp):
2018-09-17 18:23:20 -04:00
obs = list_of_list_of_strs_outer_product(inp)
assert exp == obs
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize(
"s",
[
"~",
"~/",
"x=~/place",
"x=one:~/place",
"x=one:~/place:~/yo",
"x=~/one:~/place:~/yo",
],
)
2016-08-25 01:04:08 -04:00
def test_expand_path(s, home_env):
2018-08-30 09:18:49 -05:00
if os.sep != "/":
s = s.replace("/", os.sep)
if os.pathsep != ":":
s = s.replace(":", os.pathsep)
assert expand_path(s) == s.replace("~", HOME_PATH)
2016-08-25 01:04:08 -04:00
2016-09-09 19:21:26 -04:00
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize("kind", [str, "s", "S", "str", "string"])
2016-08-20 15:39:02 -04:00
def test_convert_macro_arg_str(kind):
2018-08-30 09:18:49 -05:00
raw_arg = "value"
2016-08-20 15:39:02 -04:00
arg = convert_macro_arg(raw_arg, kind, None, None)
assert arg is raw_arg
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize("kind", [AST, "a", "Ast"])
2016-08-20 15:39:02 -04:00
def test_convert_macro_arg_ast(kind):
2018-08-30 09:18:49 -05:00
raw_arg = "42"
2016-08-20 15:39:02 -04:00
arg = convert_macro_arg(raw_arg, kind, {}, None)
assert isinstance(arg, AST)
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize("kind", [types.CodeType, compile, "c", "code", "compile"])
2016-08-20 15:39:02 -04:00
def test_convert_macro_arg_code(kind):
2018-08-30 09:18:49 -05:00
raw_arg = "42"
2016-08-20 15:39:02 -04:00
arg = convert_macro_arg(raw_arg, kind, {}, None)
assert isinstance(arg, types.CodeType)
@pytest.mark.parametrize("kind", [eval, "v", "eval"])
2016-08-20 15:39:02 -04:00
def test_convert_macro_arg_eval(kind):
# literals
2018-08-30 09:18:49 -05:00
raw_arg = "42"
2016-08-20 15:39:02 -04:00
arg = convert_macro_arg(raw_arg, kind, {}, None)
assert arg == 42
# exprs
2018-08-30 09:18:49 -05:00
raw_arg = "x + 41"
arg = convert_macro_arg(raw_arg, kind, {}, {"x": 1})
2016-08-20 15:39:02 -04:00
assert arg == 42
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize("kind", [exec, "x", "exec"])
2016-08-20 21:11:07 -04:00
def test_convert_macro_arg_exec(kind):
2016-08-20 15:39:02 -04:00
# at global scope
2018-08-30 09:18:49 -05:00
raw_arg = "def f(x, y):\n return x + y"
2016-08-20 15:39:02 -04:00
glbs = {}
arg = convert_macro_arg(raw_arg, kind, glbs, None)
assert arg is None
2018-08-30 09:18:49 -05:00
assert "f" in glbs
assert glbs["f"](1, 41) == 42
2016-08-20 15:39:02 -04:00
# at local scope
2018-08-30 09:18:49 -05:00
raw_arg = "def g(z):\n return x + z\ny += 42"
glbs = {"x": 40}
locs = {"y": 1}
2016-08-20 15:39:02 -04:00
arg = convert_macro_arg(raw_arg, kind, glbs, locs)
assert arg is None
2018-08-30 09:18:49 -05:00
assert "g" in locs
assert locs["g"](1) == 41
assert "y" in locs
assert locs["y"] == 43
2016-08-20 15:39:02 -04:00
2016-08-20 15:50:36 -04:00
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize("kind", [type, "t", "type"])
def test_convert_macro_arg_type(kind):
2016-08-20 21:11:07 -04:00
# literals
2018-08-30 09:18:49 -05:00
raw_arg = "42"
2016-08-20 21:11:07 -04:00
arg = convert_macro_arg(raw_arg, kind, {}, None)
assert arg is int
# exprs
2018-08-30 09:18:49 -05:00
raw_arg = "x + 41"
arg = convert_macro_arg(raw_arg, kind, {}, {"x": 1})
2016-08-20 21:11:07 -04:00
assert arg is int
2016-08-28 13:46:05 -04:00
def test_in_macro_call():
2016-08-20 15:50:36 -04:00
def f():
pass
2018-08-30 09:18:49 -05:00
2016-08-28 13:46:05 -04:00
with in_macro_call(f, True, True):
2016-08-20 15:50:36 -04:00
assert f.macro_globals
assert f.macro_locals
2018-08-30 09:18:49 -05:00
assert not hasattr(f, "macro_globals")
assert not hasattr(f, "macro_locals")
2016-08-20 15:50:36 -04:00
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize("arg", ["x", "42", "x + y"])
2016-08-20 15:50:36 -04:00
def test_call_macro_str(arg):
2018-08-30 09:18:49 -05:00
def f(x: str):
2016-08-20 15:50:36 -04:00
return x
2018-08-30 09:18:49 -05:00
2016-08-20 15:50:36 -04:00
rtn = call_macro(f, [arg], None, None)
assert rtn is arg
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize("arg", ["x", "42", "x + y"])
2016-08-20 16:00:30 -04:00
def test_call_macro_ast(arg):
2018-08-30 09:18:49 -05:00
def f(x: AST):
2016-08-20 16:00:30 -04:00
return x
2018-08-30 09:18:49 -05:00
2016-08-20 16:00:30 -04:00
rtn = call_macro(f, [arg], {}, None)
assert isinstance(rtn, AST)
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize("arg", ["x", "42", "x + y"])
2016-08-20 15:50:36 -04:00
def test_call_macro_code(arg):
2018-08-30 09:18:49 -05:00
def f(x: compile):
2016-08-20 15:50:36 -04:00
return x
2018-08-30 09:18:49 -05:00
2016-08-20 15:50:36 -04:00
rtn = call_macro(f, [arg], {}, None)
assert isinstance(rtn, types.CodeType)
2016-08-20 16:00:30 -04:00
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize("arg", ["x", "42", "x + y"])
2016-08-20 16:00:30 -04:00
def test_call_macro_eval(arg):
2018-08-30 09:18:49 -05:00
def f(x: eval):
2016-08-20 16:00:30 -04:00
return x
2018-08-30 09:18:49 -05:00
rtn = call_macro(f, [arg], {"x": 42, "y": 0}, None)
2016-08-20 16:00:30 -04:00
assert rtn == 42
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize(
"arg", ["if y:\n pass", "if 42:\n pass", "if x + y:\n pass"]
)
2016-08-20 16:00:30 -04:00
def test_call_macro_exec(arg):
2018-08-30 09:18:49 -05:00
def f(x: exec):
2016-08-20 16:00:30 -04:00
return x
2018-08-30 09:18:49 -05:00
rtn = call_macro(f, [arg], {"x": 42, "y": 0}, None)
2016-08-20 16:00:30 -04:00
assert rtn is None
2016-08-27 13:06:08 -04:00
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize("arg", ["x", "42", "x + y"])
2016-08-27 13:06:08 -04:00
def test_call_macro_raw_arg(arg):
2018-08-30 09:18:49 -05:00
def f(x: str):
2016-08-27 13:06:08 -04:00
return x
2018-08-30 09:18:49 -05:00
rtn = call_macro(f, ["*", arg], {"x": 42, "y": 0}, None)
2016-08-27 13:06:08 -04:00
assert rtn == 42
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize("arg", ["x", "42", "x + y"])
2016-08-27 13:06:08 -04:00
def test_call_macro_raw_kwarg(arg):
2018-08-30 09:18:49 -05:00
def f(x: str):
2016-08-27 13:06:08 -04:00
return x
2018-08-30 09:18:49 -05:00
rtn = call_macro(f, ["*", "x=" + arg], {"x": 42, "y": 0}, None)
2016-08-27 13:06:08 -04:00
assert rtn == 42
2016-08-27 13:27:15 -04:00
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize("arg", ["x", "42", "x + y"])
2016-08-27 13:27:15 -04:00
def test_call_macro_raw_kwargs(arg):
2018-08-30 09:18:49 -05:00
def f(x: str):
2016-08-27 13:27:15 -04:00
return x
2018-08-30 09:18:49 -05:00
rtn = call_macro(f, ["*", '**{"x" :' + arg + "}"], {"x": 42, "y": 0}, None)
2016-08-27 13:27:15 -04:00
assert rtn == 42
2016-08-28 23:16:56 -04:00
2019-02-14 16:19:13 -05:00
def test_call_macro_ast_eval_expr():
def f(x: ("ast", "eval")):
return x
rtn = call_macro(f, ["x == 5"], {}, None)
assert isinstance(rtn, Expression)
def test_call_macro_ast_single_expr():
def f(x: ("ast", "single")):
return x
rtn = call_macro(f, ["x == 5"], {}, None)
assert isinstance(rtn, Interactive)
def test_call_macro_ast_exec_expr():
def f(x: ("ast", "exec")):
return x
rtn = call_macro(f, ["x == 5"], {}, None)
assert isinstance(rtn, Module)
def test_call_macro_ast_eval_statement():
def f(x: ("ast", "eval")):
return x
try:
call_macro(f, ["x = 5"], {}, None)
2019-02-14 16:19:13 -05:00
assert False
except SyntaxError:
# It doesn't make sense to pass a statement to
# something that expects to be evaled
assert True
else:
assert False
def test_call_macro_ast_single_statement():
def f(x: ("ast", "single")):
return x
rtn = call_macro(f, ["x = 5"], {}, None)
assert isinstance(rtn, Interactive)
def test_call_macro_ast_exec_statement():
def f(x: ("ast", "exec")):
return x
rtn = call_macro(f, ["x = 5"], {}, None)
assert isinstance(rtn, Module)
2016-08-28 23:16:56 -04:00
def test_enter_macro():
obj = lambda: None
2018-08-30 09:18:49 -05:00
rtn = enter_macro(obj, "wakka", True, True)
2016-08-28 23:16:56 -04:00
assert obj is rtn
2018-08-30 09:18:49 -05:00
assert obj.macro_block == "wakka"
2016-08-28 23:16:56 -04:00
assert obj.macro_globals
assert obj.macro_locals