Skip to content

Commit 8e00da8

Browse files
committed
Add TmuxCommand, wrap tmux_cmd in old behavior
1 parent a9678ad commit 8e00da8

File tree

3 files changed

+54
-13
lines changed

3 files changed

+54
-13
lines changed

libtmux/common.py

+36-5
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,7 @@ def show_environment(self, name=None):
147147
return vars_dict
148148

149149

150-
class tmux_cmd:
151-
150+
class TmuxCommand:
152151
"""
153152
:term:`tmux(1)` command via :py:mod:`subprocess`.
154153
@@ -188,8 +187,8 @@ class tmux_cmd:
188187
Notes
189188
-----
190189
191-
.. versionchanged:: 0.8
192-
Renamed from ``tmux`` to ``tmux_cmd``.
190+
.. versionadded:: 0.8.4
191+
Wrap to split execution from command from instance of it
193192
"""
194193

195194
def __init__(self, *args, **kwargs):
@@ -239,6 +238,38 @@ def execute(self):
239238
return self
240239

241240

241+
def tmux_cmd(*args, **kwargs):
242+
"""Wrapper around TmuxCommand. Executes instantly.
243+
244+
Examples
245+
--------
246+
247+
.. code-block:: python
248+
249+
proc = tmux_cmd('new-session', '-s%' % 'my session')
250+
251+
if proc.stderr:
252+
raise exc.LibTmuxException(
253+
'Command: %s returned error: %s' % (proc.cmd, proc.stderr)
254+
)
255+
256+
print('tmux command returned %s' % proc.stdout)
257+
258+
Equivalent to:
259+
260+
.. code-block:: bash
261+
262+
$ tmux new-session -s my session
263+
264+
Notes
265+
-----
266+
267+
.. versionchanged:: 0.8
268+
Renamed from ``tmux`` to ``tmux_cmd``.
269+
"""
270+
return TmuxCommand(*args, **kwargs).execute()
271+
272+
242273
class TmuxMappingObject(MutableMapping):
243274

244275
r"""Base: :py:class:`MutableMapping`.
@@ -470,7 +501,7 @@ def get_version() -> LooseVersion:
470501
:class:`distutils.version.LooseVersion`
471502
tmux version according to :func:`libtmux.common.which`'s tmux
472503
"""
473-
proc = tmux_cmd("-V").execute()
504+
proc = tmux_cmd("-V")
474505
if proc.stderr:
475506
if proc.stderr[0] == "tmux: unknown option -- V":
476507
if sys.platform.startswith("openbsd"): # openbsd has no tmux -V

libtmux/server.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
EnvironmentMixin,
1414
PaneDict,
1515
SessionDict,
16+
TmuxCommand,
1617
TmuxRelationalObject,
1718
WindowDict,
1819
has_gte_version,
1920
session_check_name,
20-
tmux_cmd,
2121
)
2222
from .session import Session
2323

@@ -127,7 +127,7 @@ def cmd(self, *args, **kwargs):
127127
else:
128128
raise ValueError("Server.colors must equal 88 or 256")
129129

130-
return tmux_cmd(*args, **kwargs).execute()
130+
return TmuxCommand(*args, **kwargs).execute()
131131

132132
def _list_sessions(self) -> t.List[SessionDict]:
133133
"""
@@ -136,7 +136,7 @@ def _list_sessions(self) -> t.List[SessionDict]:
136136
Retrieved from ``$ tmux(1) list-sessions`` stdout.
137137
138138
The :py:obj:`list` is derived from ``stdout`` in
139-
:class:`common.tmux_cmd` which wraps :py:class:`subprocess.Popen`.
139+
:class:`common.TmuxCommand` which wraps :py:class:`subprocess.Popen`.
140140
141141
Returns
142142
-------
@@ -199,7 +199,7 @@ def _list_windows(self) -> t.List[WindowDict]:
199199
Retrieved from ``$ tmux(1) list-windows`` stdout.
200200
201201
The :py:obj:`list` is derived from ``stdout`` in
202-
:class:`common.tmux_cmd` which wraps :py:class:`subprocess.Popen`.
202+
:class:`common.TmuxCommand` which wraps :py:class:`subprocess.Popen`.
203203
204204
Returns
205205
-------
@@ -261,7 +261,7 @@ def _list_panes(self) -> t.List[PaneDict]:
261261
Retrieved from ``$ tmux(1) list-panes`` stdout.
262262
263263
The :py:obj:`list` is derived from ``stdout`` in
264-
:class:`util.tmux_cmd` which wraps :py:class:`subprocess.Popen`.
264+
:class:`util.TmuxCommand` which wraps :py:class:`subprocess.Popen`.
265265
266266
Returns
267267
-------

tests/test_common.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@
2727
version_regex = re.compile(r"([0-9]\.[0-9])|(master)")
2828

2929

30-
def test_allows_master_version(monkeypatch):
31-
def mock_tmux_cmd(param):
30+
@pytest.mark.parametrize("executor", ["mock_tmux_cmd", "mock_TmuxCommand"])
31+
def test_allows_master_version(monkeypatch, executor):
32+
def mock_TmuxCommand(param):
3233
class Hi:
3334
stdout = ["tmux master"]
3435
stderr = None
@@ -38,7 +39,16 @@ def execute(self):
3839

3940
return Hi()
4041

41-
monkeypatch.setattr(libtmux.common, "tmux_cmd", mock_tmux_cmd)
42+
def mock_tmux_cmd(param):
43+
class Hi(object):
44+
stdout = ["tmux master"]
45+
stderr = None
46+
47+
return Hi()
48+
49+
mock_cmd = locals()[executor]
50+
51+
monkeypatch.setattr(libtmux.common, "tmux_cmd", mock_cmd)
4252

4353
assert has_minimum_version()
4454
assert has_gte_version(TMUX_MIN_VERSION)

0 commit comments

Comments
 (0)