6
6
import shutil
7
7
import typing as t
8
8
9
+ from libtmux .test .retry import retry_until
10
+
9
11
if t .TYPE_CHECKING :
10
12
from libtmux .session import Session
13
+ from libtmux .window import Window
11
14
12
15
logger = logging .getLogger (__name__ )
13
16
14
17
18
+ def setup_shell_window (
19
+ session : Session ,
20
+ window_name : str ,
21
+ environment : dict [str , str ] | None = None ,
22
+ ) -> Window :
23
+ """Set up a shell window with consistent environment and prompt.
24
+
25
+ Args:
26
+ session: The tmux session to create the window in
27
+ window_name: Name for the new window
28
+ environment: Optional environment variables to set in the window
29
+
30
+ Returns
31
+ -------
32
+ The created Window object with shell ready
33
+ """
34
+ env = shutil .which ("env" )
35
+ assert env is not None , "Cannot find usable `env` in PATH."
36
+
37
+ window = session .new_window (
38
+ attach = True ,
39
+ window_name = window_name ,
40
+ window_shell = f"{ env } PROMPT_COMMAND='' PS1='READY>' sh" ,
41
+ environment = environment ,
42
+ )
43
+
44
+ pane = window .active_pane
45
+ assert pane is not None
46
+
47
+ # Wait for shell to be ready
48
+ def wait_for_prompt () -> bool :
49
+ try :
50
+ pane_contents = "\n " .join (pane .capture_pane ())
51
+ return "READY>" in pane_contents and len (pane_contents .strip ()) > 0
52
+ except Exception :
53
+ return False
54
+
55
+ retry_until (wait_for_prompt , 2 , raises = True )
56
+ return window
57
+
58
+
15
59
def test_resize_pane (session : Session ) -> None :
16
- """Test Pane.resize_pane()."""
17
- window = session .attached_window
18
- window .rename_window ("test_resize_pane" )
60
+ """Verify Pane.resize_pane()."""
61
+ window = setup_shell_window (session , "test_resize_pane" )
62
+ pane = window .active_pane
63
+ assert pane is not None
19
64
20
65
pane1 = window .attached_pane
21
66
assert pane1 is not None
@@ -32,15 +77,24 @@ def test_resize_pane(session: Session) -> None:
32
77
33
78
def test_send_keys (session : Session ) -> None :
34
79
"""Verify Pane.send_keys()."""
35
- pane = session .attached_window .attached_pane
80
+ window = setup_shell_window (session , "test_send_keys" )
81
+ pane = window .active_pane
36
82
assert pane is not None
37
- pane .send_keys ("c-c" , literal = True )
38
83
39
- pane_contents = "\n " .join (pane .cmd ("capture-pane" , "-p" ).stdout )
40
- assert "c-c" in pane_contents
84
+ pane .send_keys ("echo 'test'" , literal = True )
85
+
86
+ def wait_for_echo () -> bool :
87
+ try :
88
+ pane_contents = "\n " .join (pane .capture_pane ())
89
+ return (
90
+ "test" in pane_contents
91
+ and "echo 'test'" in pane_contents
92
+ and pane_contents .count ("READY>" ) >= 2
93
+ )
94
+ except Exception :
95
+ return False
41
96
42
- pane .send_keys ("c-a" , literal = False )
43
- assert "c-a" not in pane_contents , "should not print to pane"
97
+ retry_until (wait_for_echo , 2 , raises = True )
44
98
45
99
46
100
def test_set_height (session : Session ) -> None :
@@ -75,24 +129,9 @@ def test_set_width(session: Session) -> None:
75
129
76
130
def test_capture_pane (session : Session ) -> None :
77
131
"""Verify Pane.capture_pane()."""
78
- env = shutil .which ("env" )
79
- assert env is not None , "Cannot find usable `env` in PATH."
80
-
81
- session .new_window (
82
- attach = True ,
83
- window_name = "capture_pane" ,
84
- window_shell = f"{ env } PS1='$ ' sh" ,
85
- )
86
- pane = session .attached_window .attached_pane
132
+ window = setup_shell_window (session , "test_capture_pane" )
133
+ pane = window .active_pane
87
134
assert pane is not None
135
+
88
136
pane_contents = "\n " .join (pane .capture_pane ())
89
- assert pane_contents == "$"
90
- pane .send_keys (
91
- r'printf "\n%s\n" "Hello World !"' ,
92
- literal = True ,
93
- suppress_history = False ,
94
- )
95
- pane_contents = "\n " .join (pane .capture_pane ())
96
- assert pane_contents == r'$ printf "\n%s\n" "Hello World !"{}' .format (
97
- "\n \n Hello World !\n $" ,
98
- )
137
+ assert "READY>" in pane_contents
0 commit comments