mirror of
https://github.com/nvbn/thefuck.git
synced 2025-02-21 20:38:54 +00:00
improve no_comamnd rule, test it
This commit is contained in:
parent
de343b84c1
commit
ac545c6f0a
@ -9,16 +9,27 @@ from thefuck.main import Command
|
|||||||
def command_found():
|
def command_found():
|
||||||
return b'''No command 'aptget' found, did you mean:
|
return b'''No command 'aptget' found, did you mean:
|
||||||
Command 'apt-get' from package 'apt' (main)
|
Command 'apt-get' from package 'apt' (main)
|
||||||
|
Command 'not-installed' from package 'derp' (main)
|
||||||
|
Command 'not-really-used' from package 'whatever' (main)
|
||||||
aptget: command not found
|
aptget: command not found
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def uninstalled_command_found():
|
||||||
|
return b'''No command 'pish' found, did you mean:
|
||||||
|
Command 'vish' from package 'vish' (universe)
|
||||||
|
Command 'wish' from package 'tk' (main)
|
||||||
|
Command 'fish' from package 'fish' (universe)
|
||||||
|
Command 'pdsh' from package 'pdsh' (universe)
|
||||||
|
pish: command not found
|
||||||
|
'''
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def command_not_found():
|
def command_not_found():
|
||||||
return b'''No command 'vom' found, but there are 19 similar ones
|
return b'''No command 'vom' found, but there are 19 similar ones
|
||||||
vom: command not found
|
vom: command not found
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def bins_exists(request):
|
def bins_exists(request):
|
||||||
p = patch('thefuck.rules.no_command.which',
|
p = patch('thefuck.rules.no_command.which',
|
||||||
@ -26,6 +37,27 @@ def bins_exists(request):
|
|||||||
p.start()
|
p.start()
|
||||||
request.addfinalizer(p.stop)
|
request.addfinalizer(p.stop)
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def bin_might_exist(request):
|
||||||
|
def side_effect(name):
|
||||||
|
return name in ['not-really-used', 'apt-get', '/usr/lib/command-not-found', 'test']
|
||||||
|
p = patch('thefuck.rules.no_command.which',
|
||||||
|
side_effect = side_effect)
|
||||||
|
p.start()
|
||||||
|
request.addfinalizer(p.stop)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def patch_history(request):
|
||||||
|
def side_effect(name):
|
||||||
|
print("history('{}')".format(name))
|
||||||
|
count = 2 if name == 'not-really-used' else 12
|
||||||
|
p = patch('thefuck.rules.no_command._count_history_uses',
|
||||||
|
side_effect = side_effect)
|
||||||
|
p.start()
|
||||||
|
request.addfinalizer(p.stop)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def settings():
|
def settings():
|
||||||
@ -34,8 +66,8 @@ def settings():
|
|||||||
return _Settings
|
return _Settings
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.usefixtures('bins_exists')
|
@pytest.mark.usefixtures('bin_might_exist', 'patch_history')
|
||||||
def test_match(command_found, command_not_found, settings):
|
def test_match(command_found, command_not_found, uninstalled_command_found, settings):
|
||||||
with patch('thefuck.rules.no_command.Popen') as Popen:
|
with patch('thefuck.rules.no_command.Popen') as Popen:
|
||||||
Popen.return_value.stderr.read.return_value = command_found
|
Popen.return_value.stderr.read.return_value = command_found
|
||||||
assert match(Command('aptget install vim', '', ''), settings)
|
assert match(Command('aptget install vim', '', ''), settings)
|
||||||
@ -51,8 +83,11 @@ def test_match(command_found, command_not_found, settings):
|
|||||||
Popen.assert_called_with('test aptget',
|
Popen.assert_called_with('test aptget',
|
||||||
shell=True, stderr=PIPE)
|
shell=True, stderr=PIPE)
|
||||||
|
|
||||||
|
with patch('thefuck.rules.no_command.Popen') as Popen:
|
||||||
|
Popen.return_value.stderr.read.return_value = uninstalled_command_found
|
||||||
|
assert not match(Command('pish bla blah', '', ''), settings)
|
||||||
|
|
||||||
@pytest.mark.usefixtures('bins_exists')
|
@pytest.mark.usefixtures('bin_might_exist', 'patch_history')
|
||||||
def test_get_new_command(command_found):
|
def test_get_new_command(command_found):
|
||||||
with patch('thefuck.rules.no_command._get_output',
|
with patch('thefuck.rules.no_command._get_output',
|
||||||
return_value=command_found.decode()):
|
return_value=command_found.decode()):
|
||||||
|
@ -12,9 +12,10 @@ def _get_output(command, settings):
|
|||||||
return result.stderr.read().decode()
|
return result.stderr.read().decode()
|
||||||
|
|
||||||
def _count_history_uses(name):
|
def _count_history_uses(name):
|
||||||
script = 'history | grep {}'.format(name)
|
script = "history | egrep '\\b{}\\b' | wc -l".format(name)
|
||||||
result = Popen(script, shell=True, stdout=PIPE)
|
result = Popen(script, shell=True,
|
||||||
return len(list(result.stdout))
|
stdout=PIPE)
|
||||||
|
return int(result.stdout.read())
|
||||||
|
|
||||||
def _get_candidate_commands(command, settings):
|
def _get_candidate_commands(command, settings):
|
||||||
output = _get_output(command, settings)
|
output = _get_output(command, settings)
|
||||||
@ -40,6 +41,6 @@ def get_new_command(command, settings):
|
|||||||
broken_name = re.findall(r"No command '([^']*)' found",
|
broken_name = re.findall(r"No command '([^']*)' found",
|
||||||
output)[0]
|
output)[0]
|
||||||
candidates = _get_candidate_commands(command, settings)
|
candidates = _get_candidate_commands(command, settings)
|
||||||
fixed_name = sorted(candidates, key=_count_history_uses)[0]
|
fixed_name = sorted(candidates, key=_count_history_uses, reverse=True)[0]
|
||||||
return command.script.replace(broken_name, fixed_name)
|
return command.script.replace(broken_name, fixed_name)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user