diff --git a/tests/rules/test_brew_install.py b/tests/rules/test_brew_install.py index 2ea58f1f..e8eae865 100644 --- a/tests/rules/test_brew_install.py +++ b/tests/rules/test_brew_install.py @@ -1,6 +1,6 @@ import pytest from thefuck.rules.brew_install import match, get_new_command -from thefuck.rules.brew_install import brew_formulas +from thefuck.rules.brew_install import _get_formulas from tests.utils import Command @@ -20,9 +20,7 @@ def brew_already_installed(): def _is_not_okay_to_test(): - if 'elasticsearch' not in brew_formulas: - return True - return False + return 'elasticsearch' not in _get_formulas() @pytest.mark.skipif(_is_not_okay_to_test(), diff --git a/tests/rules/test_brew_unknown_command.py b/tests/rules/test_brew_unknown_command.py index b33f7409..17adb47f 100644 --- a/tests/rules/test_brew_unknown_command.py +++ b/tests/rules/test_brew_unknown_command.py @@ -1,6 +1,6 @@ import pytest from thefuck.rules.brew_unknown_command import match, get_new_command -from thefuck.rules.brew_unknown_command import brew_commands +from thefuck.rules.brew_unknown_command import _brew_commands from tests.utils import Command @@ -16,7 +16,7 @@ def brew_unknown_cmd2(): def test_match(brew_unknown_cmd): assert match(Command('brew inst', stderr=brew_unknown_cmd), None) - for command in brew_commands: + for command in _brew_commands(): assert not match(Command('brew ' + command), None) diff --git a/thefuck/rules/brew_install.py b/thefuck/rules/brew_install.py index 751c1f7c..a1432b03 100644 --- a/thefuck/rules/brew_install.py +++ b/thefuck/rules/brew_install.py @@ -1,38 +1,38 @@ import os import re -from subprocess import check_output -from thefuck.utils import get_closest, replace_argument +from thefuck.utils import get_closest, replace_argument, which +from thefuck.specific.brew import get_brew_path_prefix -# Formulars are base on each local system's status -brew_formulas = [] -try: - brew_path_prefix = check_output(['brew', '--prefix'], - universal_newlines=True).strip() - brew_formula_path = brew_path_prefix + '/Library/Formula' +enabled_by_default = bool(which('brew')) - 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_formulas(): + # Formulas are based on each local system's status + try: + brew_path_prefix = get_brew_path_prefix() + brew_formula_path = brew_path_prefix + '/Library/Formula' + + for file_name in os.listdir(brew_formula_path): + if file_name.endswith('.rb'): + yield file_name[:-3] + except: + pass def _get_similar_formula(formula_name): - return get_closest(formula_name, brew_formulas, 1, 0.85) + return get_closest(formula_name, _get_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 = bool(_get_similar_formula(formula)) - - return has_possible_formulas + return bool(_get_similar_formula(formula)) + return False def get_new_command(command, settings): diff --git a/thefuck/rules/brew_unknown_command.py b/thefuck/rules/brew_unknown_command.py index 9b80f887..d798a7be 100644 --- a/thefuck/rules/brew_unknown_command.py +++ b/thefuck/rules/brew_unknown_command.py @@ -1,30 +1,19 @@ import os import re -import subprocess from thefuck.utils import get_closest, replace_command +from thefuck.specific.brew import get_brew_path_prefix BREW_CMD_PATH = '/Library/Homebrew/cmd' TAP_PATH = '/Library/Taps' TAP_CMD_PATH = '/%s/%s/cmd' -def _get_brew_path_prefix(): - """To get brew path""" - try: - return subprocess.check_output(['brew', '--prefix'], - universal_newlines=True).strip() - except: - return None - - def _get_brew_commands(brew_path_prefix): """To get brew default commands on local environment""" brew_cmd_path = brew_path_prefix + BREW_CMD_PATH - commands = [name.replace('.rb', '') for name in os.listdir(brew_cmd_path) - if name.endswith('.rb')] - - return commands + return [name[:-3] for name in os.listdir(brew_cmd_path) + if name.endswith('.rb')] def _get_brew_tap_specific_commands(brew_path_prefix): @@ -51,10 +40,7 @@ def _get_brew_tap_specific_commands(brew_path_prefix): def _is_brew_tap_cmd_naming(name): - if name.startswith('brew-') and name.endswith('.rb'): - return True - - return False + return name.startswith('brew-') and name.endswith('.rb') def _get_directory_names_only(path): @@ -62,35 +48,33 @@ def _get_directory_names_only(path): if os.path.isdir(os.path.join(path, d))] -brew_path_prefix = _get_brew_path_prefix() +def _brew_commands(): + brew_path_prefix = get_brew_path_prefix() + if brew_path_prefix: + try: + return _get_brew_commands(brew_path_prefix) \ + + _get_brew_tap_specific_commands(brew_path_prefix) + except OSError: + pass -# Failback commands for testing (Based on Homebrew 0.9.5) -brew_commands = ['info', 'home', 'options', 'install', 'uninstall', - 'search', 'list', 'update', 'upgrade', 'pin', 'unpin', - 'doctor', 'create', 'edit'] - -if brew_path_prefix: - try: - brew_commands = _get_brew_commands(brew_path_prefix) \ - + _get_brew_tap_specific_commands(brew_path_prefix) - except OSError: - pass + # Failback commands for testing (Based on Homebrew 0.9.5) + return ['info', 'home', 'options', 'install', 'uninstall', + 'search', 'list', 'update', 'upgrade', 'pin', 'unpin', + 'doctor', 'create', 'edit'] def match(command, settings): is_proper_command = ('brew' in command.script and 'Unknown command' in command.stderr) - has_possible_commands = False if is_proper_command: broken_cmd = re.findall(r'Error: Unknown command: ([a-z]+)', command.stderr)[0] - has_possible_commands = bool(get_closest(broken_cmd, brew_commands)) - - return has_possible_commands + return bool(get_closest(broken_cmd, _brew_commands())) + return False def get_new_command(command, settings): broken_cmd = re.findall(r'Error: Unknown command: ([a-z]+)', command.stderr)[0] - return replace_command(command, broken_cmd, brew_commands) + return replace_command(command, broken_cmd, _brew_commands()) diff --git a/thefuck/specific/brew.py b/thefuck/specific/brew.py new file mode 100644 index 00000000..66d01597 --- /dev/null +++ b/thefuck/specific/brew.py @@ -0,0 +1,15 @@ +import subprocess +from thefuck.utils import memoize, which + + +enabled_by_default = bool(which('brew')) + + +@memoize +def get_brew_path_prefix(): + """To get brew path""" + try: + return subprocess.check_output(['brew', '--prefix'], + universal_newlines=True).strip() + except: + return None