Skip to content

Commit 7347d87

Browse files
committed
Split init / exec of tmux_cmd for debuggability
tmux_cmd initialization composes the command the command will be available in the instance attribute .cmd .execute() instance method will run tmux_cmd see Server.cmd for example of new usage Related #77
1 parent bf7cdca commit 7347d87

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

libtmux/common.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,12 @@ class tmux_cmd(object):
161161
162162
.. code-block:: python
163163
164-
proc = tmux_cmd('new-session', '-s%' % 'my session')
164+
c = tmux_cmd('new-session', '-s%' % 'my session')
165+
166+
# You can actually see the command in the .cmd attribute
167+
print(c.cmd)
168+
169+
proc = c.execute()
165170
166171
if proc.stderr:
167172
raise exc.LibTmuxException(
@@ -201,6 +206,8 @@ def __init__(self, *args, **kwargs):
201206

202207
self.cmd = cmd
203208

209+
def execute(self):
210+
cmd = self.cmd
204211
try:
205212
self.process = subprocess.Popen(
206213
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE
@@ -229,6 +236,7 @@ def __init__(self, *args, **kwargs):
229236
self.stdout = self.stderr[0]
230237

231238
logger.debug('self.stdout for %s: \n%s' % (' '.join(cmd), self.stdout))
239+
return self
232240

233241

234242
class TmuxMappingObject(MutableMapping):
@@ -455,7 +463,7 @@ def get_version():
455463
:class:`distutils.version.LooseVersion`
456464
tmux version according to :func:`libtmux.common.which`'s tmux
457465
"""
458-
proc = tmux_cmd('-V')
466+
proc = tmux_cmd('-V').execute()
459467
if proc.stderr:
460468
if proc.stderr[0] == 'tmux: unknown option -- V':
461469
if sys.platform.startswith("openbsd"): # openbsd has no tmux -V

libtmux/server.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -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)
129+
return tmux_cmd(*args, **kwargs).execute()
130130

131131
def _list_sessions(self):
132132
"""

tests/test_common.py

+18
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ class Hi(object):
3333
stdout = ['tmux master']
3434
stderr = None
3535

36+
def execute(self):
37+
return self
38+
3639
return Hi()
3740

3841
monkeypatch.setattr(libtmux.common, 'tmux_cmd', mock_tmux_cmd)
@@ -51,6 +54,9 @@ class Hi(object):
5154
stdout = ['tmux next-2.9']
5255
stderr = None
5356

57+
def execute(self):
58+
return self
59+
5460
return Hi()
5561

5662
monkeypatch.setattr(libtmux.common, 'tmux_cmd', mock_tmux_cmd)
@@ -66,6 +72,9 @@ def mock_tmux_cmd(param):
6672
class Hi(object):
6773
stderr = ['tmux: unknown option -- V']
6874

75+
def execute(self):
76+
return self
77+
6978
return Hi()
7079

7180
monkeypatch.setattr(libtmux.common, 'tmux_cmd', mock_tmux_cmd)
@@ -83,6 +92,9 @@ def mock_tmux_cmd(param):
8392
class Hi(object):
8493
stderr = ['tmux: unknown option -- V']
8594

95+
def execute(self):
96+
return self
97+
8698
return Hi()
8799

88100
monkeypatch.setattr(libtmux.common, 'tmux_cmd', mock_tmux_cmd)
@@ -183,6 +195,12 @@ def test_tmux_cmd_unicode(session):
183195
session.cmd('new-window', '-t', 3, '-n', 'юникод', '-F', u'Ελληνικά')
184196

185197

198+
def test_tmux_cmd_makes_cmd_available():
199+
"""tmux_cmd objects should make .cmd attribute available."""
200+
command = tmux_cmd('-V')
201+
assert hasattr(command, 'cmd')
202+
203+
186204
@pytest.mark.parametrize(
187205
"session_name,raises,exc_msg_regex",
188206
[

0 commit comments

Comments
 (0)