Skip to content

Commit 9fe718f

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 83eeb9d commit 9fe718f

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

Diff for: 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
@@ -225,6 +232,7 @@ def __init__(self, *args, **kwargs):
225232
self.stdout = self.stderr[0]
226233

227234
logger.debug('self.stdout for %s: \n%s' % (' '.join(cmd), self.stdout))
235+
return self
228236

229237

230238
class TmuxMappingObject(MutableMapping):
@@ -451,7 +459,7 @@ def get_version():
451459
:class:`distutils.version.LooseVersion`
452460
tmux version according to :func:`libtmux.common.which`'s tmux
453461
"""
454-
proc = tmux_cmd('-V')
462+
proc = tmux_cmd('-V').execute()
455463
if proc.stderr:
456464
if proc.stderr[0] == 'tmux: unknown option -- V':
457465
if sys.platform.startswith("openbsd"): # openbsd has no tmux -V

Diff for: 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
"""

Diff for: tests/test_common.py

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

37+
def execute(self):
38+
return self
39+
3740
return Hi()
3841

3942
monkeypatch.setattr(libtmux.common, 'tmux_cmd', mock_tmux_cmd)
@@ -52,6 +55,9 @@ class Hi(object):
5255
stdout = ['tmux next-2.9']
5356
stderr = None
5457

58+
def execute(self):
59+
return self
60+
5561
return Hi()
5662

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

76+
def execute(self):
77+
return self
78+
7079
return Hi()
7180

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

96+
def execute(self):
97+
return self
98+
8799
return Hi()
88100

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

186198

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

0 commit comments

Comments
 (0)