mirror of
https://github.com/nvbn/thefuck.git
synced 2025-01-31 02:01:13 +00:00
Make no_command
work only when apt available
This commit is contained in:
parent
2eb777a5bb
commit
1503dcf294
@ -1,7 +1,7 @@
|
||||
from subprocess import PIPE
|
||||
from mock import patch, Mock
|
||||
import pytest
|
||||
from thefuck.rules.no_command import match, get_new_command
|
||||
from thefuck.rules.no_command_apt import match, get_new_command
|
||||
from thefuck.main import Command
|
||||
|
||||
|
||||
@ -19,8 +19,17 @@ vom: command not found
|
||||
'''
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def bins_exists(request):
|
||||
p = patch('thefuck.rules.no_command_apt.which',
|
||||
return_value=True)
|
||||
p.start()
|
||||
request.addfinalizer(p.stop)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures('bins_exists')
|
||||
def test_match(command_found, command_not_found):
|
||||
with patch('thefuck.rules.no_command.Popen') as Popen:
|
||||
with patch('thefuck.rules.no_command_apt.Popen') as Popen:
|
||||
Popen.return_value.stderr.read.return_value = command_found
|
||||
assert match(Command('aptget install vim', '', ''), None)
|
||||
Popen.assert_called_once_with('/usr/lib/command-not-found aptget',
|
||||
@ -28,7 +37,7 @@ def test_match(command_found, command_not_found):
|
||||
Popen.return_value.stderr.read.return_value = command_not_found
|
||||
assert not match(Command('ls', '', ''), None)
|
||||
|
||||
with patch('thefuck.rules.no_command.Popen') as Popen:
|
||||
with patch('thefuck.rules.no_command_apt.Popen') as Popen:
|
||||
Popen.return_value.stderr.read.return_value = command_found
|
||||
assert match(Command('sudo aptget install vim', '', ''),
|
||||
Mock(command_not_found='test'))
|
||||
@ -36,8 +45,9 @@ def test_match(command_found, command_not_found):
|
||||
shell=True, stderr=PIPE)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures('bins_exists')
|
||||
def test_get_new_command(command_found):
|
||||
with patch('thefuck.rules.no_command._get_output',
|
||||
with patch('thefuck.rules.no_command_apt._get_output',
|
||||
return_value=command_found.decode()):
|
||||
assert get_new_command(Command('aptget install vim', '', ''), None)\
|
||||
== 'apt-get install vim'
|
@ -1,19 +1,23 @@
|
||||
from subprocess import Popen, PIPE
|
||||
import re
|
||||
from thefuck.utils import which
|
||||
|
||||
|
||||
def _get_bin(settings):
|
||||
return getattr(settings, 'command_not_found', '/usr/lib/command-not-found')
|
||||
|
||||
|
||||
def _get_output(command, settings):
|
||||
name = command.script.split(' ')[command.script.startswith('sudo')]
|
||||
check_script = '{} {}'.format(getattr(settings, 'command_not_found',
|
||||
'/usr/lib/command-not-found'),
|
||||
name)
|
||||
check_script = '{} {}'.format(_get_bin(settings), name)
|
||||
result = Popen(check_script, shell=True, stderr=PIPE)
|
||||
return result.stderr.read().decode()
|
||||
|
||||
|
||||
def match(command, settings):
|
||||
output = _get_output(command, settings)
|
||||
return "No command" in output and "from package" in output
|
||||
if which('apt-get') and which(_get_bin(settings)):
|
||||
output = _get_output(command, settings)
|
||||
return "No command" in output and "from package" in output
|
||||
|
||||
|
||||
def get_new_command(command, settings):
|
19
thefuck/utils.py
Normal file
19
thefuck/utils.py
Normal file
@ -0,0 +1,19 @@
|
||||
import os
|
||||
|
||||
|
||||
def which(program):
|
||||
def is_exe(fpath):
|
||||
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
|
||||
|
||||
fpath, fname = os.path.split(program)
|
||||
if fpath:
|
||||
if is_exe(program):
|
||||
return program
|
||||
else:
|
||||
for path in os.environ["PATH"].split(os.pathsep):
|
||||
path = path.strip('"')
|
||||
exe_file = os.path.join(path, program)
|
||||
if is_exe(exe_file):
|
||||
return exe_file
|
||||
|
||||
return None
|
Loading…
x
Reference in New Issue
Block a user