-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathtest_window.py
502 lines (387 loc) · 16.7 KB
/
test_window.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
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
"""Test for libtmux Window object."""
from __future__ import annotations
import logging
import shutil
import typing as t
import pytest
from libtmux import exc
from libtmux.common import has_gte_version, has_lt_version, has_version
from libtmux.pane import Pane
from libtmux.server import Server
from libtmux.test.retry import retry_until
from libtmux.window import Window
if t.TYPE_CHECKING:
from libtmux.session import Session
logger = logging.getLogger(__name__)
def test_select_window(session: Session) -> None:
"""Test Window.select_window()."""
window_count = len(session._windows)
# to do, get option for base-index from tmux
# for now however, let's get the index from the first window.
assert window_count == 1
assert session.attached_window is not None
assert session.attached_window.index is not None
window_base_index = int(session.attached_window.index)
window = session.new_window(window_name="testing 3")
# self.assertEqual(2,
# int(session.attached_window.index))
assert window.index is not None
assert int(window_base_index) + 1 == int(window.index)
session.select_window(str(window_base_index))
assert window_base_index == int(session.attached_window.index)
session.select_window("testing 3")
assert int(window_base_index) + 1 == int(session.attached_window.index)
assert len(session._windows) == 2
def test_fresh_window_data(session: Session) -> None:
"""Verify window data is fresh."""
attached_window = session.attached_window
assert attached_window is not None
pane_base_idx = attached_window.show_window_option("pane-base-index", g=True)
assert pane_base_idx is not None
pane_base_index = int(pane_base_idx)
assert len(session.windows) == 1
assert len(session.attached_window.panes) == 1
current_windows = len(session._windows)
assert session.get("session_id") != "@0"
assert current_windows == 1
assert len(session.attached_window.panes) == 1
assert isinstance(session.server, Server)
# len(session.attached_window.panes))
assert len(session.windows), 1
assert len(session.attached_window.panes) == 1
for w in session.windows:
assert isinstance(w, Window)
window = session.attached_window
assert isinstance(window, Window)
assert len(session.attached_window.panes) == 1
window.split_window()
attached_window = session.attached_window
assert attached_window is not None
attached_window.select_pane(pane_base_index)
attached_pane = session.attached_pane
assert attached_pane is not None
attached_pane.send_keys("cd /srv/www/flaskr")
attached_window.select_pane(pane_base_index + 1)
attached_pane = session.attached_pane
assert attached_pane is not None
attached_pane.send_keys("source .venv/bin/activate")
session.new_window(window_name="second")
current_windows += 1
assert current_windows == len(session._windows)
session.new_window(window_name="hey")
current_windows += 1
assert current_windows == len(session._windows)
session.select_window("1")
session.kill_window(target_window="hey")
current_windows -= 1
assert current_windows == len(session._windows)
def test_newest_pane_data(session: Session) -> None:
"""Test window.panes has fresh data."""
window = session.new_window(window_name="test", attach=True)
assert isinstance(window, Window)
assert len(window.panes) == 1
window.split_window(attach=True)
assert len(window.panes) == 2
# note: the below used to accept -h, removing because split_window now
# has attach as its only argument now
window.split_window(attach=True)
assert len(window.panes) == 3
def test_attached_pane(session: Session) -> None:
"""Window.attached_window returns active Pane."""
window = session.attached_window # current window
assert isinstance(window.attached_pane, Pane)
def test_split_window(session: Session) -> None:
"""Window.split_window() splits window, returns new Pane, vertical."""
window_name = "test split window"
window = session.new_window(window_name=window_name, attach=True)
pane = window.split_window()
assert len(window.panes) == 2
assert isinstance(pane, Pane)
assert window.width is not None
assert window.panes[0].height is not None
assert float(window.panes[0].height) <= ((float(window.width) + 1) / 2)
def test_split_window_shell(session: Session) -> None:
"""Window.split_window() splits window, returns new Pane, vertical."""
window_name = "test split window"
cmd = "sleep 1m"
window = session.new_window(window_name=window_name, attach=True)
pane = window.split_window(shell=cmd)
assert len(window.panes) == 2
assert isinstance(pane, Pane)
assert window.width is not None
assert window.panes[0].height is not None
assert float(window.panes[0].height) <= ((float(window.width) + 1) / 2)
if has_gte_version("3.2"):
assert pane.get("pane_start_command", "").replace('"', "") == cmd
else:
assert pane.get("pane_start_command") == cmd
def test_split_window_horizontal(session: Session) -> None:
"""Window.split_window() splits window, returns new Pane, horizontal."""
window_name = "test split window"
window = session.new_window(window_name=window_name, attach=True)
pane = window.split_window(vertical=False)
assert len(window.panes) == 2
assert isinstance(pane, Pane)
assert window.width is not None
assert window.panes[0].width is not None
assert float(window.panes[0].width) <= ((float(window.width) + 1) / 2)
@pytest.mark.filterwarnings("ignore:.*deprecated in favor of Window.split()")
@pytest.mark.filterwarnings("ignore:.*vertical is not required to pass with direction.")
def test_split_percentage(
session: Session,
) -> None:
"""Test deprecated percent param."""
window = session.new_window(window_name="split window size")
window.resize(height=100, width=100)
window_height_before = (
int(window.window_height) if isinstance(window.window_height, str) else 0
)
if has_version("3.4"):
pytest.skip(
"tmux 3.4 has a split-window bug."
+ " See https://github.com/tmux/tmux/pull/3840.",
)
with pytest.warns(match="Deprecated in favor of size.*"):
pane = window.split_window(percent=10)
assert pane.pane_height == str(int(window_height_before * 0.1))
def test_split_window_size(session: Session) -> None:
"""Window.split_window() respects size."""
window = session.new_window(window_name="split_window window size")
window.resize(height=100, width=100)
if has_gte_version("3.1"):
pane = window.split_window(size=10)
assert pane.pane_height == "10"
pane = window.split_window(vertical=False, size=10)
assert pane.pane_width == "10"
pane = window.split_window(size="10%")
assert pane.pane_height == "8"
pane = window.split_window(vertical=False, size="10%")
assert pane.pane_width == "8"
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
)
pane = window.split_window(size="10%")
assert pane.pane_height == str(int(window_height_before * 0.1))
pane = window.split_window(vertical=False, size="10%")
assert pane.pane_width == str(int(window_width_before * 0.1))
@pytest.mark.parametrize(
("window_name_before", "window_name_after"),
[("test", "ha ha ha fjewlkjflwef"), ("test", "hello \\ wazzup 0")],
)
def test_window_rename(
session: Session,
window_name_before: str,
window_name_after: str,
) -> None:
"""Test Window.rename_window()."""
window_name_before = "test"
window_name_after = "ha ha ha fjewlkjflwef"
session.set_option("automatic-rename", "off")
window = session.new_window(window_name=window_name_before, attach=True)
assert window == session.attached_window
assert window.get("window_name") == window_name_before
window.rename_window(window_name_after)
window = session.attached_window
assert window.get("window_name") == window_name_after
window = session.attached_window
assert window.get("window_name") == window_name_after
def test_kill_window(session: Session) -> None:
"""Test window.kill_window() kills window."""
session.new_window()
# create a second window to not kick out the client.
# there is another way to do this via options too.
w = session.attached_window
assert isinstance(w.get("window_id"), str)
assert len(session.windows.filter(window_id=w.get("window_id"))) == 1
w.kill_window()
assert len(session.windows.filter(window_id=w.get("window_id"))) == 0
def test_show_window_options(session: Session) -> None:
"""Window.show_window_options() returns dict."""
window = session.new_window(window_name="test_window")
options = window.show_window_options()
assert isinstance(options, dict)
def test_set_show_window_options(session: Session) -> None:
"""Set option then Window.show_window_options(key)."""
window = session.new_window(window_name="test_window")
window.set_window_option("main-pane-height", 20)
assert window.show_window_option("main-pane-height") == 20
window.set_window_option("main-pane-height", 40)
assert window.show_window_option("main-pane-height") == 40
assert window.show_window_options()["main-pane-height"] == 40
if has_gte_version("2.3"):
window.set_window_option("pane-border-format", " #P ")
assert window.show_window_option("pane-border-format") == " #P "
def test_empty_window_option_returns_None(session: Session) -> None:
"""Verify unset window option returns None."""
window = session.new_window(window_name="test_window")
assert window.show_window_option("alternate-screen") is None
def test_show_window_option(session: Session) -> None:
"""Set option then Window.show_window_option(key)."""
window = session.new_window(window_name="test_window")
window.set_window_option("main-pane-height", 20)
assert window.show_window_option("main-pane-height") == 20
window.set_window_option("main-pane-height", 40)
assert window.show_window_option("main-pane-height") == 40
assert window.show_window_option("main-pane-height") == 40
def test_show_window_option_unknown(session: Session) -> None:
"""Window.show_window_option raises UnknownOption for bad option key."""
window = session.new_window(window_name="test_window")
cmd_exception: type[exc.OptionError] = exc.UnknownOption
if has_gte_version("3.0"):
cmd_exception = exc.InvalidOption
with pytest.raises(cmd_exception):
window.show_window_option("moooz")
def test_show_window_option_ambiguous(session: Session) -> None:
"""show_window_option raises AmbiguousOption for ambiguous option."""
window = session.new_window(window_name="test_window")
with pytest.raises(exc.AmbiguousOption):
window.show_window_option("clock-mode")
def test_set_window_option_ambiguous(session: Session) -> None:
"""set_window_option raises AmbiguousOption for ambiguous option."""
window = session.new_window(window_name="test_window")
with pytest.raises(exc.AmbiguousOption):
window.set_window_option("clock-mode", 12)
def test_set_window_option_invalid(session: Session) -> None:
"""Window.set_window_option raises ValueError for invalid option key."""
window = session.new_window(window_name="test_window")
if has_gte_version("2.4"):
with pytest.raises(exc.InvalidOption):
window.set_window_option("afewewfew", 43)
else:
with pytest.raises(exc.UnknownOption):
window.set_window_option("afewewfew", 43)
def test_move_window(session: Session) -> None:
"""Window.move_window results in changed index."""
window = session.new_window(window_name="test_window")
assert window.index is not None
new_index = str(int(window.index) + 1)
window.move_window(new_index)
assert window.index == new_index
def test_move_window_to_other_session(server: Server, session: Session) -> None:
"""Window.move_window to other session."""
window = session.new_window(window_name="test_window")
new_session = server.new_session("test_move_window")
window.move_window(session=new_session.get("session_id"))
window_id = window.get("window_id")
assert window_id is not None
assert new_session.get_by_id(window_id) == window
def test_select_layout_accepts_no_arg(server: Server, session: Session) -> None:
"""Tmux allows select-layout with no arguments, so let's allow it here."""
window = session.new_window(window_name="test_window")
window.select_layout()
@pytest.mark.skipif(
has_lt_version("3.2"),
reason="needs filter introduced in tmux >= 3.2",
)
def test_empty_window_name(session: Session) -> None:
"""New windows can be created with empty string for window name."""
session.set_option("automatic-rename", "off")
window = session.new_window(window_name="''", attach=True)
assert window == session.attached_window
assert window.get("window_name") == "''"
assert session.name is not None
cmd = session.cmd(
"list-windows",
"-F",
"#{window_name}",
"-f",
"#{==:#{session_name}," + session.name + "}",
)
assert "''" in cmd.stdout
def setup_shell_window(
session: Session,
window_name: str,
environment: dict[str, str] | None = None,
) -> Window:
"""Set up a shell window with consistent environment and prompt.
Args:
session: The tmux session to create the window in
window_name: Name for the new window
environment: Optional environment variables to set in the window
Returns
-------
The created Window object with shell ready
"""
env = shutil.which("env")
assert env is not None, "Cannot find usable `env` in PATH."
window = session.new_window(
attach=True,
window_name=window_name,
window_shell=f"{env} PROMPT_COMMAND='' PS1='READY>' sh",
environment=environment,
)
pane = window.active_pane
assert pane is not None
# Wait for shell to be ready
def wait_for_prompt() -> bool:
try:
pane_contents = "\n".join(pane.capture_pane())
return "READY>" in pane_contents and len(pane_contents.strip()) > 0
except Exception:
return False
retry_until(wait_for_prompt, 2, raises=True)
return window
@pytest.mark.skipif(
has_lt_version("3.0"),
reason="needs -e flag for split-window which was introduced in 3.0",
)
@pytest.mark.parametrize(
"environment",
[
{"ENV_VAR": "pane"},
{"ENV_VAR_1": "pane_1", "ENV_VAR_2": "pane_2"},
],
)
def test_split_window_with_environment(
session: Session,
environment: dict[str, str],
) -> None:
"""Verify splitting window with environment variables."""
env = shutil.which("env")
assert env is not None, "Cannot find usable `env` in PATH."
window = setup_shell_window(session, "split_with_environment")
pane = window.split(
shell=f"{env} PROMPT_COMMAND='' PS1='READY>' sh",
environment=environment,
)
assert pane is not None
# Wait for shell to be ready
def wait_for_prompt() -> bool:
try:
pane_contents = "\n".join(pane.capture_pane())
return "READY>" in pane_contents and len(pane_contents.strip()) > 0
except Exception:
return False
retry_until(wait_for_prompt, 2, raises=True)
for k, v in environment.items():
pane.send_keys(f"echo ${k}", literal=True)
def wait_for_output(value: str = v) -> bool:
try:
pane_contents = pane.capture_pane()
return any(value in line for line in pane_contents)
except Exception:
return False
retry_until(wait_for_output, 2, raises=True)
@pytest.mark.skipif(
has_gte_version("3.0"),
reason="3.0 has the -e flag on split-window",
)
def test_split_window_with_environment_logs_warning_for_old_tmux(
session: Session,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Verify splitting window with environment variables warns if tmux too old."""
env = shutil.which("env")
assert env is not None, "Cannot find usable `env` in Path."
window = session.new_window(window_name="split_window_with_environment")
window.split_window(
shell=f"{env} PS1='$ ' sh",
environment={"ENV_VAR": "pane"},
)
assert any("Environment flag ignored" in record.msg for record in caplog.records), (
"Warning missing"
)