2016-09-11 00:50:52 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""Test XonshLexer for pygments"""
|
|
|
|
|
|
|
|
import os
|
2016-09-12 11:23:20 +08:00
|
|
|
import builtins
|
|
|
|
|
2016-09-11 16:17:56 +08:00
|
|
|
import pytest
|
2018-08-30 09:18:49 -05:00
|
|
|
from pygments.token import (
|
|
|
|
Keyword,
|
|
|
|
Name,
|
|
|
|
String,
|
|
|
|
Error,
|
|
|
|
Number,
|
|
|
|
Operator,
|
|
|
|
Punctuation,
|
|
|
|
Text,
|
|
|
|
)
|
2016-09-11 16:17:56 +08:00
|
|
|
from tools import skip_if_on_windows
|
2016-09-11 00:50:52 +08:00
|
|
|
|
2016-09-12 11:52:09 +08:00
|
|
|
from xonsh.platform import ON_WINDOWS
|
2016-09-11 00:50:52 +08:00
|
|
|
from xonsh.built_ins import load_builtins, unload_builtins
|
|
|
|
from xonsh.pyghooks import XonshLexer
|
|
|
|
|
|
|
|
|
2018-11-19 20:22:18 -05:00
|
|
|
@pytest.fixture(autouse=True)
|
2018-11-19 23:24:32 -05:00
|
|
|
def load_command_cache(xonsh_builtins):
|
2016-09-11 00:50:52 +08:00
|
|
|
load_builtins()
|
2016-09-12 11:52:09 +08:00
|
|
|
if ON_WINDOWS:
|
2018-08-30 09:18:49 -05:00
|
|
|
for key in ("cd", "bash"):
|
2016-09-12 11:52:09 +08:00
|
|
|
builtins.aliases[key] = lambda *args, **kwargs: None
|
2016-09-11 00:50:52 +08:00
|
|
|
yield
|
|
|
|
unload_builtins()
|
|
|
|
|
|
|
|
|
|
|
|
def check_token(code, tokens):
|
|
|
|
"""Make sure that all tokens appears in code in order"""
|
|
|
|
lx = XonshLexer()
|
|
|
|
tks = list(lx.get_tokens(code))
|
|
|
|
|
|
|
|
for tk in tokens:
|
|
|
|
while tks:
|
|
|
|
if tk == tks[0]:
|
|
|
|
break
|
|
|
|
tks = tks[1:]
|
|
|
|
else:
|
2018-08-30 09:18:49 -05:00
|
|
|
msg = "Token {!r} missing: {!r}".format(tk, list(lx.get_tokens(code)))
|
2016-09-11 00:50:52 +08:00
|
|
|
pytest.fail(msg)
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
|
|
@skip_if_on_windows
|
|
|
|
def test_ls():
|
2018-08-30 09:18:49 -05:00
|
|
|
check_token("ls -al", [(Name.Builtin, "ls")])
|
2016-09-11 00:50:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
@skip_if_on_windows
|
|
|
|
def test_bin_ls():
|
2018-08-30 09:18:49 -05:00
|
|
|
check_token("/bin/ls -al", [(Name.Builtin, "/bin/ls")])
|
2016-09-11 00:50:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_py_print():
|
2018-08-30 09:18:49 -05:00
|
|
|
check_token('print("hello")', [(Keyword, "print"), (String.Double, "hello")])
|
2016-09-11 00:50:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_invalid_cmd():
|
2018-08-30 09:18:49 -05:00
|
|
|
check_token("non-existance-cmd -al", [(Name, "non")]) # parse as python
|
|
|
|
check_token(
|
|
|
|
"![non-existance-cmd -al]", [(Error, "non-existance-cmd")]
|
|
|
|
) # parse as error
|
|
|
|
check_token("for i in range(10):", [(Keyword, "for")]) # as py keyword
|
|
|
|
check_token("(1, )", [(Punctuation, "("), (Number.Integer, "1")])
|
2016-09-11 00:50:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_multi_cmd():
|
2018-08-30 09:18:49 -05:00
|
|
|
check_token(
|
|
|
|
"cd && cd", [(Name.Builtin, "cd"), (Operator, "&&"), (Name.Builtin, "cd")]
|
|
|
|
)
|
|
|
|
check_token(
|
|
|
|
"cd || non-existance-cmd",
|
|
|
|
[(Name.Builtin, "cd"), (Operator, "||"), (Error, "non-existance-cmd")],
|
|
|
|
)
|
2016-09-11 00:50:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_nested():
|
2018-08-30 09:18:49 -05:00
|
|
|
check_token(
|
|
|
|
'echo @("hello")',
|
|
|
|
[
|
|
|
|
(Name.Builtin, "echo"),
|
|
|
|
(Keyword, "@"),
|
|
|
|
(Punctuation, "("),
|
|
|
|
(String.Double, "hello"),
|
|
|
|
(Punctuation, ")"),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
check_token(
|
|
|
|
"print($(cd))",
|
|
|
|
[
|
|
|
|
(Keyword, "print"),
|
|
|
|
(Punctuation, "("),
|
|
|
|
(Keyword, "$"),
|
|
|
|
(Punctuation, "("),
|
|
|
|
(Name.Builtin, "cd"),
|
|
|
|
(Punctuation, ")"),
|
|
|
|
(Punctuation, ")"),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
check_token(
|
|
|
|
r'print(![echo "])\""])',
|
|
|
|
[
|
|
|
|
(Keyword, "print"),
|
|
|
|
(Keyword, "!"),
|
|
|
|
(Punctuation, "["),
|
|
|
|
(Name.Builtin, "echo"),
|
|
|
|
(String.Double, r'"])\""'),
|
|
|
|
(Punctuation, "]"),
|
|
|
|
],
|
|
|
|
)
|
2016-09-11 00:50:52 +08:00
|
|
|
|
|
|
|
|
2016-09-13 21:37:24 +08:00
|
|
|
def test_path(tmpdir):
|
2018-08-30 09:18:49 -05:00
|
|
|
test_dir = str(tmpdir.mkdir("xonsh-test-highlight-path"))
|
|
|
|
check_token(
|
|
|
|
"cd {}".format(test_dir), [(Name.Builtin, "cd"), (Name.Constant, test_dir)]
|
|
|
|
)
|
|
|
|
check_token(
|
|
|
|
"cd {}-xxx".format(test_dir),
|
|
|
|
[(Name.Builtin, "cd"), (Text, "{}-xxx".format(test_dir))],
|
|
|
|
)
|
|
|
|
check_token("cd X={}".format(test_dir), [(Name.Constant, test_dir)])
|
2016-09-11 16:09:10 +08:00
|
|
|
|
2018-09-13 14:03:35 -04:00
|
|
|
with builtins.__xonsh__.env.swap(AUTO_CD=True):
|
2016-09-13 11:29:49 +08:00
|
|
|
check_token(test_dir, [(Name.Constant, test_dir)])
|
|
|
|
|
2016-09-11 16:09:10 +08:00
|
|
|
|
2016-09-13 10:54:20 +08:00
|
|
|
def test_subproc_args():
|
2018-08-30 09:18:49 -05:00
|
|
|
check_token("cd 192.168.0.1", [(Text, "192.168.0.1")])
|
2016-09-13 10:54:20 +08:00
|
|
|
|
|
|
|
|
2016-09-11 16:09:10 +08:00
|
|
|
def test_backtick():
|
2018-08-30 09:18:49 -05:00
|
|
|
check_token(
|
|
|
|
r"echo g`.*\w+`",
|
|
|
|
[
|
|
|
|
(String.Affix, "g"),
|
|
|
|
(String.Backtick, "`"),
|
|
|
|
(String.Regex, "."),
|
|
|
|
(String.Regex, "*"),
|
|
|
|
(String.Escape, r"\w"),
|
|
|
|
],
|
|
|
|
)
|
2016-09-12 11:47:18 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_macro():
|
2018-08-30 09:18:49 -05:00
|
|
|
check_token(
|
|
|
|
r"g!(42, *, 65)",
|
|
|
|
[(Name, "g"), (Keyword, "!"), (Punctuation, "("), (Number.Integer, "42")],
|
|
|
|
)
|
|
|
|
check_token(
|
|
|
|
r"echo! hello world",
|
|
|
|
[(Name.Builtin, "echo"), (Keyword, "!"), (String, "hello world")],
|
|
|
|
)
|
|
|
|
check_token(
|
|
|
|
r"bash -c ! export var=42; echo $var",
|
|
|
|
[
|
|
|
|
(Name.Builtin, "bash"),
|
|
|
|
(Text, "-c"),
|
|
|
|
(Keyword, "!"),
|
|
|
|
(String, "export var=42; echo $var"),
|
|
|
|
],
|
|
|
|
)
|