Skip to content

Commit 5facc9c

Browse files
committed
Add TmuxCommand, wrap tmux_cmd in old behavior
1 parent 687ff4b commit 5facc9c

File tree

3 files changed

+54
-12
lines changed

3 files changed

+54
-12
lines changed

libtmux/common.py

+36-4
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def show_environment(self, name=None):
143143
return vars_dict
144144

145145

146-
class tmux_cmd(object):
146+
class TmuxCommand(object):
147147

148148
"""
149149
:term:`tmux(1)` command via :py:mod:`subprocess`.
@@ -184,8 +184,8 @@ class tmux_cmd(object):
184184
Notes
185185
-----
186186
187-
.. versionchanged:: 0.8
188-
Renamed from ``tmux`` to ``tmux_cmd``.
187+
.. versionadded:: 0.8.4
188+
Wrap to split execution from command from instance of it
189189
"""
190190

191191
def __init__(self, *args, **kwargs):
@@ -239,6 +239,38 @@ def execute(self):
239239
return self
240240

241241

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

244276
r"""Base: :py:class:`MutableMapping`.
@@ -463,7 +495,7 @@ def get_version():
463495
:class:`distutils.version.LooseVersion`
464496
tmux version according to :func:`libtmux.common.which`'s tmux
465497
"""
466-
proc = tmux_cmd('-V').execute()
498+
proc = tmux_cmd('-V')
467499
if proc.stderr:
468500
if proc.stderr[0] == 'tmux: unknown option -- V':
469501
if sys.platform.startswith("openbsd"): # openbsd has no tmux -V

libtmux/server.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
from . import exc, formats
1414
from .common import (
1515
EnvironmentMixin,
16+
TmuxCommand,
1617
TmuxRelationalObject,
1718
has_gte_version,
1819
session_check_name,
19-
tmux_cmd,
2020
)
2121
from .session import Session
2222

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

129-
return tmux_cmd(*args, **kwargs).execute()
129+
return TmuxCommand(*args, **kwargs).execute()
130130

131131
def _list_sessions(self):
132132
"""
@@ -135,7 +135,7 @@ def _list_sessions(self):
135135
Retrieved from ``$ tmux(1) list-sessions`` stdout.
136136
137137
The :py:obj:`list` is derived from ``stdout`` in
138-
:class:`common.tmux_cmd` which wraps :py:class:`subprocess.Popen`.
138+
:class:`common.TmuxCommand` which wraps :py:class:`subprocess.Popen`.
139139
140140
Returns
141141
-------
@@ -197,7 +197,7 @@ def _list_windows(self):
197197
Retrieved from ``$ tmux(1) list-windows`` stdout.
198198
199199
The :py:obj:`list` is derived from ``stdout`` in
200-
:class:`common.tmux_cmd` which wraps :py:class:`subprocess.Popen`.
200+
:class:`common.TmuxCommand` which wraps :py:class:`subprocess.Popen`.
201201
202202
Returns
203203
-------
@@ -256,7 +256,7 @@ def _list_panes(self):
256256
Retrieved from ``$ tmux(1) list-panes`` stdout.
257257
258258
The :py:obj:`list` is derived from ``stdout`` in
259-
:class:`util.tmux_cmd` which wraps :py:class:`subprocess.Popen`.
259+
:class:`util.TmuxCommand` which wraps :py:class:`subprocess.Popen`.
260260
261261
Returns
262262
-------

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(object):
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)