9
9
10
10
import kaptan
11
11
import pytest
12
-
13
- from . import fixtures_dir
14
12
from libtmux import Window
15
13
from libtmux .common import has_gte_version
16
14
from libtmux .test import temp_session
15
+
17
16
from tmuxp import config , exc
18
17
from tmuxp ._compat import text_type
19
18
from tmuxp .workspacebuilder import WorkspaceBuilder
20
19
21
- from . import example_dir
20
+ from . import example_dir , fixtures_dir
22
21
from .fixtures ._util import loadfixture
23
22
24
-
25
23
RETRY_TIMEOUT_SECONDS = int (os .getenv ('RETRY_TIMEOUT_SECONDS' , 8 ))
26
24
27
25
26
+ def retry (seconds = RETRY_TIMEOUT_SECONDS ):
27
+ """Retry a block of code until a time limit or ``break``.
28
+
29
+ .. code-block:: python
30
+
31
+ while retry():
32
+ p = w.attached_pane
33
+ p.server._update_panes()
34
+ if p.current_path == pane_path:
35
+ break
36
+
37
+
38
+ :param seconds: Seconds to retry, defaults to ``RETRY_TIMEOUT_SECONDS``,
39
+ which is configurable via environmental variables.
40
+ :type seconds: int
41
+ :rtype: void
42
+
43
+ :todo: Move to libtmux.test
44
+ """
45
+ return (lambda : time .time () < time .time () + seconds )()
46
+
47
+
28
48
def test_split_windows (session ):
29
49
yaml_config = loadfixture ("workspacebuilder/two_pane.yaml" )
30
50
s = session
@@ -110,15 +130,11 @@ def test_focus_pane_index(session):
110
130
111
131
pane_path = '/usr'
112
132
113
- timeout = time .time () + RETRY_TIMEOUT_SECONDS # seconds timeout
114
-
115
- while True :
133
+ while retry ():
116
134
p = w .attached_pane
117
135
p .server ._update_panes ()
118
136
if p .current_path == pane_path :
119
137
break
120
- elif time .time () > timeout :
121
- break
122
138
123
139
assert p .current_path == pane_path
124
140
@@ -131,14 +147,12 @@ def test_focus_pane_index(session):
131
147
132
148
p = None
133
149
pane_path = '/'
134
- timeout = time . time () + RETRY_TIMEOUT_SECONDS # seconds timeout
135
- while True :
150
+
151
+ while retry () :
136
152
p = window3 .attached_pane
137
153
p .server ._update_panes ()
138
154
if p .current_path == pane_path :
139
155
break
140
- elif time .time () > timeout :
141
- break
142
156
143
157
assert p .current_path == pane_path
144
158
@@ -302,17 +316,14 @@ def test_window_options_after(session):
302
316
303
317
def assert_last_line (p , s ):
304
318
correct = False
305
- timeout = time .time () + RETRY_TIMEOUT_SECONDS # seconds timeout
306
319
307
- while True :
320
+ while retry () :
308
321
pane_out = p .cmd ('capture-pane' , '-p' , '-J' ).stdout
309
322
while not pane_out [- 1 ].strip (): # delete trailing lines tmux 1.8
310
323
pane_out .pop ()
311
324
if len (pane_out ) > 1 and pane_out [- 2 ].strip () == s :
312
325
correct = True
313
326
break
314
- elif time .time () > timeout :
315
- break
316
327
317
328
# Print output for easier debugging if assertion fails
318
329
if not correct :
@@ -351,13 +362,11 @@ def test_window_shell(session):
351
362
for w , wconf in builder .iter_create_windows (s ):
352
363
if 'window_shell' in wconf :
353
364
assert wconf ['window_shell' ] == text_type ('top' )
354
- timeout = time . time () + RETRY_TIMEOUT_SECONDS # seconds timeout
355
- while True :
365
+
366
+ while retry () :
356
367
session .server ._update_windows ()
357
368
if w ['window_name' ] != 'top' :
358
369
break
359
- elif time .time () > timeout :
360
- break
361
370
362
371
assert w .name != text_type ('top' )
363
372
@@ -403,38 +412,29 @@ def test_automatic_rename_option(session):
403
412
assert s .name != 'tmuxp'
404
413
w = s .windows [0 ]
405
414
406
- timeout = time .time () + RETRY_TIMEOUT_SECONDS # seconds timeout
407
- while True :
415
+ while retry ():
408
416
session .server ._update_windows ()
409
417
if w .name != 'sh' :
410
418
break
411
- elif time .time () > timeout :
412
- break
413
419
414
420
assert w .name != 'sh'
415
421
416
422
pane_base_index = w .show_window_option ('pane-base-index' , g = True )
417
423
w .select_pane (pane_base_index )
418
424
419
- timeout = time .time () + RETRY_TIMEOUT_SECONDS # seconds timeout
420
- while True :
425
+ while retry ():
421
426
session .server ._update_windows ()
422
427
if w .name == 'sh' :
423
428
break
424
- elif time .time () > timeout :
425
- break
426
429
427
430
assert w .name == text_type ('sh' )
428
431
429
432
w .select_pane ('-D' )
430
433
431
- timeout = time .time () + RETRY_TIMEOUT_SECONDS # seconds timeout
432
- while True :
434
+ while retry ():
433
435
session .server ._update_windows ()
434
436
if w ['window_name' ] != 'sh' :
435
437
break
436
- elif time .time () > timeout :
437
- break
438
438
439
439
assert w .name != text_type ('sh' )
440
440
@@ -490,8 +490,7 @@ def test_start_directory(session, tmpdir):
490
490
491
491
for path , window in zip (dirs , session .windows ):
492
492
for p in window .panes :
493
- timeout = time .time () + RETRY_TIMEOUT_SECONDS # seconds timeout
494
- while True :
493
+ while retry ():
495
494
p .server ._update_panes ()
496
495
pane_path = p .current_path
497
496
if pane_path is None :
@@ -505,8 +504,6 @@ def test_start_directory(session, tmpdir):
505
504
path in pane_path
506
505
)
507
506
break
508
- elif time .time () > timeout :
509
- break
510
507
511
508
# handle case with OS X adding /private/ to /tmp/ paths
512
509
assert result
@@ -558,8 +555,7 @@ def test_start_directory_relative(session, tmpdir):
558
555
559
556
for path , window in zip (dirs , session .windows ):
560
557
for p in window .panes :
561
- timeout = time .time () + RETRY_TIMEOUT_SECONDS # seconds timeout
562
- while True :
558
+ while retry ():
563
559
p .server ._update_panes ()
564
560
# Handle case where directories resolve to /private/ in OSX
565
561
pane_path = p .current_path
@@ -574,8 +570,6 @@ def test_start_directory_relative(session, tmpdir):
574
570
path in pane_path
575
571
)
576
572
break
577
- elif time .time () > timeout :
578
- break
579
573
580
574
assert result
581
575
@@ -629,13 +623,10 @@ def test_pane_order(session):
629
623
# at 0 since python list.
630
624
pane_path = pane_paths [p_index - pane_base_index ]
631
625
632
- timeout = time .time () + RETRY_TIMEOUT_SECONDS # seconds timeout
633
- while True :
626
+ while retry ():
634
627
p .server ._update_panes ()
635
628
if p .current_path == pane_path :
636
629
break
637
- elif time .time () > timeout :
638
- break
639
630
640
631
assert p .current_path , pane_path
641
632
0 commit comments