mirror of
https://github.com/nvbn/thefuck.git
synced 2025-01-18 12:06:04 +00:00
* fix: Update output for brew_install test: fixed * chore: fixing flake8 styles * feat: show more suggestions * test: new functions added and multi suggestions * refactor: rename to _get_suggestions
This commit is contained in:
parent
f1b7d879bd
commit
ed40463105
@ -1,17 +1,26 @@
|
|||||||
import pytest
|
import pytest
|
||||||
from thefuck.rules.brew_install import match, get_new_command
|
from thefuck.rules.brew_install import match, get_new_command, _get_suggestions
|
||||||
from thefuck.rules.brew_install import _get_formulas
|
|
||||||
from thefuck.types import Command
|
from thefuck.types import Command
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def brew_no_available_formula():
|
def brew_no_available_formula_one():
|
||||||
return '''Error: No available formula for elsticsearch '''
|
return '''Warning: No available formula with the name "giss". Did you mean gist?'''
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def brew_no_available_formula_two():
|
||||||
|
return '''Warning: No available formula with the name "elasticserar". Did you mean elasticsearch or elasticsearch@6?'''
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def brew_no_available_formula_three():
|
||||||
|
return '''Warning: No available formula with the name "gitt". Did you mean git, gitg or gist?'''
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def brew_install_no_argument():
|
def brew_install_no_argument():
|
||||||
return '''This command requires a formula argument'''
|
return '''Install a formula or cask. Additional options specific to a formula may be'''
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
@ -19,28 +28,38 @@ def brew_already_installed():
|
|||||||
return '''Warning: git-2.3.5 already installed'''
|
return '''Warning: git-2.3.5 already installed'''
|
||||||
|
|
||||||
|
|
||||||
def _is_not_okay_to_test():
|
def test_suggestions():
|
||||||
return 'elasticsearch' not in _get_formulas()
|
assert _get_suggestions("one") == ['one']
|
||||||
|
assert _get_suggestions("one or two") == ['one', 'two']
|
||||||
|
assert _get_suggestions("one, two or three") == ['one', 'two', 'three']
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.skipif(_is_not_okay_to_test(),
|
def test_match(brew_no_available_formula_one, brew_no_available_formula_two,
|
||||||
reason='No need to run if there\'s no formula')
|
brew_no_available_formula_three, brew_already_installed,
|
||||||
def test_match(brew_no_available_formula, brew_already_installed,
|
|
||||||
brew_install_no_argument):
|
brew_install_no_argument):
|
||||||
assert match(Command('brew install elsticsearch',
|
assert match(Command('brew install giss',
|
||||||
brew_no_available_formula))
|
brew_no_available_formula_one))
|
||||||
|
assert match(Command('brew install elasticserar',
|
||||||
|
brew_no_available_formula_two))
|
||||||
|
assert match(Command('brew install gitt',
|
||||||
|
brew_no_available_formula_three))
|
||||||
assert not match(Command('brew install git',
|
assert not match(Command('brew install git',
|
||||||
brew_already_installed))
|
brew_already_installed))
|
||||||
assert not match(Command('brew install', brew_install_no_argument))
|
assert not match(Command('brew install', brew_install_no_argument))
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.skipif(_is_not_okay_to_test(),
|
def test_get_new_command(brew_no_available_formula_one, brew_no_available_formula_two,
|
||||||
reason='No need to run if there\'s no formula')
|
brew_no_available_formula_three):
|
||||||
def test_get_new_command(brew_no_available_formula):
|
assert get_new_command(Command('brew install giss',
|
||||||
assert get_new_command(Command('brew install elsticsearch',
|
brew_no_available_formula_one))\
|
||||||
brew_no_available_formula))\
|
== ['brew install gist']
|
||||||
== 'brew install elasticsearch'
|
assert get_new_command(Command('brew install elasticsear',
|
||||||
|
brew_no_available_formula_two))\
|
||||||
|
== ['brew install elasticsearch', 'brew install elasticsearch@6']
|
||||||
|
assert get_new_command(Command('brew install gitt',
|
||||||
|
brew_no_available_formula_three))\
|
||||||
|
== ['brew install git', 'brew install gitg', 'brew install gist']
|
||||||
|
|
||||||
assert get_new_command(Command('brew install aa',
|
assert get_new_command(Command('brew install aa',
|
||||||
brew_no_available_formula))\
|
brew_no_available_formula_one))\
|
||||||
!= 'brew install aha'
|
!= 'brew install aha'
|
||||||
|
@ -1,42 +1,24 @@
|
|||||||
import os
|
|
||||||
import re
|
import re
|
||||||
from thefuck.utils import get_closest, replace_argument
|
from thefuck.utils import for_app
|
||||||
from thefuck.specific.brew import get_brew_path_prefix, brew_available
|
from thefuck.specific.brew import brew_available
|
||||||
|
|
||||||
enabled_by_default = brew_available
|
enabled_by_default = brew_available
|
||||||
|
|
||||||
|
|
||||||
def _get_formulas():
|
def _get_suggestions(str):
|
||||||
# Formulas are based on each local system's status
|
suggestions = str.replace(" or ", ", ").split(", ")
|
||||||
try:
|
return suggestions
|
||||||
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 Exception:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def _get_similar_formula(formula_name):
|
|
||||||
return get_closest(formula_name, _get_formulas(), cutoff=0.85)
|
|
||||||
|
|
||||||
|
|
||||||
|
@for_app('brew', at_least=2)
|
||||||
def match(command):
|
def match(command):
|
||||||
is_proper_command = ('brew install' in command.script and
|
is_proper_command = ('install' in command.script and
|
||||||
'No available formula' in command.output)
|
'No available formula' in command.output and
|
||||||
|
'Did you mean' in command.output)
|
||||||
if is_proper_command:
|
return is_proper_command
|
||||||
formula = re.findall(r'Error: No available formula for ([a-z]+)',
|
|
||||||
command.output)[0]
|
|
||||||
return bool(_get_similar_formula(formula))
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
def get_new_command(command):
|
def get_new_command(command):
|
||||||
not_exist_formula = re.findall(r'Error: No available formula for ([a-z]+)',
|
matcher = re.search('Warning: No available formula with the name "(?:[^"]+)". Did you mean (.+)\\?', command.output)
|
||||||
command.output)[0]
|
suggestions = _get_suggestions(matcher.group(1))
|
||||||
exist_formula = _get_similar_formula(not_exist_formula)
|
return ["brew install " + formula for formula in suggestions]
|
||||||
|
|
||||||
return replace_argument(command.script, not_exist_formula, exist_formula)
|
|
||||||
|
Loading…
Reference in New Issue
Block a user