1212from libtmux .common import has_gte_version , has_lt_version
1313from libtmux .constants import WindowDirection
1414from libtmux .pane import Pane
15+ from libtmux .server import Server
1516from libtmux .session import Session
1617from libtmux .test .constants import TEST_SESSION_PREFIX
1718from libtmux .test .random import namer
19+ from libtmux .test .retry import retry_until
1820from libtmux .window import Window
1921
2022if t .TYPE_CHECKING :
2325logger = logging .getLogger (__name__ )
2426
2527
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+
2669def test_has_session (server : Server , session : Session ) -> None :
2770 """Server.has_session returns True if has session_name exists."""
2871 TEST_SESSION_NAME = session .session_name
@@ -328,20 +371,26 @@ def test_new_window_with_environment(
328371 environment : dict [str , str ],
329372) -> None :
330373 """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" ,
338377 environment = environment ,
339378 )
340379 pane = window .active_pane
341380 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 )
345394
346395
347396@pytest .mark .skipif (
@@ -353,13 +402,9 @@ def test_new_window_with_environment_logs_warning_for_old_tmux(
353402 caplog : pytest .LogCaptureFixture ,
354403) -> None :
355404 """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" ,
363408 environment = {"ENV_VAR" : "window" },
364409 )
365410
0 commit comments