From ca8222e764756e61044a50e79669c463afd42319 Mon Sep 17 00:00:00 2001 From: mcarton Date: Wed, 12 Aug 2015 18:10:26 +0200 Subject: [PATCH] Add the `pacman_not_found` rule --- tests/rules/test_pacman_not_found.py | 48 ++++++++++++++++++++++++++++ thefuck/archlinux.py | 2 +- thefuck/rules/pacman.py | 4 +-- thefuck/rules/pacman_not_found.py | 24 ++++++++++++++ 4 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 tests/rules/test_pacman_not_found.py create mode 100644 thefuck/rules/pacman_not_found.py diff --git a/tests/rules/test_pacman_not_found.py b/tests/rules/test_pacman_not_found.py new file mode 100644 index 00000000..d772b23c --- /dev/null +++ b/tests/rules/test_pacman_not_found.py @@ -0,0 +1,48 @@ +import pytest +from mock import patch +from thefuck.rules import pacman_not_found +from thefuck.rules.pacman_not_found import match, get_new_command +from tests.utils import Command + +PKGFILE_OUTPUT_LLC = '''extra/llvm 3.6.0-5 /usr/bin/llc +extra/llvm35 3.5.2-13/usr/bin/llc''' + + +@pytest.mark.skipif(not getattr(pacman_not_found, 'enabled_by_default', True), + reason='Skip if pacman is not available') +@pytest.mark.parametrize('command', [ + Command(script='yaourt -S llc', stderr='error: target not found: llc'), + Command(script='pacman llc', stderr='error: target not found: llc'), + Command(script='sudo pacman llc', stderr='error: target not found: llc')]) +def test_match(command): + assert match(command, None) + + +@pytest.mark.parametrize('command', [ + Command(script='yaourt -S llc', stderr='error: target not found: llc'), + Command(script='pacman llc', stderr='error: target not found: llc'), + Command(script='sudo pacman llc', stderr='error: target not found: llc')]) +@patch('thefuck.archlinux.subprocess') +def test_match_mocked(subp_mock, command): + subp_mock.check_output.return_value = PKGFILE_OUTPUT_LLC + assert match(command, None) + + +@pytest.mark.skipif(not getattr(pacman_not_found, 'enabled_by_default', True), + reason='Skip if pacman is not available') +@pytest.mark.parametrize('command, fixed', [ + (Command(script='yaourt -S llc', stderr='error: target not found: llc'), ['yaourt -S extra/llvm', 'yaourt -S extra/llvm35']), + (Command(script='pacman -S llc', stderr='error: target not found: llc'), ['pacman -S extra/llvm', 'pacman -S extra/llvm35']), + (Command(script='sudo pacman -S llc', stderr='error: target not found: llc'), ['sudo pacman -S extra/llvm', 'sudo pacman -S extra/llvm35'])]) +def test_get_new_command(command, fixed): + assert get_new_command(command, None) == fixed + + +@pytest.mark.parametrize('command, fixed', [ + (Command(script='yaourt -S llc', stderr='error: target not found: llc'), ['yaourt -S extra/llvm', 'yaourt -S extra/llvm35']), + (Command(script='pacman -S llc', stderr='error: target not found: llc'), ['pacman -S extra/llvm', 'pacman -S extra/llvm35']), + (Command(script='sudo pacman -S llc', stderr='error: target not found: llc'), ['sudo pacman -S extra/llvm', 'sudo pacman -S extra/llvm35'])]) +@patch('thefuck.archlinux.subprocess') +def test_get_new_command_mocked(subp_mock, command, fixed): + subp_mock.check_output.return_value = PKGFILE_OUTPUT_LLC + assert get_new_command(command, None) == fixed diff --git a/thefuck/archlinux.py b/thefuck/archlinux.py index a60f063c..04b97149 100644 --- a/thefuck/archlinux.py +++ b/thefuck/archlinux.py @@ -11,7 +11,7 @@ def get_pkgfile(command): instead. """ try: - command = command.script.strip() + command = command.strip() if command.startswith('sudo '): command = command[5:] diff --git a/thefuck/rules/pacman.py b/thefuck/rules/pacman.py index 9b0a8873..04201cd4 100644 --- a/thefuck/rules/pacman.py +++ b/thefuck/rules/pacman.py @@ -3,11 +3,11 @@ from thefuck import shells def match(command, settings): - return 'not found' in command.stderr and get_pkgfile(command) + return 'not found' in command.stderr and get_pkgfile(command.script) def get_new_command(command, settings): - packages = get_pkgfile(command) + packages = get_pkgfile(command.script) formatme = shells.and_('{} -S {}', '{}') return [formatme.format(pacman, package, command.script) diff --git a/thefuck/rules/pacman_not_found.py b/thefuck/rules/pacman_not_found.py new file mode 100644 index 00000000..4ea1b64a --- /dev/null +++ b/thefuck/rules/pacman_not_found.py @@ -0,0 +1,24 @@ +""" Fixes wrong package names with pacman or yaourt. + +For example the `llc` program is in package `llvm` so this: + yaourt -S llc +should be: + yaourt -S llvm +""" + +from thefuck.utils import replace_command +from thefuck.archlinux import archlinux_env, get_pkgfile + + +def match(command, settings): + return (command.script.startswith(('pacman', 'sudo pacman', 'yaourt')) + and 'error: target not found:' in command.stderr) + + +def get_new_command(command, settings): + pgr = command.script.split()[-1] + + return replace_command(command, pgr, get_pkgfile(pgr)) + + +enabled_by_default, _ = archlinux_env()