1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-03-25 03:59:08 +00:00
thefuck/tests/rules/test_no_command.py

98 lines
3.4 KiB
Python
Raw Normal View History

2015-04-08 19:00:03 +02:00
from subprocess import PIPE
2015-04-17 16:24:03 +02:00
from mock import patch, Mock
import pytest
from thefuck.rules.no_command import match, get_new_command
2015-04-08 19:00:03 +02:00
from thefuck.main import Command
@pytest.fixture
def command_found():
return b'''No command 'aptget' found, did you mean:
Command 'apt-get' from package 'apt' (main)
2015-04-18 16:32:50 -05:00
Command 'not-installed' from package 'derp' (main)
Command 'not-really-used' from package 'whatever' (main)
2015-04-08 19:00:03 +02:00
aptget: command not found
'''
2015-04-18 16:32:50 -05:00
@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
'''
2015-04-08 19:00:03 +02:00
@pytest.fixture
def command_not_found():
return b'''No command 'vom' found, but there are 19 similar ones
vom: command not found
'''
@pytest.fixture
def bins_exists(request):
p = patch('thefuck.rules.no_command.which',
return_value=True)
p.start()
request.addfinalizer(p.stop)
2015-04-18 16:32:50 -05:00
@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
def settings():
class _Settings(object):
pass
return _Settings
2015-04-18 16:32:50 -05:00
@pytest.mark.usefixtures('bin_might_exist', 'patch_history')
def test_match(command_found, command_not_found, uninstalled_command_found, settings):
with patch('thefuck.rules.no_command.Popen') as Popen:
2015-04-08 19:00:03 +02:00
Popen.return_value.stderr.read.return_value = command_found
assert match(Command('aptget install vim', '', ''), settings)
2015-04-18 10:52:00 -05:00
Popen.assert_called_with('/usr/lib/command-not-found aptget',
2015-04-08 19:00:03 +02:00
shell=True, stderr=PIPE)
Popen.return_value.stderr.read.return_value = command_not_found
assert not match(Command('ls', '', ''), settings)
2015-04-08 19:00:03 +02:00
with patch('thefuck.rules.no_command.Popen') as Popen:
2015-04-08 19:00:03 +02:00
Popen.return_value.stderr.read.return_value = command_found
2015-04-08 21:08:35 +02:00
assert match(Command('sudo aptget install vim', '', ''),
Mock(command_not_found='test'))
2015-04-18 10:52:00 -05:00
Popen.assert_called_with('test aptget',
2015-04-08 19:00:03 +02:00
shell=True, stderr=PIPE)
2015-04-18 16:32:50 -05:00
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)
2015-04-08 19:00:03 +02:00
2015-04-18 16:32:50 -05:00
@pytest.mark.usefixtures('bin_might_exist', 'patch_history')
2015-04-08 19:00:03 +02:00
def test_get_new_command(command_found):
with patch('thefuck.rules.no_command._get_output',
2015-04-08 19:00:03 +02:00
return_value=command_found.decode()):
assert get_new_command(Command('aptget install vim', '', ''), settings)\
2015-04-08 19:00:03 +02:00
== 'apt-get install vim'
assert get_new_command(Command('sudo aptget install vim', '', ''), settings) \
2015-04-08 19:00:03 +02:00
== 'sudo apt-get install vim'