mirror of
https://github.com/nvbn/thefuck.git
synced 2025-02-22 12:58:33 +00:00
#N/A: Make apt-get rule more accurate, remove unstable tests
This commit is contained in:
parent
b2a5009116
commit
0c2083485d
@ -1,68 +1,39 @@
|
|||||||
import pytest
|
import pytest
|
||||||
from mock import Mock, patch
|
|
||||||
from thefuck.rules import apt_get
|
|
||||||
from thefuck.rules.apt_get import match, get_new_command
|
from thefuck.rules.apt_get import match, get_new_command
|
||||||
from tests.utils import Command
|
from tests.utils import Command
|
||||||
|
|
||||||
|
|
||||||
# python-commandnotfound is available in ubuntu 14.04+
|
@pytest.mark.parametrize('command, packages', [
|
||||||
@pytest.mark.skipif(not getattr(apt_get, 'enabled_by_default', True),
|
|
||||||
reason='Skip if python-commandnotfound is not available')
|
|
||||||
@pytest.mark.parametrize('command', [
|
|
||||||
Command(script='vim', stderr='vim: command not found')])
|
|
||||||
def test_match(command):
|
|
||||||
assert match(command)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('command, return_value', [
|
|
||||||
(Command(script='vim', stderr='vim: command not found'),
|
(Command(script='vim', stderr='vim: command not found'),
|
||||||
[('vim', 'main'), ('vim-tiny', 'main')]),
|
[('vim', 'main'), ('vim-tiny', 'main')]),
|
||||||
(Command(script='sudo vim', stderr='vim: command not found'),
|
(Command(script='sudo vim', stderr='vim: command not found'),
|
||||||
[('vim', 'main'), ('vim-tiny', 'main')])])
|
[('vim', 'main'), ('vim-tiny', 'main')])])
|
||||||
@patch('thefuck.rules.apt_get.CommandNotFound', create=True)
|
def test_match(mocker, command, packages):
|
||||||
@patch.multiple(apt_get, create=True, apt_get='apt_get')
|
mocker.patch('thefuck.rules.apt_get.which', return_value=None)
|
||||||
def test_match_mocked(cmdnf_mock, command, return_value):
|
mock = mocker.patch('thefuck.rules.apt_get.command_not_found')
|
||||||
get_packages = Mock(return_value=return_value)
|
mock.getPackages.return_value = packages
|
||||||
cmdnf_mock.CommandNotFound.return_value = Mock(getPackages=get_packages)
|
|
||||||
assert match(command)
|
assert match(command)
|
||||||
assert cmdnf_mock.CommandNotFound.called
|
|
||||||
assert get_packages.called
|
|
||||||
|
|
||||||
|
|
||||||
# python-commandnotfound is available in ubuntu 14.04+
|
@pytest.mark.parametrize('command, packages, which', [
|
||||||
@pytest.mark.skipif(not getattr(apt_get, 'enabled_by_default', True),
|
(Command(script='a_bad_cmd', stderr='a_bad_cmd: command not found'),
|
||||||
reason='Skip if python-commandnotfound is not available')
|
[], None),
|
||||||
@pytest.mark.parametrize('command', [
|
(Command(script='vim', stderr=''), [], None),
|
||||||
Command(script='a_bad_cmd', stderr='a_bad_cmd: command not found'),
|
(Command(), [], None),
|
||||||
Command(script='vim', stderr=''), Command()])
|
(Command(script='vim', stderr='vim: command not found'),
|
||||||
def test_not_match(command):
|
['vim'], '/usr/bin/vim'),
|
||||||
|
(Command(script='sudo vim', stderr='vim: command not found'),
|
||||||
|
['vim'], '/usr/bin/vim')])
|
||||||
|
def test_not_match(mocker, command, packages, which):
|
||||||
|
mocker.patch('thefuck.rules.apt_get.which', return_value=which)
|
||||||
|
mock = mocker.patch('thefuck.rules.apt_get.command_not_found')
|
||||||
|
mock.getPackages.return_value = packages
|
||||||
|
|
||||||
assert not match(command)
|
assert not match(command)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('command, return_value', [
|
@pytest.mark.parametrize('command, new_command, packages', [
|
||||||
(Command(script='a_bad_cmd', stderr='a_bad_cmd: command not found'), []),
|
|
||||||
(Command(script='vim', stderr=''), []), (Command(), [])])
|
|
||||||
@patch('thefuck.rules.apt_get.CommandNotFound', create=True)
|
|
||||||
@patch.multiple(apt_get, create=True, apt_get='apt_get')
|
|
||||||
def test_not_match_mocked(cmdnf_mock, command, return_value):
|
|
||||||
get_packages = Mock(return_value=return_value)
|
|
||||||
cmdnf_mock.CommandNotFound.return_value = Mock(getPackages=get_packages)
|
|
||||||
assert not match(command)
|
|
||||||
|
|
||||||
|
|
||||||
# python-commandnotfound is available in ubuntu 14.04+
|
|
||||||
@pytest.mark.skipif(not getattr(apt_get, 'enabled_by_default', True),
|
|
||||||
reason='Skip if python-commandnotfound is not available')
|
|
||||||
@pytest.mark.parametrize('command, new_command', [
|
|
||||||
(Command('vim'), 'sudo apt-get install vim && vim'),
|
|
||||||
(Command('convert'), 'sudo apt-get install imagemagick && convert'),
|
|
||||||
(Command('sudo vim'), 'sudo apt-get install vim && sudo vim'),
|
|
||||||
(Command('sudo convert'), 'sudo apt-get install imagemagick && sudo convert')])
|
|
||||||
def test_get_new_command(command, new_command):
|
|
||||||
assert get_new_command(command) == new_command
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('command, new_command, return_value', [
|
|
||||||
(Command('vim'), 'sudo apt-get install vim && vim',
|
(Command('vim'), 'sudo apt-get install vim && vim',
|
||||||
[('vim', 'main'), ('vim-tiny', 'main')]),
|
[('vim', 'main'), ('vim-tiny', 'main')]),
|
||||||
(Command('convert'), 'sudo apt-get install imagemagick && convert',
|
(Command('convert'), 'sudo apt-get install imagemagick && convert',
|
||||||
@ -73,9 +44,7 @@ def test_get_new_command(command, new_command):
|
|||||||
(Command('sudo convert'), 'sudo apt-get install imagemagick && sudo convert',
|
(Command('sudo convert'), 'sudo apt-get install imagemagick && sudo convert',
|
||||||
[('imagemagick', 'main'),
|
[('imagemagick', 'main'),
|
||||||
('graphicsmagick-imagemagick-compat', 'universe')])])
|
('graphicsmagick-imagemagick-compat', 'universe')])])
|
||||||
@patch('thefuck.rules.apt_get.CommandNotFound', create=True)
|
def test_get_new_command(mocker, command, new_command, packages):
|
||||||
@patch.multiple(apt_get, create=True, apt_get='apt_get')
|
mock = mocker.patch('thefuck.rules.apt_get.command_not_found')
|
||||||
def test_get_new_command_mocked(cmdnf_mock, command, new_command, return_value):
|
mock.getPackages.return_value = packages
|
||||||
get_packages = Mock(return_value=return_value)
|
|
||||||
cmdnf_mock.CommandNotFound.return_value = Mock(getPackages=get_packages)
|
|
||||||
assert get_new_command(command) == new_command
|
assert get_new_command(command) == new_command
|
||||||
|
@ -1,32 +1,43 @@
|
|||||||
from thefuck.specific.apt import apt_available
|
from thefuck.specific.apt import apt_available
|
||||||
from thefuck.utils import memoize
|
from thefuck.utils import memoize, which
|
||||||
from thefuck.shells import shell
|
from thefuck.shells import shell
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import CommandNotFound
|
from CommandNotFound import CommandNotFound
|
||||||
|
|
||||||
|
command_not_found = CommandNotFound()
|
||||||
enabled_by_default = apt_available
|
enabled_by_default = apt_available
|
||||||
except ImportError:
|
except ImportError:
|
||||||
enabled_by_default = False
|
enabled_by_default = False
|
||||||
|
|
||||||
|
|
||||||
|
def _get_executable(command):
|
||||||
|
if command.script_parts[0] == 'sudo':
|
||||||
|
return command.script_parts[1]
|
||||||
|
else:
|
||||||
|
return command.script_parts[0]
|
||||||
|
|
||||||
|
|
||||||
@memoize
|
@memoize
|
||||||
def get_package(command):
|
def get_package(executable):
|
||||||
try:
|
try:
|
||||||
c = CommandNotFound.CommandNotFound()
|
packages = command_not_found.getPackages(executable)
|
||||||
cmd = command.split(' ')
|
return packages[0][0]
|
||||||
pkgs = c.getPackages(cmd[0] if cmd[0] != 'sudo' else cmd[1])
|
|
||||||
name, _ = pkgs[0]
|
|
||||||
return name
|
|
||||||
except IndexError:
|
except IndexError:
|
||||||
# IndexError is thrown when no matching package is found
|
# IndexError is thrown when no matching package is found
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def match(command):
|
def match(command):
|
||||||
return 'not found' in command.stderr and get_package(command.script)
|
if 'not found' in command.stderr:
|
||||||
|
executable = _get_executable(command)
|
||||||
|
return not which(executable) and get_package(executable)
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
def get_new_command(command):
|
def get_new_command(command):
|
||||||
name = get_package(command.script)
|
executable = _get_executable(command)
|
||||||
|
name = get_package(executable)
|
||||||
formatme = shell.and_('sudo apt-get install {}', '{}')
|
formatme = shell.and_('sudo apt-get install {}', '{}')
|
||||||
return formatme.format(name, command.script)
|
return formatme.format(name, command.script)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user