Skip to content

Commit

Permalink
Split init / exec of tmux_cmd for debuggability
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tony committed Aug 16, 2020
1 parent bf7cdca commit 7347d87
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
12 changes: 10 additions & 2 deletions libtmux/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,12 @@ class tmux_cmd(object):
.. code-block:: python
proc = tmux_cmd('new-session', '-s%' % 'my session')
c = tmux_cmd('new-session', '-s%' % 'my session')
# You can actually see the command in the .cmd attribute
print(c.cmd)
proc = c.execute()
if proc.stderr:
raise exc.LibTmuxException(
Expand Down Expand Up @@ -201,6 +206,8 @@ def __init__(self, *args, **kwargs):

self.cmd = cmd

def execute(self):
cmd = self.cmd
try:
self.process = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE
Expand Down Expand Up @@ -229,6 +236,7 @@ def __init__(self, *args, **kwargs):
self.stdout = self.stderr[0]

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


class TmuxMappingObject(MutableMapping):
Expand Down Expand Up @@ -455,7 +463,7 @@ def get_version():
:class:`distutils.version.LooseVersion`
tmux version according to :func:`libtmux.common.which`'s tmux
"""
proc = tmux_cmd('-V')
proc = tmux_cmd('-V').execute()
if proc.stderr:
if proc.stderr[0] == 'tmux: unknown option -- V':
if sys.platform.startswith("openbsd"): # openbsd has no tmux -V
Expand Down
2 changes: 1 addition & 1 deletion libtmux/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def cmd(self, *args, **kwargs):
else:
raise ValueError('Server.colors must equal 88 or 256')

return tmux_cmd(*args, **kwargs)
return tmux_cmd(*args, **kwargs).execute()

def _list_sessions(self):
"""
Expand Down
18 changes: 18 additions & 0 deletions tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class Hi(object):
stdout = ['tmux master']
stderr = None

def execute(self):
return self

return Hi()

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

def execute(self):
return self

return Hi()

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

def execute(self):
return self

return Hi()

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

def execute(self):
return self

return Hi()

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


def test_tmux_cmd_makes_cmd_available():
"""tmux_cmd objects should make .cmd attribute available."""
command = tmux_cmd('-V')
assert hasattr(command, 'cmd')


@pytest.mark.parametrize(
"session_name,raises,exc_msg_regex",
[
Expand Down

0 comments on commit 7347d87

Please sign in to comment.