Skip to content

Commit 6b8ab3e

Browse files
committed
!squash tests
1 parent fdfdd80 commit 6b8ab3e

File tree

5 files changed

+216
-56
lines changed

5 files changed

+216
-56
lines changed

tests/legacy_api/test_pane.py

+67-28
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,61 @@
66
import shutil
77
import typing as t
88

9+
from libtmux.test.retry import retry_until
10+
911
if t.TYPE_CHECKING:
1012
from libtmux.session import Session
13+
from libtmux.window import Window
1114

1215
logger = logging.getLogger(__name__)
1316

1417

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+
1559
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
1964

2065
pane1 = window.attached_pane
2166
assert pane1 is not None
@@ -32,15 +77,24 @@ def test_resize_pane(session: Session) -> None:
3277

3378
def test_send_keys(session: Session) -> None:
3479
"""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
3682
assert pane is not None
37-
pane.send_keys("c-c", literal=True)
3883

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
4196

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)
4498

4599

46100
def test_set_height(session: Session) -> None:
@@ -75,24 +129,9 @@ def test_set_width(session: Session) -> None:
75129

76130
def test_capture_pane(session: Session) -> None:
77131
"""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
87134
assert pane is not None
135+
88136
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\nHello World !\n$",
98-
)
137+
assert "READY>" in pane_contents

tests/legacy_api/test_session.py

+57-10
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from libtmux.session import Session
1515
from libtmux.test.constants import TEST_SESSION_PREFIX
1616
from libtmux.test.random import namer
17+
from libtmux.test.retry import retry_until
1718
from libtmux.window import Window
1819

1920
if t.TYPE_CHECKING:
@@ -264,6 +265,47 @@ def test_cmd_inserts_session_id(session: Session) -> None:
264265
assert cmd.cmd[-1] == last_arg
265266

266267

268+
def setup_shell_window(
269+
session: Session,
270+
window_name: str,
271+
environment: dict[str, str] | None = None,
272+
) -> Window:
273+
"""Set up a shell window with consistent environment and prompt.
274+
275+
Args:
276+
session: The tmux session to create the window in
277+
window_name: Name for the new window
278+
environment: Optional environment variables to set in the window
279+
280+
Returns
281+
-------
282+
The created Window object with shell ready
283+
"""
284+
env = shutil.which("env")
285+
assert env is not None, "Cannot find usable `env` in PATH."
286+
287+
window = session.new_window(
288+
attach=True,
289+
window_name=window_name,
290+
window_shell=f"{env} PROMPT_COMMAND='' PS1='READY>' sh",
291+
environment=environment,
292+
)
293+
294+
pane = window.active_pane
295+
assert pane is not None
296+
297+
# Wait for shell to be ready
298+
def wait_for_prompt() -> bool:
299+
try:
300+
pane_contents = "\n".join(pane.capture_pane())
301+
return "READY>" in pane_contents and len(pane_contents.strip()) > 0
302+
except Exception:
303+
return False
304+
305+
retry_until(wait_for_prompt, 2, raises=True)
306+
return window
307+
308+
267309
@pytest.mark.skipif(
268310
has_lt_version("3.0"),
269311
reason="needs -e flag for new-window which was introduced in 3.0",
@@ -280,20 +322,25 @@ def test_new_window_with_environment(
280322
environment: dict[str, str],
281323
) -> None:
282324
"""Verify new window with environment vars."""
283-
env = shutil.which("env")
284-
assert env is not None, "Cannot find usable `env` in PATH."
285-
286-
window = session.new_window(
287-
attach=True,
288-
window_name="window_with_environment",
289-
window_shell=f"{env} PS1='$ ' sh",
325+
window = setup_shell_window(
326+
session,
327+
"window_with_environment",
290328
environment=environment,
291329
)
292-
pane = window.attached_pane
330+
pane = window.active_pane
293331
assert pane is not None
332+
294333
for k, v in environment.items():
295-
pane.send_keys(f"echo ${k}")
296-
assert pane.capture_pane()[-2] == v
334+
pane.send_keys(f"echo ${k}", literal=True)
335+
336+
def wait_for_output(value: str = v) -> bool:
337+
try:
338+
pane_contents = pane.capture_pane()
339+
return any(value in line for line in pane_contents)
340+
except Exception:
341+
return False
342+
343+
retry_until(wait_for_output, 2, raises=True)
297344

298345

299346
@pytest.mark.skipif(

tests/legacy_api/test_window.py

+67-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

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

109
import pytest
@@ -13,6 +12,7 @@
1312
from libtmux.common import has_gte_version, has_lt_version, has_version
1413
from libtmux.pane import Pane
1514
from libtmux.server import Server
15+
from libtmux.test.retry import retry_until
1616
from libtmux.window import Window
1717

1818
if t.TYPE_CHECKING:
@@ -389,6 +389,47 @@ def test_empty_window_name(session: Session) -> None:
389389
assert "''" in cmd.stdout
390390

391391

392+
def setup_shell_window(
393+
session: Session,
394+
window_name: str,
395+
environment: dict[str, str] | None = None,
396+
) -> Window:
397+
"""Set up a shell window with consistent environment and prompt.
398+
399+
Args:
400+
session: The tmux session to create the window in
401+
window_name: Name for the new window
402+
environment: Optional environment variables to set in the window
403+
404+
Returns
405+
-------
406+
The created Window object with shell ready
407+
"""
408+
env = shutil.which("env")
409+
assert env is not None, "Cannot find usable `env` in PATH."
410+
411+
window = session.new_window(
412+
attach=True,
413+
window_name=window_name,
414+
window_shell=f"{env} PROMPT_COMMAND='' PS1='READY>' sh",
415+
environment=environment,
416+
)
417+
418+
pane = window.active_pane
419+
assert pane is not None
420+
421+
# Wait for shell to be ready
422+
def wait_for_prompt() -> bool:
423+
try:
424+
pane_contents = "\n".join(pane.capture_pane())
425+
return "READY>" in pane_contents and len(pane_contents.strip()) > 0
426+
except Exception:
427+
return False
428+
429+
retry_until(wait_for_prompt, 2, raises=True)
430+
return window
431+
432+
392433
@pytest.mark.skipif(
393434
has_lt_version("3.0"),
394435
reason="needs -e flag for split-window which was introduced in 3.0",
@@ -406,19 +447,36 @@ def test_split_window_with_environment(
406447
) -> None:
407448
"""Verify splitting window with environment variables."""
408449
env = shutil.which("env")
409-
assert env is not None, "Cannot find usable `env` in Path."
450+
assert env is not None, "Cannot find usable `env` in PATH."
410451

411-
window = session.new_window(window_name="split_window_with_environment")
412-
pane = window.split_window(
413-
shell=f"{env} PS1='$ ' sh",
452+
window = setup_shell_window(session, "split_with_environment")
453+
pane = window.split(
454+
shell=f"{env} PROMPT_COMMAND='' PS1='READY>' sh",
414455
environment=environment,
415456
)
416457
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)
458+
459+
# Wait for shell to be ready
460+
def wait_for_prompt() -> bool:
461+
try:
462+
pane_contents = "\n".join(pane.capture_pane())
463+
return "READY>" in pane_contents and len(pane_contents.strip()) > 0
464+
except Exception:
465+
return False
466+
467+
retry_until(wait_for_prompt, 2, raises=True)
468+
419469
for k, v in environment.items():
420-
pane.send_keys(f"echo ${k}")
421-
assert pane.capture_pane()[-2] == v
470+
pane.send_keys(f"echo ${k}", literal=True)
471+
472+
def wait_for_output(value: str = v) -> bool:
473+
try:
474+
pane_contents = pane.capture_pane()
475+
return any(value in line for line in pane_contents)
476+
except Exception:
477+
return False
478+
479+
retry_until(wait_for_output, 2, raises=True)
422480

423481

424482
@pytest.mark.skipif(

tests/test_session.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -379,11 +379,10 @@ def test_new_window_with_environment(
379379
pane = window.active_pane
380380
assert pane is not None
381381

382-
for k, expected_value in environment.items():
382+
for k, v in environment.items():
383383
pane.send_keys(f"echo ${k}", literal=True)
384384

385-
# Wait for command output
386-
def wait_for_output(value: str = expected_value) -> bool:
385+
def wait_for_output(value: str = v) -> bool:
387386
try:
388387
pane_contents = pane.capture_pane()
389388
return any(value in line for line in pane_contents)

tests/test_window.py

+23-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

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

109
import pytest
@@ -19,6 +18,7 @@
1918
)
2019
from libtmux.pane import Pane
2120
from libtmux.server import Server
21+
from libtmux.test.retry import retry_until
2222
from libtmux.window import Window
2323

2424
if t.TYPE_CHECKING:
@@ -444,15 +444,32 @@ def test_split_with_environment(
444444

445445
window = session.new_window(window_name="split_with_environment")
446446
pane = window.split(
447-
shell=f"{env} PS1='$ ' sh",
447+
shell=f"{env} PROMPT_COMMAND='' PS1='READY>' sh",
448448
environment=environment,
449449
)
450450
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)
451+
452+
# Wait for shell to be ready
453+
def wait_for_prompt() -> bool:
454+
try:
455+
pane_contents = "\n".join(pane.capture_pane())
456+
return "READY>" in pane_contents and len(pane_contents.strip()) > 0
457+
except Exception:
458+
return False
459+
460+
retry_until(wait_for_prompt, 2, raises=True)
461+
453462
for k, v in environment.items():
454-
pane.send_keys(f"echo ${k}")
455-
assert pane.capture_pane()[-2] == v
463+
pane.send_keys(f"echo ${k}", literal=True)
464+
465+
def wait_for_output(value: str = v) -> bool:
466+
try:
467+
pane_contents = pane.capture_pane()
468+
return any(value in line for line in pane_contents)
469+
except Exception:
470+
return False
471+
472+
retry_until(wait_for_output, 2, raises=True)
456473

457474

458475
@pytest.mark.skipif(

0 commit comments

Comments
 (0)