1
0
mirror of https://github.com/nvbn/thefuck.git synced 2024-10-06 02:41:10 +01:00
thefuck/tests/test_corrector.py

103 lines
4.2 KiB
Python
Raw Normal View History

2015-07-28 20:04:27 +01:00
import pytest
from pathlib import PosixPath, Path
from mock import Mock
from thefuck import corrector, conf, types
2015-08-01 17:16:22 +01:00
from tests.utils import Rule, Command, CorrectedCommand
from thefuck.corrector import make_corrected_commands, get_corrected_commands
2015-07-28 20:04:27 +01:00
def test_load_rule(mocker):
match = object()
get_new_command = object()
load_source = mocker.patch(
'thefuck.corrector.load_source',
return_value=Mock(match=match,
get_new_command=get_new_command,
enabled_by_default=True,
priority=900,
requires_output=True))
2015-09-07 10:12:16 +01:00
assert corrector.load_rule(Path('/rules/bash.py')) \
2015-07-28 20:04:27 +01:00
== Rule('bash', match, get_new_command, priority=900)
load_source.assert_called_once_with('bash', '/rules/bash.py')
class TestGetRules(object):
@pytest.fixture
2015-07-28 20:04:27 +01:00
def glob(self, mocker):
results = {}
mocker.patch('thefuck.corrector.Path.glob',
new_callable=lambda: lambda *_: results.pop('value', []))
return lambda value: results.update({'value': value})
2015-07-28 20:04:27 +01:00
@pytest.fixture(autouse=True)
def load_source(self, monkeypatch):
2015-07-28 20:04:27 +01:00
monkeypatch.setattr('thefuck.corrector.load_source',
lambda x, _: Rule(x))
def _compare_names(self, rules, names):
assert {r.name for r in rules} == set(names)
def _prepare_rules(self, rules):
if rules == conf.DEFAULT_RULES:
return rules
else:
return types.RulesNamesList(rules)
@pytest.mark.parametrize('paths, conf_rules, exclude_rules, loaded_rules', [
(['git.py', 'bash.py'], conf.DEFAULT_RULES, [], ['git', 'bash']),
(['git.py', 'bash.py'], ['git'], [], ['git']),
(['git.py', 'bash.py'], conf.DEFAULT_RULES, ['git'], ['bash']),
(['git.py', 'bash.py'], ['git'], ['git'], [])])
2015-09-07 10:12:16 +01:00
def test_get_rules(self, glob, settings, paths, conf_rules, exclude_rules,
loaded_rules):
glob([PosixPath(path) for path in paths])
2015-09-07 10:12:16 +01:00
settings.update(rules=self._prepare_rules(conf_rules),
priority={},
exclude_rules=self._prepare_rules(exclude_rules))
2015-09-07 10:12:16 +01:00
rules = corrector.get_rules(Path('~'))
self._compare_names(rules, loaded_rules)
2015-07-28 20:04:27 +01:00
2015-09-01 12:43:27 +01:00
class TestIsRuleMatch(object):
2015-09-07 10:12:16 +01:00
def test_no_match(self):
2015-09-01 12:43:27 +01:00
assert not corrector.is_rule_match(
Command('ls'), Rule('', lambda _: False))
2015-07-28 20:04:27 +01:00
2015-09-07 10:12:16 +01:00
def test_match(self):
rule = Rule('', lambda x: x.script == 'cd ..')
2015-09-07 10:12:16 +01:00
assert corrector.is_rule_match(Command('cd ..'), rule)
2015-07-28 20:04:27 +01:00
2015-09-07 10:12:16 +01:00
@pytest.mark.usefixtures('no_colors')
def test_when_rule_failed(self, capsys):
2015-09-01 12:43:27 +01:00
rule = Rule('test', Mock(side_effect=OSError('Denied')),
requires_output=False)
2015-09-07 10:12:16 +01:00
assert not corrector.is_rule_match(Command('ls'), rule)
2015-07-28 20:04:27 +01:00
assert capsys.readouterr()[1].split('\n')[0] == '[WARN] Rule test:'
2015-09-01 12:43:27 +01:00
class TestMakeCorrectedCommands(object):
2015-07-28 20:04:27 +01:00
def test_with_rule_returns_list(self):
rule = Rule(get_new_command=lambda x: [x.script + '!', x.script + '@'],
2015-07-28 20:04:27 +01:00
priority=100)
2015-09-07 10:12:16 +01:00
assert list(make_corrected_commands(Command(script='test'), rule)) \
2015-08-01 17:16:22 +01:00
== [CorrectedCommand(script='test!', priority=100),
CorrectedCommand(script='test@', priority=200)]
2015-07-28 20:04:27 +01:00
def test_with_rule_returns_command(self):
rule = Rule(get_new_command=lambda x: x.script + '!',
2015-07-28 20:04:27 +01:00
priority=100)
2015-09-07 10:12:16 +01:00
assert list(make_corrected_commands(Command(script='test'), rule)) \
2015-08-01 17:16:22 +01:00
== [CorrectedCommand(script='test!', priority=100)]
2015-07-28 20:04:27 +01:00
def test_get_corrected_commands(mocker):
command = Command('test', 'test', 'test')
rules = [Rule(match=lambda _: False),
Rule(match=lambda _: True,
get_new_command=lambda x: x.script + '!', priority=100),
Rule(match=lambda _: True,
get_new_command=lambda x: [x.script + '@', x.script + ';'],
2015-07-28 20:04:27 +01:00
priority=60)]
mocker.patch('thefuck.corrector.get_rules', return_value=rules)
2015-09-07 10:12:16 +01:00
assert [cmd.script for cmd in get_corrected_commands(command, None)] \
== ['test!', 'test@', 'test;']