12
12
from libtmux .common import has_gte_version , has_lt_version
13
13
from libtmux .constants import WindowDirection
14
14
from libtmux .pane import Pane
15
+ from libtmux .server import Server
15
16
from libtmux .session import Session
16
17
from libtmux .test .constants import TEST_SESSION_PREFIX
17
18
from libtmux .test .random import namer
19
+ from libtmux .test .retry import retry_until
18
20
from libtmux .window import Window
19
21
20
22
if t .TYPE_CHECKING :
23
25
logger = logging .getLogger (__name__ )
24
26
25
27
28
+ def setup_shell_window (
29
+ session : Session ,
30
+ window_name : str ,
31
+ environment : dict [str , str ] | None = None ,
32
+ ) -> Window :
33
+ """Set up a shell window with consistent environment and prompt.
34
+
35
+ Args:
36
+ session: The tmux session to create the window in
37
+ window_name: Name for the new window
38
+ environment: Optional environment variables to set in the window
39
+
40
+ Returns
41
+ -------
42
+ The created Window object with shell ready
43
+ """
44
+ env = shutil .which ("env" )
45
+ assert env is not None , "Cannot find usable `env` in PATH."
46
+
47
+ window = session .new_window (
48
+ attach = True ,
49
+ window_name = window_name ,
50
+ window_shell = f"{ env } PROMPT_COMMAND='' PS1='READY>' sh" ,
51
+ environment = environment ,
52
+ )
53
+
54
+ pane = window .active_pane
55
+ assert pane is not None
56
+
57
+ # Wait for shell to be ready
58
+ def wait_for_prompt () -> bool :
59
+ try :
60
+ pane_contents = "\n " .join (pane .capture_pane ())
61
+ return "READY>" in pane_contents and len (pane_contents .strip ()) > 0
62
+ except Exception :
63
+ return False
64
+
65
+ retry_until (wait_for_prompt , 2 , raises = True )
66
+ return window
67
+
68
+
26
69
def test_has_session (server : Server , session : Session ) -> None :
27
70
"""Server.has_session returns True if has session_name exists."""
28
71
TEST_SESSION_NAME = session .session_name
@@ -328,20 +371,26 @@ def test_new_window_with_environment(
328
371
environment : dict [str , str ],
329
372
) -> None :
330
373
"""Verify new window with environment vars."""
331
- env = shutil .which ("env" )
332
- assert env is not None , "Cannot find usable `env` in PATH."
333
-
334
- window = session .new_window (
335
- attach = True ,
336
- window_name = "window_with_environment" ,
337
- window_shell = f"{ env } PS1='$ ' sh" ,
374
+ window = setup_shell_window (
375
+ session ,
376
+ "window_with_environment" ,
338
377
environment = environment ,
339
378
)
340
379
pane = window .active_pane
341
380
assert pane is not None
342
- for k , v in environment .items ():
343
- pane .send_keys (f"echo ${ k } " )
344
- assert pane .capture_pane ()[- 2 ] == v
381
+
382
+ for k , expected_value in environment .items ():
383
+ pane .send_keys (f"echo ${ k } " , literal = True )
384
+
385
+ # Wait for command output
386
+ def wait_for_output (value : str = expected_value ) -> bool :
387
+ try :
388
+ pane_contents = pane .capture_pane ()
389
+ return any (value in line for line in pane_contents )
390
+ except Exception :
391
+ return False
392
+
393
+ retry_until (wait_for_output , 2 , raises = True )
345
394
346
395
347
396
@pytest .mark .skipif (
@@ -353,13 +402,9 @@ def test_new_window_with_environment_logs_warning_for_old_tmux(
353
402
caplog : pytest .LogCaptureFixture ,
354
403
) -> None :
355
404
"""Verify new window with environment vars create a warning if tmux is too old."""
356
- env = shutil .which ("env" )
357
- assert env is not None , "Cannot find usable `env` in PATH."
358
-
359
- session .new_window (
360
- attach = True ,
361
- window_name = "window_with_environment" ,
362
- window_shell = f"{ env } PS1='$ ' sh" ,
405
+ setup_shell_window (
406
+ session ,
407
+ "window_with_environment" ,
363
408
environment = {"ENV_VAR" : "window" },
364
409
)
365
410
0 commit comments