Skip to content

Commit 1c8f8ef

Browse files
committed
Add TmuxCommand, wrap tmux_cmd in old behavior
1 parent 5520d71 commit 1c8f8ef

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
@@ -142,7 +142,7 @@ def show_environment(self, name=None):
142142
return vars_dict
143143

144144

145-
class tmux_cmd(object):
145+
class TmuxCommand(object):
146146

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

190190
def __init__(self, *args, **kwargs):
@@ -234,6 +234,38 @@ def execute(self):
234234
return self
235235

236236

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

239271
r"""Base: :py:class:`MutableMapping`.
@@ -458,7 +490,7 @@ def get_version():
458490
:class:`distutils.version.LooseVersion`
459491
tmux version according to :func:`libtmux.common.which`'s tmux
460492
"""
461-
proc = tmux_cmd("-V").execute()
493+
proc = tmux_cmd("-V")
462494
if proc.stderr:
463495
if proc.stderr[0] == "tmux: unknown option -- V":
464496
if sys.platform.startswith("openbsd"): # openbsd has no tmux -V

libtmux/server.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
from . import exc, formats
1111
from .common import (
1212
EnvironmentMixin,
13+
TmuxCommand,
1314
TmuxRelationalObject,
1415
has_gte_version,
1516
session_check_name,
16-
tmux_cmd,
1717
)
1818
from .session import Session
1919

@@ -123,7 +123,7 @@ def cmd(self, *args, **kwargs):
123123
else:
124124
raise ValueError("Server.colors must equal 88 or 256")
125125

126-
return tmux_cmd(*args, **kwargs).execute()
126+
return TmuxCommand(*args, **kwargs).execute()
127127

128128
def _list_sessions(self):
129129
"""
@@ -132,7 +132,7 @@ def _list_sessions(self):
132132
Retrieved from ``$ tmux(1) list-sessions`` stdout.
133133
134134
The :py:obj:`list` is derived from ``stdout`` in
135-
:class:`common.tmux_cmd` which wraps :py:class:`subprocess.Popen`.
135+
:class:`common.TmuxCommand` which wraps :py:class:`subprocess.Popen`.
136136
137137
Returns
138138
-------
@@ -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
-------
@@ -260,7 +260,7 @@ def _list_panes(self):
260260
Retrieved from ``$ tmux(1) list-panes`` stdout.
261261
262262
The :py:obj:`list` is derived from ``stdout`` in
263-
:class:`util.tmux_cmd` which wraps :py:class:`subprocess.Popen`.
263+
:class:`util.TmuxCommand` which wraps :py:class:`subprocess.Popen`.
264264
265265
Returns
266266
-------

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)