xonsh/tests/test_ptk_highlight.py

224 lines
6 KiB
Python
Raw Normal View History

"""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,
)
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
update test xsh usage (#4581) * todo * test: remove usage of DummyEnv and setting .env attribute on xession fixture one step closer to making too much of tweaking to xession during tests * test: fix tests vox and gitstatus-prompt * docs: update test-fixture usage * fix: import flake8 error * test: remove direct access to XSH in tests * test: remove usage of XSH in test files * todo * test: use tmp-dir to create stubs * refactor: use fixture factory to mock out XonshSession * refactor: remove direct access of XSH from functions * refactor: remove direct access of XSH from functions * fix: qa checks * refactor: rename variables to match their values * test: update failing tests because it had no PATH set previously * fix: remove builtins usage from pyghooks.py * style: * refactor: update tests to use fixtures * fix: env varialbe is setup per function some tests accidentally update the env variables and that is leaking into next tests * fix: failing vox tests * test: set commands_cache per test * test: fix failing tests * fix: failing tests on linux ptk-highlight * fix: failing tests on Windows cwd-prompt * test: copy env as to not affect original object * fix: lazily evaluate cmds-cache in pyghooks * test: fix failing tests * fix: qa errors import * test: set commands-cache per test while caching path results * test: speedup test_thread_local_swap * fix: failing tests on windows * refactor: Execer doesn't control session * refactor: XSH.unload will take care of reversing builtins attrs set * test: use env.update over monkeypatch * Revert "test: use env.update over monkeypatch" This reverts commit 010a5022247a098f1741966b8af1bf758663480e.
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)
break
update test xsh usage (#4581) * todo * test: remove usage of DummyEnv and setting .env attribute on xession fixture one step closer to making too much of tweaking to xession during tests * test: fix tests vox and gitstatus-prompt * docs: update test-fixture usage * fix: import flake8 error * test: remove direct access to XSH in tests * test: remove usage of XSH in test files * todo * test: use tmp-dir to create stubs * refactor: use fixture factory to mock out XonshSession * refactor: remove direct access of XSH from functions * refactor: remove direct access of XSH from functions * fix: qa checks * refactor: rename variables to match their values * test: update failing tests because it had no PATH set previously * fix: remove builtins usage from pyghooks.py * style: * refactor: update tests to use fixtures * fix: env varialbe is setup per function some tests accidentally update the env variables and that is leaking into next tests * fix: failing vox tests * test: set commands_cache per test * test: fix failing tests * fix: failing tests on linux ptk-highlight * fix: failing tests on Windows cwd-prompt * test: copy env as to not affect original object * fix: lazily evaluate cmds-cache in pyghooks * test: fix failing tests * fix: qa errors import * test: set commands-cache per test while caching path results * test: speedup test_thread_local_swap * fix: failing tests on windows * refactor: Execer doesn't control session * refactor: XSH.unload will take care of reversing builtins attrs set * test: use env.update over monkeypatch * Revert "test: use env.update over monkeypatch" This reverts commit 010a5022247a098f1741966b8af1bf758663480e.
2022-01-08 04:03:22 +05:30
return factory
update test xsh usage (#4581) * todo * test: remove usage of DummyEnv and setting .env attribute on xession fixture one step closer to making too much of tweaking to xession during tests * test: fix tests vox and gitstatus-prompt * docs: update test-fixture usage * fix: import flake8 error * test: remove direct access to XSH in tests * test: remove usage of XSH in test files * todo * test: use tmp-dir to create stubs * refactor: use fixture factory to mock out XonshSession * refactor: remove direct access of XSH from functions * refactor: remove direct access of XSH from functions * fix: qa checks * refactor: rename variables to match their values * test: update failing tests because it had no PATH set previously * fix: remove builtins usage from pyghooks.py * style: * refactor: update tests to use fixtures * fix: env varialbe is setup per function some tests accidentally update the env variables and that is leaking into next tests * fix: failing vox tests * test: set commands_cache per test * test: fix failing tests * fix: failing tests on linux ptk-highlight * fix: failing tests on Windows cwd-prompt * test: copy env as to not affect original object * fix: lazily evaluate cmds-cache in pyghooks * test: fix failing tests * fix: qa errors import * test: set commands-cache per test while caching path results * test: speedup test_thread_local_swap * fix: failing tests on windows * refactor: Execer doesn't control session * refactor: XSH.unload will take care of reversing builtins attrs set * test: use env.update over monkeypatch * Revert "test: use env.update over monkeypatch" This reverts commit 010a5022247a098f1741966b8af1bf758663480e.
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, ")"),
(Text.Whitespace, "\n"),
update test xsh usage (#4581) * todo * test: remove usage of DummyEnv and setting .env attribute on xession fixture one step closer to making too much of tweaking to xession during tests * test: fix tests vox and gitstatus-prompt * docs: update test-fixture usage * fix: import flake8 error * test: remove direct access to XSH in tests * test: remove usage of XSH in test files * todo * test: use tmp-dir to create stubs * refactor: use fixture factory to mock out XonshSession * refactor: remove direct access of XSH from functions * refactor: remove direct access of XSH from functions * fix: qa checks * refactor: rename variables to match their values * test: update failing tests because it had no PATH set previously * fix: remove builtins usage from pyghooks.py * style: * refactor: update tests to use fixtures * fix: env varialbe is setup per function some tests accidentally update the env variables and that is leaking into next tests * fix: failing vox tests * test: set commands_cache per test * test: fix failing tests * fix: failing tests on linux ptk-highlight * fix: failing tests on Windows cwd-prompt * test: copy env as to not affect original object * fix: lazily evaluate cmds-cache in pyghooks * test: fix failing tests * fix: qa errors import * test: set commands-cache per test while caching path results * test: speedup test_thread_local_swap * fix: failing tests on windows * refactor: Execer doesn't control session * refactor: XSH.unload will take care of reversing builtins attrs set * test: use env.update over monkeypatch * Revert "test: use env.update over monkeypatch" This reverts commit 010a5022247a098f1741966b8af1bf758663480e.
2022-01-08 04:03:22 +05:30
]
},
"invalid-cmd": {
"non-existance-cmd -al": [
(Name, "non"),
2019-10-23 18:38:16 -04:00
],
update test xsh usage (#4581) * todo * test: remove usage of DummyEnv and setting .env attribute on xession fixture one step closer to making too much of tweaking to xession during tests * test: fix tests vox and gitstatus-prompt * docs: update test-fixture usage * fix: import flake8 error * test: remove direct access to XSH in tests * test: remove usage of XSH in test files * todo * test: use tmp-dir to create stubs * refactor: use fixture factory to mock out XonshSession * refactor: remove direct access of XSH from functions * refactor: remove direct access of XSH from functions * fix: qa checks * refactor: rename variables to match their values * test: update failing tests because it had no PATH set previously * fix: remove builtins usage from pyghooks.py * style: * refactor: update tests to use fixtures * fix: env varialbe is setup per function some tests accidentally update the env variables and that is leaking into next tests * fix: failing vox tests * test: set commands_cache per test * test: fix failing tests * fix: failing tests on linux ptk-highlight * fix: failing tests on Windows cwd-prompt * test: copy env as to not affect original object * fix: lazily evaluate cmds-cache in pyghooks * test: fix failing tests * fix: qa errors import * test: set commands-cache per test while caching path results * test: speedup test_thread_local_swap * fix: failing tests on windows * refactor: Execer doesn't control session * refactor: XSH.unload will take care of reversing builtins attrs set * test: use env.update over monkeypatch * Revert "test: use env.update over monkeypatch" This reverts commit 010a5022247a098f1741966b8af1bf758663480e.
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, ")"),
],
update test xsh usage (#4581) * todo * test: remove usage of DummyEnv and setting .env attribute on xession fixture one step closer to making too much of tweaking to xession during tests * test: fix tests vox and gitstatus-prompt * docs: update test-fixture usage * fix: import flake8 error * test: remove direct access to XSH in tests * test: remove usage of XSH in test files * todo * test: use tmp-dir to create stubs * refactor: use fixture factory to mock out XonshSession * refactor: remove direct access of XSH from functions * refactor: remove direct access of XSH from functions * fix: qa checks * refactor: rename variables to match their values * test: update failing tests because it had no PATH set previously * fix: remove builtins usage from pyghooks.py * style: * refactor: update tests to use fixtures * fix: env varialbe is setup per function some tests accidentally update the env variables and that is leaking into next tests * fix: failing vox tests * test: set commands_cache per test * test: fix failing tests * fix: failing tests on linux ptk-highlight * fix: failing tests on Windows cwd-prompt * test: copy env as to not affect original object * fix: lazily evaluate cmds-cache in pyghooks * test: fix failing tests * fix: qa errors import * test: set commands-cache per test while caching path results * test: speedup test_thread_local_swap * fix: failing tests on windows * refactor: Execer doesn't control session * refactor: XSH.unload will take care of reversing builtins attrs set * test: use env.update over monkeypatch * Revert "test: use env.update over monkeypatch" This reverts commit 010a5022247a098f1741966b8af1bf758663480e.
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, ")"),
(Text.Whitespace, "\n"),
2018-08-30 09:18:49 -05:00
],
update test xsh usage (#4581) * todo * test: remove usage of DummyEnv and setting .env attribute on xession fixture one step closer to making too much of tweaking to xession during tests * test: fix tests vox and gitstatus-prompt * docs: update test-fixture usage * fix: import flake8 error * test: remove direct access to XSH in tests * test: remove usage of XSH in test files * todo * test: use tmp-dir to create stubs * refactor: use fixture factory to mock out XonshSession * refactor: remove direct access of XSH from functions * refactor: remove direct access of XSH from functions * fix: qa checks * refactor: rename variables to match their values * test: update failing tests because it had no PATH set previously * fix: remove builtins usage from pyghooks.py * style: * refactor: update tests to use fixtures * fix: env varialbe is setup per function some tests accidentally update the env variables and that is leaking into next tests * fix: failing vox tests * test: set commands_cache per test * test: fix failing tests * fix: failing tests on linux ptk-highlight * fix: failing tests on Windows cwd-prompt * test: copy env as to not affect original object * fix: lazily evaluate cmds-cache in pyghooks * test: fix failing tests * fix: qa errors import * test: set commands-cache per test while caching path results * test: speedup test_thread_local_swap * fix: failing tests on windows * refactor: Execer doesn't control session * refactor: XSH.unload will take care of reversing builtins attrs set * test: use env.update over monkeypatch * Revert "test: use env.update over monkeypatch" This reverts commit 010a5022247a098f1741966b8af1bf758663480e.
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, ")"),
(Text.Whitespace, "\n"),
2018-08-30 09:18:49 -05:00
],
update test xsh usage (#4581) * todo * test: remove usage of DummyEnv and setting .env attribute on xession fixture one step closer to making too much of tweaking to xession during tests * test: fix tests vox and gitstatus-prompt * docs: update test-fixture usage * fix: import flake8 error * test: remove direct access to XSH in tests * test: remove usage of XSH in test files * todo * test: use tmp-dir to create stubs * refactor: use fixture factory to mock out XonshSession * refactor: remove direct access of XSH from functions * refactor: remove direct access of XSH from functions * fix: qa checks * refactor: rename variables to match their values * test: update failing tests because it had no PATH set previously * fix: remove builtins usage from pyghooks.py * style: * refactor: update tests to use fixtures * fix: env varialbe is setup per function some tests accidentally update the env variables and that is leaking into next tests * fix: failing vox tests * test: set commands_cache per test * test: fix failing tests * fix: failing tests on linux ptk-highlight * fix: failing tests on Windows cwd-prompt * test: copy env as to not affect original object * fix: lazily evaluate cmds-cache in pyghooks * test: fix failing tests * fix: qa errors import * test: set commands-cache per test while caching path results * test: speedup test_thread_local_swap * fix: failing tests on windows * refactor: Execer doesn't control session * refactor: XSH.unload will take care of reversing builtins attrs set * test: use env.update over monkeypatch * Revert "test: use env.update over monkeypatch" This reverts commit 010a5022247a098f1741966b8af1bf758663480e.
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)
# 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
update test xsh usage (#4581) * todo * test: remove usage of DummyEnv and setting .env attribute on xession fixture one step closer to making too much of tweaking to xession during tests * test: fix tests vox and gitstatus-prompt * docs: update test-fixture usage * fix: import flake8 error * test: remove direct access to XSH in tests * test: remove usage of XSH in test files * todo * test: use tmp-dir to create stubs * refactor: use fixture factory to mock out XonshSession * refactor: remove direct access of XSH from functions * refactor: remove direct access of XSH from functions * fix: qa checks * refactor: rename variables to match their values * test: update failing tests because it had no PATH set previously * fix: remove builtins usage from pyghooks.py * style: * refactor: update tests to use fixtures * fix: env varialbe is setup per function some tests accidentally update the env variables and that is leaking into next tests * fix: failing vox tests * test: set commands_cache per test * test: fix failing tests * fix: failing tests on linux ptk-highlight * fix: failing tests on Windows cwd-prompt * test: copy env as to not affect original object * fix: lazily evaluate cmds-cache in pyghooks * test: fix failing tests * fix: qa errors import * test: set commands-cache per test while caching path results * test: speedup test_thread_local_swap * fix: failing tests on windows * refactor: Execer doesn't control session * refactor: XSH.unload will take care of reversing builtins attrs set * test: use env.update over monkeypatch * Revert "test: use env.update over monkeypatch" This reverts commit 010a5022247a098f1741966b8af1bf758663480e.
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"))
check_token(f"cd {test_dir}", [(Name.Builtin, "cd"), (Color.BOLD_BLUE, test_dir)])
2018-08-30 09:18:49 -05:00
check_token(
f"cd {test_dir}-xxx",
[(Name.Builtin, "cd"), (Text, f"{test_dir}-xxx")],
2018-08-30 09:18:49 -05:00
)
check_token(f"cd X={test_dir}", [(Color.BOLD_BLUE, test_dir)])
2016-09-11 16:09:10 +08:00
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
@skip_if_on_windows
update test xsh usage (#4581) * todo * test: remove usage of DummyEnv and setting .env attribute on xession fixture one step closer to making too much of tweaking to xession during tests * test: fix tests vox and gitstatus-prompt * docs: update test-fixture usage * fix: import flake8 error * test: remove direct access to XSH in tests * test: remove usage of XSH in test files * todo * test: use tmp-dir to create stubs * refactor: use fixture factory to mock out XonshSession * refactor: remove direct access of XSH from functions * refactor: remove direct access of XSH from functions * fix: qa checks * refactor: rename variables to match their values * test: update failing tests because it had no PATH set previously * fix: remove builtins usage from pyghooks.py * style: * refactor: update tests to use fixtures * fix: env varialbe is setup per function some tests accidentally update the env variables and that is leaking into next tests * fix: failing vox tests * test: set commands_cache per test * test: fix failing tests * fix: failing tests on linux ptk-highlight * fix: failing tests on Windows cwd-prompt * test: copy env as to not affect original object * fix: lazily evaluate cmds-cache in pyghooks * test: fix failing tests * fix: qa errors import * test: set commands-cache per test while caching path results * test: speedup test_thread_local_swap * fix: failing tests on windows * refactor: Execer doesn't control session * refactor: XSH.unload will take care of reversing builtins attrs set * test: use env.update over monkeypatch * Revert "test: use env.update over monkeypatch" This reverts commit 010a5022247a098f1741966b8af1bf758663480e.
2022-01-08 04:03:22 +05:30
def test_color_on_lscolors_change(tmpdir, xonsh_builtins_ls_colors, check_token):
"""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"))
2020-08-26 10:10:59 -05:00
lsc["di"] = ("GREEN",)
check_token(f"cd {test_dir}", [(Name.Builtin, "cd"), (Color.GREEN, test_dir)])
2020-08-26 10:10:59 -05:00
del lsc["di"]
check_token(f"cd {test_dir}", [(Name.Builtin, "cd"), (Text, test_dir)])