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