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'),