Add useful constructors for Rule and Command for tests

This commit is contained in:
nvbn
2015-04-25 02:35:26 +02:00
parent bb6b9a638c
commit b7cb407637
18 changed files with 141 additions and 115 deletions
+16 -15
View File
@@ -2,6 +2,7 @@ from subprocess import PIPE
from pathlib import PosixPath, Path
from mock import patch, Mock
from thefuck import main, conf, types
from tests.utils import Rule, Command
def test_load_rule():
@@ -13,7 +14,7 @@ def test_load_rule():
get_new_command=get_new_command,
enabled_by_default=True)) as load_source:
assert main.load_rule(Path('/rules/bash.py')) \
== types.Rule('bash', match, get_new_command, True)
== Rule('bash', match, get_new_command)
load_source.assert_called_once_with('bash', '/rules/bash.py')
@@ -26,15 +27,15 @@ def test_get_rules():
assert list(main.get_rules(
Path('~'),
Mock(rules=conf.DEFAULT_RULES))) \
== [types.Rule('bash', 'bash', 'bash', True),
types.Rule('lisp', 'lisp', 'lisp', True),
types.Rule('bash', 'bash', 'bash', True),
types.Rule('lisp', 'lisp', 'lisp', True)]
== [Rule('bash', 'bash', 'bash'),
Rule('lisp', 'lisp', 'lisp'),
Rule('bash', 'bash', 'bash'),
Rule('lisp', 'lisp', 'lisp')]
assert list(main.get_rules(
Path('~'),
Mock(rules=types.RulesNamesList(['bash'])))) \
== [types.Rule('bash', 'bash', 'bash', True),
types.Rule('bash', 'bash', 'bash', True)]
== [Rule('bash', 'bash', 'bash'),
Rule('bash', 'bash', 'bash')]
def test_get_command():
@@ -47,7 +48,7 @@ def test_get_command():
Popen.return_value.stderr.read.return_value = b'stderr'
assert main.get_command(Mock(), ['thefuck', 'apt-get',
'search', 'vim']) \
== types.Command('apt-get search vim', 'stdout', 'stderr')
== Command('apt-get search vim', 'stdout', 'stderr')
Popen.assert_called_once_with('apt-get search vim',
shell=True,
stdout=PIPE,
@@ -57,12 +58,12 @@ def test_get_command():
def test_get_matched_rule(capsys):
rules = [types.Rule('', lambda x, _: x.script == 'cd ..', None, True),
types.Rule('', lambda *_: False, None, True),
types.Rule('rule', Mock(side_effect=OSError('Denied')), None, True)]
assert main.get_matched_rule(types.Command('ls', '', ''),
rules = [Rule('', lambda x, _: x.script == 'cd ..'),
Rule('', lambda *_: False),
Rule('rule', Mock(side_effect=OSError('Denied')))]
assert main.get_matched_rule(Command('ls'),
rules, Mock(no_colors=True)) is None
assert main.get_matched_rule(types.Command('cd ..', '', ''),
assert main.get_matched_rule(Command('cd ..'),
rules, Mock(no_colors=True)) == rules[0]
assert capsys.readouterr()[1].split('\n')[0] \
== '[WARN] Rule rule:'
@@ -70,11 +71,11 @@ def test_get_matched_rule(capsys):
def test_run_rule(capsys):
with patch('thefuck.main.confirm', return_value=True):
main.run_rule(types.Rule('', None, lambda *_: 'new-command', True),
main.run_rule(Rule(get_new_command=lambda *_: 'new-command'),
None, None)
assert capsys.readouterr() == ('new-command\n', '')
with patch('thefuck.main.confirm', return_value=False):
main.run_rule(types.Rule('', None, lambda *_: 'new-command', True),
main.run_rule(Rule(get_new_command=lambda *_: 'new-command'),
None, None)
assert capsys.readouterr() == ('', '')