Skip to content

Commit 4c4aea2

Browse files
committed
Add TmuxCommandNotFound to tmux_cmd, fix which
- common.which was including environment PATHs, added an kwarg to suppress. - fixed tests to assert unfound path is a None, and tested for append_env_path - fixed test function name to be more appropriate
1 parent f377c75 commit 4c4aea2

File tree

3 files changed

+37
-14
lines changed

3 files changed

+37
-14
lines changed

libtmux/common.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,12 @@ class tmux_cmd(object):
150150
"""
151151

152152
def __init__(self, *args, **kwargs):
153-
cmd = [which('tmux')]
153+
154+
tmux_bin = which('tmux')
155+
if not tmux_bin:
156+
raise(exc.TmuxCommandNotFound)
157+
158+
cmd = [tmux_bin]
154159
cmd += args # add the command arguments to cmd
155160
cmd = [str(c) for c in cmd]
156161

@@ -187,8 +192,10 @@ def __init__(self, *args, **kwargs):
187192
if not self.stdout:
188193
self.stdout = self.stderr[0]
189194

190-
logger.debug('self.stdout for %s: \n%s' %
191-
(' '.join(cmd), self.stdout))
195+
logger.debug(
196+
'self.stdout for %s: \n%s' %
197+
(' '.join(cmd), self.stdout)
198+
)
192199

193200

194201
class TmuxMappingObject(collections.MutableMapping):
@@ -329,10 +336,9 @@ def get_by_id(self, id):
329336
return None
330337

331338

332-
def which(exe=None,
333-
default_paths=[
334-
'/bin', '/sbin', '/usr/bin', '/usr/sbin', '/usr/local/bin']
335-
):
339+
def which(exe=None, default_paths=[
340+
'/bin', '/sbin', '/usr/bin', '/usr/sbin', '/usr/local/bin'
341+
], append_env_path=True):
336342
"""Return path of bin. Python clone of /usr/bin/which.
337343
338344
from salt.util - https://www.github.com/saltstack/salt - license apache
@@ -341,6 +347,8 @@ def which(exe=None,
341347
:type exe: string
342348
:param default_path: Application to search PATHs for.
343349
:type default_path: list
350+
:param append_env_path: Append PATHs in environmental variables.
351+
:type append_env_path: bool
344352
:rtype: string
345353
346354
"""
@@ -356,8 +364,12 @@ def _is_executable_file_or_link(exe):
356364
# Enhance POSIX path for the reliability at some environments, when
357365
# $PATH is changing. This also keeps order, where 'first came, first
358366
# win' for cases to find optional alternatives
359-
search_path = os.environ.get('PATH') and \
360-
os.environ['PATH'].split(os.pathsep) or list()
367+
if append_env_path:
368+
search_path = os.environ.get('PATH') and \
369+
os.environ['PATH'].split(os.pathsep) or list()
370+
else:
371+
search_path = []
372+
361373
for default_path in default_paths:
362374
if default_path not in search_path:
363375
search_path.append(default_path)

libtmux/exc.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ class TmuxSessionExists(LibTmuxException):
2020
pass
2121

2222

23+
class TmuxCommandNotFound(LibTmuxException):
24+
25+
"""Application binary for tmux not found."""
26+
27+
pass
28+
29+
2330
class BadSessionName(LibTmuxException):
2431

2532
"""Disallowed session name for tmux (empty, contains periods or colons)."""

tests/test_common.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
import pytest
77

8-
from libtmux.common import has_required_tmux_version, which, session_check_name, is_version
8+
from libtmux.common import (
9+
has_required_tmux_version, which, session_check_name, is_version
10+
)
911
from libtmux.exc import LibTmuxException, BadSessionName
1012

1113
version_regex = re.compile(r'([0-9]\.[0-9])|(master)')
@@ -38,10 +40,11 @@ def test_ignores_letter_versions():
3840
assert result == r'1.8'
3941

4042
# Should not throw
41-
assert type(is_version('1.8')) is bool
43+
assert type(is_version('1.8')) is bool
4244
assert type(is_version('1.8a')) is bool
4345
assert type(is_version('1.9a')) is bool
4446

47+
4548
def test_error_version_less_1_7():
4649
with pytest.raises(LibTmuxException) as excinfo:
4750
has_required_tmux_version('1.7')
@@ -55,10 +58,11 @@ def test_error_version_less_1_7():
5558
has_required_tmux_version('1.9a')
5659

5760

58-
def test_which_no_tmuxp_found(monkeypatch):
61+
def test_which_no_bin_found(monkeypatch):
5962
monkeypatch.setenv("PATH", "/")
60-
which('tmuxp')
61-
which('tmuxp', '/')
63+
assert which('top')
64+
assert which('top', default_paths=['/'])
65+
assert not which('top', default_paths=['/'], append_env_path=False)
6266

6367

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

0 commit comments

Comments
 (0)