-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathtest_pane.py
319 lines (255 loc) · 9.64 KB
/
test_pane.py
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
"""Tests for libtmux Pane object."""
import logging
import shutil
import pytest
from libtmux.common import has_gte_version, has_lt_version, has_lte_version
from libtmux.constants import PaneDirection, ResizeAdjustmentDirection
from libtmux.session import Session
from libtmux.test import retry_until
logger = logging.getLogger(__name__)
def test_send_keys(session: Session) -> None:
"""Verify Pane.send_keys()."""
pane = session.active_window.active_pane
assert pane is not None
pane.send_keys("c-c", literal=True)
pane_contents = "\n".join(pane.cmd("capture-pane", "-p").stdout)
assert "c-c" in pane_contents
pane.send_keys("c-a", literal=False)
assert "c-a" not in pane_contents, "should not print to pane"
def test_set_height(session: Session) -> None:
"""Verify Pane.set_height()."""
window = session.new_window(window_name="test_set_height")
window.split()
pane1 = window.active_pane
assert pane1 is not None
pane1_height = pane1.pane_height
pane1.set_height(4)
assert pane1.pane_height != pane1_height
assert pane1.pane_height is not None
assert int(pane1.pane_height) == 4
def test_set_width(session: Session) -> None:
"""Verify Pane.set_width()."""
window = session.new_window(window_name="test_set_width")
window.split()
window.select_layout("main-vertical")
pane1 = window.active_pane
assert pane1 is not None
pane1_width = pane1.pane_width
pane1.set_width(10)
assert pane1.pane_width != pane1_width
assert pane1.pane_width is not None
assert int(pane1.pane_width) == 10
pane1.reset()
def test_capture_pane(session: Session) -> None:
"""Verify Pane.capture_pane()."""
env = shutil.which("env")
assert env is not None, "Cannot find usable `env` in PATH."
session.new_window(
attach=True,
window_name="capture_pane",
window_shell=f"{env} PS1='$ ' sh",
)
pane = session.active_window.active_pane
assert pane is not None
pane_contents = "\n".join(pane.capture_pane())
assert pane_contents == "$"
pane.send_keys(
r'printf "\n%s\n" "Hello World !"',
literal=True,
suppress_history=False,
)
pane_contents = "\n".join(pane.capture_pane())
assert pane_contents == r'$ printf "\n%s\n" "Hello World !"{}'.format(
"\n\nHello World !\n$",
)
def test_capture_pane_start(session: Session) -> None:
"""Assert Pane.capture_pane() with ``start`` param."""
env = shutil.which("env")
assert env is not None, "Cannot find usable `env` in PATH."
session.new_window(
attach=True,
window_name="capture_pane_start",
window_shell=f"{env} PS1='$ ' sh",
)
pane = session.active_window.active_pane
assert pane is not None
pane_contents = "\n".join(pane.capture_pane())
assert pane_contents == "$"
pane.send_keys(r'printf "%s"', literal=True, suppress_history=False)
pane_contents = "\n".join(pane.capture_pane())
assert pane_contents == '$ printf "%s"\n$'
pane.send_keys("clear -x", literal=True, suppress_history=False)
def wait_until_pane_cleared() -> bool:
pane_contents = "\n".join(pane.capture_pane())
return "clear -x" not in pane_contents
retry_until(wait_until_pane_cleared, 1, raises=True)
def pane_contents_shell_prompt() -> bool:
pane_contents = "\n".join(pane.capture_pane())
return pane_contents == "$"
retry_until(pane_contents_shell_prompt, 1, raises=True)
pane_contents_history_start = pane.capture_pane(start=-2)
assert pane_contents_history_start[0] == '$ printf "%s"'
assert pane_contents_history_start[1] == "$ clear -x"
assert pane_contents_history_start[-1] == "$"
pane.send_keys("")
def pane_contents_capture_visible_only_shows_prompt() -> bool:
pane_contents = "\n".join(pane.capture_pane(start=1))
return pane_contents == "$"
assert retry_until(pane_contents_capture_visible_only_shows_prompt, 1, raises=True)
def test_capture_pane_end(session: Session) -> None:
"""Assert Pane.capture_pane() with ``end`` param."""
env = shutil.which("env")
assert env is not None, "Cannot find usable `env` in PATH."
session.new_window(
attach=True,
window_name="capture_pane_end",
window_shell=f"{env} PS1='$ ' sh",
)
pane = session.active_window.active_pane
assert pane is not None
pane_contents = "\n".join(pane.capture_pane())
assert pane_contents == "$"
pane.send_keys(r'printf "%s"', literal=True, suppress_history=False)
pane_contents = "\n".join(pane.capture_pane())
assert pane_contents == '$ printf "%s"\n$'
pane_contents = "\n".join(pane.capture_pane(end=0))
assert pane_contents == '$ printf "%s"'
pane_contents = "\n".join(pane.capture_pane(end="-"))
assert pane_contents == '$ printf "%s"\n$'
@pytest.mark.skipif(
has_lte_version("3.1"),
reason="3.2 has the -Z flag on split-window",
)
def test_pane_split_window_zoom(
session: Session,
) -> None:
"""Verify splitting window with zoom."""
window_without_zoom = session.new_window(window_name="split_without_zoom")
initial_pane_without_zoom = window_without_zoom.active_pane
assert initial_pane_without_zoom is not None
window_with_zoom = session.new_window(window_name="split_with_zoom")
initial_pane_with_zoom = window_with_zoom.active_pane
assert initial_pane_with_zoom is not None
pane_without_zoom = initial_pane_without_zoom.split(
zoom=False,
)
pane_with_zoom = initial_pane_with_zoom.split(
zoom=True,
)
assert pane_without_zoom.width == pane_without_zoom.window_width
assert pane_without_zoom.height is not None
assert pane_without_zoom.window_height is not None
assert pane_without_zoom.height < pane_without_zoom.window_height
assert pane_with_zoom.width == pane_with_zoom.window_width
assert pane_with_zoom.height == pane_with_zoom.window_height
@pytest.mark.skipif(
has_lt_version("2.9"),
reason="resize-window only exists in tmux 2.9+",
)
def test_resize_pane(
session: Session,
) -> None:
"""Verify resizing window."""
session.cmd("detach-client", "-s")
window = session.active_window
pane = window.split(attach=False)
window.split(direction=PaneDirection.Above, attach=False)
assert pane is not None
window.resize(height=500, width=500)
pane_height_adjustment = 10
assert pane.pane_height is not None
assert pane.pane_width is not None
#
# Manual resizing
#
# Manual: Height
pane_height_before = int(pane.pane_height)
pane.resize_pane(
height="50",
)
assert int(pane.pane_height) == 50
# Manual: Width
window.select_layout("main-horizontal")
pane.resize_pane(
width="75",
)
assert int(pane.pane_width) == 75
if has_gte_version("3.1"):
# Manual: Height percentage
window.select_layout("main-vertical")
pane_height_before = int(pane.pane_height)
pane.resize_pane(
height="15%",
)
assert int(pane.pane_height) == 75
# Manual: Width percentage
window.select_layout("main-horizontal")
pane.resize_pane(
width="15%",
)
assert int(pane.pane_width) == 75
#
# Adjustments
#
# Adjustment: Down
pane_height_before = int(pane.pane_height)
pane.resize_pane(
adjustment_direction=ResizeAdjustmentDirection.Down,
adjustment=pane_height_adjustment * 2,
)
assert pane_height_before - (pane_height_adjustment * 2) == int(pane.pane_height)
# Adjustment: Up
pane_height_before = int(pane.pane_height)
pane.resize_pane(
adjustment_direction=ResizeAdjustmentDirection.Up,
adjustment=pane_height_adjustment,
)
assert pane_height_before + pane_height_adjustment == int(pane.pane_height)
#
# Zoom
#
pane.resize_pane(height=50)
# Zoom
pane.resize_pane(height=2)
pane_height_before = int(pane.pane_height)
pane.resize_pane(
zoom=True,
)
pane_height_expanded = int(pane.pane_height)
assert pane_height_before < pane_height_expanded
def test_split_pane_size(session: Session) -> None:
"""Pane.split()."""
window = session.new_window(window_name="split window size")
window.resize(height=100, width=100)
pane = window.active_pane
assert pane is not None
if has_gte_version("3.1"):
short_pane = pane.split(size=10)
assert short_pane.pane_height == "10"
assert short_pane.at_left
assert short_pane.at_right
assert not short_pane.at_top
assert short_pane.at_bottom
narrow_pane = pane.split(direction=PaneDirection.Right, size=10)
assert narrow_pane.pane_width == "10"
assert not narrow_pane.at_left
assert narrow_pane.at_right
assert narrow_pane.at_top
assert not narrow_pane.at_bottom
new_pane = pane.split(size="10%")
assert new_pane.pane_height == "8"
new_pane = short_pane.split(direction=PaneDirection.Right, size="10%")
assert new_pane.pane_width == "10"
assert not new_pane.at_left
assert new_pane.at_right
else:
window_height_before = (
int(window.window_height) if isinstance(window.window_height, str) else 0
)
window_width_before = (
int(window.window_width) if isinstance(window.window_width, str) else 0
)
new_pane = pane.split(size="10%")
assert new_pane.pane_height == str(int(window_height_before * 0.1))
new_pane = new_pane.split(direction=PaneDirection.Right, size="10%")
assert new_pane.pane_width == str(int(window_width_before * 0.1))