-
Notifications
You must be signed in to change notification settings - Fork 231
/
Copy pathtest_builder.py
1552 lines (1244 loc) · 47.4 KB
/
test_builder.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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Test for tmuxp workspace builder."""
import functools
import os
import pathlib
import textwrap
import time
import typing as t
import libtmux
import pytest
from libtmux._internal.query_list import ObjectDoesNotExist
from libtmux.common import has_gte_version, has_lt_version
from libtmux.exc import LibTmuxException
from libtmux.pane import Pane
from libtmux.server import Server
from libtmux.session import Session
from libtmux.test import retry_until, temp_session
from libtmux.window import Window
from tests.constants import EXAMPLE_PATH, FIXTURE_PATH
from tests.fixtures import utils as test_utils
from tmuxp import exc
from tmuxp._internal.config_reader import ConfigReader
from tmuxp.cli.load import load_plugins
from tmuxp.workspace import loader
from tmuxp.workspace.builder import WorkspaceBuilder
if t.TYPE_CHECKING:
class AssertCallbackProtocol(t.Protocol):
"""Assertion callback type protocol."""
def __call__(self, cmd: str, hist: str) -> bool:
"""Run function code for testing assertion."""
...
def test_split_windows(session: Session) -> None:
"""Test workspace builder splits windows in a tmux session."""
workspace = ConfigReader._from_file(
test_utils.get_workspace_file("workspace/builder/two_pane.yaml"),
)
builder = WorkspaceBuilder(session_config=workspace, server=session.server)
window_count = len(session.windows) # current window count
assert len(session.windows) == window_count
for w, wconf in builder.iter_create_windows(session):
for _ in builder.iter_create_panes(w, wconf):
w.select_layout("tiled") # fix glitch with pane size
assert len(session.windows) == window_count
assert isinstance(w, Window)
assert len(session.windows) == window_count
window_count += 1
def test_split_windows_three_pane(session: Session) -> None:
"""Test workspace builder splits windows in a tmux session."""
workspace = ConfigReader._from_file(
test_utils.get_workspace_file("workspace/builder/three_pane.yaml"),
)
builder = WorkspaceBuilder(session_config=workspace, server=session.server)
window_count = len(session.windows) # current window count
assert len(session.windows) == window_count
for w, wconf in builder.iter_create_windows(session):
for _ in builder.iter_create_panes(w, wconf):
w.select_layout("tiled") # fix glitch with pane size
assert len(session.windows) == window_count
assert isinstance(w, Window)
assert len(session.windows) == window_count
window_count += 1
w.set_option("main-pane-height", 50)
w.select_layout(wconf["layout"])
def test_focus_pane_index(session: Session) -> None:
"""Test focus of pane by index works correctly, including with pane-base-index."""
workspace = ConfigReader._from_file(
test_utils.get_workspace_file("workspace/builder/focus_and_pane.yaml"),
)
workspace = loader.expand(workspace)
workspace = loader.trickle(workspace)
builder = WorkspaceBuilder(session_config=workspace, server=session.server)
builder.build(session=session)
assert session.active_window.name == "focused window"
_pane_base_index = session.active_window._show_option(
"pane-base-index",
_global=True,
)
assert isinstance(_pane_base_index, int)
pane_base_index = int(_pane_base_index)
pane_base_index = 0 if not pane_base_index else int(pane_base_index)
# get the pane index for each pane
pane_base_indexes = [
int(pane.index)
for pane in session.active_window.panes
if pane is not None and pane.index is not None
]
pane_indexes_should_be = [pane_base_index + x for x in range(3)]
assert pane_indexes_should_be == pane_base_indexes
w = session.active_window
assert w.name != "man"
pane_path = "/usr"
p = None
def f_check() -> bool:
nonlocal p
p = w.active_pane
assert p is not None
return p.pane_current_path == pane_path
assert retry_until(f_check)
assert p is not None
assert p.pane_current_path == pane_path
proc = session.cmd("show-option", "-gv", "base-index")
base_index = int(proc.stdout[0])
window3 = session.windows.get(window_index=str(base_index + 2))
assert isinstance(window3, Window)
p = None
pane_path = "/"
def f_check_again() -> bool:
nonlocal p
p = window3.active_pane
assert p is not None
return p.pane_current_path == pane_path
assert retry_until(f_check_again)
assert p is not None
assert p.pane_current_path is not None
assert isinstance(p.pane_current_path, str)
assert p.pane_current_path == pane_path
@pytest.mark.skip(
reason="""
Test needs to be rewritten, assertion not reliable across platforms
and CI. See https://github.com/tmux-python/tmuxp/issues/310.
""".strip(),
)
def test_suppress_history(session: Session) -> None:
"""Test suppression of command history."""
workspace = ConfigReader._from_file(
test_utils.get_workspace_file("workspace/builder/suppress_history.yaml"),
)
workspace = loader.expand(workspace)
workspace = loader.trickle(workspace)
builder = WorkspaceBuilder(session_config=workspace, server=session.server)
builder.build(session=session)
inHistoryWindow = session.windows.get(window_name="inHistory")
assert inHistoryWindow is not None
isMissingWindow = session.windows.get(window_name="isMissing")
assert isMissingWindow is not None
def assertHistory(cmd: str, hist: str) -> bool:
return "inHistory" in cmd and cmd.endswith(hist)
def assertIsMissing(cmd: str, hist: str) -> bool:
return "isMissing" in cmd and not cmd.endswith(hist)
for w, window_name, assertCase in [
(inHistoryWindow, "inHistory", assertHistory),
(isMissingWindow, "isMissing", assertIsMissing),
]:
assert w.name == window_name
w.select()
p = w.active_pane
assert p is not None
p.select()
# Print the last-in-history command in the pane
p.cmd("send-keys", " fc -ln -1")
p.cmd("send-keys", "Enter")
buffer_name = "test"
sent_cmd = None
def f(p: Pane, buffer_name: str, assertCase: AssertCallbackProtocol) -> bool:
# from v0.7.4 libtmux session.cmd adds target -t self.id by default
# show-buffer doesn't accept -t, use global cmd.
# Get the contents of the pane
p.cmd("capture-pane", "-b", buffer_name)
captured_pane = session.server.cmd("show-buffer", "-b", buffer_name)
session.server.cmd("delete-buffer", "-b", buffer_name)
# Parse the sent and last-in-history commands
sent_cmd = captured_pane.stdout[0].strip()
history_cmd = captured_pane.stdout[-2].strip()
return assertCase(sent_cmd, history_cmd)
_f = functools.partial(f, p=p, buffer_name=buffer_name, assertCase=assertCase)
assert retry_until(_f), f"Unknown sent command: [{sent_cmd}] in {assertCase}"
def test_session_options(session: Session) -> None:
"""Test setting of options to session scope."""
workspace = ConfigReader._from_file(
test_utils.get_workspace_file("workspace/builder/session_options.yaml"),
)
workspace = loader.expand(workspace)
builder = WorkspaceBuilder(session_config=workspace, server=session.server)
builder.build(session=session)
_default_shell = session._show_option("default-shell")
assert isinstance(_default_shell, str)
assert "/bin/sh" in _default_shell
_default_command = session._show_option("default-command")
assert isinstance(_default_command, str)
assert "/bin/sh" in _default_command
def test_global_options(session: Session) -> None:
"""Test setting of global options."""
workspace = ConfigReader._from_file(
test_utils.get_workspace_file("workspace/builder/global_options.yaml"),
)
workspace = loader.expand(workspace)
builder = WorkspaceBuilder(session_config=workspace, server=session.server)
builder.build(session=session)
_status_position = session._show_option("status-position", _global=True)
assert isinstance(_status_position, str)
assert "top" in _status_position
assert session._show_option("repeat-time", _global=True) == 493
def test_global_session_env_options(
session: Session,
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Test setting of global option variables."""
visual_silence = "on"
monkeypatch.setenv("VISUAL_SILENCE", str(visual_silence))
repeat_time = 738
monkeypatch.setenv("REPEAT_TIME", str(repeat_time))
main_pane_height = 8
monkeypatch.setenv("MAIN_PANE_HEIGHT", str(main_pane_height))
workspace = ConfigReader._from_file(
test_utils.get_workspace_file("workspace/builder/env_var_options.yaml"),
)
workspace = loader.expand(workspace)
builder = WorkspaceBuilder(session_config=workspace, server=session.server)
builder.build(session=session)
_visual_silence = session._show_option("visual-silence", _global=True)
assert isinstance(_visual_silence, bool)
assert _visual_silence is True
assert repeat_time == session._show_option("repeat-time")
assert main_pane_height == session.active_window._show_option(
"main-pane-height",
)
def test_window_options(
session: Session,
) -> None:
"""Test setting of window options."""
workspace = ConfigReader._from_file(
test_utils.get_workspace_file("workspace/builder/window_options.yaml"),
)
workspace = loader.expand(workspace)
if has_gte_version("2.3"):
workspace["windows"][0]["options"]["pane-border-format"] = " #P "
builder = WorkspaceBuilder(session_config=workspace, server=session.server)
window_count = len(session.windows) # current window count
assert len(session.windows) == window_count
for w, wconf in builder.iter_create_windows(session):
for _ in builder.iter_create_panes(w, wconf):
w.select_layout("tiled") # fix glitch with pane size
assert len(session.windows) == window_count
assert isinstance(w, Window)
assert w._show_option("main-pane-height") == 5
if has_gte_version("2.3"):
assert w._show_option("pane-border-format") == " #P "
assert len(session.windows) == window_count
window_count += 1
w.select_layout(wconf["layout"])
@pytest.mark.flaky(reruns=5)
def test_window_options_after(
session: Session,
) -> None:
"""Test setting window options via options_after (WorkspaceBuilder.after_window)."""
workspace = ConfigReader._from_file(
test_utils.get_workspace_file("workspace/builder/window_options_after.yaml"),
)
workspace = loader.expand(workspace)
builder = WorkspaceBuilder(session_config=workspace, server=session.server)
builder.build(session=session)
def assert_last_line(p: Pane, s: str) -> bool:
def f() -> bool:
pane_out = p.cmd("capture-pane", "-p", "-J").stdout
while not pane_out[-1].strip(): # delete trailing lines tmux 1.8
pane_out.pop()
return len(pane_out) > 1 and pane_out[-2].strip() == s
# Print output for easier debugging if assertion fails
return retry_until(f, raises=False)
for i, pane in enumerate(session.active_window.panes):
assert assert_last_line(pane, str(i)), (
"Initial command did not execute properly/" + str(i)
)
pane.cmd("send-keys", "Up") # Will repeat echo
pane.enter() # in each iteration
assert assert_last_line(pane, str(i)), (
"Repeated command did not execute properly/" + str(i)
)
session.cmd("send-keys", " echo moo")
session.cmd("send-keys", "Enter")
for pane in session.active_window.panes:
assert assert_last_line(
pane,
"moo",
), "Synchronized command did not execute properly"
def test_window_shell(
session: Session,
) -> None:
"""Test execution of commands via tmuxp configuration."""
workspace = ConfigReader._from_file(
test_utils.get_workspace_file("workspace/builder/window_shell.yaml"),
)
workspace = loader.expand(workspace)
builder = WorkspaceBuilder(session_config=workspace, server=session.server)
for w, wconf in builder.iter_create_windows(session):
if "window_shell" in wconf:
assert wconf["window_shell"] == "top"
def f(w: Window) -> bool:
return w.window_name != "top"
_f = functools.partial(f, w=w)
retry_until(_f)
assert w.name != "top"
@pytest.mark.skipif(
has_lt_version("3.0"),
reason="needs -e flag for new-window and split-window introduced in tmux 3.0",
)
def test_environment_variables(
session: Session,
) -> None:
"""Test setting of environmental variables in tmux via workspace builder."""
workspace = ConfigReader._from_file(
test_utils.get_workspace_file("workspace/builder/environment_vars.yaml"),
)
workspace = loader.expand(workspace)
builder = WorkspaceBuilder(session_config=workspace, server=session.server)
builder.build(session)
# Give slow shells some time to settle as otherwise tests might fail.
time.sleep(0.3)
assert session.getenv("FOO") == "SESSION"
assert session.getenv("PATH") == "/tmp"
no_overrides_win = session.windows[0]
pane = no_overrides_win.panes[0]
pane.send_keys("echo $FOO")
assert pane.capture_pane()[1] == "SESSION"
window_overrides_win = session.windows[1]
pane = window_overrides_win.panes[0]
pane.send_keys("echo $FOO")
assert pane.capture_pane()[1] == "WINDOW"
pane_overrides_win = session.windows[2]
pane = pane_overrides_win.panes[0]
pane.send_keys("echo $FOO")
assert pane.capture_pane()[1] == "PANE"
both_overrides_win = session.windows[3]
pane = both_overrides_win.panes[0]
pane.send_keys("echo $FOO")
assert pane.capture_pane()[1] == "WINDOW"
pane = both_overrides_win.panes[1]
pane.send_keys("echo $FOO")
assert pane.capture_pane()[1] == "PANE"
@pytest.mark.skipif(
has_gte_version("3.0"),
reason="warnings are not needed for tmux >= 3.0",
)
def test_environment_variables_warns_prior_to_tmux_3_0(
session: Session,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Warns when environmental variables cannot be set prior to tmux 3.0."""
workspace = ConfigReader._from_file(
test_utils.get_workspace_file("workspace/builder/environment_vars.yaml"),
)
workspace = loader.expand(workspace)
builder = WorkspaceBuilder(session_config=workspace, server=session.server)
builder.build(session)
# environment on sessions should work as this is done using set-environment
# on the session itself
assert session.getenv("FOO") == "SESSION"
assert session.getenv("PATH") == "/tmp"
assert (
sum(
1
for record in caplog.records
if "Cannot set environment for new windows." in record.msg
)
# From window_overrides and both_overrides, but not
# both_overrides_in_first_pane.
== 2
), "Warning on creating windows missing"
assert (
sum(
1
for record in caplog.records
if "Cannot set environment for new panes." in record.msg
)
# From pane_overrides and both_overrides, but not both_overrides_in_first_pane.
== 2
), "Warning on creating panes missing"
assert (
sum(
1
for record in caplog.records
if "Cannot set environment for new panes and windows." in record.msg
)
# From both_overrides_in_first_pane.
== 1
)
def test_automatic_rename_option(
server: "Server",
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Test workspace builder with automatic renaming enabled."""
monkeypatch.setenv("DISABLE_AUTO_TITLE", "true")
monkeypatch.setenv("ROWS", "36")
workspace = ConfigReader._from_file(
test_utils.get_workspace_file("workspace/builder/window_automatic_rename.yaml"),
)
# This should be a command guaranteed to be terminal name across systems
portable_command = workspace["windows"][0]["panes"][0]["shell_command"][0]["cmd"]
# If a command is like "man ls", get the command base name, "ls"
if " " in portable_command:
portable_command = portable_command.split(" ")[0]
builder = WorkspaceBuilder(session_config=workspace, server=server)
builder.build()
assert builder.session is not None
session: Session = builder.session
w: Window = session.windows[0]
assert len(session.windows) == 1
assert w.name != "renamed_window"
def check_window_name_mismatch() -> bool:
return bool(w.name != portable_command)
assert retry_until(check_window_name_mismatch, 5, interval=0.25)
def check_window_name_match() -> bool:
assert w._show_option("automatic-rename")
return w.name in {
pathlib.Path(os.getenv("SHELL", "bash")).name,
portable_command,
}
assert retry_until(
check_window_name_match,
4,
interval=0.05,
), f"Window name {w.name} should be {portable_command}"
w.select_pane("-D")
assert retry_until(check_window_name_mismatch, 2, interval=0.25)
def test_blank_pane_spawn(
session: Session,
) -> None:
"""Test various ways of spawning blank panes from a tmuxp configuration.
:todo: Verify blank panes of various types build into workspaces.
"""
yaml_workspace_file = EXAMPLE_PATH / "blank-panes.yaml"
test_config = ConfigReader._from_file(yaml_workspace_file)
test_config = loader.expand(test_config)
builder = WorkspaceBuilder(session_config=test_config, server=session.server)
builder.build(session=session)
assert session == builder.session
window1 = session.windows.get(window_name="Blank pane test")
assert window1 is not None
assert len(window1.panes) == 3
window2 = session.windows.get(window_name="More blank panes")
assert window2 is not None
assert len(window2.panes) == 3
window3 = session.windows.get(window_name="Empty string (return)")
assert window3 is not None
assert len(window3.panes) == 3
window4 = session.windows.get(window_name="Blank with options")
assert window4 is not None
assert len(window4.panes) == 2
def test_start_directory(session: Session, tmp_path: pathlib.Path) -> None:
"""Test workspace builder setting start_directory relative to current directory."""
test_dir = tmp_path / "foo bar"
test_dir.mkdir()
yaml_workspace = test_utils.read_workspace_file(
"workspace/builder/start_directory.yaml",
)
test_config = yaml_workspace.format(TEST_DIR=test_dir)
workspace = ConfigReader._load(fmt="yaml", content=test_config)
workspace = loader.expand(workspace)
workspace = loader.trickle(workspace)
builder = WorkspaceBuilder(session_config=workspace, server=session.server)
builder.build(session=session)
assert session == builder.session
dirs = ["/usr/bin", "/dev", str(test_dir), "/usr", "/usr"]
for path, window in zip(dirs, session.windows):
for p in window.panes:
def f(path: str, p: Pane) -> bool:
pane_path = p.pane_current_path
return (
pane_path is not None and path in pane_path
) or pane_path == path
_f = functools.partial(f, path=path, p=p)
# handle case with OS X adding /private/ to /tmp/ paths
assert retry_until(_f)
def test_start_directory_relative(session: Session, tmp_path: pathlib.Path) -> None:
"""Test workspace builder setting start_directory relative to project file.
Same as above test, but with relative start directory, mimicking
loading it from a location of project file. Like::
$ tmuxp load ~/workspace/myproject/.tmuxp.yaml
instead of::
$ cd ~/workspace/myproject/.tmuxp.yaml
$ tmuxp load .
"""
yaml_workspace = test_utils.read_workspace_file(
"workspace/builder/start_directory_relative.yaml",
)
test_dir = tmp_path / "foo bar"
test_dir.mkdir()
config_dir = tmp_path / "testRelConfigDir"
config_dir.mkdir()
test_config = yaml_workspace.format(TEST_DIR=test_dir)
workspace = ConfigReader._load(fmt="yaml", content=test_config)
# the second argument of os.getcwd() mimics the behavior
# the CLI loader will do, but it passes in the workspace file's location.
workspace = loader.expand(workspace, config_dir)
workspace = loader.trickle(workspace)
assert config_dir.exists()
assert test_dir.exists()
builder = WorkspaceBuilder(session_config=workspace, server=session.server)
builder.build(session=session)
assert session == builder.session
dirs = ["/usr/bin", "/dev", str(test_dir), str(config_dir), str(config_dir)]
for path, window in zip(dirs, session.windows):
for p in window.panes:
def f(path: str, p: Pane) -> bool:
pane_path = p.pane_current_path
return (
pane_path is not None and path in pane_path
) or pane_path == path
_f = functools.partial(f, path=path, p=p)
# handle case with OS X adding /private/ to /tmp/ paths
assert retry_until(_f)
@pytest.mark.skipif(
has_lt_version("3.2a"),
reason="needs format introduced in tmux >= 3.2a",
)
def test_start_directory_sets_session_path(server: Server) -> None:
"""Test start_directory setting path in session_path."""
workspace = ConfigReader._from_file(
test_utils.get_workspace_file(
"workspace/builder/start_directory_session_path.yaml",
),
)
workspace = loader.expand(workspace)
workspace = loader.trickle(workspace)
builder = WorkspaceBuilder(session_config=workspace, server=server)
builder.build()
session = builder.session
expected = f"{session.id}|/usr"
cmd = server.cmd("list-sessions", "-F", "#{session_id}|#{session_path}")
assert expected in cmd.stdout
def test_pane_order(session: Session) -> None:
"""Pane ordering based on position in config and ``pane_index``.
Regression test for https://github.com/tmux-python/tmuxp/issues/15.
"""
yaml_workspace = test_utils.read_workspace_file(
"workspace/builder/pane_ordering.yaml",
).format(HOME=str(pathlib.Path().home().resolve()))
# test order of `panes` (and pane_index) above against pane_dirs
pane_paths = [
"/usr/bin",
"/usr",
"/etc",
str(pathlib.Path().home().resolve()),
]
workspace = ConfigReader._load(fmt="yaml", content=yaml_workspace)
workspace = loader.expand(workspace)
workspace = loader.trickle(workspace)
builder = WorkspaceBuilder(session_config=workspace, server=session.server)
window_count = len(session.windows) # current window count
assert len(session.windows) == window_count
for w, wconf in builder.iter_create_windows(session):
for _ in builder.iter_create_panes(w, wconf):
w.select_layout("tiled") # fix glitch with pane size
assert len(session.windows) == window_count
assert isinstance(w, Window)
assert len(session.windows) == window_count
window_count += 1
for w in session.windows:
pane_base_index = w._show_option("pane-base-index", _global=True)
assert isinstance(pane_base_index, int)
for p_index, p in enumerate(w.panes, start=pane_base_index):
assert p.index is not None
assert int(p_index) == int(p.index)
# pane-base-index start at base-index, pane_paths always start
# at 0 since python list.
pane_path = pane_paths[p_index - pane_base_index]
def f(pane_path: str, p: Pane) -> bool:
p.refresh()
return p.pane_current_path == pane_path
_f = functools.partial(f, pane_path=pane_path, p=p)
assert retry_until(_f)
def test_window_index(
session: Session,
) -> None:
"""Test window_index respected by workspace builder."""
proc = session.cmd("show-option", "-gv", "base-index")
base_index = int(proc.stdout[0])
name_index_map = {"zero": 0 + base_index, "one": 1 + base_index, "five": 5}
workspace = ConfigReader._from_file(
test_utils.get_workspace_file("workspace/builder/window_index.yaml"),
)
workspace = loader.expand(workspace)
workspace = loader.trickle(workspace)
builder = WorkspaceBuilder(session_config=workspace, server=session.server)
for window, _ in builder.iter_create_windows(session):
expected_index = name_index_map[window.window_name]
assert int(window.window_index) == expected_index
def test_before_script_throw_error_if_retcode_error(
server: Server,
) -> None:
"""Test tmuxp configuration before_script when command fails."""
config_script_fails = test_utils.read_workspace_file(
"workspace/builder/config_script_fails.yaml",
)
yaml_workspace = config_script_fails.format(
script_failed=FIXTURE_PATH / "script_failed.sh",
)
workspace = ConfigReader._load(fmt="yaml", content=yaml_workspace)
workspace = loader.expand(workspace)
workspace = loader.trickle(workspace)
builder = WorkspaceBuilder(session_config=workspace, server=server)
with temp_session(server) as sess:
session_name = sess.name
assert session_name is not None
with pytest.raises(exc.BeforeLoadScriptError):
builder.build(session=sess)
result = server.has_session(session_name)
assert not result, "Kills session if before_script exits with errcode"
def test_before_script_throw_error_if_file_not_exists(
server: Server,
) -> None:
"""Test tmuxp configuration before_script when script does not exist."""
config_script_not_exists = test_utils.read_workspace_file(
"workspace/builder/config_script_not_exists.yaml",
)
yaml_workspace = config_script_not_exists.format(
script_not_exists=FIXTURE_PATH / "script_not_exists.sh",
)
workspace = ConfigReader._load(fmt="yaml", content=yaml_workspace)
workspace = loader.expand(workspace)
workspace = loader.trickle(workspace)
builder = WorkspaceBuilder(session_config=workspace, server=server)
with temp_session(server) as session:
session_name = session.name
assert session_name is not None
temp_session_exists = server.has_session(session_name)
assert temp_session_exists
with pytest.raises((exc.BeforeLoadScriptNotExists, OSError)) as excinfo:
builder.build(session=session)
excinfo.match(r"No such file or directory")
result = server.has_session(session_name)
assert not result, "Kills session if before_script doesn't exist"
def test_before_script_true_if_test_passes(
server: Server,
) -> None:
"""Test tmuxp configuration before_script when command succeeds."""
config_script_completes = test_utils.read_workspace_file(
"workspace/builder/config_script_completes.yaml",
)
script_complete_sh = FIXTURE_PATH / "script_complete.sh"
assert script_complete_sh.exists()
yaml_workspace = config_script_completes.format(script_complete=script_complete_sh)
workspace = ConfigReader._load(fmt="yaml", content=yaml_workspace)
workspace = loader.expand(workspace)
workspace = loader.trickle(workspace)
builder = WorkspaceBuilder(session_config=workspace, server=server)
with temp_session(server) as session:
builder.build(session=session)
def test_before_script_true_if_test_passes_with_args(
server: Server,
) -> None:
"""Test tmuxp configuration before_script when command passes w/ args."""
config_script_completes = test_utils.read_workspace_file(
"workspace/builder/config_script_completes.yaml",
)
script_complete_sh = FIXTURE_PATH / "script_complete.sh"
assert script_complete_sh.exists()
yaml_workspace = config_script_completes.format(script_complete=script_complete_sh)
workspace = ConfigReader._load(fmt="yaml", content=yaml_workspace)
workspace = loader.expand(workspace)
workspace = loader.trickle(workspace)
builder = WorkspaceBuilder(session_config=workspace, server=server)
with temp_session(server) as session:
builder.build(session=session)
def test_plugin_system_before_workspace_builder(
monkeypatch_plugin_test_packages: None,
session: Session,
) -> None:
"""Test tmuxp configuration plugin hook before workspace builder starts."""
workspace = ConfigReader._from_file(
path=test_utils.get_workspace_file("workspace/builder/plugin_bwb.yaml"),
)
workspace = loader.expand(workspace)
builder = WorkspaceBuilder(
session_config=workspace,
plugins=load_plugins(workspace),
server=session.server,
)
assert len(builder.plugins) > 0
builder.build(session=session)
proc = session.cmd("display-message", "-p", "'#S'")
assert proc.stdout[0] == "'plugin_test_bwb'"
def test_plugin_system_on_window_create(
monkeypatch_plugin_test_packages: None,
session: Session,
) -> None:
"""Test tmuxp configuration plugin hooks work on window creation."""
workspace = ConfigReader._from_file(
path=test_utils.get_workspace_file("workspace/builder/plugin_owc.yaml"),
)
workspace = loader.expand(workspace)
builder = WorkspaceBuilder(
session_config=workspace,
plugins=load_plugins(workspace),
server=session.server,
)
assert len(builder.plugins) > 0
builder.build(session=session)
proc = session.cmd("display-message", "-p", "'#W'")
assert proc.stdout[0] == "'plugin_test_owc'"
def test_plugin_system_after_window_finished(
monkeypatch_plugin_test_packages: None,
session: Session,
) -> None:
"""Test tmuxp configuration plugin hooks work after windows created."""
workspace = ConfigReader._from_file(
path=test_utils.get_workspace_file("workspace/builder/plugin_awf.yaml"),
)
workspace = loader.expand(workspace)
builder = WorkspaceBuilder(
session_config=workspace,
plugins=load_plugins(workspace),
server=session.server,
)
assert len(builder.plugins) > 0
builder.build(session=session)
proc = session.cmd("display-message", "-p", "'#W'")
assert proc.stdout[0] == "'plugin_test_awf'"
def test_plugin_system_on_window_create_multiple_windows(
session: Session,
) -> None:
"""Test tmuxp configuration plugin hooks work on windows creation."""
workspace = ConfigReader._from_file(
path=test_utils.get_workspace_file(
"workspace/builder/plugin_owc_multiple_windows.yaml",
),
)
workspace = loader.expand(workspace)
builder = WorkspaceBuilder(
session_config=workspace,
plugins=load_plugins(workspace),
server=session.server,
)
assert len(builder.plugins) > 0
builder.build(session=session)
proc = session.cmd("list-windows", "-F", "'#W'")
assert "'plugin_test_owc_mw'" in proc.stdout
assert "'plugin_test_owc_mw_2'" in proc.stdout
def test_plugin_system_after_window_finished_multiple_windows(
monkeypatch_plugin_test_packages: None,
session: Session,
) -> None:
"""Test tmuxp configuration plugin hooks work after windows created."""
workspace = ConfigReader._from_file(
path=test_utils.get_workspace_file(
"workspace/builder/plugin_awf_multiple_windows.yaml",
),
)
workspace = loader.expand(workspace)
builder = WorkspaceBuilder(
session_config=workspace,
plugins=load_plugins(workspace),
server=session.server,
)
assert len(builder.plugins) > 0
builder.build(session=session)
proc = session.cmd("list-windows", "-F", "'#W'")
assert "'plugin_test_awf_mw'" in proc.stdout
assert "'plugin_test_awf_mw_2'" in proc.stdout
def test_plugin_system_multiple_plugins(
monkeypatch_plugin_test_packages: None,
session: Session,
) -> None:
"""Test tmuxp plugin system works with multiple plugins."""
workspace = ConfigReader._from_file(
path=test_utils.get_workspace_file(
"workspace/builder/plugin_multiple_plugins.yaml",
),
)
workspace = loader.expand(workspace)
builder = WorkspaceBuilder(
session_config=workspace,
plugins=load_plugins(workspace),
server=session.server,
)
assert len(builder.plugins) > 0
builder.build(session=session)
# Drop through to the before_script plugin hook
proc = session.cmd("display-message", "-p", "'#S'")
assert proc.stdout[0] == "'plugin_test_bwb'"
# Drop through to the after_window_finished. This won't succeed
# unless on_window_create succeeds because of how the test plugin