xonsh/tests/test_ptk_highlight.py
Noorhteen Raja NJ 38295a1dd9
Remove globals (#4280)
* refactor: remove usage of global variables in abbrevs.py

* chore: add flake8-mutable to prevent mutable defaults

* fix: abbrevs expand test

* refactor: add xonsh session singleton

* refactor: fix circular errors when using xonshSession as singleton

* refactor: remove black magicked builtin attributes

* style: black format tests as well

* refactor: update tests to use xonsh-session singleton

* refactor: update abbrevs to not use builtins

* test: remove DummyCommandsCache and patch orig class

* fix: failing test_command_completers

* test: use monkeypatch to update xession fixture

* fix: failing test_pipelines

* fix: failing test_main

* chore: run test suit as single invocation

* test: fix tests/test_xonsh.xsh

* refactor: remove builtins from docs/conf.py

* fix: mypy error in jobs

* fix: test error from test_main

* test: close xession error in test_command_completers

* chore: use pytest-cov for reporting coverage

this will include subprocess calls, and will increase coverage

* style:
2021-05-20 13:14:26 +03:00

235 lines
5.9 KiB
Python

# -*- coding: utf-8 -*-
"""Test XonshLexer for pygments"""
import gc
import pytest
from pygments.token import (
Keyword,
Name,
String,
Error,
Number,
Operator,
Punctuation,
Text,
Literal,
)
from tools import skip_if_on_windows
from xonsh.platform import ON_WINDOWS
from xonsh.built_ins import XSH
from xonsh.pyghooks import XonshLexer, Color, XonshStyle, on_lscolors_change
from xonsh.environ import LsColors
from xonsh.events import events, EventManager
from tools import DummyShell
@pytest.fixture(autouse=True)
def load_command_cache(xession):
gc.collect()
XSH.unload()
XSH.load()
if ON_WINDOWS:
for key in ("cd", "bash"):
xession.aliases[key] = lambda *args, **kwargs: None
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:
msg = "Token {!r} missing: {!r}".format(tk, list(lx.get_tokens(code)))
pytest.fail(msg)
break
@skip_if_on_windows
def test_ls():
check_token("ls -al", [(Name.Builtin, "ls")])
@skip_if_on_windows
def test_bin_ls():
check_token("/bin/ls -al", [(Name.Builtin, "/bin/ls")])
@skip_if_on_windows
def test_py_print():
check_token(
'print("hello")',
[
(Name.Builtin, "print"),
(Punctuation, "("),
(Literal.String.Double, '"'),
(Literal.String.Double, "hello"),
(Literal.String.Double, '"'),
(Punctuation, ")"),
(Text, "\n"),
],
)
@skip_if_on_windows
def test_invalid_cmd():
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")])
@skip_if_on_windows
def test_multi_cmd():
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")],
)
@skip_if_on_windows
def test_nested():
check_token(
'echo @("hello")',
[
(Name.Builtin, "echo"),
(Keyword, "@"),
(Punctuation, "("),
(String.Double, "hello"),
(Punctuation, ")"),
],
)
check_token(
"print($(cd))",
[
(Name.Builtin, "print"),
(Punctuation, "("),
(Keyword, "$"),
(Punctuation, "("),
(Name.Builtin, "cd"),
(Punctuation, ")"),
(Punctuation, ")"),
(Text, "\n"),
],
)
check_token(
r'print(![echo "])\""])',
[
(Name.Builtin, "print"),
(Punctuation, "("),
(Keyword, "!"),
(Punctuation, "["),
(Name.Builtin, "echo"),
(Text, " "),
(Literal.String.Double, '"])\\""'),
(Punctuation, "]"),
(Punctuation, ")"),
(Text, "\n"),
],
)
# 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()
@pytest.fixture
def xonsh_builtins_ls_colors(xession, events_fxt):
xession.shell = DummyShell() # because load_command_cache zaps it.
xession.shell.shell_type = "prompt_toolkit"
lsc = LsColors(LsColors.default_settings)
xession.env["LS_COLORS"] = lsc # establish LS_COLORS before style.
xession.shell.shell.styler = XonshStyle() # default style
events.on_lscolors_change(on_lscolors_change)
yield xession
@skip_if_on_windows
def test_path(tmpdir, xonsh_builtins_ls_colors):
test_dir = str(tmpdir.mkdir("xonsh-test-highlight-path"))
check_token(
"cd {}".format(test_dir), [(Name.Builtin, "cd"), (Color.BOLD_BLUE, test_dir)]
)
check_token(
"cd {}-xxx".format(test_dir),
[(Name.Builtin, "cd"), (Text, "{}-xxx".format(test_dir))],
)
check_token("cd X={}".format(test_dir), [(Color.BOLD_BLUE, test_dir)])
with xonsh_builtins_ls_colors.env.swap(AUTO_CD=True):
check_token(test_dir, [(Name.Constant, test_dir)])
@skip_if_on_windows
def test_color_on_lscolors_change(tmpdir, xonsh_builtins_ls_colors):
"""Verify colorizer returns Token.Text if file type not defined in LS_COLORS"""
lsc = xonsh_builtins_ls_colors.env["LS_COLORS"]
test_dir = str(tmpdir.mkdir("xonsh-test-highlight-path"))
lsc["di"] = ("GREEN",)
check_token(
"cd {}".format(test_dir), [(Name.Builtin, "cd"), (Color.GREEN, test_dir)]
)
del lsc["di"]
check_token("cd {}".format(test_dir), [(Name.Builtin, "cd"), (Text, test_dir)])
@skip_if_on_windows
def test_subproc_args():
check_token("cd 192.168.0.1", [(Text, "192.168.0.1")])
@skip_if_on_windows
def test_backtick():
check_token(
r"echo g`.*\w+`",
[
(String.Affix, "g"),
(String.Backtick, "`"),
(String.Regex, "."),
(String.Regex, "*"),
(String.Escape, r"\w"),
],
)
@skip_if_on_windows
def test_macro():
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"),
],
)