2019-03-11 18:34:54 -04:00
|
|
|
"""Tests pygments hooks."""
|
|
|
|
import pytest
|
2019-12-26 05:51:48 -05:00
|
|
|
import os
|
|
|
|
import stat
|
2019-03-11 18:34:54 -04:00
|
|
|
|
2019-12-26 05:51:48 -05:00
|
|
|
from tempfile import TemporaryDirectory
|
2019-03-11 18:34:54 -04:00
|
|
|
|
2020-01-03 21:30:14 -05:00
|
|
|
from xonsh.pyghooks import (
|
|
|
|
XonshStyle,
|
|
|
|
Color,
|
|
|
|
color_name_to_pygments_code,
|
|
|
|
code_by_name,
|
|
|
|
color_file,
|
|
|
|
file_color_tokens,
|
|
|
|
)
|
2019-12-26 05:51:48 -05:00
|
|
|
|
|
|
|
from xonsh.environ import LsColors
|
2020-05-05 06:42:28 -04:00
|
|
|
from tools import skip_if_on_windows
|
2019-03-11 18:34:54 -04:00
|
|
|
|
2020-01-03 21:30:14 -05:00
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def xonsh_builtins_LS_COLORS(xonsh_builtins):
|
|
|
|
"""Xonsh environment including LS_COLORS"""
|
|
|
|
e = xonsh_builtins.__xonsh__.env
|
|
|
|
lsc = LsColors(LsColors.default_settings)
|
|
|
|
xonsh_builtins.__xonsh__.env["LS_COLORS"] = lsc
|
2020-04-18 10:44:27 -04:00
|
|
|
xonsh_builtins.__xonsh__.shell.shell_type = "prompt_toolkit"
|
2020-01-03 21:30:14 -05:00
|
|
|
# styler = XonshStyle() # default style
|
|
|
|
# xonsh_builtins.__xonsh__.shell.shell.styler = styler
|
|
|
|
# can't really instantiate XonshStyle separate from a shell??
|
|
|
|
|
|
|
|
yield xonsh_builtins
|
|
|
|
xonsh_builtins.__xonsh__.env = e
|
|
|
|
|
|
|
|
|
2019-03-11 18:34:54 -04:00
|
|
|
DEFAULT_STYLES = {
|
|
|
|
# Reset
|
2019-03-12 18:36:18 -04:00
|
|
|
Color.NO_COLOR: "noinherit", # Text Reset
|
2019-03-11 18:34:54 -04:00
|
|
|
# Regular Colors
|
2019-03-12 18:36:18 -04:00
|
|
|
Color.BLACK: "ansiblack",
|
|
|
|
Color.BLUE: "ansiblue",
|
|
|
|
Color.CYAN: "ansicyan",
|
|
|
|
Color.GREEN: "ansigreen",
|
|
|
|
Color.PURPLE: "ansimagenta",
|
|
|
|
Color.RED: "ansired",
|
|
|
|
Color.WHITE: "ansigray",
|
|
|
|
Color.YELLOW: "ansiyellow",
|
|
|
|
Color.INTENSE_BLACK: "ansibrightblack",
|
|
|
|
Color.INTENSE_BLUE: "ansibrightblue",
|
|
|
|
Color.INTENSE_CYAN: "ansibrightcyan",
|
|
|
|
Color.INTENSE_GREEN: "ansibrightgreen",
|
|
|
|
Color.INTENSE_PURPLE: "ansibrightmagenta",
|
|
|
|
Color.INTENSE_RED: "ansibrightred",
|
|
|
|
Color.INTENSE_WHITE: "ansiwhite",
|
|
|
|
Color.INTENSE_YELLOW: "ansibrightyellow",
|
2019-03-11 18:34:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"name, exp",
|
|
|
|
[
|
|
|
|
("NO_COLOR", "noinherit"),
|
|
|
|
("RED", "ansired"),
|
|
|
|
("BACKGROUND_RED", "bg:ansired"),
|
|
|
|
("BACKGROUND_INTENSE_RED", "bg:ansibrightred"),
|
|
|
|
("BOLD_RED", "bold ansired"),
|
|
|
|
("UNDERLINE_RED", "underline ansired"),
|
|
|
|
("BOLD_UNDERLINE_RED", "bold underline ansired"),
|
|
|
|
("UNDERLINE_BOLD_RED", "underline bold ansired"),
|
|
|
|
# test unsupported modifiers
|
|
|
|
("BOLD_FAINT_RED", "bold ansired"),
|
|
|
|
("BOLD_SLOWBLINK_RED", "bold ansired"),
|
|
|
|
("BOLD_FASTBLINK_RED", "bold ansired"),
|
|
|
|
("BOLD_INVERT_RED", "bold ansired"),
|
|
|
|
("BOLD_CONCEAL_RED", "bold ansired"),
|
|
|
|
("BOLD_STRIKETHROUGH_RED", "bold ansired"),
|
|
|
|
# test hexes
|
|
|
|
("#000", "#000"),
|
|
|
|
("#000000", "#000000"),
|
|
|
|
("BACKGROUND_#000", "bg:#000"),
|
|
|
|
("BACKGROUND_#000000", "bg:#000000"),
|
|
|
|
("BG#000", "bg:#000"),
|
|
|
|
("bg#000000", "bg:#000000"),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_color_name_to_pygments_code(name, exp):
|
|
|
|
styles = DEFAULT_STYLES.copy()
|
|
|
|
obs = color_name_to_pygments_code(name, styles)
|
|
|
|
assert obs == exp
|
2019-03-12 18:36:18 -04:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"name, exp",
|
|
|
|
[
|
|
|
|
("NO_COLOR", "noinherit"),
|
|
|
|
("RED", "ansired"),
|
|
|
|
("BACKGROUND_RED", "bg:ansired"),
|
|
|
|
("BACKGROUND_INTENSE_RED", "bg:ansibrightred"),
|
|
|
|
("BOLD_RED", "bold ansired"),
|
|
|
|
("UNDERLINE_RED", "underline ansired"),
|
|
|
|
("BOLD_UNDERLINE_RED", "bold underline ansired"),
|
|
|
|
("UNDERLINE_BOLD_RED", "underline bold ansired"),
|
|
|
|
# test unsupported modifiers
|
|
|
|
("BOLD_FAINT_RED", "bold ansired"),
|
|
|
|
("BOLD_SLOWBLINK_RED", "bold ansired"),
|
|
|
|
("BOLD_FASTBLINK_RED", "bold ansired"),
|
|
|
|
("BOLD_INVERT_RED", "bold ansired"),
|
|
|
|
("BOLD_CONCEAL_RED", "bold ansired"),
|
|
|
|
("BOLD_STRIKETHROUGH_RED", "bold ansired"),
|
|
|
|
# test hexes
|
|
|
|
("#000", "#000"),
|
|
|
|
("#000000", "#000000"),
|
|
|
|
("BACKGROUND_#000", "bg:#000"),
|
|
|
|
("BACKGROUND_#000000", "bg:#000000"),
|
|
|
|
("BG#000", "bg:#000"),
|
|
|
|
("bg#000000", "bg:#000000"),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_code_by_name(name, exp):
|
|
|
|
styles = DEFAULT_STYLES.copy()
|
|
|
|
obs = code_by_name(name, styles)
|
|
|
|
assert obs == exp
|
2019-12-26 05:51:48 -05:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2020-01-03 21:30:14 -05:00
|
|
|
"in_tuple, exp_ct, exp_ansi_colors",
|
|
|
|
[
|
|
|
|
(("NO_COLOR",), Color.NO_COLOR, "noinherit"),
|
|
|
|
(("GREEN",), Color.GREEN, "ansigreen"),
|
|
|
|
(("BOLD_RED",), Color.BOLD_RED, "bold ansired"),
|
2020-01-04 13:56:02 -05:00
|
|
|
(
|
|
|
|
("BACKGROUND_BLACK", "BOLD_GREEN"),
|
|
|
|
Color.BACKGROUND_BLACK__BOLD_GREEN,
|
|
|
|
"bg:ansiblack bold ansigreen",
|
|
|
|
),
|
2020-01-03 21:30:14 -05:00
|
|
|
],
|
2019-12-26 05:51:48 -05:00
|
|
|
)
|
2020-01-04 13:56:02 -05:00
|
|
|
def test_color_token_by_name(
|
|
|
|
in_tuple, exp_ct, exp_ansi_colors, xonsh_builtins_LS_COLORS
|
|
|
|
):
|
2019-12-26 05:51:48 -05:00
|
|
|
from xonsh.pyghooks import XonshStyle, color_token_by_name
|
2020-01-04 09:55:58 -05:00
|
|
|
|
2020-01-03 21:30:14 -05:00
|
|
|
xs = XonshStyle()
|
|
|
|
styles = xs.styles
|
|
|
|
ct = color_token_by_name(in_tuple, styles)
|
|
|
|
ansi_colors = styles[ct] # if keyerror, ct was not cached
|
2019-12-26 05:51:48 -05:00
|
|
|
assert ct == exp_ct, "returned color token is right"
|
|
|
|
assert ansi_colors == exp_ansi_colors, "color token mapped to correct color string"
|
|
|
|
|
|
|
|
|
2020-01-03 21:30:14 -05:00
|
|
|
def test_XonshStyle_init_file_color_tokens(xonsh_builtins_LS_COLORS):
|
|
|
|
xs = XonshStyle()
|
|
|
|
assert xs.styles
|
|
|
|
assert type(file_color_tokens) is dict
|
2020-01-04 13:56:02 -05:00
|
|
|
assert set(file_color_tokens.keys()) == set(
|
|
|
|
xonsh_builtins_LS_COLORS.__xonsh__.env["LS_COLORS"].keys()
|
|
|
|
)
|
2019-12-26 05:51:48 -05:00
|
|
|
|
2020-01-04 09:55:58 -05:00
|
|
|
|
2020-01-03 21:30:14 -05:00
|
|
|
_cf = {
|
|
|
|
"rs": "regular",
|
|
|
|
"di": "simple_dir",
|
|
|
|
"ln": "simple_link",
|
|
|
|
"mh": None,
|
|
|
|
"pi": "pipe",
|
|
|
|
"so": None,
|
|
|
|
"do": None,
|
|
|
|
# bug ci failures: 'bd': '/dev/sda',
|
|
|
|
# bug ci failures:'cd': '/dev/tty',
|
|
|
|
"or": "orphan_link",
|
|
|
|
"mi": None,
|
|
|
|
"su": "set_uid",
|
|
|
|
"sg": "set_gid",
|
|
|
|
"ca": None,
|
|
|
|
"tw": "sticky_ow_dir",
|
|
|
|
"ow": "other_writable_dir",
|
|
|
|
"st": "sticky_dir",
|
|
|
|
"ex": "executable",
|
|
|
|
"*.emf": "foo.emf",
|
|
|
|
"*.zip": "foo.zip",
|
|
|
|
"*.ogg": "foo.ogg",
|
2019-12-26 05:51:48 -05:00
|
|
|
}
|
2020-01-03 21:30:14 -05:00
|
|
|
|
2020-01-04 09:55:58 -05:00
|
|
|
|
2020-01-04 08:32:42 -05:00
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
def colorizable_files():
|
2020-01-04 09:55:58 -05:00
|
|
|
"""populate temp dir with sample files.
|
2020-01-04 08:32:42 -05:00
|
|
|
(too hard to emit indivual test cases when fixture invoked in mark.parametrize)"""
|
2019-12-26 05:51:48 -05:00
|
|
|
|
|
|
|
with TemporaryDirectory() as tempdir:
|
2020-01-03 21:30:14 -05:00
|
|
|
for k, v in _cf.items():
|
|
|
|
|
2019-12-26 05:51:48 -05:00
|
|
|
if v is None:
|
|
|
|
continue
|
2020-01-03 21:30:14 -05:00
|
|
|
if v.startswith("/"):
|
2019-12-26 05:51:48 -05:00
|
|
|
file_path = v
|
|
|
|
else:
|
2020-01-03 21:30:14 -05:00
|
|
|
file_path = tempdir + "/" + v
|
2019-12-26 05:51:48 -05:00
|
|
|
try:
|
2020-05-05 06:42:28 -04:00
|
|
|
os.lstat(file_path)
|
2019-12-26 05:51:48 -05:00
|
|
|
except FileNotFoundError:
|
2020-01-03 21:30:14 -05:00
|
|
|
if file_path.endswith("_dir"):
|
2019-12-26 05:51:48 -05:00
|
|
|
os.mkdir(file_path)
|
|
|
|
else:
|
2020-01-03 21:30:14 -05:00
|
|
|
open(file_path, "a").close()
|
|
|
|
if k in ("di", "rg"):
|
2019-12-26 05:51:48 -05:00
|
|
|
pass
|
2020-01-03 21:30:14 -05:00
|
|
|
elif k == "ex":
|
2019-12-26 05:51:48 -05:00
|
|
|
os.chmod(file_path, stat.S_IXUSR)
|
2020-01-03 21:30:14 -05:00
|
|
|
elif k == "ln":
|
|
|
|
os.rename(file_path, file_path + "_target")
|
|
|
|
os.symlink(file_path + "_target", file_path)
|
|
|
|
elif k == "or":
|
|
|
|
os.rename(file_path, file_path + "_target")
|
|
|
|
os.symlink(file_path + "_target", file_path)
|
|
|
|
os.remove(file_path + "_target")
|
|
|
|
elif k == "pi":
|
2019-12-26 05:51:48 -05:00
|
|
|
os.remove(file_path)
|
|
|
|
os.mkfifo(file_path)
|
2020-01-03 21:30:14 -05:00
|
|
|
elif k == "su":
|
2019-12-26 05:51:48 -05:00
|
|
|
os.chmod(file_path, stat.S_ISUID)
|
2020-01-03 21:30:14 -05:00
|
|
|
elif k == "sg":
|
2019-12-26 05:51:48 -05:00
|
|
|
os.chmod(file_path, stat.S_ISGID)
|
2020-01-03 21:30:14 -05:00
|
|
|
elif k == "st":
|
2020-01-04 13:56:02 -05:00
|
|
|
os.chmod(
|
|
|
|
file_path, stat.S_ISVTX | stat.S_IRUSR | stat.S_IWUSR
|
|
|
|
) # TempDir requires o:r
|
2020-01-03 21:30:14 -05:00
|
|
|
elif k == "tw":
|
|
|
|
os.chmod(
|
2020-01-04 13:56:02 -05:00
|
|
|
file_path,
|
|
|
|
stat.S_ISVTX | stat.S_IWOTH | stat.S_IRUSR | stat.S_IWUSR,
|
2020-01-03 21:30:14 -05:00
|
|
|
)
|
|
|
|
elif k == "ow":
|
2019-12-26 05:51:48 -05:00
|
|
|
os.chmod(file_path, stat.S_IWOTH | stat.S_IRUSR | stat.S_IWUSR)
|
|
|
|
else:
|
2020-01-03 21:30:14 -05:00
|
|
|
pass # cauterize those elseless ifs!
|
|
|
|
|
2020-01-04 08:32:42 -05:00
|
|
|
yield tempdir
|
2019-12-26 05:51:48 -05:00
|
|
|
|
2020-01-03 21:30:14 -05:00
|
|
|
pass # tempdir get cleaned up here.
|
|
|
|
|
2019-12-26 05:51:48 -05:00
|
|
|
|
2020-01-04 13:56:02 -05:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"key,file_path", [(key, file_path) for key, file_path in _cf.items() if file_path]
|
|
|
|
)
|
2019-12-26 05:51:48 -05:00
|
|
|
@skip_if_on_windows
|
2020-01-04 08:32:42 -05:00
|
|
|
def test_colorize_file(key, file_path, colorizable_files, xonsh_builtins_LS_COLORS):
|
2020-01-04 13:56:02 -05:00
|
|
|
xonsh_builtins_LS_COLORS.__xonsh__.shell.shell.styler = (
|
|
|
|
XonshStyle()
|
|
|
|
) # default style
|
2020-01-04 09:55:58 -05:00
|
|
|
ffp = colorizable_files + "/" + file_path
|
2020-01-04 08:32:42 -05:00
|
|
|
mode = (os.lstat(ffp)).st_mode
|
|
|
|
color_token, color_key = color_file(ffp, mode)
|
|
|
|
assert color_key == key, "File classified as expected kind"
|
|
|
|
assert color_token == file_color_tokens[key], "Color token is as expected"
|