xonsh/tests/test_builtins.py

232 lines
5.7 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2015-05-14 18:18:46 -05:00
"""Tests the xonsh builtins."""
2015-02-19 23:58:25 -06:00
from __future__ import unicode_literals, print_function
import os
import re
2016-08-20 15:39:02 -04:00
import builtins
import types
from ast import AST
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
2015-07-29 23:58:25 +02:00
from xonsh import built_ins
2016-06-11 22:38:37 -04:00
from xonsh.built_ins import reglob, pathsearch, helper, superhelper, \
2016-06-16 00:55:52 -04:00
ensure_list_of_strs, list_of_strs_or_callables, regexsearch, \
2016-08-20 15:50:36 -04:00
globsearch, convert_macro_arg, macro_context, call_macro
2015-05-14 18:18:46 -05:00
from xonsh.environ import Env
2015-02-19 23:58:25 -06:00
2016-06-25 13:06:59 +03:00
from tools import skip_if_on_windows
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
@pytest.mark.parametrize('testfile', reglob('test_.*'))
2016-06-29 18:32:42 +03:00
def test_reglob_tests(testfile):
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(xonsh_builtins):
2016-07-03 12:00:24 +03:00
"""Set `__xonsh_env__ ` to a new Env instance on `xonsh_builtins`"""
2016-06-29 18:32:42 +03:00
xonsh_builtins.__xonsh_env__ = Env(HOME=HOME_PATH)
return xonsh_builtins
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)
exp = {p for p in exp if re.match(r'\w\w.*', p)}
exp = {os.path.join(HOME_PATH, p) for p in exp}
obs = set(pathsearch(regexsearch, r'~/\w\w.*'))
assert exp == obs
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
2016-06-25 13:06:59 +03:00
obs = pathsearch(regexsearch, '~')
assert 1 == len(obs)
assert exp == obs[0]
@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}
obs = set(pathsearch(regexsearch, '~/.*'))
assert exp == obs
@skip_if_on_windows
2016-06-29 18:32:42 +03:00
def test_repath_HOME_PATH_var(home_env):
exp = HOME_PATH
2016-06-25 13:06:59 +03:00
obs = pathsearch(regexsearch, '$HOME')
assert 1 == len(obs)
assert exp == obs[0]
@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"}')
assert 1 == len(obs)
assert exp == obs[0]
2016-06-29 18:32:42 +03:00
def test_helper_int(home_env):
2016-06-25 13:06:59 +03:00
helper(int, 'int')
2016-06-29 18:32:42 +03:00
def test_helper_helper(home_env):
2016-06-25 13:06:59 +03:00
helper(helper, 'helper')
2016-06-29 18:32:42 +03:00
def test_helper_env(home_env):
2016-06-25 13:06:59 +03:00
helper(Env, 'Env')
2016-06-29 18:32:42 +03:00
def test_superhelper_int(home_env):
2016-06-25 13:06:59 +03:00
superhelper(int, 'int')
2016-06-29 18:32:42 +03:00
def test_superhelper_helper(home_env):
2016-06-25 13:06:59 +03:00
superhelper(helper, 'helper')
2016-06-29 18:32:42 +03:00
def test_superhelper_env(home_env):
2016-06-25 13:06:59 +03:00
superhelper(Env, 'Env')
2016-06-29 18:32:42 +03:00
@pytest.mark.parametrize('exp, inp', [
(['yo'], 'yo'),
(['yo'], ['yo']),
(['42'], 42),
(['42'], [42])
])
def test_ensure_list_of_strs(exp, inp):
obs = ensure_list_of_strs(inp)
assert exp == obs
f = lambda x: 20
@pytest.mark.parametrize('exp, inp', [
(['yo'], 'yo'),
(['yo'], ['yo']),
(['42'], 42),
(['42'], [42]),
([f], f),
([f], [f])
])
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
@pytest.mark.parametrize('kind', [str, 's', 'S', 'str', 'string'])
def test_convert_macro_arg_str(kind):
raw_arg = 'value'
arg = convert_macro_arg(raw_arg, kind, None, None)
assert arg is raw_arg
@pytest.mark.parametrize('kind', [AST, 'a', 'Ast'])
def test_convert_macro_arg_ast(kind):
raw_arg = '42'
arg = convert_macro_arg(raw_arg, kind, {}, None)
assert isinstance(arg, AST)
@pytest.mark.parametrize('kind', [types.CodeType, compile, 'c', 'code',
'compile'])
def test_convert_macro_arg_code(kind):
raw_arg = '42'
arg = convert_macro_arg(raw_arg, kind, {}, None)
assert isinstance(arg, types.CodeType)
@pytest.mark.parametrize('kind', [eval, 'v', 'eval'])
def test_convert_macro_arg_eval(kind):
# literals
raw_arg = '42'
arg = convert_macro_arg(raw_arg, kind, {}, None)
assert arg == 42
# exprs
raw_arg = 'x + 41'
arg = convert_macro_arg(raw_arg, kind, {}, {'x': 1})
assert arg == 42
@pytest.mark.parametrize('kind', [exec, 'x', 'exec'])
def test_convert_macro_arg_eval(kind):
# at global scope
raw_arg = 'def f(x, y):\n return x + y'
glbs = {}
arg = convert_macro_arg(raw_arg, kind, glbs, None)
assert arg is None
assert 'f' in glbs
assert glbs['f'](1, 41) == 42
# at local scope
raw_arg = 'def g(z):\n return x + z\ny += 42'
glbs = {'x': 40}
locs = {'y': 1}
arg = convert_macro_arg(raw_arg, kind, glbs, locs)
assert arg is None
assert 'g' in locs
assert locs['g'](1) == 41
assert 'y' in locs
assert locs['y'] == 43
2016-08-20 15:50:36 -04:00
def test_macro_context():
def f():
pass
with macro_context(f, True, True):
assert f.macro_globals
assert f.macro_locals
assert not hasattr(f, 'macro_globals')
assert not hasattr(f, 'macro_locals')
@pytest.mark.parametrize('arg', ['x', '42', 'x + y'])
def test_call_macro_str(arg):
def f(x : str):
return x
rtn = call_macro(f, [arg], None, None)
assert rtn is arg
2016-08-20 16:00:30 -04:00
@pytest.mark.parametrize('arg', ['x', '42', 'x + y'])
def test_call_macro_ast(arg):
def f(x : AST):
return x
rtn = call_macro(f, [arg], {}, None)
assert isinstance(rtn, AST)
2016-08-20 15:50:36 -04:00
@pytest.mark.parametrize('arg', ['x', '42', 'x + y'])
def test_call_macro_code(arg):
def f(x : compile):
return x
rtn = call_macro(f, [arg], {}, None)
assert isinstance(rtn, types.CodeType)
2016-08-20 16:00:30 -04:00
@pytest.mark.parametrize('arg', ['x', '42', 'x + y'])
def test_call_macro_eval(arg):
def f(x : eval):
return x
rtn = call_macro(f, [arg], {'x': 42, 'y': 0}, None)
assert rtn == 42
@pytest.mark.parametrize('arg', ['if y:\n pass',
'if 42:\n pass',
'if x + y:\n pass'])
def test_call_macro_exec(arg):
def f(x : exec):
return x
rtn = call_macro(f, [arg], {'x': 42, 'y': 0}, None)
assert rtn is None