2016-09-11 00:50:52 +08:00
|
|
|
"""Test XonshLexer for pygments"""
|
|
|
|
|
2016-09-11 16:17:56 +08:00
|
|
|
import pytest
|
2018-08-30 09:18:49 -05:00
|
|
|
from pygments.token import (
|
2022-01-31 21:26:34 +05:30
|
|
|
Error,
|
2018-08-30 09:18:49 -05:00
|
|
|
Keyword,
|
2022-01-31 21:26:34 +05:30
|
|
|
Literal,
|
2018-08-30 09:18:49 -05:00
|
|
|
Name,
|
|
|
|
Number,
|
|
|
|
Operator,
|
|
|
|
Punctuation,
|
2022-01-31 21:26:34 +05:30
|
|
|
String,
|
2018-08-30 09:18:49 -05:00
|
|
|
Text,
|
|
|
|
)
|
2016-09-11 00:50:52 +08:00
|
|
|
|
2019-12-26 05:51:48 -05:00
|
|
|
from xonsh.environ import LsColors
|
2022-01-31 21:26:34 +05:30
|
|
|
from xonsh.events import EventManager, events
|
|
|
|
from xonsh.pyghooks import Color, XonshLexer, XonshStyle, on_lscolors_change
|
2022-03-24 00:46:50 +05:30
|
|
|
from xonsh.pytest.tools import DummyShell, skip_if_on_windows
|
2016-09-11 00:50:52 +08:00
|
|
|
|
|
|
|
|
2022-01-08 04:03:22 +05:30
|
|
|
@pytest.fixture
|
|
|
|
def xsh(xession, monkeypatch):
|
|
|
|
for key in ("cd", "bash"):
|
|
|
|
monkeypatch.setitem(xession.aliases, key, lambda *args, **kwargs: None)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
|
def check_token(xsh):
|
|
|
|
def factory(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:
|
|
|
|
msg = f"Token {tk!r} missing: {list(lx.get_tokens(code))!r}"
|
|
|
|
pytest.fail(msg)
|
2016-09-11 00:50:52 +08:00
|
|
|
break
|
|
|
|
|
2022-01-08 04:03:22 +05:30
|
|
|
return factory
|
2016-09-11 00:50:52 +08:00
|
|
|
|
|
|
|
|
2022-01-08 04:03:22 +05:30
|
|
|
_cases = {
|
|
|
|
"ls": {
|
|
|
|
"ls -al": [
|
|
|
|
(Name.Builtin, "ls"),
|
|
|
|
],
|
|
|
|
},
|
|
|
|
"ls-bin": {
|
|
|
|
"/bin/ls -al": [
|
|
|
|
(Name.Builtin, "/bin/ls"),
|
|
|
|
],
|
|
|
|
},
|
|
|
|
"print": {
|
|
|
|
'print("hello")': [
|
2019-10-23 18:38:16 -04:00
|
|
|
(Name.Builtin, "print"),
|
|
|
|
(Punctuation, "("),
|
|
|
|
(Literal.String.Double, '"'),
|
|
|
|
(Literal.String.Double, "hello"),
|
|
|
|
(Literal.String.Double, '"'),
|
|
|
|
(Punctuation, ")"),
|
2023-01-11 17:19:08 -05:00
|
|
|
(Text.Whitespace, "\n"),
|
2022-01-08 04:03:22 +05:30
|
|
|
]
|
|
|
|
},
|
|
|
|
"invalid-cmd": {
|
|
|
|
"non-existance-cmd -al": [
|
|
|
|
(Name, "non"),
|
2019-10-23 18:38:16 -04:00
|
|
|
],
|
2022-01-08 04:03:22 +05:30
|
|
|
"![non-existance-cmd -al]": [
|
|
|
|
(Error, "non-existance-cmd"),
|
|
|
|
],
|
|
|
|
"for i in range(10):": [
|
|
|
|
(Keyword, "for"),
|
|
|
|
],
|
|
|
|
"(1, )": [
|
|
|
|
(Punctuation, "("),
|
|
|
|
(Number.Integer, "1"),
|
|
|
|
],
|
|
|
|
},
|
|
|
|
"multi-cmd": {
|
|
|
|
"cd && cd": [
|
|
|
|
(Name.Builtin, "cd"),
|
|
|
|
(Operator, "&&"),
|
|
|
|
(Name.Builtin, "cd"),
|
|
|
|
],
|
|
|
|
"cd || non-existance-cmd": [
|
|
|
|
(Name.Builtin, "cd"),
|
|
|
|
(Operator, "||"),
|
|
|
|
(Error, "non-existance-cmd"),
|
|
|
|
],
|
|
|
|
},
|
|
|
|
"nested": {
|
|
|
|
'echo @("hello")': [
|
2018-08-30 09:18:49 -05:00
|
|
|
(Name.Builtin, "echo"),
|
|
|
|
(Keyword, "@"),
|
|
|
|
(Punctuation, "("),
|
|
|
|
(String.Double, "hello"),
|
|
|
|
(Punctuation, ")"),
|
|
|
|
],
|
2022-01-08 04:03:22 +05:30
|
|
|
"print($(cd))": [
|
2019-10-23 18:38:16 -04:00
|
|
|
(Name.Builtin, "print"),
|
2018-08-30 09:18:49 -05:00
|
|
|
(Punctuation, "("),
|
|
|
|
(Keyword, "$"),
|
|
|
|
(Punctuation, "("),
|
|
|
|
(Name.Builtin, "cd"),
|
|
|
|
(Punctuation, ")"),
|
|
|
|
(Punctuation, ")"),
|
2023-01-11 17:19:08 -05:00
|
|
|
(Text.Whitespace, "\n"),
|
2018-08-30 09:18:49 -05:00
|
|
|
],
|
2022-01-08 04:03:22 +05:30
|
|
|
r'print(![echo "])\""])': [
|
2019-10-23 18:38:16 -04:00
|
|
|
(Name.Builtin, "print"),
|
|
|
|
(Punctuation, "("),
|
2018-08-30 09:18:49 -05:00
|
|
|
(Keyword, "!"),
|
|
|
|
(Punctuation, "["),
|
|
|
|
(Name.Builtin, "echo"),
|
2019-10-23 18:38:16 -04:00
|
|
|
(Text, " "),
|
|
|
|
(Literal.String.Double, '"])\\""'),
|
2018-08-30 09:18:49 -05:00
|
|
|
(Punctuation, "]"),
|
2019-10-23 18:38:16 -04:00
|
|
|
(Punctuation, ")"),
|
2023-01-11 17:19:08 -05:00
|
|
|
(Text.Whitespace, "\n"),
|
2018-08-30 09:18:49 -05:00
|
|
|
],
|
2022-01-08 04:03:22 +05:30
|
|
|
},
|
|
|
|
"subproc-args": {
|
|
|
|
"cd 192.168.0.1": [
|
|
|
|
(Text, "192.168.0.1"),
|
|
|
|
],
|
|
|
|
},
|
|
|
|
"backtick": {
|
|
|
|
r"echo g`.*\w+`": [
|
|
|
|
(String.Affix, "g"),
|
|
|
|
(String.Backtick, "`"),
|
|
|
|
(String.Regex, "."),
|
|
|
|
(String.Regex, "*"),
|
|
|
|
(String.Escape, r"\w"),
|
|
|
|
],
|
|
|
|
},
|
|
|
|
"macro": {
|
|
|
|
r"g!(42, *, 65)": [
|
|
|
|
(Name, "g"),
|
|
|
|
(Keyword, "!"),
|
|
|
|
(Punctuation, "("),
|
|
|
|
(Number.Integer, "42"),
|
|
|
|
],
|
|
|
|
r"echo! hello world": [
|
|
|
|
(Name.Builtin, "echo"),
|
|
|
|
(Keyword, "!"),
|
|
|
|
(String, "hello world"),
|
|
|
|
],
|
|
|
|
r"bash -c ! export var=42; echo $var": [
|
|
|
|
(Name.Builtin, "bash"),
|
|
|
|
(Text, "-c"),
|
|
|
|
(Keyword, "!"),
|
|
|
|
(String, "export var=42; echo $var"),
|
|
|
|
],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def _convert_cases():
|
|
|
|
for title, input_dict in _cases.items():
|
|
|
|
for idx, item in enumerate(input_dict.items()):
|
|
|
|
yield pytest.param(*item, id=f"{title}-{idx}")
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("inp, expected", list(_convert_cases()))
|
|
|
|
@skip_if_on_windows
|
|
|
|
def test_xonsh_lexer(inp, expected, check_token):
|
|
|
|
check_token(inp, expected)
|
2016-09-11 00:50:52 +08:00
|
|
|
|
2020-03-29 16:05:10 -04:00
|
|
|
|
|
|
|
# can't seem to get thie test to import pyghooks and define on_lscolors_change handler like live code does.
|
|
|
|
# so we declare the event handler directly here.
|
|
|
|
@pytest.fixture
|
|
|
|
def events_fxt():
|
|
|
|
return EventManager()
|
|
|
|
|
2020-04-18 10:44:27 -04:00
|
|
|
|
2020-03-14 22:33:48 -04:00
|
|
|
@pytest.fixture
|
2021-05-20 15:44:26 +05:30
|
|
|
def xonsh_builtins_ls_colors(xession, events_fxt):
|
|
|
|
xession.shell = DummyShell() # because load_command_cache zaps it.
|
|
|
|
xession.shell.shell_type = "prompt_toolkit"
|
2020-01-03 21:30:14 -05:00
|
|
|
lsc = LsColors(LsColors.default_settings)
|
2021-05-20 15:44:26 +05:30
|
|
|
xession.env["LS_COLORS"] = lsc # establish LS_COLORS before style.
|
|
|
|
xession.shell.shell.styler = XonshStyle() # default style
|
2019-12-26 05:51:48 -05:00
|
|
|
|
2020-03-29 16:05:10 -04:00
|
|
|
events.on_lscolors_change(on_lscolors_change)
|
2020-04-18 10:44:27 -04:00
|
|
|
|
2021-05-20 15:44:26 +05:30
|
|
|
yield xession
|
2020-03-14 22:33:48 -04:00
|
|
|
|
2020-04-18 10:44:27 -04:00
|
|
|
|
2020-03-14 22:33:48 -04:00
|
|
|
@skip_if_on_windows
|
2022-01-08 04:03:22 +05:30
|
|
|
def test_path(tmpdir, xonsh_builtins_ls_colors, check_token):
|
2018-08-30 09:18:49 -05:00
|
|
|
test_dir = str(tmpdir.mkdir("xonsh-test-highlight-path"))
|
2021-12-07 01:12:26 +05:30
|
|
|
check_token(f"cd {test_dir}", [(Name.Builtin, "cd"), (Color.BOLD_BLUE, test_dir)])
|
2018-08-30 09:18:49 -05:00
|
|
|
check_token(
|
2021-12-07 01:12:26 +05:30
|
|
|
f"cd {test_dir}-xxx",
|
|
|
|
[(Name.Builtin, "cd"), (Text, f"{test_dir}-xxx")],
|
2018-08-30 09:18:49 -05:00
|
|
|
)
|
2021-12-07 01:12:26 +05:30
|
|
|
check_token(f"cd X={test_dir}", [(Color.BOLD_BLUE, test_dir)])
|
2016-09-11 16:09:10 +08:00
|
|
|
|
2021-05-20 15:44:26 +05:30
|
|
|
with xonsh_builtins_ls_colors.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
|
|
|
|
2020-03-14 22:33:48 -04:00
|
|
|
@skip_if_on_windows
|
2022-01-08 04:03:22 +05:30
|
|
|
def test_color_on_lscolors_change(tmpdir, xonsh_builtins_ls_colors, check_token):
|
2020-03-14 22:33:48 -04:00
|
|
|
"""Verify colorizer returns Token.Text if file type not defined in LS_COLORS"""
|
|
|
|
|
2021-05-20 15:44:26 +05:30
|
|
|
lsc = xonsh_builtins_ls_colors.env["LS_COLORS"]
|
2020-03-14 22:33:48 -04:00
|
|
|
test_dir = str(tmpdir.mkdir("xonsh-test-highlight-path"))
|
|
|
|
|
2020-08-26 10:10:59 -05:00
|
|
|
lsc["di"] = ("GREEN",)
|
2020-03-14 22:33:48 -04:00
|
|
|
|
2021-12-07 01:12:26 +05:30
|
|
|
check_token(f"cd {test_dir}", [(Name.Builtin, "cd"), (Color.GREEN, test_dir)])
|
2020-03-14 22:33:48 -04:00
|
|
|
|
2020-08-26 10:10:59 -05:00
|
|
|
del lsc["di"]
|
2020-04-18 10:44:27 -04:00
|
|
|
|
2021-12-07 01:12:26 +05:30
|
|
|
check_token(f"cd {test_dir}", [(Name.Builtin, "cd"), (Text, test_dir)])
|