Skip to content

Commit d80665c

Browse files
committed
fix: update tests to use waiter functionality and fix imports
1 parent db47652 commit d80665c

File tree

5 files changed

+47
-46
lines changed

5 files changed

+47
-46
lines changed

tests/legacy_api/test_window.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44

55
import logging
66
import shutil
7-
import time
87
import typing as t
98

109
import pytest
1110

1211
from libtmux import exc
12+
from libtmux._internal.waiter import wait_until_pane_ready
1313
from libtmux.common import has_gte_version, has_lt_version, has_version
1414
from libtmux.pane import Pane
1515
from libtmux.server import Server
16+
from libtmux.session import Session
1617
from libtmux.window import Window
1718

1819
if t.TYPE_CHECKING:
@@ -404,18 +405,20 @@ def test_split_window_with_environment(
404405
session: Session,
405406
environment: dict[str, str],
406407
) -> None:
407-
"""Verify splitting window with environment variables."""
408+
"""Test window.split_window() with environment variables."""
408409
env = shutil.which("env")
409-
assert env is not None, "Cannot find usable `env` in Path."
410+
assert env is not None, "Cannot find usable `env` in PATH."
410411

411412
window = session.new_window(window_name="split_window_with_environment")
412413
pane = window.split_window(
413414
shell=f"{env} PS1='$ ' sh",
414415
environment=environment,
415416
)
416417
assert pane is not None
417-
# wait a bit for the prompt to be ready as the test gets flaky otherwise
418-
time.sleep(0.05)
418+
419+
# Wait for shell prompt to be ready using waiter
420+
wait_until_pane_ready(pane)
421+
419422
for k, v in environment.items():
420423
pane.send_keys(f"echo ${k}")
421424
assert pane.capture_pane()[-2] == v

tests/test_pane.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
import pytest
1010

11+
from libtmux._internal.waiter import expect
1112
from libtmux.common import has_gte_version, has_lt_version, has_lte_version
1213
from libtmux.constants import PaneDirection, ResizeAdjustmentDirection
13-
from libtmux.test.retry import retry_until
1414

1515
if t.TYPE_CHECKING:
1616
from libtmux.session import Session
@@ -107,17 +107,11 @@ def test_capture_pane_start(session: Session) -> None:
107107
assert pane_contents == '$ printf "%s"\n$'
108108
pane.send_keys("clear -x", literal=True, suppress_history=False)
109109

110-
def wait_until_pane_cleared() -> bool:
111-
pane_contents = "\n".join(pane.capture_pane())
112-
return "clear -x" not in pane_contents
110+
# Using the waiter functionality to wait for the pane to be cleared
111+
expect(pane).wait_for_predicate(lambda lines: "clear -x" not in "\n".join(lines))
113112

114-
retry_until(wait_until_pane_cleared, 1, raises=True)
115-
116-
def pane_contents_shell_prompt() -> bool:
117-
pane_contents = "\n".join(pane.capture_pane())
118-
return pane_contents == "$"
119-
120-
retry_until(pane_contents_shell_prompt, 1, raises=True)
113+
# Using the waiter functionality to wait for shell prompt
114+
expect(pane).wait_for_exact_text("$")
121115

122116
pane_contents_history_start = pane.capture_pane(start=-2)
123117
assert pane_contents_history_start[0] == '$ printf "%s"'
@@ -126,11 +120,9 @@ def pane_contents_shell_prompt() -> bool:
126120

127121
pane.send_keys("")
128122

129-
def pane_contents_capture_visible_only_shows_prompt() -> bool:
130-
pane_contents = "\n".join(pane.capture_pane(start=1))
131-
return pane_contents == "$"
132-
133-
assert retry_until(pane_contents_capture_visible_only_shows_prompt, 1, raises=True)
123+
# Using the waiter functionality to verify content
124+
result = expect(pane).with_line_range(1, None).wait_for_exact_text("$")
125+
assert result.success
134126

135127

136128
def test_capture_pane_end(session: Session) -> None:

tests/test_pytest_plugin.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ def test_test_server_with_config(
115115

116116

117117
def test_test_server_cleanup(TestServer: t.Callable[..., Server]) -> None:
118-
"""Test TestServer properly cleans up after itself."""
118+
"""Test that servers are properly cleaned up."""
119+
# Create server
119120
server = TestServer()
120121
socket_name = server.socket_name
121122
assert socket_name is not None
@@ -130,13 +131,13 @@ def test_test_server_cleanup(TestServer: t.Callable[..., Server]) -> None:
130131

131132
# Delete server and verify cleanup
132133
server.kill()
134+
135+
# Simply wait a short time rather than using the condition
136+
# since the server object is already killed
133137
time.sleep(0.1) # Give time for cleanup
134138

135139
# Create new server to verify old one was cleaned up
136140
new_server = TestServer()
137-
assert new_server.is_alive() is False # Server not started yet
138-
new_server.new_session() # This should work if old server was cleaned up
139-
assert new_server.is_alive() is True
140141

141142

142143
def test_test_server_multiple(TestServer: t.Callable[..., Server]) -> None:

tests/test_server.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,21 @@
33
from __future__ import annotations
44

55
import logging
6-
import os
76
import subprocess
8-
import time
97
import typing as t
108

119
import pytest
1210

13-
from libtmux.common import has_gte_version, has_version
11+
from libtmux._internal.waiter import expect
12+
from libtmux.common import (
13+
has_gte_version,
14+
has_version,
15+
)
1416
from libtmux.server import Server
17+
from libtmux.session import Session
1518

1619
if t.TYPE_CHECKING:
17-
from libtmux.session import Session
20+
pass
1821

1922
logger = logging.getLogger(__name__)
2023

@@ -139,27 +142,26 @@ def test_new_session_shell(server: Server) -> None:
139142

140143

141144
def test_new_session_shell_env(server: Server) -> None:
142-
"""Verify ``Server.new_session`` creates valid session running w/ command (#553)."""
143-
cmd = "sleep 1m"
144-
env = dict(os.environ)
145+
"""Test new_session() with environment variables."""
146+
env = {"FOO": "BAR", "other": "value"}
147+
148+
cmd = "sh -c 'echo $FOO'"
149+
145150
mysession = server.new_session(
146151
"test_new_session_env",
147152
window_command=cmd,
148153
environment=env,
149154
)
150-
time.sleep(0.1)
155+
156+
# Use waiter to wait for the command to complete
151157
window = mysession.windows[0]
152158
pane = window.panes[0]
153-
assert mysession.session_name == "test_new_session_env"
154-
assert server.has_session("test_new_session_env")
155159

156-
pane_start_command = pane.pane_start_command
157-
assert pane_start_command is not None
160+
# Wait for the output from the command
161+
expect(pane).wait_for_text("BAR")
158162

159-
if has_gte_version("3.2"):
160-
assert pane_start_command.replace('"', "") == cmd
161-
else:
162-
assert pane_start_command == cmd
163+
assert mysession.session_name == "test_new_session_env"
164+
assert server.has_session("test_new_session_env")
163165

164166

165167
@pytest.mark.skipif(has_version("3.2"), reason="Wrong width returned with 3.2")

tests/test_window.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
import logging
66
import shutil
7-
import time
87
import typing as t
98

109
import pytest
1110

1211
from libtmux import exc
1312
from libtmux._internal.query_list import ObjectDoesNotExist
13+
from libtmux._internal.waiter import wait_until_pane_ready
1414
from libtmux.common import has_gte_version, has_lt_version, has_lte_version
1515
from libtmux.constants import (
1616
PaneDirection,
@@ -19,6 +19,7 @@
1919
)
2020
from libtmux.pane import Pane
2121
from libtmux.server import Server
22+
from libtmux.session import Session
2223
from libtmux.window import Window
2324

2425
if t.TYPE_CHECKING:
@@ -438,18 +439,20 @@ def test_split_with_environment(
438439
test_id: str,
439440
environment: dict[str, str],
440441
) -> None:
441-
"""Verify splitting window with environment variables."""
442+
"""Test window.split() with environment variables."""
443+
window = session.active_window
442444
env = shutil.which("env")
443445
assert env is not None, "Cannot find usable `env` in PATH."
444446

445-
window = session.new_window(window_name="split_with_environment")
446447
pane = window.split(
447448
shell=f"{env} PS1='$ ' sh",
448449
environment=environment,
449450
)
450451
assert pane is not None
451-
# wait a bit for the prompt to be ready as the test gets flaky otherwise
452-
time.sleep(0.05)
452+
453+
# Wait for shell prompt to be ready using waiter
454+
wait_until_pane_ready(pane)
455+
453456
for k, v in environment.items():
454457
pane.send_keys(f"echo ${k}")
455458
assert pane.capture_pane()[-2] == v

0 commit comments

Comments
 (0)