File: test_sh.py

package info (click to toggle)
qtile 0.34.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,004 kB
  • sloc: python: 49,959; ansic: 4,371; xml: 324; sh: 260; makefile: 218
file content (173 lines) | stat: -rw-r--r-- 5,252 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
import pytest

from libqtile import config, ipc, resources
from libqtile.bar import Bar
from libqtile.command.base import expose_command
from libqtile.command.interface import IPCCommandInterface
from libqtile.confreader import Config
from libqtile.layout import Max
from libqtile.sh import COMMA_MATCHER, QSh, split_args
from libqtile.widget import TextBox


class ShConfig(Config):
    class ShellWidget(TextBox):
        @expose_command
        def return_args(self, *args: str, **kwargs: dict[str, str]):
            return {"length": len(args), "args": list(args), "kwargs": kwargs}

    keys = []
    mouse = []
    groups = [
        config.Group("a"),
        config.Group("b"),
    ]
    layouts = [
        Max(),
    ]
    floating_layout = resources.default_config.floating_layout
    screens = [config.Screen(top=Bar([ShellWidget("")], 20))]


sh_config = pytest.mark.parametrize("manager", [ShConfig], indirect=True)


@sh_config
def test_columnize(manager):
    client = ipc.Client(manager.sockfile)
    command = IPCCommandInterface(client)
    sh = QSh(command)
    assert sh.columnize(["one", "two"]) == "one  two"

    sh.termwidth = 1
    assert sh.columnize(["one", "two"], update_termwidth=False) == "one\ntwo"

    sh.termwidth = 15
    v = sh.columnize(["one", "two", "three", "four", "five"], update_termwidth=False)
    assert v == "one    two  \nthree  four \nfive "


@sh_config
def test_ls(manager):
    client = ipc.Client(manager.sockfile)
    command = IPCCommandInterface(client)
    sh = QSh(command)
    assert sh.do_ls(None) == "bar/     group/   layout/  screen/  widget/  window/  core/  "
    assert sh.do_ls("") == "bar/     group/   layout/  screen/  widget/  window/  core/  "
    assert sh.do_ls("layout") == "layout/group/   layout/window/  layout/screen/  layout[0]/    "

    assert sh.do_cd("layout") == "layout"
    assert sh.do_ls(None) == "group/   window/  screen/"
    assert (
        sh.do_ls("screen")
        == "screen/layout/  screen/window/  screen/bar/     screen/widget/  screen/group/ "
    )


@sh_config
def test_do_cd(manager):
    client = ipc.Client(manager.sockfile)
    command = IPCCommandInterface(client)
    sh = QSh(command)
    assert sh.do_cd("layout") == "layout"
    assert sh.do_cd("../layout/0") == "layout[0]"
    assert sh.do_cd("..") == "/"
    assert sh.do_cd("layout") == "layout"
    assert sh.do_cd("../layout0/wibble") == "No such path."
    assert sh.do_cd(None) == "/"


@sh_config
def test_call(manager):
    client = ipc.Client(manager.sockfile)
    command = IPCCommandInterface(client)
    sh = QSh(command)
    assert sh.process_line("status()") == "OK"

    v = sh.process_line("nonexistent()")
    assert v == "Command does not exist: nonexistent"

    v = sh.process_line("status(((")
    assert v == "Invalid command: status((("

    v = sh.process_line("status(1)")
    assert v.startswith("Caught command exception")


@sh_config
def test_complete(manager):
    client = ipc.Client(manager.sockfile)
    command = IPCCommandInterface(client)
    sh = QSh(command)
    assert sh._complete("c", "c") == [
        "cd",
        "change_window_order",
        "commands",
        "critical",
    ]

    assert sh._complete("cd l", "l") == ["layout/"]
    assert sh._complete("cd layout/", "layout/") == [
        "layout/" + x for x in ["group", "window", "screen", "0"]
    ]
    assert sh._complete("cd layout/", "layout/g") == ["layout/group/"]


@sh_config
def test_help(manager):
    client = ipc.Client(manager.sockfile)
    command = IPCCommandInterface(client)
    sh = QSh(command)
    assert sh.do_help("nonexistent").startswith("No such command")
    assert sh.do_help("help")


@sh_config
def test_eval(manager):
    client = ipc.Client(manager.sockfile)
    command = IPCCommandInterface(client)
    sh = QSh(command)
    sh.process_line("eval(self._test_val=(1,2))")
    _, result = sh.process_line("eval(self._test_val)")
    assert result == "(1, 2)"


@pytest.mark.parametrize(
    "input,output",
    [
        ("1, 2, 3", ["1", " 2", " 3"]),
        ("1, 2, 3, '4, 5, 6'", ["1", " 2", " 3", " '4, 5, 6'"]),
        ('1, "2, 3, 4", 5, 6', ["1", ' "2, 3, 4"', " 5", " 6"]),
    ],
)
def test_comma_split_regex(input, output):
    assert COMMA_MATCHER.split(input) == output


@pytest.mark.parametrize(
    "input,args,kwargs",
    [
        ("1, 2, 3", ["1", "2", "3"], {}),
        ("1, 2, 3, '4, 5, 6'", ["1", "2", "3", "4, 5, 6"], {}),
        ("1, 2, 3, 4=5", ["1", "2", "3"], {"4": "5"}),
        ("1, 2, 3, 4='5, 6'", ["1", "2", "3"], {"4": "5, 6"}),
        ("1, 2, 3, 4 = '5, 6'", ["1", "2", "3"], {"4": "5, 6"}),
        ("1, 2, 3, 4='5=6'", ["1", "2", "3"], {"4": "5=6"}),
    ],
)
def test_args_splitting(input, args, kwargs):
    assert split_args(COMMA_MATCHER.split(input)) == (args, kwargs)


@sh_config
def test_comma_splitting(manager):
    client = ipc.Client(manager.sockfile)
    command = IPCCommandInterface(client)
    sh = QSh(command)

    assert sh.do_cd("widget/shellwidget") == "widget[shellwidget]"

    result = sh.process_line("return_args(1, 2, '3, 4, 5', test_kwarg='test')")
    assert result["length"] == 3
    assert result["args"][2] == "3, 4, 5"
    assert result["kwargs"] == {"test_kwarg": "test"}