Skip to content

Commit 7d612e0

Browse files
committed
!squash more pytest examples
1 parent 7fbc75a commit 7d612e0

File tree

3 files changed

+94
-24
lines changed

3 files changed

+94
-24
lines changed

tests/pytest_examples/test_complex_layouts.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,46 @@ def test_complex_layouts(session) -> None:
99
"""Test creating and interacting with complex window layouts."""
1010
# Create a window with multiple panes in a specific layout
1111
window = session.new_window(window_name="complex-layout")
12+
main_pane = window.attached_pane
13+
main_pane.send_keys("echo 'Main Pane'", enter=True)
1214

13-
# Start with a simple pane
14-
main_pane = window.active_pane
15+
# Split vertically
16+
right_pane = main_pane.split_window(vertical=True)
17+
left_pane = main_pane # For clarity
1518

16-
# Split into a left pane and right pane
17-
left_pane = main_pane
18-
right_pane = window.split_window(vertical=True) # Vertical split (right pane)
19+
# Give window manager time to adjust
20+
time.sleep(1)
1921

20-
# Verify we have two panes
21-
assert len(window.panes) == 2
22+
# Verify we have 2 panes
23+
assert len(window.panes) == 2, f"Expected 2 panes, but got {len(window.panes)}"
2224

2325
# Apply a layout
2426
window.select_layout("main-vertical")
27+
time.sleep(1) # Give tmux time to apply the layout
2528

2629
# Verify the layout was applied
2730
current_layout = window.get("window_layout")
2831
assert current_layout is not None, "Layout was not set"
2932

3033
# Send unique commands to each pane for identification
34+
left_pane.send_keys("clear", enter=True)
35+
right_pane.send_keys("clear", enter=True)
36+
time.sleep(1)
37+
3138
left_pane.send_keys("echo 'Left Pane'", enter=True)
3239
right_pane.send_keys("echo 'Right Pane'", enter=True)
33-
time.sleep(2) # Increase sleep time to ensure output is captured
40+
time.sleep(3) # Increase sleep time to ensure output is captured
3441

3542
# Verify each pane has the correct content
36-
assert any("Left Pane" in line for line in left_pane.capture_pane())
37-
assert any("Right Pane" in line for line in right_pane.capture_pane())
43+
left_output = left_pane.capture_pane()
44+
right_output = right_pane.capture_pane()
45+
46+
assert any("Left Pane" in line for line in left_output), (
47+
f"Left pane content not found in: {left_output}"
48+
)
49+
assert any("Right Pane" in line for line in right_output), (
50+
f"Right pane content not found in: {right_output}"
51+
)
3852

3953

4054
def test_tiled_layout(session) -> None:

tests/pytest_examples/test_process_control.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,43 @@ def pane(window):
2626
def test_process_detection(pane) -> None:
2727
"""Test detecting running processes in a pane."""
2828
# Start a long-running process
29-
pane.send_keys("sleep 10", enter=True)
29+
pane.send_keys("sleep 10 &", enter=True) # Run in background
3030
time.sleep(0.5)
3131

32-
# Get information about the running process
33-
ps_output = pane.cmd("ps", "-o", "pid,command").stdout
32+
# Get the pane's TTY
33+
pane_tty = pane.cmd("display-message", "-p", "#{pane_tty}").stdout[0]
34+
35+
# Clear the pane
36+
pane.send_keys("clear", enter=True)
37+
time.sleep(0.5)
38+
39+
# Get information about the running process using system ps command
40+
pane.send_keys(f"ps -t {pane_tty} -o pid,command | grep sleep", enter=True)
41+
time.sleep(1)
42+
43+
# Capture the output
44+
ps_output = pane.capture_pane()
3445

3546
# Verify the sleep command is running
36-
assert any("sleep 10" in line for line in ps_output)
47+
assert any("sleep 10" in line for line in ps_output), (
48+
f"Expected 'sleep 10' in: {ps_output}"
49+
)
50+
51+
# Kill the process (find PID and kill it)
52+
pane.send_keys("pkill -f 'sleep 10'", enter=True)
53+
time.sleep(1)
3754

38-
# Kill the process
39-
pane.send_keys(chr(3), literal=True) # Send Ctrl+C
55+
# Clear the pane again
56+
pane.send_keys("clear", enter=True)
4057
time.sleep(0.5)
4158

4259
# Verify the process has stopped
43-
ps_output = pane.cmd("ps", "-o", "pid,command").stdout
44-
assert not any("sleep 10" in line for line in ps_output)
60+
pane.send_keys(f"ps -t {pane_tty} -o pid,command | grep sleep", enter=True)
61+
time.sleep(1)
62+
ps_output = pane.capture_pane()
63+
assert not any("sleep 10" in line for line in ps_output), (
64+
f"Found 'sleep 10' in: {ps_output}"
65+
)
4566

4667

4768
def test_command_output_scrollback(pane) -> None:

tests/pytest_examples/test_window_management.py

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from __future__ import annotations
44

5+
import time
6+
57

68
def test_window_renaming(session) -> None:
79
"""Test renaming windows."""
@@ -28,6 +30,9 @@ def test_window_moving(session) -> None:
2830
session.new_window(window_name="window-2")
2931
window3 = session.new_window(window_name="window-3")
3032

33+
# Let tmux settle
34+
time.sleep(0.5)
35+
3136
# Get initial indices
3237
index1 = window1.index
3338

@@ -41,9 +46,19 @@ def test_window_moving(session) -> None:
4146
# Verify window 1 has moved
4247
assert window1.index != index1
4348

44-
# Move window 3 to index 1
49+
# Get a free index for window 3 to move to
50+
# Note: We can't just use index 1 as it might be taken
51+
all_indices = [int(w.index) for w in session.windows]
52+
for i in range(1, 10):
53+
if i not in all_indices:
54+
free_index = str(i)
55+
break
56+
else:
57+
free_index = "10" # Fallback
58+
59+
# Move window 3 to free index
4560
initial_index3 = window3.index
46-
window3.move_window(destination="1")
61+
window3.move_window(destination=free_index)
4762

4863
# Refresh windows
4964
session.list_windows() # This refreshes the windows
@@ -60,26 +75,46 @@ def test_window_switching(session) -> None:
6075
window2 = session.new_window(window_name="switch-test-2")
6176
window3 = session.new_window(window_name="switch-test-3")
6277

63-
# Verify window 3 is active (most recently created)
64-
assert session.active_window.id == window3.id
78+
# Give tmux time to update
79+
time.sleep(0.5)
80+
81+
# Refresh session to get current state
82+
session.refresh()
83+
84+
# Tmux may set any window as active - can vary across platforms
85+
# So first we'll explicitly set window3 as active
86+
session.select_window(window3.index)
87+
time.sleep(0.5)
88+
session.refresh()
89+
90+
# Now verify window 3 is active
91+
assert session.active_window.id == window3.id, (
92+
f"Expected active window {window3.id}, got {session.active_window.id}"
93+
)
6594

6695
# Switch to window 1
6796
session.select_window(window1.index)
97+
time.sleep(0.5)
6898

6999
# Refresh the session information
70100
session.refresh()
71101

72102
# Verify window 1 is now active
73-
assert session.active_window.id == window1.id
103+
assert session.active_window.id == window1.id, (
104+
f"Expected active window {window1.id}, got {session.active_window.id}"
105+
)
74106

75107
# Switch to window 2 by name
76108
session.select_window("switch-test-2")
109+
time.sleep(0.5)
77110

78111
# Refresh the session information
79112
session.refresh()
80113

81114
# Verify window 2 is now active
82-
assert session.active_window.id == window2.id
115+
assert session.active_window.id == window2.id, (
116+
f"Expected active window {window2.id}, got {session.active_window.id}"
117+
)
83118

84119

85120
def test_window_killing(session) -> None:

0 commit comments

Comments
 (0)