2015-11-16 14:04:32 -08:00
|
|
|
# -*- 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
|
|
|
|
2016-07-01 15:43:16 +03:00
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
2016-07-01 21:52:37 +03:00
|
|
|
def xonsh_execer_autouse(xonsh_builtins, xonsh_execer):
|
2016-07-01 15:43:16 +03:00
|
|
|
return xonsh_execer
|
|
|
|
|
2015-02-03 02:02:57 -06:00
|
|
|
|
2016-07-01 15:43:16 +03: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
|
|
|
|
2016-07-01 15:43:16 +03: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
|
|
|
|
2016-07-01 15:43:16 +03: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")
|
|
|
|
|
2015-05-09 01:58:51 -04:00
|
|
|
|
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")
|
|
|
|
|
2015-05-09 01:58:51 -04:00
|
|
|
|
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"
|
2016-07-01 15:43:16 +03:00
|
|
|
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"
|
2016-07-01 15:43:16 +03:00
|
|
|
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'
|
2016-07-01 15:43:16 +03:00
|
|
|
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"
|
2016-07-01 15:43:16 +03:00
|
|
|
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
|
|
|
|
2016-05-19 20:12:07 -04:00
|
|
|
def test_good_rhs_subproc():
|
2017-06-07 11:51:17 -04:00
|
|
|
# nonsense but parsable
|
2018-08-30 09:18:49 -05:00
|
|
|
code = "str().split() | ![grep exit]\n"
|
|
|
|
assert code
|
|
|
|
|
2016-05-19 20:12:07 -04:00
|
|
|
|
|
|
|
def test_bad_rhs_subproc():
|
2017-06-07 11:51:17 -04:00
|
|
|
# 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)
|
2016-05-19 20:12:07 -04:00
|
|
|
|
2018-08-30 09:18:49 -05:00
|
|
|
|
2015-08-05 12:05:13 +03: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"
|
2016-07-01 15:43:16 +03:00
|
|
|
assert check_parse(code)
|
2015-08-05 12:05:13 +03:00
|
|
|
|
2018-08-30 09:18:49 -05:00
|
|
|
|
2016-02-21 14:37:03 -05:00
|
|
|
def test_command_in_func():
|
2018-08-30 09:18:49 -05:00
|
|
|
code = "def f():\n" " echo hello\n"
|
2016-07-01 15:43:16 +03:00
|
|
|
assert check_parse(code)
|
2016-02-21 14:37:03 -05:00
|
|
|
|
2018-08-30 09:18:49 -05:00
|
|
|
|
2016-02-21 14:37:03 -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"
|
2016-07-01 15:43:16 +03:00
|
|
|
assert check_parse(code)
|
2016-02-21 14:37:03 -05:00
|
|
|
|
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'
|
2016-07-01 15:43:16 +03:00
|
|
|
assert check_parse(code)
|
2016-05-19 18:52:50 -04:00
|
|
|
|
2018-07-19 21:01:11 -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"
|
2016-07-01 15:43:16 +03:00
|
|
|
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"
|
2016-07-01 15:43:16 +03:00
|
|
|
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"
|
2016-07-01 15:43:16 +03:00
|
|
|
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-07-19 21:01:11 -04:00
|
|
|
|
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")
|
|
|
|
|
|
|
|
|
2021-05-12 19:39:11 +03:00
|
|
|
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)
|
2021-06-07 23:10:40 +05:30
|
|
|
|
|
|
|
|
|
|
|
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)
|