xonsh/tests/test_aliases.py

137 lines
4.6 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2015-03-22 02:46:33 +01:00
"""Testing built_ins.Aliases"""
from __future__ import unicode_literals, print_function
2015-03-25 18:32:10 -05:00
2015-12-19 17:29:48 -05:00
import os
import tempfile
2016-06-22 22:45:42 +03:00
import pytest
2015-03-25 18:32:10 -05:00
2015-12-19 17:29:48 -05:00
import xonsh.built_ins as built_ins
2016-04-10 12:13:36 -04:00
from xonsh.aliases import Aliases
from xonsh.aliases import _which
2015-12-19 17:29:48 -05:00
from xonsh.environ import Env
2015-12-19 18:44:58 -05:00
from xonsh.tools import ON_WINDOWS
2015-03-22 02:46:33 +01:00
2015-12-19 18:09:06 -05:00
from tools import mock_xonsh_env
2015-03-22 02:46:33 +01:00
def cd(args, stdin=None):
return args
ALIASES = Aliases({'o': ['omg', 'lala']},
color_ls=['ls', '--color=true'],
ls="ls '- -'",
cd=cd,
indirect_cd='cd ..')
RAW = ALIASES._raw
def test_imports():
2016-06-22 22:45:42 +03:00
expected = {
2015-03-22 02:46:33 +01:00
'o': ['omg', 'lala'],
'ls': ['ls', '- -'],
'color_ls': ['ls', '--color=true'],
'cd': cd,
'indirect_cd': ['cd', '..']
2016-06-22 22:45:42 +03:00
}
assert RAW == expected
2015-03-22 02:46:33 +01:00
def test_eval_normal():
2016-04-10 12:13:36 -04:00
with mock_xonsh_env({}):
2016-06-22 22:45:42 +03:00
assert ALIASES.get('o') == ['omg', 'lala']
2015-07-29 23:58:25 +02:00
2015-03-22 02:46:33 +01:00
def test_eval_self_reference():
2016-04-10 12:13:36 -04:00
with mock_xonsh_env({}):
2016-06-22 22:45:42 +03:00
assert ALIASES.get('ls') == ['ls', '- -']
2015-07-29 23:58:25 +02:00
2015-03-22 02:46:33 +01:00
def test_eval_recursive():
2016-04-10 12:13:36 -04:00
with mock_xonsh_env({}):
2016-06-22 22:45:42 +03:00
assert ALIASES.get('color_ls') == ['ls', '- -', '--color=true']
2015-03-22 02:46:33 +01:00
2016-06-22 22:45:42 +03:00
@pytest.mark.skipif(ON_WINDOWS, reason='Unix stuff')
2015-03-22 02:46:33 +01:00
def test_eval_recursive_callable_partial():
2016-06-28 20:22:11 -04:00
env = Env(HOME=os.path.expanduser('~'))
with mock_xonsh_env(env):
2016-06-22 22:45:42 +03:00
assert ALIASES.get('indirect_cd')(['arg2', 'arg3']) == ['..', 'arg2', 'arg3']
class TestWhich:
2016-06-21 11:43:08 -04:00
# Tests for the _whichgen function which is the only thing we
# use from the _which.py module.
def setup(self):
# Setup two folders with some test files.
self.testdirs = [tempfile.TemporaryDirectory(),
tempfile.TemporaryDirectory()]
if ON_WINDOWS:
self.testapps = ['whichtestapp1.exe',
'whichtestapp2.wta']
2016-06-21 11:43:08 -04:00
self.exts = ['.EXE']
else:
self.testapps = ['whichtestapp1']
2016-06-21 11:43:08 -04:00
self.exts = None
for app in self.testapps:
for d in self.testdirs:
path = os.path.join(d.name, app)
open(path, 'wb').write(b'')
os.chmod(path, 0o755)
def teardown_module(self):
for d in self.testdirs:
d.cleanup()
def test_whichgen(self):
testdir = self.testdirs[0].name
arg = 'whichtestapp1'
2016-06-21 11:43:08 -04:00
matches = list(_which.whichgen(arg, path=[testdir], exts=self.exts))
assert len(matches) == 1
assert self._file_match(matches[0][0], os.path.join(testdir, arg))
def test_whichgen_failure(self):
testdir = self.testdirs[0].name
arg = 'not_a_file'
2016-06-21 11:43:08 -04:00
matches = list(_which.whichgen(arg, path=[testdir], exts=self.exts))
assert len(matches) == 0
def test_whichgen_verbose(self):
testdir = self.testdirs[0].name
arg = 'whichtestapp1'
2016-06-21 11:43:08 -04:00
matches = list(_which.whichgen(arg, path=[testdir], exts=self.exts,
verbose=True))
assert len(matches) == 1
match, from_where = matches[0]
assert self._file_match(match, os.path.join(testdir, arg))
assert from_where == 'from given path element 0'
def test_whichgen_multiple(self):
testdir0 = self.testdirs[0].name
testdir1 = self.testdirs[1].name
arg = 'whichtestapp1'
2016-06-21 11:43:08 -04:00
matches = list(_which.whichgen(arg, path=[testdir0, testdir1],
exts=self.exts))
assert len(matches) == 2
assert self._file_match(matches[0][0], os.path.join(testdir0, arg))
assert self._file_match(matches[1][0], os.path.join(testdir1, arg))
if ON_WINDOWS:
def test_whichgen_ext_failure(self):
testdir = self.testdirs[0].name
arg = 'whichtestapp2'
2016-06-21 11:50:04 -04:00
matches = list(_which.whichgen(arg, path=[testdir], exts=self.exts))
assert len(matches) == 0
def test_whichgen_ext_success(self):
testdir = self.testdirs[0].name
arg = 'whichtestapp2'
2016-06-21 11:43:08 -04:00
matches = list(_which.whichgen(arg, path=[testdir], exts=['.wta']))
assert len(matches) == 1
assert self._file_match(matches[0][0], os.path.join(testdir, arg))
def _file_match(self, path1, path2):
if ON_WINDOWS:
path1 = os.path.normpath(os.path.normcase(path1))
path2 = os.path.normpath(os.path.normcase(path2))
path1 = os.path.splitext(path1)[0]
path2 = os.path.splitext(path2)[0]
return path1 == path2
else:
return os.path.samefile(path1, path2)