xonsh/tests/test_execer.py

168 lines
3.4 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2015-02-03 02:02:57 -06:00
"""Tests the xonsh lexer."""
import os
2020-03-19 11:56:11 +01:00
from tools import (
check_eval,
check_exec,
check_parse,
skip_if_on_unix,
skip_if_on_windows,
)
2016-06-22 17:32:02 -04:00
import pytest
2015-02-11 02:01:35 -06:00
@pytest.fixture(autouse=True)
def xonsh_execer_autouse(xonsh_builtins, xonsh_execer):
return xonsh_execer
2015-02-03 02:02:57 -06:00
@skip_if_on_unix
2016-06-05 16:25:43 -04:00
def test_win_ipconfig():
2018-08-30 09:18:49 -05:00
assert check_eval(os.environ["SYSTEMROOT"] + "\\System32\\ipconfig.exe /all")
2015-03-01 19:13:09 -06:00
@skip_if_on_unix
2016-06-05 16:25:43 -04:00
def test_ipconfig():
2018-08-30 09:18:49 -05:00
assert check_eval("ipconfig /all")
2015-02-11 02:01:35 -06:00
@skip_if_on_windows
2016-06-05 16:25:43 -04:00
def test_bin_ls():
2018-08-30 09:18:49 -05:00
assert check_eval("/bin/ls -l")
2015-02-11 02:01:35 -06:00
2016-04-09 00:25:53 -04:00
def test_ls_dashl():
2018-08-30 09:18:49 -05:00
assert check_parse("ls -l")
2016-04-09 00:25:53 -04:00
def test_which_ls():
2018-08-30 09:18:49 -05:00
assert check_parse("which ls")
2016-04-09 00:25:53 -04:00
def test_echo_hello():
2018-08-30 09:18:49 -05:00
assert check_parse("echo hello")
2015-07-29 23:58:25 +02:00
2017-11-09 20:20:40 -05:00
def test_echo_star_with_semi():
2018-08-30 09:18:49 -05:00
assert check_parse("echo * spam ; ![echo eggs]\n")
2017-11-09 20:20:40 -05:00
2015-03-10 21:38:11 -05:00
def test_simple_func():
2018-08-30 09:18:49 -05:00
code = "def prompt():\n" " return '{user}'.format(user='me')\n"
assert check_parse(code)
2015-03-10 21:38:11 -05:00
2018-08-30 09:18:49 -05:00
2016-06-11 21:02:28 -04:00
def test_lookup_alias():
2018-08-30 09:18:49 -05:00
code = "def foo(a, s=None):\n" ' return "bar"\n' "@(foo)\n"
assert check_parse(code)
2016-06-11 21:02:28 -04:00
2018-08-30 09:18:49 -05:00
2016-06-11 21:02:28 -04:00
def test_lookup_anon_alias():
2018-08-30 09:18:49 -05:00
code = 'echo "hi" | @(lambda a, s=None: a[0]) foo bar baz\n'
assert check_parse(code)
2016-06-11 21:02:28 -04:00
2018-08-30 09:18:49 -05:00
2015-03-10 21:38:11 -05:00
def test_simple_func_broken():
2018-08-30 09:18:49 -05:00
code = "def prompt():\n" " return '{user}'.format(\n" " user='me')\n"
assert check_parse(code)
2015-03-10 21:38:11 -05:00
2018-08-30 09:18:49 -05:00
2015-08-02 17:34:13 -05:00
def test_bad_indent():
2018-08-30 09:18:49 -05:00
code = "if True:\n" "x = 1\n"
2016-06-22 17:32:02 -04:00
with pytest.raises(SyntaxError):
check_parse(code)
2015-03-10 21:38:11 -05:00
2018-08-30 09:18:49 -05:00
def test_good_rhs_subproc():
# nonsense but parsable
2018-08-30 09:18:49 -05:00
code = "str().split() | ![grep exit]\n"
assert code
def test_bad_rhs_subproc():
# nonsense but unparsable
2018-08-30 09:18:49 -05:00
code = "str().split() | grep exit\n"
2016-06-22 17:32:02 -04:00
with pytest.raises(SyntaxError):
check_parse(code)
2018-08-30 09:18:49 -05:00
def test_indent_with_empty_line():
2018-08-30 09:18:49 -05:00
code = "if True:\n" "\n" " some_command for_sub_process_mode\n"
assert check_parse(code)
2018-08-30 09:18:49 -05:00
def test_command_in_func():
2018-08-30 09:18:49 -05:00
code = "def f():\n" " echo hello\n"
assert check_parse(code)
2018-08-30 09:18:49 -05:00
def test_command_in_func_with_comment():
2018-08-30 09:18:49 -05:00
code = "def f():\n" " echo hello # comment\n"
assert check_parse(code)
2018-08-30 09:18:49 -05:00
2016-05-19 18:52:50 -04:00
def test_pyeval_redirect():
code = 'echo @("foo") > bar\n'
assert check_parse(code)
2016-05-19 18:52:50 -04:00
def test_pyeval_multiline_str():
code = 'echo @("""hello\nmom""")\n'
assert check_parse(code)
2016-05-24 23:55:07 -04:00
def test_echo_comma():
2018-08-30 09:18:49 -05:00
code = "echo ,\n"
assert check_parse(code)
2016-05-24 23:55:07 -04:00
2018-08-30 09:18:49 -05:00
2016-05-24 23:55:07 -04:00
def test_echo_comma_val():
2018-08-30 09:18:49 -05:00
code = "echo ,1\n"
assert check_parse(code)
2016-05-24 23:55:07 -04:00
2018-08-30 09:18:49 -05:00
2016-05-24 23:55:07 -04:00
def test_echo_comma_2val():
2018-08-30 09:18:49 -05:00
code = "echo 1,2\n"
assert check_parse(code)
2017-02-15 00:58:38 -05:00
def test_echo_line_cont():
code = 'echo "1 \\\n2"\n'
assert check_parse(code)
2018-08-30 09:18:49 -05:00
@pytest.mark.parametrize(
"code",
[
"echo a and \\\necho b\n",
"echo a \\\n or echo b\n",
"echo a \\\n or echo b and \\\n echo c\n",
2019-02-04 18:00:21 -05:00
"if True:\\\n echo a \\\n b\n",
2018-08-30 09:18:49 -05:00
],
)
2018-08-27 14:20:11 -05:00
def test_two_echo_line_cont(code):
assert check_parse(code)
2020-03-19 11:56:11 +01:00
def test_eval_eol():
assert check_eval("0") and check_eval("0\n")
def test_annotated_assign():
# issue #3959 - didn't work because of `CtxAwareTransformer`
assert check_exec("x : int = 42")
2020-03-19 11:56:11 +01:00
def test_exec_eol():
locs = dict()
assert check_exec("a=0", locs=locs) and check_exec("a=0\n", locs=locs)
def test_exec_print(capsys):
ls = {"nested": "some long list"}
check_exec("print(ls)", locs=dict(ls=ls))
out, err = capsys.readouterr()
assert out.strip() == repr(ls)