Skip to content

Commit bae5fe3

Browse files
committed
tests(refactor): Update tests for DeprecatedError behavior (#611)
why: Tests need to verify DeprecatedError is raised for legacy APIs. what: - Update legacy_api tests to use pytest.raises(DeprecatedError) - Update other tests to use new APIs instead of deprecated ones - Use namespace imports for exception classes
1 parent 629f574 commit bae5fe3

File tree

13 files changed

+471
-1063
lines changed

13 files changed

+471
-1063
lines changed

tests/legacy_api/test_common.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
"""Tests for utility functions in libtmux."""
1+
"""Tests for utility functions in libtmux.
2+
3+
Note: This file tests common utilities, not deprecated APIs.
4+
The tests use the current API.
5+
"""
26

37
from __future__ import annotations
48

@@ -9,6 +13,7 @@
913
import pytest
1014

1115
import libtmux
16+
from libtmux import exc
1217
from libtmux._compat import LooseVersion
1318
from libtmux.common import (
1419
TMUX_MAX_VERSION,
@@ -24,7 +29,6 @@
2429
session_check_name,
2530
tmux_cmd,
2631
)
27-
from libtmux.exc import BadSessionName, LibTmuxException, TmuxCommandNotFound
2832

2933
if t.TYPE_CHECKING:
3034
from libtmux.session import Session
@@ -100,7 +104,7 @@ def mock_tmux_cmd(*args: t.Any, **kwargs: t.Any) -> Hi:
100104
return Hi()
101105

102106
monkeypatch.setattr(libtmux.common, "tmux_cmd", mock_tmux_cmd)
103-
with pytest.raises(LibTmuxException) as exc_info:
107+
with pytest.raises(exc.LibTmuxException) as exc_info:
104108
get_version()
105109
exc_info.match("does not meet the minimum tmux version requirement")
106110

@@ -135,11 +139,11 @@ def mock_get_version() -> LooseVersion:
135139
return LooseVersion("1.7")
136140

137141
monkeypatch.setattr(libtmux.common, "get_version", mock_get_version)
138-
with pytest.raises(LibTmuxException) as excinfo:
142+
with pytest.raises(exc.LibTmuxException) as excinfo:
139143
has_minimum_version()
140144
excinfo.match(r"libtmux only supports")
141145

142-
with pytest.raises(LibTmuxException) as excinfo:
146+
with pytest.raises(exc.LibTmuxException) as excinfo:
143147
has_minimum_version()
144148

145149
excinfo.match(r"libtmux only supports")
@@ -191,7 +195,7 @@ def test_has_lte_version() -> None:
191195
def test_tmux_cmd_raises_on_not_found(monkeypatch: pytest.MonkeyPatch) -> None:
192196
"""Verify raises if tmux command not found."""
193197
monkeypatch.setenv("PATH", "")
194-
with pytest.raises(TmuxCommandNotFound):
198+
with pytest.raises(exc.TmuxCommandNotFound):
195199
tmux_cmd("-V")
196200

197201

@@ -226,7 +230,7 @@ def test_session_check_name(
226230
) -> None:
227231
"""Verify session_check_name()."""
228232
if raises:
229-
with pytest.raises(BadSessionName) as exc_info:
233+
with pytest.raises(exc.BadSessionName) as exc_info:
230234
session_check_name(session_name)
231235
if exc_msg_regex is not None:
232236
assert exc_info.match(exc_msg_regex)

tests/legacy_api/test_pane.py

Lines changed: 48 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,71 @@
1-
"""Tests for libtmux Pane object."""
1+
"""Tests for deprecated libtmux Pane APIs.
2+
3+
These tests verify that deprecated methods raise DeprecatedError.
4+
"""
25

36
from __future__ import annotations
47

5-
import logging
6-
import shutil
78
import typing as t
89

9-
if t.TYPE_CHECKING:
10-
from libtmux.session import Session
11-
12-
logger = logging.getLogger(__name__)
13-
14-
15-
def test_resize_pane(session: Session) -> None:
16-
"""Test Pane.resize_pane()."""
17-
window = session.attached_window
18-
window.rename_window("test_resize_pane")
19-
20-
pane1 = window.attached_pane
21-
assert pane1 is not None
22-
pane1_height = pane1["pane_height"]
23-
window.split_window()
10+
import pytest
2411

25-
pane1.resize_pane(height=4)
26-
assert pane1["pane_height"] != pane1_height
27-
assert int(pane1["pane_height"]) == 4
12+
from libtmux import exc
2813

29-
pane1.resize_pane(height=3)
30-
assert int(pane1["pane_height"]) == 3
14+
if t.TYPE_CHECKING:
15+
from libtmux.session import Session
3116

3217

33-
def test_send_keys(session: Session) -> None:
34-
"""Verify Pane.send_keys()."""
35-
pane = session.attached_window.attached_pane
18+
def test_resize_pane_raises_deprecated_error(session: Session) -> None:
19+
"""Test Pane.resize_pane() raises DeprecatedError."""
20+
window = session.active_window
21+
pane = window.active_pane
3622
assert pane is not None
37-
pane.send_keys("c-c", literal=True)
3823

39-
pane_contents = "\n".join(pane.cmd("capture-pane", "-p").stdout)
40-
assert "c-c" in pane_contents
24+
with pytest.raises(
25+
exc.DeprecatedError, match=r"Pane\.resize_pane\(\) was deprecated"
26+
):
27+
pane.resize_pane(height=4)
4128

42-
pane.send_keys("c-a", literal=False)
43-
assert "c-a" not in pane_contents, "should not print to pane"
4429

30+
def test_select_pane_raises_deprecated_error(session: Session) -> None:
31+
"""Test Pane.select_pane() raises DeprecatedError."""
32+
window = session.active_window
33+
pane = window.active_pane
34+
assert pane is not None
4535

46-
def test_set_height(session: Session) -> None:
47-
"""Verify Pane.set_height()."""
48-
window = session.new_window(window_name="test_set_height")
49-
window.split_window()
50-
pane1 = window.attached_pane
51-
assert pane1 is not None
52-
pane1_height = pane1["pane_height"]
53-
54-
pane1.set_height(4)
55-
assert pane1["pane_height"] != pane1_height
56-
assert int(pane1["pane_height"]) == 4
36+
with pytest.raises(
37+
exc.DeprecatedError, match=r"Pane\.select_pane\(\) was deprecated"
38+
):
39+
pane.select_pane()
5740

5841

59-
def test_set_width(session: Session) -> None:
60-
"""Verify Pane.set_width()."""
61-
window = session.new_window(window_name="test_set_width")
62-
window.split_window()
42+
def test_split_window_raises_deprecated_error(session: Session) -> None:
43+
"""Test Pane.split_window() raises DeprecatedError."""
44+
window = session.active_window
45+
pane = window.active_pane
46+
assert pane is not None
6347

64-
window.select_layout("main-vertical")
65-
pane1 = window.attached_pane
66-
assert pane1 is not None
67-
pane1_width = pane1["pane_width"]
48+
with pytest.raises(
49+
exc.DeprecatedError, match=r"Pane\.split_window\(\) was deprecated"
50+
):
51+
pane.split_window()
6852

69-
pane1.set_width(10)
70-
assert pane1["pane_width"] != pane1_width
71-
assert int(pane1["pane_width"]) == 10
7253

73-
pane1.reset()
54+
def test_pane_get_raises_deprecated_error(session: Session) -> None:
55+
"""Test Pane.get() raises DeprecatedError."""
56+
window = session.active_window
57+
pane = window.active_pane
58+
assert pane is not None
7459

60+
with pytest.raises(exc.DeprecatedError, match=r"Pane\.get\(\) was deprecated"):
61+
pane.get("pane_id")
7562

76-
def test_capture_pane(session: Session) -> None:
77-
"""Verify Pane.capture_pane()."""
78-
env = shutil.which("env")
79-
assert env is not None, "Cannot find usable `env` in PATH."
8063

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
64+
def test_pane_getitem_raises_deprecated_error(session: Session) -> None:
65+
"""Test Pane.__getitem__() raises DeprecatedError."""
66+
window = session.active_window
67+
pane = window.active_pane
8768
assert pane is not None
88-
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-
)
69+
70+
with pytest.raises(exc.DeprecatedError, match=r"Pane\[key\] lookup was deprecated"):
71+
_ = pane["pane_id"]

0 commit comments

Comments
 (0)