Skip to content

Commit 96387d9

Browse files
committed
Add TmuxCommand, wrap tmux_cmd in old behavior
1 parent 9fe718f commit 96387d9

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):
@@ -235,6 +235,38 @@ def execute(self):
235235
return self
236236

237237

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

240272
r"""Base: :py:class:`MutableMapping`.
@@ -459,7 +491,7 @@ def get_version():
459491
:class:`distutils.version.LooseVersion`
460492
tmux version according to :func:`libtmux.common.which`'s tmux
461493
"""
462-
proc = tmux_cmd('-V').execute()
494+
proc = tmux_cmd('-V')
463495
if proc.stderr:
464496
if proc.stderr[0] == 'tmux: unknown option -- V':
465497
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
@@ -28,8 +28,9 @@
2828
version_regex = re.compile(r'([0-9]\.[0-9])|(master)')
2929

3030

31-
def test_allows_master_version(monkeypatch):
32-
def mock_tmux_cmd(param):
31+
@pytest.mark.parametrize('executor', ['mock_tmux_cmd', 'mock_TmuxCommand'])
32+
def test_allows_master_version(monkeypatch, executor):
33+
def mock_TmuxCommand(param):
3334
class Hi(object):
3435
stdout = ['tmux master']
3536
stderr = None
@@ -39,7 +40,16 @@ def execute(self):
3940

4041
return Hi()
4142

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

4454
assert has_minimum_version()
4555
assert has_gte_version(TMUX_MIN_VERSION)

0 commit comments

Comments
 (0)