1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-02-20 20:09:07 +00:00

#N/A: Make apt-get rule more accurate, remove unstable tests

This commit is contained in:
Vladimir Iakovlev 2016-08-13 15:30:57 +03:00
parent b2a5009116
commit 0c2083485d
2 changed files with 45 additions and 65 deletions

View File

@ -1,68 +1,39 @@
import pytest
from mock import Mock, patch
from thefuck.rules import apt_get
from thefuck.rules.apt_get import match, get_new_command
from tests.utils import 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', [
Command(script='vim', stderr='vim: command not found')])
def test_match(command):
assert match(command)
@pytest.mark.parametrize('command, return_value', [
@pytest.mark.parametrize('command, packages', [
(Command(script='vim', stderr='vim: command not found'),
[('vim', 'main'), ('vim-tiny', 'main')]),
(Command(script='sudo vim', stderr='vim: command not found'),
[('vim', 'main'), ('vim-tiny', 'main')])])
@patch('thefuck.rules.apt_get.CommandNotFound', create=True)
@patch.multiple(apt_get, create=True, apt_get='apt_get')
def test_match_mocked(cmdnf_mock, command, return_value):
get_packages = Mock(return_value=return_value)
cmdnf_mock.CommandNotFound.return_value = Mock(getPackages=get_packages)
def test_match(mocker, command, packages):
mocker.patch('thefuck.rules.apt_get.which', return_value=None)
mock = mocker.patch('thefuck.rules.apt_get.command_not_found')
mock.getPackages.return_value = packages
assert match(command)
assert cmdnf_mock.CommandNotFound.called
assert get_packages.called
# 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', [
Command(script='a_bad_cmd', stderr='a_bad_cmd: command not found'),
Command(script='vim', stderr=''), Command()])
def test_not_match(command):
@pytest.mark.parametrize('command, packages, which', [
(Command(script='a_bad_cmd', stderr='a_bad_cmd: command not found'),
[], None),
(Command(script='vim', stderr=''), [], None),
(Command(), [], None),
(Command(script='vim', stderr='vim: command not found'),
['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)
@pytest.mark.parametrize('command, return_value', [
(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', [
@pytest.mark.parametrize('command, new_command, packages', [
(Command('vim'), 'sudo apt-get install vim && vim',
[('vim', 'main'), ('vim-tiny', 'main')]),
(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',
[('imagemagick', 'main'),
('graphicsmagick-imagemagick-compat', 'universe')])])
@patch('thefuck.rules.apt_get.CommandNotFound', create=True)
@patch.multiple(apt_get, create=True, apt_get='apt_get')
def test_get_new_command_mocked(cmdnf_mock, command, new_command, return_value):
get_packages = Mock(return_value=return_value)
cmdnf_mock.CommandNotFound.return_value = Mock(getPackages=get_packages)
def test_get_new_command(mocker, command, new_command, packages):
mock = mocker.patch('thefuck.rules.apt_get.command_not_found')
mock.getPackages.return_value = packages
assert get_new_command(command) == new_command

View File

@ -1,32 +1,43 @@
from thefuck.specific.apt import apt_available
from thefuck.utils import memoize
from thefuck.utils import memoize, which
from thefuck.shells import shell
try:
import CommandNotFound
from CommandNotFound import CommandNotFound
command_not_found = CommandNotFound()
enabled_by_default = apt_available
except ImportError:
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
def get_package(command):
def get_package(executable):
try:
c = CommandNotFound.CommandNotFound()
cmd = command.split(' ')
pkgs = c.getPackages(cmd[0] if cmd[0] != 'sudo' else cmd[1])
name, _ = pkgs[0]
return name
packages = command_not_found.getPackages(executable)
return packages[0][0]
except IndexError:
# IndexError is thrown when no matching package is found
return None
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):
name = get_package(command.script)
executable = _get_executable(command)
name = get_package(executable)
formatme = shell.and_('sudo apt-get install {}', '{}')
return formatme.format(name, command.script)