Skip to content

Commit 9e3ed58

Browse files
committed
Add tests for legacy API
1 parent 5efd2f1 commit 9e3ed58

9 files changed

+1488
-0
lines changed

tests/legacy_api/__init__.py

Whitespace-only changes.

tests/legacy_api/test_common.py

+212
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
"""Tests for utility functions in libtmux."""
2+
3+
import re
4+
import sys
5+
import typing as t
6+
from typing import Optional
7+
8+
import pytest
9+
10+
import libtmux
11+
from libtmux._compat import LooseVersion
12+
from libtmux.common import (
13+
TMUX_MAX_VERSION,
14+
TMUX_MIN_VERSION,
15+
get_libtmux_version,
16+
get_version,
17+
has_gt_version,
18+
has_gte_version,
19+
has_lt_version,
20+
has_lte_version,
21+
has_minimum_version,
22+
has_version,
23+
session_check_name,
24+
tmux_cmd,
25+
)
26+
from libtmux.exc import BadSessionName, LibTmuxException, TmuxCommandNotFound
27+
from libtmux.session import Session
28+
29+
version_regex = re.compile(r"([0-9]\.[0-9])|(master)")
30+
31+
32+
def test_allows_master_version(monkeypatch: pytest.MonkeyPatch) -> None:
33+
class Hi:
34+
stdout = ["tmux master"]
35+
stderr = None
36+
37+
def mock_tmux_cmd(*args: t.Any, **kwargs: t.Any) -> Hi:
38+
return Hi()
39+
40+
monkeypatch.setattr(libtmux.common, "tmux_cmd", mock_tmux_cmd)
41+
42+
assert has_minimum_version()
43+
assert has_gte_version(TMUX_MIN_VERSION)
44+
assert has_gt_version(TMUX_MAX_VERSION), "Greater than the max-supported version"
45+
assert (
46+
"%s-master" % TMUX_MAX_VERSION == get_version()
47+
), "Is the latest supported version with -master appended"
48+
49+
50+
def test_allows_next_version(monkeypatch: pytest.MonkeyPatch) -> None:
51+
TMUX_NEXT_VERSION = str(float(TMUX_MAX_VERSION) + 0.1)
52+
53+
class Hi:
54+
stdout = [f"tmux next-{TMUX_NEXT_VERSION}"]
55+
stderr = None
56+
57+
def mock_tmux_cmd(*args: t.Any, **kwargs: t.Any) -> Hi:
58+
return Hi()
59+
60+
monkeypatch.setattr(libtmux.common, "tmux_cmd", mock_tmux_cmd)
61+
62+
assert has_minimum_version()
63+
assert has_gte_version(TMUX_MIN_VERSION)
64+
assert has_gt_version(TMUX_MAX_VERSION), "Greater than the max-supported version"
65+
assert TMUX_NEXT_VERSION == get_version()
66+
67+
68+
def test_get_version_openbsd(monkeypatch: pytest.MonkeyPatch) -> None:
69+
class Hi:
70+
stderr = ["tmux: unknown option -- V"]
71+
72+
def mock_tmux_cmd(*args: t.Any, **kwargs: t.Any) -> Hi:
73+
return Hi()
74+
75+
monkeypatch.setattr(libtmux.common, "tmux_cmd", mock_tmux_cmd)
76+
monkeypatch.setattr(sys, "platform", "openbsd 5.2")
77+
assert has_minimum_version()
78+
assert has_gte_version(TMUX_MIN_VERSION)
79+
assert has_gt_version(TMUX_MAX_VERSION), "Greater than the max-supported version"
80+
assert (
81+
"%s-openbsd" % TMUX_MAX_VERSION == get_version()
82+
), "Is the latest supported version with -openbsd appended"
83+
84+
85+
def test_get_version_too_low(monkeypatch: pytest.MonkeyPatch) -> None:
86+
class Hi:
87+
stderr = ["tmux: unknown option -- V"]
88+
89+
def mock_tmux_cmd(*args: t.Any, **kwargs: t.Any) -> Hi:
90+
return Hi()
91+
92+
monkeypatch.setattr(libtmux.common, "tmux_cmd", mock_tmux_cmd)
93+
with pytest.raises(LibTmuxException) as exc_info:
94+
get_version()
95+
exc_info.match("is running tmux 1.3 or earlier")
96+
97+
98+
def test_ignores_letter_versions(monkeypatch: pytest.MonkeyPatch) -> None:
99+
"""Ignore letters such as 1.8b.
100+
101+
See ticket https://github.com/tmux-python/tmuxp/issues/55.
102+
103+
In version 0.1.7 this is adjusted to use LooseVersion, in order to
104+
allow letters.
105+
106+
"""
107+
monkeypatch.setattr(libtmux.common, "TMUX_MIN_VERSION", "1.9a")
108+
result = has_minimum_version()
109+
assert result
110+
111+
monkeypatch.setattr(libtmux.common, "TMUX_MIN_VERSION", "1.8a")
112+
result = has_minimum_version()
113+
assert result
114+
115+
# Should not throw
116+
assert type(has_version("1.8")) is bool
117+
assert type(has_version("1.8a")) is bool
118+
assert type(has_version("1.9a")) is bool
119+
120+
121+
def test_error_version_less_1_7(monkeypatch: pytest.MonkeyPatch) -> None:
122+
def mock_get_version() -> LooseVersion:
123+
return LooseVersion("1.7")
124+
125+
monkeypatch.setattr(libtmux.common, "get_version", mock_get_version)
126+
with pytest.raises(LibTmuxException) as excinfo:
127+
has_minimum_version()
128+
excinfo.match(r"libtmux only supports")
129+
130+
with pytest.raises(LibTmuxException) as excinfo:
131+
has_minimum_version()
132+
133+
excinfo.match(r"libtmux only supports")
134+
135+
136+
def test_has_version() -> None:
137+
assert has_version(str(get_version()))
138+
139+
140+
def test_has_gt_version() -> None:
141+
assert has_gt_version("1.6")
142+
assert has_gt_version("1.6b")
143+
144+
assert not has_gt_version("4.0")
145+
assert not has_gt_version("4.0b")
146+
147+
148+
def test_has_gte_version() -> None:
149+
assert has_gte_version("1.6")
150+
assert has_gte_version("1.6b")
151+
assert has_gte_version(str(get_version()))
152+
153+
assert not has_gte_version("4.0")
154+
assert not has_gte_version("4.0b")
155+
156+
157+
def test_has_lt_version() -> None:
158+
assert has_lt_version("4.0a")
159+
assert has_lt_version("4.0")
160+
161+
assert not has_lt_version("1.7")
162+
assert not has_lt_version(str(get_version()))
163+
164+
165+
def test_has_lte_version() -> None:
166+
assert has_lte_version("4.0a")
167+
assert has_lte_version("4.0")
168+
assert has_lte_version(str(get_version()))
169+
170+
assert not has_lte_version("1.7")
171+
assert not has_lte_version("1.7b")
172+
173+
174+
def test_tmux_cmd_raises_on_not_found(monkeypatch: pytest.MonkeyPatch) -> None:
175+
monkeypatch.setenv("PATH", "")
176+
with pytest.raises(TmuxCommandNotFound):
177+
tmux_cmd("-V")
178+
179+
180+
def test_tmux_cmd_unicode(session: Session) -> None:
181+
session.cmd("new-window", "-t", 3, "-n", "юникод", "-F", "Ελληνικά")
182+
183+
184+
@pytest.mark.parametrize(
185+
"session_name,raises,exc_msg_regex",
186+
[
187+
("", True, "may not be empty"),
188+
(None, True, "may not be empty"),
189+
("my great session.", True, "may not contain periods"),
190+
("name: great session", True, "may not contain colons"),
191+
("new great session", False, None),
192+
("ajf8a3fa83fads,,,a", False, None),
193+
],
194+
)
195+
def test_session_check_name(
196+
session_name: Optional[str], raises: bool, exc_msg_regex: Optional[str]
197+
) -> None:
198+
if raises:
199+
with pytest.raises(BadSessionName) as exc_info:
200+
session_check_name(session_name)
201+
if exc_msg_regex is not None:
202+
assert exc_info.match(exc_msg_regex)
203+
else:
204+
session_check_name(session_name)
205+
206+
207+
def test_get_libtmux_version() -> None:
208+
from libtmux.__about__ import __version__
209+
210+
version = get_libtmux_version()
211+
assert isinstance(version, LooseVersion)
212+
assert LooseVersion(__version__) == version

tests/legacy_api/test_pane.py

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
"""Tests for libtmux Pane object."""
2+
import logging
3+
import shutil
4+
5+
from libtmux.session import Session
6+
7+
logger = logging.getLogger(__name__)
8+
9+
10+
def test_resize_pane(session: Session) -> None:
11+
"""Test Pane.resize_pane()."""
12+
13+
window = session.attached_window
14+
window.rename_window("test_resize_pane")
15+
16+
pane1 = window.attached_pane
17+
assert pane1 is not None
18+
pane1_height = pane1["pane_height"]
19+
window.split_window()
20+
21+
pane1.resize_pane(height=4)
22+
assert pane1["pane_height"] != pane1_height
23+
assert int(pane1["pane_height"]) == 4
24+
25+
pane1.resize_pane(height=3)
26+
assert int(pane1["pane_height"]) == 3
27+
28+
29+
def test_send_keys(session: Session) -> None:
30+
pane = session.attached_window.attached_pane
31+
assert pane is not None
32+
pane.send_keys("c-c", literal=True)
33+
34+
pane_contents = "\n".join(pane.cmd("capture-pane", "-p").stdout)
35+
assert "c-c" in pane_contents
36+
37+
pane.send_keys("c-a", literal=False)
38+
assert "c-a" not in pane_contents, "should not print to pane"
39+
40+
41+
def test_set_height(session: Session) -> None:
42+
window = session.new_window(window_name="test_set_height")
43+
window.split_window()
44+
pane1 = window.attached_pane
45+
assert pane1 is not None
46+
pane1_height = pane1["pane_height"]
47+
48+
pane1.set_height(4)
49+
assert pane1["pane_height"] != pane1_height
50+
assert int(pane1["pane_height"]) == 4
51+
52+
53+
def test_set_width(session: Session) -> None:
54+
window = session.new_window(window_name="test_set_width")
55+
window.split_window()
56+
57+
window.select_layout("main-vertical")
58+
pane1 = window.attached_pane
59+
assert pane1 is not None
60+
pane1_width = pane1["pane_width"]
61+
62+
pane1.set_width(10)
63+
assert pane1["pane_width"] != pane1_width
64+
assert int(pane1["pane_width"]) == 10
65+
66+
pane1.reset()
67+
68+
69+
def test_capture_pane(session: Session) -> None:
70+
env = shutil.which("env")
71+
assert env is not None, "Cannot find usable `env` in PATH."
72+
73+
session.new_window(
74+
attach=True,
75+
window_name="capture_pane",
76+
window_shell=f"{env} PS1='$ ' sh",
77+
)
78+
pane = session.attached_window.attached_pane
79+
assert pane is not None
80+
pane_contents = "\n".join(pane.capture_pane())
81+
assert pane_contents == "$"
82+
pane.send_keys(
83+
r'printf "\n%s\n" "Hello World !"', literal=True, suppress_history=False
84+
)
85+
pane_contents = "\n".join(pane.capture_pane())
86+
assert pane_contents == r'$ printf "\n%s\n" "Hello World !"{}'.format(
87+
"\n\nHello World !\n$"
88+
)

0 commit comments

Comments
 (0)