From 54b5cd61226994ebd5700fceb2e184ecfdc15f5f Mon Sep 17 00:00:00 2001 From: Namwoo Kim Date: Thu, 23 Apr 2015 17:42:03 +0900 Subject: [PATCH] Add a support for brew unavailable formulas --- tests/rules/test_brew_install.py | 49 ++++++++++++++++++++++++++++++++ thefuck/rules/brew_install.py | 43 ++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 tests/rules/test_brew_install.py create mode 100644 thefuck/rules/brew_install.py diff --git a/tests/rules/test_brew_install.py b/tests/rules/test_brew_install.py new file mode 100644 index 00000000..bf06cd95 --- /dev/null +++ b/tests/rules/test_brew_install.py @@ -0,0 +1,49 @@ +import pytest +from thefuck.types import Command +from thefuck.rules.brew_install import match, get_new_command +from thefuck.rules.brew_install import brew_formulas + + +@pytest.fixture +def brew_no_available_formula(): + return '''Error: No available formula for elsticsearch ''' + + +@pytest.fixture +def brew_install_no_argument(): + return '''This command requires a formula argument''' + + +@pytest.fixture +def brew_already_installed(): + return '''Warning: git-2.3.5 already installed''' + + +def _is_not_okay_to_test(): + if 'elasticsearch' not in brew_formulas: + return True + return False + + +@pytest.mark.skipif(_is_not_okay_to_test(), + reason='No need to run if there\'s no formula') +def test_match(brew_no_available_formula, brew_already_installed, + brew_install_no_argument): + assert match(Command('brew install elsticsearch', '', + brew_no_available_formula), None) + assert not match(Command('brew install git', '', + brew_already_installed), None) + assert not match(Command('brew install', '', brew_install_no_argument), + None) + + +@pytest.mark.skipif(_is_not_okay_to_test(), + reason='No need to run if there\'s no formula') +def test_get_new_command(brew_no_available_formula): + assert get_new_command(Command('brew install elsticsearch', '', + brew_no_available_formula), None)\ + == 'brew install elasticsearch' + + assert get_new_command(Command('brew install aa', '', + brew_no_available_formula), + None) != 'brew install aha' diff --git a/thefuck/rules/brew_install.py b/thefuck/rules/brew_install.py new file mode 100644 index 00000000..0f339506 --- /dev/null +++ b/thefuck/rules/brew_install.py @@ -0,0 +1,43 @@ +import difflib +import os +import re +from subprocess import check_output + +import thefuck.logs + +# Formulars are base on each local system's status +brew_formulas = [] +try: + brew_path_prefix = check_output(['brew', '--prefix']).strip() + brew_formula_path = brew_path_prefix + '/Library/Formula' + + for file_name in os.listdir(brew_formula_path): + if file_name.endswith('.rb'): + brew_formulas.append(file_name.replace('.rb', '')) +except: + pass + + +def _get_similar_formulars(formula_name): + return difflib.get_close_matches(formula_name, brew_formulas, 1, 0.85) + + +def match(command, settings): + is_proper_command = ('brew install' in command.script and + 'No available formula' in command.stderr) + + has_possible_formulas = False + if is_proper_command: + formula = re.findall(r'Error: No available formula for ([a-z]+)', + command.stderr)[0] + has_possible_formulas = len(_get_similar_formulars(formula)) > 0 + + return has_possible_formulas + + +def get_new_command(command, settings): + not_exist_formula = re.findall(r'Error: No available formula for ([a-z]+)', + command.stderr)[0] + exist_formula = _get_similar_formulars(not_exist_formula)[0] + + return command.script.replace(not_exist_formula, exist_formula, 1)