File: test_pyghooks.py

package info (click to toggle)
xonsh 0.13.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,024 kB
  • sloc: python: 46,350; makefile: 136; sh: 41; xml: 17
file content (404 lines) | stat: -rw-r--r-- 13,216 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
"""Tests pygments hooks."""
import os
import pathlib
import stat
from tempfile import TemporaryDirectory

import pytest

from xonsh.environ import LsColors
from xonsh.platform import ON_WINDOWS
from xonsh.pyghooks import (
    XSH,
    Color,
    Token,
    XonshLexer,
    XonshStyle,
    code_by_name,
    color_file,
    color_name_to_pygments_code,
    file_color_tokens,
    get_style_by_name,
    register_custom_pygments_style,
)


@pytest.fixture
def xs_LS_COLORS(xession, os_env, monkeypatch):
    """Xonsh environment including LS_COLORS"""

    # original env is needed on windows. since it will skip enhanced coloring
    # for some emulators
    monkeypatch.setattr(xession, "env", os_env)

    lsc = LsColors(LsColors.default_settings)
    xession.env["LS_COLORS"] = lsc

    # todo: a separate test for this as True
    xession.env["INTENSIFY_COLORS_ON_WIN"] = False

    xession.shell.shell_type = "prompt_toolkit"
    xession.shell.shell.styler = XonshStyle()  # default style

    yield xession


DEFAULT_STYLES = {
    # Reset
    Color.RESET: "noinherit",  # Text Reset
    # Regular Colors
    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",
}


@pytest.mark.parametrize(
    "name, exp",
    [
        ("RESET", "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


@pytest.mark.parametrize(
    "name, exp",
    [
        ("RESET", "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


@pytest.mark.parametrize(
    "in_tuple, exp_ct, exp_ansi_colors",
    [
        (("RESET",), Color.RESET, "noinherit"),
        (("GREEN",), Color.GREEN, "ansigreen"),
        (("BOLD_RED",), Color.BOLD_RED, "bold ansired"),
        (
            ("BACKGROUND_BLACK", "BOLD_GREEN"),
            Color.BACKGROUND_BLACK__BOLD_GREEN,
            "bg:ansiblack bold ansigreen",
        ),
    ],
)
def test_color_token_by_name(in_tuple, exp_ct, exp_ansi_colors, xs_LS_COLORS):
    from xonsh.pyghooks import XonshStyle, color_token_by_name

    xs = XonshStyle()
    styles = xs.styles
    ct = color_token_by_name(in_tuple, styles)
    ansi_colors = styles[ct]  # if keyerror, ct was not cached
    assert ct == exp_ct, "returned color token is right"
    assert ansi_colors == exp_ansi_colors, "color token mapped to correct color string"


def test_XonshStyle_init_file_color_tokens(xs_LS_COLORS, monkeypatch):
    keys = list(file_color_tokens)
    for n in keys:
        monkeypatch.delitem(file_color_tokens, n)
    xs = XonshStyle()
    assert xs.styles
    assert type(file_color_tokens) is dict
    assert set(file_color_tokens.keys()) == set(xs_LS_COLORS.env["LS_COLORS"].keys())


# parameterized tests for file colorization
# note 'ca' is checked by standalone test.
# requires privilege to create a file with capabilities

if ON_WINDOWS:
    # file coloring support is very limited on Windows, only test the cases we can easily make work
    # If you care about file colors, use Windows Subsystem for Linux, or another OS.

    _cf = {
        "fi": "regular",
        "di": "simple_dir",
        "ln": "sym_link",
        "pi": None,
        "so": None,
        "do": None,
        # bug ci failures: 'bd': '/dev/sda',
        # bug ci failures:'cd': '/dev/tty',
        "or": "orphan",
        "mi": None,  # never used
        "su": None,
        "sg": None,
        "ca": None,  # Separate special case test,
        "tw": None,
        "ow": None,
        "st": None,
        "ex": None,  # executable is a filetype test on Windows.
        "*.emf": "foo.emf",
        "*.zip": "foo.zip",
        "*.ogg": "foo.ogg",
        "mh": "hard_link",
    }
else:
    # full-fledged, VT100 based infrastructure
    _cf = {
        "fi": "regular",
        "di": "simple_dir",
        "ln": "sym_link",
        "pi": "pipe",
        "so": None,
        "do": None,
        # bug ci failures: 'bd': '/dev/sda',
        # bug ci failures:'cd': '/dev/tty',
        "or": "orphan",
        "mi": None,  # never used
        "su": "set_uid",
        "sg": "set_gid",
        "ca": None,  # Separate special case test,
        "tw": "sticky_ow_dir",
        "ow": "other_writable_dir",
        "st": "sticky_dir",
        "ex": "executable",
        "*.emf": "foo.emf",
        "*.zip": "foo.zip",
        "*.ogg": "foo.ogg",
        "mh": "hard_link",
    }


@pytest.fixture(scope="module")
def colorizable_files():
    """populate temp dir with sample files.
    (too hard to emit indivual test cases when fixture invoked in mark.parametrize)"""

    with TemporaryDirectory() as tempdir:
        for k, v in _cf.items():

            if v is None:
                continue
            if v.startswith("/"):
                file_path = v
            else:
                file_path = tempdir + "/" + v
            try:
                os.lstat(file_path)
            except FileNotFoundError:
                if file_path.endswith("_dir"):
                    os.mkdir(file_path)
                else:
                    open(file_path, "a").close()
                if k in ("di", "fi"):
                    pass
                elif k == "ex":
                    os.chmod(file_path, stat.S_IRWXU)  # tmpdir on windows need u+w
                elif k == "ln":  # cook ln test case.
                    os.chmod(file_path, stat.S_IRWXU)  # link to *executable* file
                    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":  # not on Windows
                    os.remove(file_path)
                    os.mkfifo(file_path)
                elif k == "su":
                    os.chmod(file_path, stat.S_ISUID)
                elif k == "sg":
                    os.chmod(file_path, stat.S_ISGID)
                elif k == "st":
                    os.chmod(
                        file_path, stat.S_ISVTX | stat.S_IRUSR | stat.S_IWUSR
                    )  # TempDir requires o:r
                elif k == "tw":
                    os.chmod(
                        file_path,
                        stat.S_ISVTX | stat.S_IWOTH | stat.S_IRUSR | stat.S_IWUSR,
                    )
                elif k == "ow":
                    os.chmod(file_path, stat.S_IWOTH | stat.S_IRUSR | stat.S_IWUSR)
                elif k == "mh":
                    os.rename(file_path, file_path + "_target")
                    os.link(file_path + "_target", file_path)
                else:
                    pass  # cauterize those elseless ifs!

                os.symlink(file_path, file_path + "_symlink")

        yield tempdir

    pass  # tempdir get cleaned up here.


@pytest.mark.parametrize(
    "key,file_path",
    [(key, file_path) for key, file_path in _cf.items() if file_path],
)
def test_colorize_file(key, file_path, colorizable_files, xs_LS_COLORS):
    """test proper file codes with symlinks colored normally"""
    ffp = colorizable_files + "/" + file_path
    stat_result = os.lstat(ffp)
    color_token, color_key = color_file(ffp, stat_result)
    assert color_key == key, "File classified as expected kind"
    assert color_token == file_color_tokens[key], "Color token is as expected"


@pytest.mark.parametrize(
    "key,file_path",
    [(key, file_path) for key, file_path in _cf.items() if file_path],
)
def test_colorize_file_symlink(key, file_path, colorizable_files, xs_LS_COLORS):
    """test proper file codes with symlinks colored target."""
    xs_LS_COLORS.env["LS_COLORS"]["ln"] = "target"
    ffp = colorizable_files + "/" + file_path + "_symlink"
    stat_result = os.lstat(ffp)
    assert stat.S_ISLNK(stat_result.st_mode)

    _, color_key = color_file(ffp, stat_result)

    try:
        tar_stat_result = os.stat(ffp)  # stat the target of the link
        tar_ffp = str(pathlib.Path(ffp).resolve())
        _, tar_color_key = color_file(tar_ffp, tar_stat_result)
        if tar_color_key.startswith("*"):
            tar_color_key = (
                "fi"  # all the *.* zoo, link is colored 'fi', not target type.
            )
    except FileNotFoundError:  # orphan symlinks always colored 'or'
        tar_color_key = "or"  # Fake if for missing file

    assert color_key == tar_color_key, "File classified as expected kind, via symlink"


import xonsh.lazyimps


def test_colorize_file_ca(xs_LS_COLORS, monkeypatch):
    def mock_os_listxattr(*args, **kwards):
        return ["security.capability"]

    monkeypatch.setattr(xonsh.pyghooks, "os_listxattr", mock_os_listxattr)

    with TemporaryDirectory() as tmpdir:
        file_path = tmpdir + "/cap_file"
        open(file_path, "a").close()
        os.chmod(
            file_path, stat.S_IRWXU
        )  # ca overrides ex, leave file deletable on Windows
        color_token, color_key = color_file(file_path, os.lstat(file_path))

        assert color_key == "ca"


@pytest.mark.parametrize(
    "name, styles, refrules",
    [
        ("test1", {}, {}),  # empty styles
        (
            "test2",
            {Token.Literal.String.Single: "#ff0000"},
            {Token.Literal.String.Single: "#ff0000"},
        ),  # Token
        (
            "test3",
            {"Token.Literal.String.Single": "#ff0000"},
            {Token.Literal.String.Single: "#ff0000"},
        ),  # str key
        (
            "test4",
            {"Literal.String.Single": "#ff0000"},
            {Token.Literal.String.Single: "#ff0000"},
        ),  # short str key
        (
            "test5",
            {"completion-menu.completion.current": "#00ff00"},
            {Token.PTK.CompletionMenu.Completion.Current: "#00ff00"},
        ),  # ptk style
        (
            "test6",
            {"RED": "#ff0000"},
            {Token.Color.RED: "#ff0000"},
        ),  # short color name
    ],
)
def test_register_custom_pygments_style(name, styles, refrules):
    register_custom_pygments_style(name, styles)
    style = get_style_by_name(name)

    # registration succeeded
    assert style is not None

    # check rules
    for rule, color in refrules.items():
        assert rule in style.styles
        assert style.styles[rule] == color


def test_can_use_xonsh_lexer_without_xession(xession, monkeypatch):
    # When Xonsh is used as a library and simply for its lexer plugin, the
    # xession's env can be unset, so test that it can yield tokens without
    # that env being set.
    monkeypatch.setattr(xession, "env", None)

    assert XSH.env is None
    lexer = XonshLexer()
    assert XSH.env is not None
    list(lexer.get_tokens_unprocessed("  some text"))