Skip to content

Commit 0f0b7c6

Browse files
committed
Merge branch 'fix_which_finding'
2 parents f377c75 + d6ef3c1 commit 0f0b7c6

File tree

4 files changed

+59
-17
lines changed

4 files changed

+59
-17
lines changed

doc/api.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ Exceptions
7474

7575
.. autoexception:: libtmux.exc.LibTmuxException
7676

77+
.. autoexception:: libtmux.exc.TmuxCommandNotFound
78+
7779
.. autoexception:: libtmux.exc.TmuxSessionExists
7880

7981
.. autoexception:: libtmux.exc.BadSessionName

libtmux/common.py

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,12 @@ class tmux_cmd(object):
127127

128128
""":term:`tmux(1)` command via :py:mod:`subprocess`.
129129
130+
:param tmux_search_paths: Default PATHs to search tmux for, defaults to
131+
``default_paths`` used in :func:`which`.
132+
:type tmux_search_path: list
133+
:param append_env_path: Append environment PATHs to tmux search paths.
134+
:type append_env_path: bool
135+
130136
Usage::
131137
132138
proc = tmux_cmd('new-session', '-s%' % 'my session')
@@ -150,7 +156,17 @@ class tmux_cmd(object):
150156
"""
151157

152158
def __init__(self, *args, **kwargs):
153-
cmd = [which('tmux')]
159+
tmux_bin = which(
160+
'tmux',
161+
default_paths=kwargs.get('tmux_search_paths', [
162+
'/bin', '/sbin', '/usr/bin', '/usr/sbin', '/usr/local/bin'
163+
]),
164+
append_env_path=kwargs.get('append_env_path', True)
165+
)
166+
if not tmux_bin:
167+
raise(exc.TmuxCommandNotFound)
168+
169+
cmd = [tmux_bin]
154170
cmd += args # add the command arguments to cmd
155171
cmd = [str(c) for c in cmd]
156172

@@ -187,8 +203,10 @@ def __init__(self, *args, **kwargs):
187203
if not self.stdout:
188204
self.stdout = self.stderr[0]
189205

190-
logger.debug('self.stdout for %s: \n%s' %
191-
(' '.join(cmd), self.stdout))
206+
logger.debug(
207+
'self.stdout for %s: \n%s' %
208+
(' '.join(cmd), self.stdout)
209+
)
192210

193211

194212
class TmuxMappingObject(collections.MutableMapping):
@@ -329,10 +347,9 @@ def get_by_id(self, id):
329347
return None
330348

331349

332-
def which(exe=None,
333-
default_paths=[
334-
'/bin', '/sbin', '/usr/bin', '/usr/sbin', '/usr/local/bin']
335-
):
350+
def which(exe=None, default_paths=[
351+
'/bin', '/sbin', '/usr/bin', '/usr/sbin', '/usr/local/bin'
352+
], append_env_path=True):
336353
"""Return path of bin. Python clone of /usr/bin/which.
337354
338355
from salt.util - https://www.github.com/saltstack/salt - license apache
@@ -341,6 +358,8 @@ def which(exe=None,
341358
:type exe: string
342359
:param default_path: Application to search PATHs for.
343360
:type default_path: list
361+
:param append_env_path: Append PATHs in environmental variables.
362+
:type append_env_path: bool
344363
:rtype: string
345364
346365
"""
@@ -356,12 +375,15 @@ def _is_executable_file_or_link(exe):
356375
# Enhance POSIX path for the reliability at some environments, when
357376
# $PATH is changing. This also keeps order, where 'first came, first
358377
# win' for cases to find optional alternatives
359-
search_path = os.environ.get('PATH') and \
360-
os.environ['PATH'].split(os.pathsep) or list()
378+
if append_env_path:
379+
search_path = os.environ.get('PATH') and \
380+
os.environ['PATH'].split(os.pathsep) or list()
381+
else:
382+
search_path = []
383+
361384
for default_path in default_paths:
362385
if default_path not in search_path:
363386
search_path.append(default_path)
364-
os.environ['PATH'] = os.pathsep.join(search_path)
365387
for path in search_path:
366388
full_path = os.path.join(path, exe)
367389
if _is_executable_file_or_link(full_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: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55

66
import pytest
77

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

1113
version_regex = re.compile(r'([0-9]\.[0-9])|(master)')
1214

@@ -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,18 @@ def test_error_version_less_1_7():
5558
has_required_tmux_version('1.9a')
5659

5760

58-
def test_which_no_tmuxp_found(monkeypatch):
59-
monkeypatch.setenv("PATH", "/")
60-
which('tmuxp')
61-
which('tmuxp', '/')
61+
def test_which_no_bin_found():
62+
assert which('top')
63+
assert which('top', default_paths=[])
64+
assert not which('top', default_paths=[], append_env_path=False)
65+
assert not which('top', default_paths=['/'], append_env_path=False)
66+
67+
68+
def test_tmux_cmd_raises_on_not_found():
69+
with pytest.raises(TmuxCommandNotFound):
70+
tmux_cmd('-V', tmux_search_paths=[], append_env_path=False)
71+
72+
tmux_cmd('-V')
6273

6374

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

0 commit comments

Comments
 (0)