mirror of
https://github.com/nvbn/thefuck.git
synced 2025-01-18 12:06:04 +00:00
Add a support for unknown brew commands - #83
This commit is contained in:
parent
0272e8a801
commit
24ce459f2c
28
tests/rules/test_brew_unknown_command.py
Normal file
28
tests/rules/test_brew_unknown_command.py
Normal file
@ -0,0 +1,28 @@
|
||||
import pytest
|
||||
from thefuck.types import Command
|
||||
from thefuck.rules.brew_unknown_command import match, get_new_command
|
||||
from thefuck.rules.brew_unknown_command import brew_commands
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def brew_unknown_cmd():
|
||||
return '''Error: Unknown command: inst'''
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def brew_unknown_cmd_instaa():
|
||||
return '''Error: Unknown command: instaa'''
|
||||
|
||||
|
||||
def test_match(brew_unknown_cmd):
|
||||
assert match(Command('brew inst', '', brew_unknown_cmd), None)
|
||||
for command in brew_commands:
|
||||
assert not match(Command('brew ' + command, '', ''), None)
|
||||
|
||||
|
||||
def test_get_new_command(brew_unknown_cmd, brew_unknown_cmd_instaa):
|
||||
assert get_new_command(Command('brew inst', '', brew_unknown_cmd), None)\
|
||||
== 'brew list'
|
||||
|
||||
assert get_new_command(Command('brew instaa', '', brew_unknown_cmd_instaa),
|
||||
None) == 'brew install'
|
33
thefuck/rules/brew_unknown_command.py
Normal file
33
thefuck/rules/brew_unknown_command.py
Normal file
@ -0,0 +1,33 @@
|
||||
import difflib
|
||||
import re
|
||||
import thefuck.logs
|
||||
|
||||
# This commands are based on Homebrew 0.9.5
|
||||
brew_commands = ['info', 'home', 'options', 'install', 'uninstall', 'search',
|
||||
'list', 'update', 'upgrade', 'pin', 'unpin', 'doctor',
|
||||
'create', 'edit']
|
||||
|
||||
|
||||
def _get_similar_commands(command):
|
||||
return difflib.get_close_matches(command, brew_commands)
|
||||
|
||||
|
||||
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 = len(_get_similar_commands(broken_cmd)) > 0
|
||||
|
||||
return has_possible_commands
|
||||
|
||||
|
||||
def get_new_command(command, settings):
|
||||
broken_cmd = re.findall(r'Error: Unknown command: ([a-z]+)',
|
||||
command.stderr)[0]
|
||||
new_cmd = _get_similar_commands(broken_cmd)[0]
|
||||
|
||||
return command.script.replace(broken_cmd, new_cmd, 1)
|
Loading…
x
Reference in New Issue
Block a user