xonsh/tests/test_builtins.py

109 lines
3.1 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
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, \
globsearch
2015-05-14 18:18:46 -05:00
from xonsh.environ import Env
from xonsh.tools import ON_WINDOWS
2015-02-19 23:58:25 -06:00
2015-12-19 18:09:06 -05:00
from tools import mock_xonsh_env
2015-02-22 23:46:18 -06:00
def test_reglob_tests():
testfiles = reglob('test_.*')
for f in testfiles:
2016-06-22 18:23:36 -04:00
assert (f.startswith('test_'))
2015-02-22 23:46:18 -06:00
2016-06-22 22:50:01 +03:00
@pytest.mark.skipif(ON_WINDOWS, reason='Unix stuff')
2015-08-02 13:36:44 -05:00
def test_repath_backslash():
home = os.path.expanduser('~')
2015-12-19 17:29:48 -05:00
built_ins.ENV = Env(HOME=home)
2015-12-19 18:09:06 -05:00
with mock_xonsh_env(built_ins.ENV):
exp = os.listdir(home)
exp = {p for p in exp if re.match(r'\w\w.*', p)}
exp = {os.path.join(home, p) for p in exp}
2016-06-16 00:55:52 -04:00
obs = set(pathsearch(regexsearch, r'~/\w\w.*'))
2016-06-22 22:50:01 +03:00
assert exp == obs
2015-07-29 23:58:25 +02:00
2016-06-22 22:50:01 +03:00
@pytest.mark.skipif(ON_WINDOWS, reason='Unix stuff')
2015-02-22 23:46:18 -06:00
def test_repath_home_itself():
exp = os.path.expanduser('~')
2015-12-19 17:29:48 -05:00
built_ins.ENV = Env(HOME=exp)
2015-12-19 18:09:06 -05:00
with mock_xonsh_env(built_ins.ENV):
2016-06-16 00:55:52 -04:00
obs = pathsearch(regexsearch, '~')
2016-06-22 22:50:01 +03:00
assert 1 == len(obs)
assert exp == obs[0]
2015-02-22 23:46:18 -06:00
2016-06-22 22:50:01 +03:00
@pytest.mark.skipif(ON_WINDOWS, reason='Unix stuff')
2015-02-22 23:46:18 -06:00
def test_repath_home_contents():
home = os.path.expanduser('~')
2015-12-19 17:29:48 -05:00
built_ins.ENV = Env(HOME=home)
2015-12-19 18:09:06 -05:00
with mock_xonsh_env(built_ins.ENV):
exp = os.listdir(home)
exp = {os.path.join(home, p) for p in exp}
2016-06-16 00:55:52 -04:00
obs = set(pathsearch(regexsearch, '~/.*'))
2016-06-22 22:50:01 +03:00
assert exp == obs
2015-02-22 23:46:18 -06:00
2016-06-22 22:50:01 +03:00
@pytest.mark.skipif(ON_WINDOWS, reason='Unix stuff')
2015-02-22 23:46:18 -06:00
def test_repath_home_var():
exp = os.path.expanduser('~')
built_ins.ENV = Env(HOME=exp)
2015-12-19 18:09:06 -05:00
with mock_xonsh_env(built_ins.ENV):
2016-06-16 00:55:52 -04:00
obs = pathsearch(regexsearch, '$HOME')
2016-06-22 22:50:01 +03:00
assert 1 == len(obs)
assert exp == obs[0]
2015-02-22 23:46:18 -06:00
2016-06-22 22:50:01 +03:00
@pytest.mark.skipif(ON_WINDOWS, reason='Unix stuff')
2015-02-22 23:46:18 -06:00
def test_repath_home_var_brace():
exp = os.path.expanduser('~')
built_ins.ENV = Env(HOME=exp)
2015-12-19 18:09:06 -05:00
with mock_xonsh_env(built_ins.ENV):
2016-06-16 00:55:52 -04:00
obs = pathsearch(regexsearch, '${"HOME"}')
2016-06-22 22:50:01 +03:00
assert 1 == len(obs)
assert exp == obs[0]
2015-02-22 23:46:18 -06:00
def test_helper_int():
2016-02-18 00:45:01 -05:00
with mock_xonsh_env({}):
helper(int, 'int')
def test_helper_helper():
2016-02-18 00:45:01 -05:00
with mock_xonsh_env({}):
helper(helper, 'helper')
2015-02-23 22:21:46 -06:00
def test_helper_env():
2016-02-18 00:45:01 -05:00
with mock_xonsh_env({}):
helper(Env, 'Env')
2015-02-23 22:21:46 -06:00
def test_superhelper_int():
2016-02-18 00:45:01 -05:00
with mock_xonsh_env({}):
superhelper(int, 'int')
def test_superhelper_helper():
2016-02-18 00:45:01 -05:00
with mock_xonsh_env({}):
superhelper(helper, 'helper')
2015-02-23 22:21:46 -06:00
def test_superhelper_env():
2016-02-18 00:45:01 -05:00
with mock_xonsh_env({}):
superhelper(Env, 'Env')
2015-03-28 17:55:48 -05:00
def test_ensure_list_of_strs():
cases = [(['yo'], 'yo'), (['yo'], ['yo']), (['42'], 42), (['42'], [42])]
for exp, inp in cases:
obs = ensure_list_of_strs(inp)
2016-06-22 22:50:01 +03:00
assert exp == obs
2015-02-19 23:58:25 -06:00
2016-06-11 01:40:26 -04:00
def test_list_of_strs_or_callables():
f = lambda x: 20
cases = [(['yo'], 'yo'), (['yo'], ['yo']), (['42'], 42), (['42'], [42]),
([f], f), ([f], [f])]
for exp, inp in cases:
obs = list_of_strs_or_callables(inp)
2016-06-22 22:50:01 +03:00
assert exp == obs