1
0
mirror of https://github.com/nvbn/thefuck.git synced 2024-10-06 10:51:11 +01:00
thefuck/tests/test_main.py

116 lines
5.0 KiB
Python
Raw Normal View History

2015-04-08 17:15:49 +01:00
from subprocess import PIPE
from pathlib import PosixPath, Path
2015-04-17 15:24:03 +01:00
from mock import patch, Mock
2015-04-22 22:04:22 +01:00
from thefuck import main, conf, types
from tests.utils import Rule, Command
2015-04-08 17:15:49 +01:00
def test_load_rule():
match = object()
get_new_command = object()
with patch('thefuck.main.load_source',
return_value=Mock(
match=match,
get_new_command=get_new_command,
enabled_by_default=True)) as load_source:
assert main.load_rule(Path('/rules/bash.py')) \
== Rule('bash', match, get_new_command)
2015-04-08 17:15:49 +01: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',
lambda x, _: Mock(match=x, get_new_command=x,
enabled_by_default=True)):
2015-04-08 17:15:49 +01:00
glob.return_value = [PosixPath('bash.py'), PosixPath('lisp.py')]
assert list(main.get_rules(
2015-04-08 17:15:49 +01:00
Path('~'),
2015-04-22 21:37:11 +01:00
Mock(rules=conf.DEFAULT_RULES))) \
== [Rule('bash', 'bash', 'bash'),
Rule('lisp', 'lisp', 'lisp'),
Rule('bash', 'bash', 'bash'),
Rule('lisp', 'lisp', 'lisp')]
assert list(main.get_rules(
2015-04-08 17:15:49 +01:00
Path('~'),
2015-04-22 22:04:22 +01:00
Mock(rules=types.RulesNamesList(['bash'])))) \
== [Rule('bash', 'bash', 'bash'),
Rule('bash', 'bash', 'bash')]
2015-04-08 17:15:49 +01:00
def test_get_command():
with patch('thefuck.main.Popen') as Popen, \
patch('thefuck.main.os.environ',
new_callable=lambda: {}), \
patch('thefuck.main.wait_output',
return_value=True):
2015-04-08 17:15:49 +01:00
Popen.return_value.stdout.read.return_value = b'stdout'
Popen.return_value.stderr.read.return_value = b'stderr'
2015-04-21 05:36:51 +01:00
assert main.get_command(Mock(), ['thefuck', 'apt-get',
'search', 'vim']) \
== Command('apt-get search vim', 'stdout', 'stderr')
2015-04-08 17:15:49 +01:00
Popen.assert_called_once_with('apt-get search vim',
shell=True,
stdout=PIPE,
stderr=PIPE,
env={'LANG': 'C'})
assert main.get_command(Mock(), ['']) is None
2015-04-08 17:15:49 +01:00
def test_get_matched_rule(capsys):
rules = [Rule('', lambda x, _: x.script == 'cd ..'),
Rule('', lambda *_: False),
Rule('rule', Mock(side_effect=OSError('Denied')))]
assert main.get_matched_rule(Command('ls'),
2015-04-22 05:03:06 +01:00
rules, Mock(no_colors=True)) is None
assert main.get_matched_rule(Command('cd ..'),
2015-04-22 05:03:06 +01:00
rules, Mock(no_colors=True)) == rules[0]
assert capsys.readouterr()[1].split('\n')[0] \
2015-04-22 05:03:06 +01:00
== '[WARN] Rule rule:'
2015-04-21 04:30:15 +01:00
def test_run_rule(capsys):
with patch('thefuck.main.confirm', return_value=True):
main.run_rule(Rule(get_new_command=lambda *_: 'new-command'),
2015-04-21 04:30:15 +01:00
None, None)
assert capsys.readouterr() == ('new-command\n', '')
# With side effect:
side_effect = Mock()
settings = Mock()
command = Mock()
main.run_rule(Rule(get_new_command=lambda *_: 'new-command',
side_effect=side_effect),
command, settings)
assert capsys.readouterr() == ('new-command\n', '')
side_effect.assert_called_once_with(command, settings)
2015-04-21 04:30:15 +01:00
with patch('thefuck.main.confirm', return_value=False):
main.run_rule(Rule(get_new_command=lambda *_: 'new-command'),
2015-04-21 04:30:15 +01:00
None, None)
assert capsys.readouterr() == ('', '')
def test_confirm(capsys):
# When confirmation not required:
assert main.confirm('command', None, Mock(require_confirmation=False))
2015-04-21 04:30:15 +01:00
assert capsys.readouterr() == ('', 'command\n')
# With side effect and without confirmation:
assert main.confirm('command', Mock(), Mock(require_confirmation=False))
assert capsys.readouterr() == ('', 'command*\n')
2015-04-21 04:30:15 +01:00
# When confirmation required and confirmed:
with patch('thefuck.main.sys.stdin.read', return_value='\n'):
assert main.confirm(
'command', None, Mock(require_confirmation=True,
no_colors=True))
2015-04-22 05:03:06 +01:00
assert capsys.readouterr() == ('', 'command [enter/ctrl+c]')
# With side effect:
assert main.confirm(
'command', Mock(), Mock(require_confirmation=True,
no_colors=True))
assert capsys.readouterr() == ('', 'command* [enter/ctrl+c]')
2015-04-21 04:30:15 +01:00
# When confirmation required and ctrl+c:
with patch('thefuck.main.sys.stdin.read', side_effect=KeyboardInterrupt):
assert not main.confirm('command', None,
Mock(require_confirmation=True,
no_colors=True))
2015-04-22 05:03:06 +01:00
assert capsys.readouterr() == ('', 'command [enter/ctrl+c]Aborted\n')