2015-04-08 18:15:49 +02:00
|
|
|
from subprocess import PIPE
|
|
|
|
from pathlib import PosixPath, Path
|
2015-04-17 16:24:03 +02:00
|
|
|
from mock import patch, Mock
|
2015-04-08 18:15:49 +02:00
|
|
|
from thefuck import main
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_settings():
|
|
|
|
with patch('thefuck.main.load_source', return_value=Mock(rules=['bash'])):
|
|
|
|
assert main.get_settings(Path('/')).rules == ['bash']
|
|
|
|
with patch('thefuck.main.load_source', return_value=Mock(spec=[])):
|
|
|
|
assert main.get_settings(Path('/')).rules is None
|
|
|
|
|
|
|
|
|
|
|
|
def test_is_rule_enabled():
|
2015-04-22 15:59:44 +02:00
|
|
|
assert main.is_rule_enabled(Mock(rules=None),
|
|
|
|
main.Rule('bash', None, None, True))
|
|
|
|
assert not main.is_rule_enabled(Mock(rules=None),
|
|
|
|
main.Rule('bash', None, None, False))
|
|
|
|
assert main.is_rule_enabled(Mock(rules=['bash']),
|
|
|
|
main.Rule('bash', None, None, True))
|
|
|
|
assert not main.is_rule_enabled(Mock(rules=['bash']),
|
|
|
|
main.Rule('lisp', None, None, True))
|
2015-04-08 18:15:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_load_rule():
|
|
|
|
match = object()
|
|
|
|
get_new_command = object()
|
|
|
|
with patch('thefuck.main.load_source',
|
|
|
|
return_value=Mock(
|
|
|
|
match=match,
|
2015-04-22 15:59:44 +02:00
|
|
|
get_new_command=get_new_command,
|
|
|
|
enabled_by_default=True)) as load_source:
|
|
|
|
assert main.load_rule(Path('/rules/bash.py')) \
|
|
|
|
== main.Rule('bash', match, get_new_command, True)
|
2015-04-08 18:15:49 +02:00
|
|
|
load_source.assert_called_once_with('bash', '/rules/bash.py')
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_rules():
|
|
|
|
with patch('thefuck.main.Path.glob') as glob, \
|
|
|
|
patch('thefuck.main.load_source',
|
2015-04-22 15:59:44 +02:00
|
|
|
lambda x, _: Mock(match=x, get_new_command=x,
|
|
|
|
enabled_by_default=True)):
|
2015-04-08 18:15:49 +02:00
|
|
|
glob.return_value = [PosixPath('bash.py'), PosixPath('lisp.py')]
|
2015-04-22 15:59:44 +02:00
|
|
|
assert list(main.get_rules(
|
2015-04-08 18:15:49 +02:00
|
|
|
Path('~'),
|
2015-04-22 15:59:44 +02:00
|
|
|
Mock(rules=None))) == [main.Rule('bash', 'bash', 'bash', True),
|
|
|
|
main.Rule('lisp', 'lisp', 'lisp', True),
|
|
|
|
main.Rule('bash', 'bash', 'bash', True),
|
|
|
|
main.Rule('lisp', 'lisp', 'lisp', True)]
|
|
|
|
assert list(main.get_rules(
|
2015-04-08 18:15:49 +02:00
|
|
|
Path('~'),
|
2015-04-22 15:59:44 +02:00
|
|
|
Mock(rules=['bash']))) == [main.Rule('bash', 'bash', 'bash', True),
|
|
|
|
main.Rule('bash', 'bash', 'bash', True)]
|
2015-04-08 18:15:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_get_command():
|
2015-04-18 22:50:18 +02:00
|
|
|
with patch('thefuck.main.Popen') as Popen, \
|
2015-04-18 21:27:43 +02:00
|
|
|
patch('thefuck.main.os.environ',
|
2015-04-18 22:50:18 +02:00
|
|
|
new_callable=lambda: {}), \
|
|
|
|
patch('thefuck.main.wait_output',
|
|
|
|
return_value=True):
|
2015-04-08 18:15:49 +02:00
|
|
|
Popen.return_value.stdout.read.return_value = b'stdout'
|
|
|
|
Popen.return_value.stderr.read.return_value = b'stderr'
|
2015-04-21 06:36:51 +02:00
|
|
|
assert main.get_command(Mock(), ['thefuck', 'apt-get',
|
|
|
|
'search', 'vim']) \
|
2015-04-08 18:15:49 +02:00
|
|
|
== main.Command('apt-get search vim', 'stdout', 'stderr')
|
|
|
|
Popen.assert_called_once_with('apt-get search vim',
|
|
|
|
shell=True,
|
|
|
|
stdout=PIPE,
|
2015-04-18 21:27:43 +02:00
|
|
|
stderr=PIPE,
|
|
|
|
env={'LANG': 'C'})
|
2015-04-21 14:26:45 +02:00
|
|
|
assert main.get_command(Mock(), ['']) is None
|
2015-04-08 18:15:49 +02:00
|
|
|
|
|
|
|
|
2015-04-21 14:40:52 +02:00
|
|
|
def test_get_matched_rule(capsys):
|
2015-04-22 15:59:44 +02:00
|
|
|
rules = [main.Rule('', lambda x, _: x.script == 'cd ..', None, True),
|
|
|
|
main.Rule('', lambda *_: False, None, True),
|
|
|
|
main.Rule('rule', Mock(side_effect=OSError('Denied')), None, True)]
|
2015-04-08 18:15:49 +02:00
|
|
|
assert main.get_matched_rule(main.Command('ls', '', ''),
|
2015-04-22 06:03:06 +02:00
|
|
|
rules, Mock(no_colors=True)) is None
|
2015-04-08 18:15:49 +02:00
|
|
|
assert main.get_matched_rule(main.Command('cd ..', '', ''),
|
2015-04-22 06:03:06 +02:00
|
|
|
rules, Mock(no_colors=True)) == rules[0]
|
2015-04-22 15:59:44 +02:00
|
|
|
assert capsys.readouterr()[1].split('\n')[0] \
|
2015-04-22 06:03:06 +02:00
|
|
|
== '[WARN] Rule rule:'
|
2015-04-21 05:30:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_run_rule(capsys):
|
|
|
|
with patch('thefuck.main.confirm', return_value=True):
|
2015-04-22 15:59:44 +02:00
|
|
|
main.run_rule(main.Rule('', None, lambda *_: 'new-command', True),
|
2015-04-21 05:30:15 +02:00
|
|
|
None, None)
|
|
|
|
assert capsys.readouterr() == ('new-command\n', '')
|
|
|
|
with patch('thefuck.main.confirm', return_value=False):
|
2015-04-22 15:59:44 +02:00
|
|
|
main.run_rule(main.Rule('', None, lambda *_: 'new-command', True),
|
2015-04-21 05:30:15 +02:00
|
|
|
None, None)
|
|
|
|
assert capsys.readouterr() == ('', '')
|
|
|
|
|
|
|
|
|
|
|
|
def test_confirm(capsys):
|
|
|
|
# When confirmation not required:
|
|
|
|
assert main.confirm('command', Mock(require_confirmation=False))
|
|
|
|
assert capsys.readouterr() == ('', 'command\n')
|
|
|
|
# When confirmation required and confirmed:
|
|
|
|
with patch('thefuck.main.sys.stdin.read', return_value='\n'):
|
2015-04-22 06:03:06 +02:00
|
|
|
assert main.confirm('command', Mock(require_confirmation=True,
|
|
|
|
no_colors=True))
|
|
|
|
assert capsys.readouterr() == ('', 'command [enter/ctrl+c]')
|
2015-04-21 05:30:15 +02:00
|
|
|
# When confirmation required and ctrl+c:
|
|
|
|
with patch('thefuck.main.sys.stdin.read', side_effect=KeyboardInterrupt):
|
2015-04-22 06:03:06 +02:00
|
|
|
assert not main.confirm('command', Mock(require_confirmation=True,
|
|
|
|
no_colors=True))
|
|
|
|
assert capsys.readouterr() == ('', 'command [enter/ctrl+c]Aborted\n')
|