Skip to content

Commit 5520d71

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 5166809 commit 5520d71

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
@@ -160,7 +160,12 @@ class tmux_cmd(object):
160160
161161
.. code-block:: python
162162
163-
proc = tmux_cmd('new-session', '-s%' % 'my session')
163+
c = tmux_cmd('new-session', '-s%' % 'my session')
164+
165+
# You can actually see the command in the .cmd attribute
166+
print(c.cmd)
167+
168+
proc = c.execute()
164169
165170
if proc.stderr:
166171
raise exc.LibTmuxException(
@@ -200,6 +205,8 @@ def __init__(self, *args, **kwargs):
200205

201206
self.cmd = cmd
202207

208+
def execute(self):
209+
cmd = self.cmd
203210
try:
204211
self.process = subprocess.Popen(
205212
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE
@@ -224,6 +231,7 @@ def __init__(self, *args, **kwargs):
224231
self.stdout = self.stderr[0]
225232

226233
logger.debug("self.stdout for %s: \n%s" % (" ".join(cmd), self.stdout))
234+
return self
227235

228236

229237
class TmuxMappingObject(MutableMapping):
@@ -450,7 +458,7 @@ def get_version():
450458
:class:`distutils.version.LooseVersion`
451459
tmux version according to :func:`libtmux.common.which`'s tmux
452460
"""
453-
proc = tmux_cmd("-V")
461+
proc = tmux_cmd("-V").execute()
454462
if proc.stderr:
455463
if proc.stderr[0] == "tmux: unknown option -- V":
456464
if sys.platform.startswith("openbsd"): # openbsd has no tmux -V

libtmux/server.py

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

128128
def _list_sessions(self):
129129
"""

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", "Ελληνικά")
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)