From c9598fb0b5694a7e739206bced0275ee8c7f3d2c Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Thu, 9 Nov 2017 19:33:24 -0600 Subject: [PATCH] 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 --- libtmux/common.py | 18 +++++++++++++----- libtmux/server.py | 2 +- tests/test_common.py | 15 +++++++++++++++ 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/libtmux/common.py b/libtmux/common.py index cb6cfa405..91019b6ae 100644 --- a/libtmux/common.py +++ b/libtmux/common.py @@ -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( @@ -178,6 +183,8 @@ def __init__(self, *args, **kwargs): self.cmd = cmd + def execute(self): + cmd = self.cmd try: self.process = subprocess.Popen( cmd, @@ -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): @@ -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)) @@ -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 @@ -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 diff --git a/libtmux/server.py b/libtmux/server.py index 904bddb63..2b1f1e61c 100644 --- a/libtmux/server.py +++ b/libtmux/server.py @@ -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. diff --git a/tests/test_common.py b/tests/test_common.py index 18c3f9278..94345e161 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -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) @@ -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') @@ -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: @@ -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'),