xonsh/tests/test_tools.py

1140 lines
30 KiB
Python
Raw Normal View History

2016-07-02 04:53:37 +03:00
# -*- coding: utf-8 -*-
2016-06-23 12:43:44 +02:00
"""Tests xonsh tools."""
2016-08-02 15:59:11 +03:00
import builtins
import datetime as dt
import os
import pathlib
2016-05-22 22:33:43 +02:00
import stat
2016-08-02 15:59:11 +03:00
from tempfile import TemporaryDirectory
2015-03-13 21:43:18 -05:00
2016-06-23 12:16:20 +03:00
import pytest
2016-05-28 17:40:54 -04:00
from xonsh.platform import ON_WINDOWS
2015-03-13 21:43:18 -05:00
from xonsh.lexer import Lexer
2016-05-22 22:33:43 +02:00
from xonsh.tools import (
EnvPath, always_false, always_true, argvquote,
bool_or_int_to_str, bool_to_str, check_for_partial_string,
dynamic_cwd_tuple_to_str, ensure_slice, ensure_string,
env_path_to_str, escape_windows_cmd_string, executables_in,
2016-06-23 10:23:23 +02:00
expand_case_matching, find_next_break, iglobpath, is_bool, is_bool_or_int,
2016-07-18 21:41:22 +03:00
is_callable, is_dynamic_cwd_width, is_env_path, is_float, is_int, is_logfile_opt,
is_string_or_callable, logfile_opt_to_str, str_to_env_path, is_string,
subexpr_from_unbalanced, subproc_toks, to_bool, to_bool_or_int,
2016-06-20 16:26:54 -04:00
to_dynamic_cwd_tuple, to_logfile_opt, pathsep_to_set, set_to_pathsep,
2016-06-21 10:56:10 -04:00
is_string_seq, pathsep_to_seq, seq_to_pathsep, is_nonstring_seq_of_strings,
2016-08-01 15:01:17 +03:00
pathsep_to_upper_seq, seq_to_upper_pathsep, expandvars, is_int_as_str, is_slice_as_str,
ensure_timestamp,
2016-06-21 10:56:10 -04:00
)
from xonsh.commands_cache import CommandsCache
from xonsh.built_ins import expand_path
from xonsh.environ import Env
2015-03-13 21:43:18 -05:00
from tools import skip_if_on_windows, skip_if_on_unix
2015-03-13 21:43:18 -05:00
LEXER = Lexer()
LEXER.build()
INDENT = ' '
2016-06-23 20:13:24 +02:00
TOOLS_ENV = {'EXPAND_ENV_VARS': True, 'XONSH_ENCODING_ERRORS':'strict'}
2016-06-23 15:04:28 -04:00
ENCODE_ENV_ONLY = {'XONSH_ENCODING_ERRORS': 'strict'}
PATHEXT_ENV = {'PATHEXT': ['.COM', '.EXE', '.BAT']}
2016-05-22 22:33:43 +02:00
2015-03-13 21:43:18 -05:00
def test_subproc_toks_x():
2016-04-09 00:12:25 -04:00
exp = '![x]'
2015-03-13 21:43:18 -05:00
obs = subproc_toks('x', lexer=LEXER, returnline=True)
assert (exp == obs)
2015-03-13 21:43:18 -05:00
2016-05-22 22:33:43 +02:00
2015-03-13 21:43:18 -05:00
def test_subproc_toks_ls_l():
2016-04-09 00:12:25 -04:00
exp = '![ls -l]'
2015-03-13 21:43:18 -05:00
obs = subproc_toks('ls -l', lexer=LEXER, returnline=True)
assert (exp == obs)
2015-03-13 21:43:18 -05:00
2016-05-22 22:33:43 +02:00
2015-03-13 21:43:18 -05:00
def test_subproc_toks_git():
s = 'git commit -am "hello doc"'
2016-04-09 00:12:25 -04:00
exp = '![{0}]'.format(s)
2015-03-13 21:43:18 -05:00
obs = subproc_toks(s, lexer=LEXER, returnline=True)
assert (exp == obs)
2015-03-13 21:43:18 -05:00
2016-05-22 22:33:43 +02:00
2015-03-13 21:43:18 -05:00
def test_subproc_toks_git_semi():
s = 'git commit -am "hello doc"'
2016-04-09 00:12:25 -04:00
exp = '![{0}];'.format(s)
2015-03-13 21:43:18 -05:00
obs = subproc_toks(s + ';', lexer=LEXER, returnline=True)
assert (exp == obs)
2015-03-13 21:43:18 -05:00
2016-05-22 22:33:43 +02:00
2015-03-13 21:43:18 -05:00
def test_subproc_toks_git_nl():
s = 'git commit -am "hello doc"'
2016-04-09 00:12:25 -04:00
exp = '![{0}]\n'.format(s)
2015-03-13 21:43:18 -05:00
obs = subproc_toks(s + '\n', lexer=LEXER, returnline=True)
assert (exp == obs)
2015-03-13 21:43:18 -05:00
2016-05-22 22:33:43 +02:00
2015-03-13 21:43:18 -05:00
def test_subproc_toks_indent_ls():
s = 'ls -l'
2016-04-09 00:12:25 -04:00
exp = INDENT + '![{0}]'.format(s)
2015-07-29 23:58:25 +02:00
obs = subproc_toks(INDENT + s, mincol=len(INDENT), lexer=LEXER,
returnline=True)
assert (exp == obs)
2016-05-22 22:33:43 +02:00
def test_subproc_toks_indent_ls_nl():
2015-03-13 21:43:18 -05:00
s = 'ls -l'
2016-04-09 00:12:25 -04:00
exp = INDENT + '![{0}]\n'.format(s)
2015-07-29 23:58:25 +02:00
obs = subproc_toks(INDENT + s + '\n', mincol=len(INDENT), lexer=LEXER,
2015-03-13 21:43:18 -05:00
returnline=True)
assert (exp == obs)
2015-03-13 21:43:18 -05:00
2016-05-22 22:33:43 +02:00
2015-03-30 19:27:04 -05:00
def test_subproc_toks_indent_ls_no_min():
s = 'ls -l'
2016-04-09 00:12:25 -04:00
exp = INDENT + '![{0}]'.format(s)
2015-03-30 19:27:04 -05:00
obs = subproc_toks(INDENT + s, lexer=LEXER, returnline=True)
assert (exp == obs)
2015-03-30 19:27:04 -05:00
2016-05-22 22:33:43 +02:00
2015-03-30 19:27:04 -05:00
def test_subproc_toks_indent_ls_no_min_nl():
s = 'ls -l'
2016-04-09 00:12:25 -04:00
exp = INDENT + '![{0}]\n'.format(s)
2015-03-30 19:27:04 -05:00
obs = subproc_toks(INDENT + s + '\n', lexer=LEXER, returnline=True)
assert (exp == obs)
2015-03-30 19:27:04 -05:00
2016-05-22 22:33:43 +02:00
2015-03-31 11:02:36 -05:00
def test_subproc_toks_indent_ls_no_min_semi():
s = 'ls'
2016-04-09 00:12:25 -04:00
exp = INDENT + '![{0}];'.format(s)
2015-03-31 11:02:36 -05:00
obs = subproc_toks(INDENT + s + ';', lexer=LEXER, returnline=True)
assert (exp == obs)
2015-03-31 11:02:36 -05:00
2016-05-22 22:33:43 +02:00
2015-03-31 19:40:35 -05:00
def test_subproc_toks_indent_ls_no_min_semi_nl():
s = 'ls'
2016-04-09 00:12:25 -04:00
exp = INDENT + '![{0}];\n'.format(s)
2015-03-31 19:40:35 -05:00
obs = subproc_toks(INDENT + s + ';\n', lexer=LEXER, returnline=True)
assert (exp == obs)
2015-03-31 19:40:35 -05:00
2016-05-22 22:33:43 +02:00
2015-03-13 21:43:18 -05:00
def test_subproc_toks_ls_comment():
s = 'ls -l'
com = ' # lets list'
2016-04-09 00:12:25 -04:00
exp = '![{0}]{1}'.format(s, com)
2015-03-13 21:43:18 -05:00
obs = subproc_toks(s + com, lexer=LEXER, returnline=True)
assert (exp == obs)
2015-03-13 21:43:18 -05:00
2016-05-22 22:33:43 +02:00
2015-03-13 21:43:18 -05:00
def test_subproc_toks_ls_42_comment():
s = 'ls 42'
com = ' # lets list'
2016-04-09 00:12:25 -04:00
exp = '![{0}]{1}'.format(s, com)
2015-03-13 21:43:18 -05:00
obs = subproc_toks(s + com, lexer=LEXER, returnline=True)
assert (exp == obs)
2015-03-13 21:43:18 -05:00
2016-05-22 22:33:43 +02:00
2015-03-13 21:43:18 -05:00
def test_subproc_toks_ls_str_comment():
s = 'ls "wakka"'
com = ' # lets list'
2016-04-09 00:12:25 -04:00
exp = '![{0}]{1}'.format(s, com)
2015-03-13 21:43:18 -05:00
obs = subproc_toks(s + com, lexer=LEXER, returnline=True)
assert (exp == obs)
2015-03-13 21:43:18 -05:00
2016-05-22 22:33:43 +02:00
def test_subproc_toks_indent_ls_comment():
ind = ' '
s = 'ls -l'
com = ' # lets list'
2016-04-09 00:12:25 -04:00
exp = '{0}![{1}]{2}'.format(ind, s, com)
obs = subproc_toks(ind + s + com, lexer=LEXER, returnline=True)
assert (exp == obs)
2016-05-22 22:33:43 +02:00
def test_subproc_toks_indent_ls_str():
ind = ' '
s = 'ls "wakka"'
com = ' # lets list'
2016-04-09 00:12:25 -04:00
exp = '{0}![{1}]{2}'.format(ind, s, com)
obs = subproc_toks(ind + s + com, lexer=LEXER, returnline=True)
assert (exp == obs)
2016-05-22 22:33:43 +02:00
2015-03-14 01:00:46 -05:00
def test_subproc_toks_ls_l_semi_ls_first():
2015-03-13 21:43:18 -05:00
lsdl = 'ls -l'
ls = 'ls'
s = '{0}; {1}'.format(lsdl, ls)
2016-04-09 00:12:25 -04:00
exp = '![{0}]; {1}'.format(lsdl, ls)
2015-03-14 00:40:32 -05:00
obs = subproc_toks(s, lexer=LEXER, maxcol=6, returnline=True)
assert (exp == obs)
2015-03-13 21:43:18 -05:00
2016-05-22 22:33:43 +02:00
2015-03-13 21:43:18 -05:00
def test_subproc_toks_ls_l_semi_ls_second():
lsdl = 'ls -l'
ls = 'ls'
s = '{0}; {1}'.format(lsdl, ls)
2016-04-09 00:12:25 -04:00
exp = '{0}; ![{1}]'.format(lsdl, ls)
2015-03-13 21:43:18 -05:00
obs = subproc_toks(s, lexer=LEXER, mincol=7, returnline=True)
assert (exp == obs)
2015-03-13 21:43:18 -05:00
2016-05-22 22:33:43 +02:00
2016-05-11 23:24:58 -04:00
def test_subproc_toks_hello_mom_first():
2015-03-14 01:00:46 -05:00
fst = "echo 'hello'"
sec = "echo 'mom'"
s = '{0}; {1}'.format(fst, sec)
2016-04-09 00:12:25 -04:00
exp = '![{0}]; {1}'.format(fst, sec)
2015-03-14 01:00:46 -05:00
obs = subproc_toks(s, lexer=LEXER, maxcol=len(fst)+1, returnline=True)
assert (exp == obs)
2015-03-14 01:00:46 -05:00
2016-05-22 22:33:43 +02:00
2016-05-11 23:24:58 -04:00
def test_subproc_toks_hello_mom_second():
2015-03-14 01:00:46 -05:00
fst = "echo 'hello'"
sec = "echo 'mom'"
s = '{0}; {1}'.format(fst, sec)
2016-04-09 00:12:25 -04:00
exp = '{0}; ![{1}]'.format(fst, sec)
2015-03-14 01:00:46 -05:00
obs = subproc_toks(s, lexer=LEXER, mincol=len(fst), returnline=True)
assert (exp == obs)
2015-03-14 01:00:46 -05:00
2016-05-22 22:33:43 +02:00
2015-03-18 20:14:53 -05:00
def test_subproc_toks_comment():
exp = None
obs = subproc_toks('# I am a comment', lexer=LEXER, returnline=True)
assert (exp == obs)
2015-03-18 20:14:53 -05:00
2016-05-22 22:33:43 +02:00
2016-04-10 23:38:12 -04:00
def test_subproc_toks_not():
exp = 'not ![echo mom]'
obs = subproc_toks('not echo mom', lexer=LEXER, returnline=True)
assert (exp == obs)
2016-04-10 23:38:12 -04:00
2016-05-22 22:33:43 +02:00
2016-04-10 23:38:12 -04:00
def test_subproc_toks_paren():
exp = '(![echo mom])'
obs = subproc_toks('(echo mom)', lexer=LEXER, returnline=True)
assert (exp == obs)
2016-04-10 23:38:12 -04:00
2016-05-22 22:33:43 +02:00
2016-04-10 23:38:12 -04:00
def test_subproc_toks_paren_ws():
exp = '(![echo mom]) '
obs = subproc_toks('(echo mom) ', lexer=LEXER, returnline=True)
assert (exp == obs)
2016-04-10 23:38:12 -04:00
2016-05-22 22:33:43 +02:00
2016-04-10 23:38:12 -04:00
def test_subproc_toks_not_paren():
exp = 'not (![echo mom])'
obs = subproc_toks('not (echo mom)', lexer=LEXER, returnline=True)
assert (exp == obs)
2016-04-10 23:38:12 -04:00
2016-05-22 22:33:43 +02:00
2016-04-10 23:38:12 -04:00
def test_subproc_toks_and_paren():
exp = 'True and (![echo mom])'
obs = subproc_toks('True and (echo mom)', lexer=LEXER, returnline=True)
assert (exp == obs)
2016-04-10 23:38:12 -04:00
2016-05-22 22:33:43 +02:00
2016-04-10 23:38:12 -04:00
def test_subproc_toks_paren_and_paren():
exp = '(![echo a]) and (echo b)'
obs = subproc_toks('(echo a) and (echo b)', maxcol=9, lexer=LEXER, returnline=True)
assert (exp == obs)
2016-04-10 23:38:12 -04:00
2016-05-22 22:33:43 +02:00
def test_subproc_toks_semicolon_only():
exp = None
obs = subproc_toks(';', lexer=LEXER, returnline=True)
assert (exp == obs)
2016-05-22 22:33:43 +02:00
2016-05-11 23:24:58 -04:00
def test_subproc_toks_pyeval():
s = 'echo @(1+1)'
exp = '![{0}]'.format(s)
obs = subproc_toks(s, lexer=LEXER, returnline=True)
assert (exp == obs)
2016-05-11 23:24:58 -04:00
2016-05-22 22:33:43 +02:00
2016-05-11 23:24:58 -04:00
def test_subproc_toks_twopyeval():
s = 'echo @(1+1) @(40 + 2)'
exp = '![{0}]'.format(s)
obs = subproc_toks(s, lexer=LEXER, returnline=True)
assert (exp == obs)
2016-05-11 23:24:58 -04:00
2016-05-22 22:33:43 +02:00
2016-05-11 23:24:58 -04:00
def test_subproc_toks_pyeval_parens():
s = 'echo @(1+1)'
inp = '({0})'.format(s)
exp = '(![{0}])'.format(s)
obs = subproc_toks(inp, lexer=LEXER, returnline=True)
assert (exp == obs)
2016-05-11 23:24:58 -04:00
2016-05-22 22:33:43 +02:00
2016-05-11 23:24:58 -04:00
def test_subproc_toks_twopyeval_parens():
s = 'echo @(1+1) @(40+2)'
inp = '({0})'.format(s)
exp = '(![{0}])'.format(s)
obs = subproc_toks(inp, lexer=LEXER, returnline=True)
assert (exp == obs)
2016-05-11 23:24:58 -04:00
2016-05-22 22:33:43 +02:00
2016-05-11 23:41:14 -04:00
def test_subproc_toks_pyeval_nested():
s = 'echo @(min(1, 42))'
exp = '![{0}]'.format(s)
obs = subproc_toks(s, lexer=LEXER, returnline=True)
assert (exp == obs)
2016-05-11 23:41:14 -04:00
2016-05-22 22:33:43 +02:00
2016-05-11 23:41:14 -04:00
def test_subproc_toks_pyeval_nested_parens():
s = 'echo @(min(1, 42))'
inp = '({0})'.format(s)
exp = '(![{0}])'.format(s)
obs = subproc_toks(inp, lexer=LEXER, returnline=True)
assert (exp == obs)
2016-05-11 23:41:14 -04:00
2016-05-22 22:33:43 +02:00
2016-05-11 23:41:14 -04:00
def test_subproc_toks_capstdout():
s = 'echo $(echo bat)'
exp = '![{0}]'.format(s)
obs = subproc_toks(s, lexer=LEXER, returnline=True)
assert (exp == obs)
2016-05-11 23:41:14 -04:00
2016-05-22 22:33:43 +02:00
2016-05-11 23:41:14 -04:00
def test_subproc_toks_capproc():
s = 'echo !(echo bat)'
exp = '![{0}]'.format(s)
obs = subproc_toks(s, lexer=LEXER, returnline=True)
assert (exp == obs)
2016-05-11 23:41:14 -04:00
2016-05-22 22:33:43 +02:00
2016-05-19 18:52:50 -04:00
def test_subproc_toks_pyeval_redirect():
s = 'echo @("foo") > bar'
inp = '{0}'.format(s)
exp = '![{0}]'.format(s)
obs = subproc_toks(inp, lexer=LEXER, returnline=True)
assert (exp == obs)
2016-05-19 18:52:50 -04:00
2016-06-24 21:21:50 +03:00
@pytest.mark.parametrize('inp, exp', [
('f(x.', 'x.'),
('f(1,x.', 'x.'),
('f((1,10),x.y', 'x.y'),
])
def test_subexpr_from_unbalanced_parens(inp, exp):
obs = subexpr_from_unbalanced(inp, '(', ')')
assert exp == obs
@pytest.mark.parametrize('line, mincol, exp', [
('ls && echo a', 0, 4),
('ls && echo a', 6, None),
('ls && echo a || echo b', 6, 14),
('(ls) && echo a', 1, 4),
('not ls && echo a', 0, 8),
('not (ls) && echo a', 0, 8),
])
def test_find_next_break(line, mincol, exp):
obs = find_next_break(line, mincol=mincol, lexer=LEXER)
assert exp == obs
@pytest.mark.parametrize('inp, exp', [
(42, True),
(42.0, False),
('42', False),
('42.0', False),
([42], False),
([], False),
(None, False),
('', False)
])
def test_is_int(inp, exp):
obs = is_int(inp)
assert exp == obs
@pytest.mark.parametrize('inp, exp', [
(42.0, True),
(42.000101010010101010101001010101010001011100001101101011100, True),
(42, False),
('42', False),
('42.0', False),
([42], False),
([], False),
(None, False),
('', False),
(False, False),
(True, False),
])
def test_is_float(inp, exp):
obs = is_float(inp)
assert exp == obs
def test_is_string_true():
assert is_string('42.0')
2016-06-24 21:21:50 +03:00
def test_is_string_false():
assert not is_string(42.0)
2015-10-31 13:31:52 -04:00
2016-05-22 22:33:43 +02:00
2016-06-24 21:21:50 +03:00
def test_is_callable_true():
assert is_callable(lambda: 42.0)
2016-06-24 21:21:50 +03:00
2016-06-24 23:51:55 +03:00
2016-06-24 21:21:50 +03:00
def test_is_callable_false():
assert not is_callable(42.0)
2016-06-24 21:21:50 +03:00
@pytest.mark.parametrize('inp', ['42.0', lambda: 42.0])
def test_is_string_or_callable_true(inp):
assert is_string_or_callable(inp)
2016-06-24 23:51:55 +03:00
2016-06-24 21:21:50 +03:00
def test_is_string_or_callable_false():
assert not is_string(42.0)
2016-06-24 21:21:50 +03:00
@pytest.mark.parametrize('inp', [42, '42'])
def test_always_true(inp):
assert always_true(inp)
2016-05-22 22:33:43 +02:00
2016-06-24 21:21:50 +03:00
@pytest.mark.parametrize('inp', [42, '42'])
def test_always_false(inp):
assert not always_false(inp)
2016-05-22 22:33:43 +02:00
2016-06-24 21:21:50 +03:00
@pytest.mark.parametrize('inp, exp', [(42, '42'), ('42', '42'),])
def test_ensure_string(inp, exp):
obs = ensure_string(inp)
assert exp == obs
2016-06-24 23:51:55 +03:00
2016-06-24 21:21:50 +03:00
@pytest.mark.parametrize('inp, exp', [
('', set()),
('a', {'a'}),
(os.pathsep.join(['a', 'b']), {'a', 'b'}),
(os.pathsep.join(['a', 'b', 'c']), {'a', 'b', 'c'}),
])
def test_pathsep_to_set(inp, exp):
obs = pathsep_to_set(inp)
assert exp == obs
2016-05-22 22:33:43 +02:00
2016-06-20 13:17:52 -04:00
2016-06-24 21:21:50 +03:00
@pytest.mark.parametrize('inp, exp', [
(set(), ''),
({'a'}, 'a'),
({'a', 'b'}, os.pathsep.join(['a', 'b'])),
({'a', 'b', 'c'}, os.pathsep.join(['a', 'b', 'c'])),
])
def test_set_to_pathsep(inp, exp):
obs = set_to_pathsep(inp, sort=(len(inp) > 1))
assert exp == obs
2016-06-20 13:17:52 -04:00
2016-06-24 21:21:50 +03:00
@pytest.mark.parametrize('inp', ['42.0', ['42.0']])
def test_is_string_seq_true(inp):
assert is_string_seq(inp)
2016-06-20 13:17:52 -04:00
2016-06-24 23:51:55 +03:00
2016-06-24 21:21:50 +03:00
def test_is_string_seq_false():
2016-06-23 03:30:55 +03:00
assert not is_string_seq([42.0])
2016-06-20 16:26:54 -04:00
2016-06-24 21:21:50 +03:00
def test_is_nonstring_seq_of_strings_true():
2016-06-23 03:30:55 +03:00
assert is_nonstring_seq_of_strings(['42.0'])
2016-06-30 23:00:00 +03:00
def test_is_nonstring_seq_of_strings_true():
2016-06-23 03:30:55 +03:00
assert not is_nonstring_seq_of_strings([42.0])
2016-06-21 10:56:10 -04:00
2016-06-30 23:00:00 +03:00
@pytest.mark.parametrize('inp, exp', [
('', []),
('a', ['a']),
(os.pathsep.join(['a', 'b']), ['a', 'b']),
(os.pathsep.join(['a', 'b', 'c']), ['a', 'b', 'c']),
])
2016-06-20 16:26:54 -04:00
def test_pathsep_to_seq():
2016-06-30 23:00:00 +03:00
obs = pathsep_to_seq(inp)
assert exp == obs
2016-06-20 16:26:54 -04:00
2016-06-30 23:00:00 +03:00
@pytest.mark.parametrize('inp, exp', [
([], ''),
(['a'], 'a'),
(['a', 'b'], os.pathsep.join(['a', 'b'])),
(['a', 'b', 'c'], os.pathsep.join(['a', 'b', 'c'])),
])
2016-06-20 16:26:54 -04:00
def test_seq_to_pathsep():
2016-06-30 23:00:00 +03:00
obs = seq_to_pathsep(inp)
assert exp == obs
2016-06-20 16:26:54 -04:00
2016-06-30 23:00:00 +03:00
@pytest.mark.parametrize('inp, exp', [
('', []),
('a', ['A']),
(os.pathsep.join(['a', 'B']), ['A', 'B']),
(os.pathsep.join(['A', 'b', 'c']), ['A', 'B', 'C']),
])
2016-06-21 10:56:10 -04:00
def test_pathsep_to_upper_seq():
2016-06-30 23:00:00 +03:00
obs = pathsep_to_upper_seq(inp)
assert exp == obs
2016-06-21 10:56:10 -04:00
2016-06-30 23:00:00 +03:00
@pytest.mark.parametrize('inp, exp', [
2016-06-21 10:56:10 -04:00
([], ''),
(['a'], 'A'),
(['a', 'b'], os.pathsep.join(['A', 'B'])),
(['a', 'B', 'c'], os.pathsep.join(['A', 'B', 'C'])),
2016-06-30 23:00:00 +03:00
])
def test_seq_to_upper_pathsep():
obs = seq_to_upper_pathsep(inp)
assert exp == obs
2016-06-21 10:56:10 -04:00
2016-06-30 23:00:00 +03:00
@pytest.mark.parametrize('inp, exp', [
('/home/wakka', False),
(['/home/jawaka'], False),
(EnvPath(['/home/jawaka']), True),
(EnvPath(['jawaka']), True),
(EnvPath(b'jawaka:wakka'), True),
])
def test_is_env_path():
2016-06-30 23:00:00 +03:00
obs = is_env_path(inp)
assert exp == obs
@pytest.mark.parametrize('inp, exp', [
('/home/wakka', ['/home/wakka']),
('/home/wakka' + os.pathsep + '/home/jawaka',
['/home/wakka', '/home/jawaka']),
(b'/home/wakka', ['/home/wakka']),
])
def test_str_to_env_path(inp, exp):
obs = str_to_env_path(inp)
assert exp == obs.paths
2016-06-21 10:56:10 -04:00
2016-06-30 23:00:00 +03:00
@pytest.mark.parametrize('inp, exp', [
(['/home/wakka'], '/home/wakka'),
(['/home/wakka', '/home/jawaka'],
'/home/wakka' + os.pathsep + '/home/jawaka'),
])
def test_env_path_to_str(inp, exp):
obs = env_path_to_str(inp)
assert exp == obs
# helper
def expand(path):
return os.path.expanduser(os.path.expandvars(path))
@pytest.mark.parametrize('env', [TOOLS_ENV, ENCODE_ENV_ONLY])
@pytest.mark.parametrize('inp, exp', [
('xonsh_dir', 'xonsh_dir'),
('.', '.'),
('../', '../'),
('~/', '~/'),
(b'~/../', '~/../'),
])
def test_env_path_getitem(inp, exp, xonsh_builtins, env):
xonsh_builtins.__xonsh_env__ = env
obs = EnvPath(inp)[0] # call to __getitem__
2016-07-04 13:20:12 +03:00
if env.get('EXPAND_ENV_VARS'):
assert expand(exp) == obs
else:
assert exp == obs
2016-06-30 23:00:00 +03:00
@pytest.mark.parametrize('env', [TOOLS_ENV, ENCODE_ENV_ONLY])
@pytest.mark.parametrize('inp, exp', [
(os.pathsep.join(['xonsh_dir', '../', '.', '~/']),
['xonsh_dir', '../', '.', '~/']),
('/home/wakka' + os.pathsep + '/home/jakka' + os.pathsep + '~/',
['/home/wakka', '/home/jakka', '~/'])
])
def test_env_path_multipath(inp, exp, xonsh_builtins, env):
# cases that involve path-separated strings
2016-06-30 23:00:00 +03:00
xonsh_builtins.__xonsh_env__ = env
if env == TOOLS_ENV:
obs = [i for i in EnvPath(inp)]
assert [expand(i) for i in exp] == obs
else:
obs = [i for i in EnvPath(inp)]
assert [i for i in exp] == obs
2016-06-21 00:52:09 +03:00
2016-06-30 23:00:00 +03:00
@pytest.mark.parametrize('inp, exp', [
(pathlib.Path('/home/wakka'), ['/home/wakka'.replace('/',os.sep)]),
(pathlib.Path('~/'), ['~']),
(pathlib.Path('.'), ['.']),
(['/home/wakka', pathlib.Path('/home/jakka'), '~/'],
['/home/wakka', '/home/jakka'.replace('/',os.sep), '~/']),
(['/home/wakka', pathlib.Path('../'), '../'],
['/home/wakka', '..', '../']),
(['/home/wakka', pathlib.Path('~/'), '~/'],
['/home/wakka', '~', '~/']),
])
def test_env_path_with_pathlib_path_objects(inp, exp, xonsh_builtins):
xonsh_builtins.__xonsh_env__ = TOOLS_ENV
# iterate over EnvPath to acquire all expanded paths
obs = [i for i in EnvPath(inp)]
assert [expand(i) for i in exp] == obs
2016-06-24 23:51:55 +03:00
2016-06-24 21:21:50 +03:00
@pytest.mark.parametrize('inp', ['42.0', [42.0]] )
def test_is_nonstring_seq_of_strings_false(inp):
assert not is_nonstring_seq_of_strings(inp)
@pytest.mark.parametrize('inp, exp', [
('', []),
('a', ['a']),
(os.pathsep.join(['a', 'b']), ['a', 'b']),
(os.pathsep.join(['a', 'b', 'c']), ['a', 'b', 'c']),
])
def test_pathsep_to_seq(inp, exp):
obs = pathsep_to_seq(inp)
assert exp == obs
@pytest.mark.parametrize('inp, exp', [
([], ''),
(['a'], 'a'),
(['a', 'b'], os.pathsep.join(['a', 'b'])),
(['a', 'b', 'c'], os.pathsep.join(['a', 'b', 'c'])),
])
def test_seq_to_pathsep(inp, exp):
obs = seq_to_pathsep(inp)
assert exp == obs
@pytest.mark.parametrize('inp, exp', [
('', []),
('a', ['A']),
(os.pathsep.join(['a', 'B']), ['A', 'B']),
(os.pathsep.join(['A', 'b', 'c']), ['A', 'B', 'C']),
])
def test_pathsep_to_upper_seq(inp, exp):
obs = pathsep_to_upper_seq(inp)
assert exp == obs
2016-06-21 10:56:10 -04:00
2016-06-24 23:51:55 +03:00
@pytest.mark.parametrize('inp, exp', [
([], ''),
(['a'], 'A'),
(['a', 'b'], os.pathsep.join(['A', 'B'])),
(['a', 'B', 'c'], os.pathsep.join(['A', 'B', 'C'])),
])
def test_seq_to_upper_pathsep(inp, exp):
obs = seq_to_upper_pathsep(inp)
assert exp == obs
@pytest.mark.parametrize('inp, exp', [
('/home/wakka', False),
(['/home/jawaka'], False),
(EnvPath(['/home/jawaka']), True),
(EnvPath(['jawaka']), True),
(EnvPath(b'jawaka:wakka'), True),
])
def test_is_env_path(inp, exp):
obs = is_env_path(inp)
assert exp == obs
@pytest.mark.parametrize('inp, exp', [
('/home/wakka', ['/home/wakka']),
('/home/wakka' + os.pathsep + '/home/jawaka',
['/home/wakka', '/home/jawaka']),
(b'/home/wakka', ['/home/wakka']),
])
def test_str_to_env_path(inp, exp):
obs = str_to_env_path(inp)
assert exp == obs.paths
@pytest.mark.parametrize('inp, exp', [
(['/home/wakka'], '/home/wakka'),
(['/home/wakka', '/home/jawaka'],
'/home/wakka' + os.pathsep + '/home/jawaka'),
])
def test_env_path_to_str(inp, exp):
obs = env_path_to_str(inp)
assert exp == obs
@pytest.mark.parametrize('inp, exp', [
(pathlib.Path('/home/wakka'), ['/home/wakka'.replace('/',os.sep)]),
(pathlib.Path('~/'), ['~']),
(pathlib.Path('.'), ['.']),
(['/home/wakka', pathlib.Path('/home/jakka'), '~/'],
['/home/wakka', '/home/jakka'.replace('/',os.sep), '~/']),
(['/home/wakka', pathlib.Path('../'), '../'],
['/home/wakka', '..', '../']),
(['/home/wakka', pathlib.Path('~/'), '~/'],
['/home/wakka', '~', '~/']),
])
2016-06-25 21:07:25 +03:00
def test_env_path_with_pathlib_path_objects(inp, exp, xonsh_builtins):
xonsh_builtins.__xonsh_env__ = TOOLS_ENV
2016-06-24 23:51:55 +03:00
# iterate over EnvPath to acquire all expanded paths
obs = [i for i in EnvPath(inp)]
assert [expand(i) for i in exp] == obs
2016-06-21 00:52:09 +03:00
2016-06-24 23:51:55 +03:00
# helper
def mkpath(*paths):
"""Build os-dependent paths properly."""
return os.sep + os.sep.join(paths)
@pytest.mark.parametrize('inp, exp', [
([mkpath('home', 'wakka'),
mkpath('home', 'jakka'),
mkpath('home', 'yakka')],
[mkpath('home', 'wakka'),
mkpath('home', 'jakka')])
])
2016-07-02 04:53:37 +03:00
def test_env_path_slice_get_all_except_last_element(inp, exp):
2016-06-24 23:51:55 +03:00
obs = EnvPath(inp)[:-1]
assert exp == obs
@pytest.mark.parametrize('inp, exp', [
2016-06-21 00:52:09 +03:00
([mkpath('home', 'wakka'),
mkpath('home', 'jakka'),
mkpath('home', 'yakka')],
[mkpath('home', 'jakka'),
2016-06-24 23:51:55 +03:00
mkpath('home', 'yakka')])
])
def test_env_path_slice_get_all_except_first_element(inp, exp):
obs = EnvPath(inp)[1:]
assert exp == obs
2016-06-21 00:52:09 +03:00
2016-06-24 23:51:55 +03:00
@pytest.mark.parametrize('inp, exp_a, exp_b', [
2016-06-21 00:52:09 +03:00
([mkpath('home', 'wakka'),
mkpath('home', 'jakka'),
mkpath('home', 'yakka'),
mkpath('home', 'takka')],
[mkpath('home', 'wakka'),
mkpath('home', 'yakka')],
[mkpath('home', 'jakka'),
2016-06-24 23:51:55 +03:00
mkpath('home', 'takka')])
])
def test_env_path_slice_path_with_step(inp, exp_a, exp_b):
obs_a = EnvPath(inp)[0::2]
assert exp_a == obs_a
obs_b = EnvPath(inp)[1::2]
assert exp_b == obs_b
2016-06-21 00:52:09 +03:00
2016-06-24 23:51:55 +03:00
@pytest.mark.parametrize('inp, exp', [
2016-06-21 00:52:09 +03:00
([mkpath('home', 'wakka'),
mkpath('home', 'xakka'),
mkpath('other', 'zakka'),
mkpath('another', 'akka'),
mkpath('home', 'bakka')],
[mkpath('other', 'zakka'),
2016-06-24 23:51:55 +03:00
mkpath('another', 'akka')])
])
def test_env_path_keep_only_non_home_paths(inp, exp):
obs = EnvPath(inp)[2:4]
assert exp == obs
@pytest.mark.parametrize('inp', [True, False])
def test_is_bool_true(inp):
assert True == is_bool(inp)
@pytest.mark.parametrize('inp', [1, 'yooo hooo!'])
def test_is_bool_false(inp):
assert False == is_bool(inp)
@pytest.mark.parametrize('inp, exp', [
(True, True),
(False, False),
(None, False),
('', False),
('0', False),
('False', False),
('NONE', False),
('TRUE', True),
('1', True),
(0, False),
(1, True),
])
def test_to_bool(inp, exp):
obs = to_bool(inp)
assert exp == obs
@pytest.mark.parametrize('inp, exp', [(True, '1'), (False, '')])
def test_bool_to_str(inp, exp):
assert bool_to_str(inp) == exp
@pytest.mark.parametrize('inp, exp', [
(True, True),
(False, True),
(1, True),
(0, True),
('Yolo', False),
(1.0, False),
])
def test_is_bool_or_int(inp, exp):
obs = is_bool_or_int(inp)
assert exp == obs
@pytest.mark.parametrize('inp, exp', [
(True, True),
(False, False),
(1, 1),
(0, 0),
('', False),
(0.0, False),
(1.0, True),
('T', True),
('f', False),
('0', 0),
('10', 10),
])
def test_to_bool_or_int(inp, exp):
obs = to_bool_or_int(inp)
assert exp == obs
@pytest.mark.parametrize('inp, exp', [
(True, '1'),
(False, ''),
(1, '1'),
(0, '0'),
])
def test_bool_or_int_to_str(inp, exp):
obs = bool_or_int_to_str(inp)
assert exp == obs
@pytest.mark.parametrize('inp, exp', [
(42, slice(42, 43)),
2016-08-29 02:45:36 +03:00
(0, slice(0, 1)),
2015-08-23 11:30:07 -04:00
(None, slice(None, None, None)),
2016-07-21 16:54:34 +03:00
(slice(1,2), slice(1,2)),
2016-08-04 14:14:22 +03:00
('-1', slice(-1, None, None)),
('42', slice(42, 43)),
('-42', slice(-42, -41)),
2015-08-23 11:30:07 -04:00
('1:2:3', slice(1, 2, 3)),
('1::3', slice(1, None, 3)),
(':', slice(None, None, None)),
2015-08-23 11:30:07 -04:00
('1:', slice(1, None, None)),
('[1:2:3]', slice(1, 2, 3)),
('(1:2:3)', slice(1, 2, 3)),
2016-07-23 09:53:38 +03:00
((4, 8, 10), slice(4, 8, 10)),
([10,20], slice(10,20))
2016-06-24 23:51:55 +03:00
])
def test_ensure_slice(inp, exp):
obs = ensure_slice(inp)
2016-06-24 23:51:55 +03:00
assert exp == obs
@pytest.mark.parametrize('inp', [
'42.3',
'3:asd5:1',
'test' ,
'6.53:100:5',
'4:-',
'2:15-:3',
'50:-:666',
object(),
[1,5,3,4],
('foo')
])
def test_ensure_slice_invalid(inp):
with pytest.raises(ValueError):
2016-07-20 15:16:46 +03:00
obs = ensure_slice(inp)
2016-07-20 15:16:46 +03:00
@pytest.mark.parametrize('inp, exp', [
('42', True),
('42.0', False),
(42, False),
([42], False),
([], False),
(None, False),
('', False),
(False, False),
(True, False),
])
def test_is_int_as_str(inp, exp):
obs = is_int_as_str(inp)
assert exp == obs
2016-06-24 23:51:55 +03:00
@pytest.mark.parametrize('inp, exp', [
('20', False),
('20%', False),
((20, 'c'), False),
((20.0, 'm'), False),
((20.0, 'c'), True),
((20.0, '%'), True),
])
def test_is_dynamic_cwd_width(inp, exp):
obs = is_dynamic_cwd_width(inp)
assert exp == obs
2016-07-20 15:16:46 +03:00
@pytest.mark.parametrize('inp, exp', [
(42, False),
(None, False),
('42', False),
('-42', False),
(slice(1,2,3), False),
([], False),
(False, False),
(True, False),
('1:2:3', True),
('1::3', True),
('1:', True),
(':', True),
('[1:2:3]', True),
('(1:2:3)', True),
('r', False),
('r:11', False),
])
def test_is_slice_as_str(inp, exp):
obs = is_slice_as_str(inp)
assert exp == obs
2016-06-24 23:51:55 +03:00
@pytest.mark.parametrize('inp, exp', [
('throwback.log', True),
('', True),
(None, True),
(True, False),
(False, False),
(42, False),
([1, 2, 3], False),
((1, 2), False),
(("wrong", "parameter"), False),
skip_if_on_windows(('/dev/null', True))
])
def test_is_logfile_opt(inp, exp):
obs = is_logfile_opt(inp)
assert exp == obs
@pytest.mark.parametrize('inp, exp', [
(True, None),
(False, None),
(1, None),
(None, None),
('throwback.log', 'throwback.log'),
2016-06-24 23:51:55 +03:00
skip_if_on_windows(('/dev/null', '/dev/null')),
skip_if_on_windows(('/dev/nonexistent_dev', None))
])
def test_to_logfile_opt(inp, exp):
obs = to_logfile_opt(inp)
assert exp == obs
@pytest.mark.parametrize('inp, exp', [
(None, ''),
('', ''),
('throwback.log', 'throwback.log'),
('/dev/null', '/dev/null')
])
def test_logfile_opt_to_str(inp, exp):
obs = logfile_opt_to_str(inp)
assert exp == obs
@pytest.mark.parametrize('inp, exp', [
('20', (20.0, 'c')),
('20%', (20.0, '%')),
((20, 'c'), (20.0, 'c')),
((20, '%'), (20.0, '%')),
((20.0, 'c'), (20.0, 'c')),
((20.0, '%'), (20.0, '%')),
('inf', (float('inf'), 'c')),
])
def test_to_dynamic_cwd_tuple(inp, exp):
obs = to_dynamic_cwd_tuple(inp)
assert exp == obs
@pytest.mark.parametrize('inp, exp', [
((20.0, 'c'), '20.0'),
((20.0, '%'), '20.0%'),
((float('inf'), 'c'), 'inf'),
])
def test_dynamic_cwd_tuple_to_str(inp, exp):
obs = dynamic_cwd_tuple_to_str(inp)
assert exp == obs
@pytest.mark.parametrize('st, esc', [
('', ''),
('foo', 'foo'),
('foo&bar', 'foo^&bar'),
('foo$?-/_"\\', 'foo$?-/_^"\\'),
('^&<>|', '^^^&^<^>^|'),
('this /?', 'this /.')
])
def test_escape_windows_cmd_string(st, esc):
obs = escape_windows_cmd_string(st)
assert esc == obs
@pytest.mark.parametrize('st, esc', [
('', '""'),
('foo', 'foo'),
(r'arg1 "hallo, "world"" "\some\path with\spaces")',
r'"arg1 \"hallo, \"world\"\" \"\some\path with\spaces\")"'),
(r'"argument"2" argument3 argument4',
r'"\"argument\"2\" argument3 argument4"'),
(r'"\foo\bar bar\foo\" arg',
r'"\"\foo\bar bar\foo\\\" arg"')
])
def test_argvquote(st, esc):
obs = argvquote(st)
assert esc == obs
2016-07-18 00:35:50 +03:00
@pytest.mark.parametrize('inp', ['no string here', ''])
def test_partial_string_none(inp):
assert check_for_partial_string(inp) == (None, None, None)
@pytest.mark.parametrize('leaders', [
(('', 0), ('not empty', 9)),
(('not empty', 9), ('', 0))
])
@pytest.mark.parametrize('prefix', ['b', 'rb', 'r' ])
@pytest.mark.parametrize('quote', ['"', '"""'])
def test_partial_string(leaders, prefix, quote):
(l, l_len), (f, f_len) = leaders
s = prefix + quote
t = s + 'test string' + quote
t_len = len(t)
# single string
test_string = l + t + f
obs = check_for_partial_string(test_string)
exp = l_len, l_len + t_len, s
assert obs == exp
# single partial
test_string = l + f + s + 'test string'
obs = check_for_partial_string(test_string)
exp = l_len + f_len, None, s
assert obs == exp
# two strings
test_string = l + t + f + l + t + f
obs = check_for_partial_string(test_string)
exp = (l_len + t_len + f_len + l_len), (l_len + t_len + f_len + l_len + t_len), s
assert obs == exp
# one string, one partial
test_string = l + t + f + l + s + 'test string'
obs = check_for_partial_string(test_string)
exp = l_len + t_len + f_len + l_len , None, s
assert obs == exp
2015-12-18 15:30:46 -05:00
2016-07-03 20:19:37 +03:00
def test_executables_in(xonsh_builtins):
2016-05-22 22:33:43 +02:00
expected = set()
2016-06-03 10:22:18 -07:00
types = ('file', 'directory', 'brokensymlink')
2016-06-20 00:09:51 +02:00
if ON_WINDOWS:
# Don't test symlinks on windows since it requires admin
types = ('file', 'directory')
2016-06-03 10:22:18 -07:00
executables = (True, False)
2016-05-22 22:33:43 +02:00
with TemporaryDirectory() as test_path:
2016-06-03 10:22:18 -07:00
for _type in types:
for executable in executables:
fname = '%s_%s' % (_type, executable)
if _type == 'none':
continue
if _type == 'file' and executable:
ext = '.exe' if ON_WINDOWS else ''
expected.add(fname + ext)
else:
ext = ''
path = os.path.join(test_path, fname + ext)
if _type == 'file':
with open(path, 'w') as f:
f.write(fname)
elif _type == 'directory':
os.mkdir(path)
elif _type == 'brokensymlink':
tmp_path = os.path.join(test_path, 'i_wont_exist')
2016-06-20 00:09:51 +02:00
with open(tmp_path, 'w') as f:
2016-06-03 10:22:18 -07:00
f.write('deleteme')
os.symlink(tmp_path, path)
os.remove(tmp_path)
2016-06-20 00:09:51 +02:00
if executable and not _type == 'brokensymlink':
2016-06-03 10:22:18 -07:00
os.chmod(path, stat.S_IXUSR | stat.S_IRUSR | stat.S_IWUSR)
if ON_WINDOWS:
2016-07-03 20:19:37 +03:00
xonsh_builtins.__xonsh_env__ = PATHEXT_ENV
result = set(executables_in(test_path))
else:
result = set(executables_in(test_path))
assert (expected == result)
2016-05-22 22:33:43 +02:00
2016-06-24 23:51:55 +03:00
@pytest.mark.parametrize('inp, exp', [
('yo', '[Yy][Oo]'),
('[a-f]123e', '[a-f]123[Ee]'),
('${HOME}/yo', '${HOME}/[Yy][Oo]'),
('./yo/mom', './[Yy][Oo]/[Mm][Oo][Mm]'),
('Eßen', '[Ee][Ss]?[Ssß][Ee][Nn]'),
])
def test_expand_case_matching(inp, exp):
obs = expand_case_matching(inp)
assert exp == obs
2016-06-07 22:12:02 -04:00
def test_commands_cache_lazy():
cc = CommandsCache()
2016-06-22 18:23:36 -04:00
assert not cc.lazyin('xonsh')
assert 0 == len(list(cc.lazyiter()))
assert 0 == cc.lazylen()
2016-07-05 12:15:02 +03:00
@pytest.mark.parametrize('inp, exp', [
("foo", "foo"),
2016-07-05 16:04:03 +03:00
("$foo $bar", "bar $bar"),
2016-07-05 12:15:02 +03:00
("$foobar", "$foobar"),
2016-07-05 16:04:03 +03:00
("$foo $spam", "bar eggs"),
2016-07-05 17:02:24 +03:00
("$an_int$spam$a_bool", "42eggsTrue"),
("bar$foo$spam$foo $an_int $none", "barbareggsbar 42 None"),
2016-07-05 12:15:02 +03:00
("$foo/bar", "bar/bar"),
2016-07-06 15:08:11 +03:00
("${'foo'} $spam", "bar eggs"),
("${'foo'} ${'a_bool'}", "bar True"),
2016-07-05 12:15:02 +03:00
("${'foo'}bar", "barbar"),
("${'foo'}/bar", "bar/bar"),
("${\"foo\'}", "${\"foo\'}"),
("$?bar", "$?bar"),
("$foo}bar", "bar}bar"),
2016-07-05 16:04:03 +03:00
("${'foo", "${'foo"),
2016-07-05 12:15:02 +03:00
skip_if_on_unix(("%foo%bar", "barbar")),
2016-07-05 17:02:24 +03:00
skip_if_on_unix(("%foo% %a_bool%", "bar True")),
skip_if_on_unix(("%foo%%an_int%", "bar42")),
2016-07-13 23:45:52 +03:00
skip_if_on_unix(("%foo% $spam ${'a_bool'}", "bar eggs True")),
2016-07-05 12:15:02 +03:00
(b"foo", "foo"),
(b"$foo bar", "bar bar"),
(b"${'foo'}bar", "barbar"),
skip_if_on_unix((b"%foo%bar", "barbar")),
])
def test_expandvars(inp, exp, xonsh_builtins):
"""Tweaked for xonsh cases from CPython `test_genericpath.py`"""
env = Env({'foo':'bar', 'spam': 'eggs', 'a_bool': True, 'an_int': 42, 'none': None})
xonsh_builtins.__xonsh_env__ = env
assert expandvars(inp) == exp
2016-08-01 15:01:17 +03:00
2016-08-10 16:49:06 +03:00
@pytest.mark.parametrize('inp, fmt, exp',[
(572392800.0, None, 572392800.0),
('42.1459', None, 42.1459),
(dt.datetime(2016, 8, 2, 13, 24), None, dt.datetime(2016, 8, 2, 13, 24).timestamp()),
('2016-8-10 16:14', None, dt.datetime(2016, 8, 10, 16, 14).timestamp()),
('2016/8/10 16:14:40', '%Y/%m/%d %H:%M:%S', dt.datetime(2016, 8, 10, 16, 14, 40).timestamp()),
2016-08-01 15:01:17 +03:00
])
2016-08-10 16:49:06 +03:00
def test_ensure_timestamp(inp, fmt, exp, xonsh_builtins):
xonsh_builtins.__xonsh_env__['XONSH_DATETIME_FORMAT'] = '%Y-%m-%d %H:%M'
obs = ensure_timestamp(inp, fmt)
2016-08-01 15:01:17 +03:00
assert exp == obs