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 Nov 10, 2017
1 parent 84fd521 commit c9598fb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
18 changes: 13 additions & 5 deletions libtmux/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,12 @@ class tmux_cmd(object):
Usage::
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 @@ -178,6 +183,8 @@ def __init__(self, *args, **kwargs):

self.cmd = cmd

def execute(self):
cmd = self.cmd
try:
self.process = subprocess.Popen(
cmd,
Expand Down Expand Up @@ -213,6 +220,7 @@ def __init__(self, *args, **kwargs):
'self.stdout for %s: \n%s' %
(' '.join(cmd), self.stdout)
)
return self


class TmuxMappingObject(collections.MutableMapping):
Expand Down Expand Up @@ -260,7 +268,7 @@ def __len__(self):
def __getattr__(self, key):
try:
return self._info[self.formatter_prefix + key]
except:
except KeyError:
raise AttributeError('%s has no property %s' %
(self.__class__, key))

Expand Down Expand Up @@ -354,8 +362,8 @@ def get_by_id(self, id):


def which(exe=None, default_paths=[
'/bin', '/sbin', '/usr/bin', '/usr/sbin', '/usr/local/bin'
], append_env_path=True):
'/bin', '/sbin', '/usr/bin', '/usr/sbin', '/usr/local/bin'
], append_env_path=True):
"""Return path of bin. Python clone of /usr/bin/which.
from salt.util - https://www.github.com/saltstack/salt - license apache
Expand Down Expand Up @@ -413,7 +421,7 @@ def get_version():
:returns: tmux version
:rtype: :class:`distutils.version.LooseVersion`
"""
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 @@ -96,7 +96,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):
"""Return list of sessions in :py:obj:`dict` form.
Expand Down
15 changes: 15 additions & 0 deletions tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ def mock_tmux_cmd(param):
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 @@ -42,6 +45,9 @@ def test_get_version_openbsd(monkeypatch):
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)
monkeypatch.setattr(sys, 'platform', 'openbsd 5.2')
Expand All @@ -59,6 +65,9 @@ def test_get_version_too_low(monkeypatch):
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)
with pytest.raises(LibTmuxException) as exc_info:
Expand Down Expand Up @@ -153,6 +162,12 @@ def test_tmux_cmd_raises_on_not_found():
tmux_cmd('-V')


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", [
('', True, 'may not be empty'),
(None, True, 'may not be empty'),
Expand Down

0 comments on commit c9598fb

Please sign in to comment.