2015-11-04 23:08:13 -02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2015-07-28 22:04:27 +03:00
|
|
|
import pytest
|
2016-02-22 18:40:28 +03:00
|
|
|
from thefuck import corrector, const
|
2016-08-14 15:15:03 +03:00
|
|
|
from thefuck.system import Path
|
2015-08-01 19:16:22 +03:00
|
|
|
from tests.utils import Rule, Command, CorrectedCommand
|
2015-09-08 14:18:11 +03:00
|
|
|
from thefuck.corrector import get_corrected_commands, organize_commands
|
2015-09-08 12:14:39 +03:00
|
|
|
|
|
|
|
|
2015-07-28 22:04:27 +03:00
|
|
|
class TestGetRules(object):
|
2015-09-06 12:55:59 +03:00
|
|
|
@pytest.fixture
|
2015-07-28 22:04:27 +03:00
|
|
|
def glob(self, mocker):
|
2015-09-06 12:55:59 +03:00
|
|
|
results = {}
|
2016-08-14 15:15:03 +03:00
|
|
|
mocker.patch('thefuck.system.Path.glob',
|
2015-09-06 12:55:59 +03:00
|
|
|
new_callable=lambda: lambda *_: results.pop('value', []))
|
|
|
|
return lambda value: results.update({'value': value})
|
2015-07-28 22:04:27 +03:00
|
|
|
|
2015-09-06 12:55:59 +03:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def load_source(self, monkeypatch):
|
2015-09-08 14:18:11 +03:00
|
|
|
monkeypatch.setattr('thefuck.types.load_source',
|
2015-07-28 22:04:27 +03:00
|
|
|
lambda x, _: Rule(x))
|
2015-09-06 12:55:59 +03:00
|
|
|
|
|
|
|
def _compare_names(self, rules, names):
|
|
|
|
assert {r.name for r in rules} == set(names)
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('paths, conf_rules, exclude_rules, loaded_rules', [
|
2016-02-22 18:40:28 +03:00
|
|
|
(['git.py', 'bash.py'], const.DEFAULT_RULES, [], ['git', 'bash']),
|
2015-09-06 12:55:59 +03:00
|
|
|
(['git.py', 'bash.py'], ['git'], [], ['git']),
|
2016-02-22 18:40:28 +03:00
|
|
|
(['git.py', 'bash.py'], const.DEFAULT_RULES, ['git'], ['bash']),
|
2015-09-06 12:55:59 +03:00
|
|
|
(['git.py', 'bash.py'], ['git'], ['git'], [])])
|
2015-09-07 12:12:16 +03:00
|
|
|
def test_get_rules(self, glob, settings, paths, conf_rules, exclude_rules,
|
|
|
|
loaded_rules):
|
2016-03-29 23:34:08 -03:00
|
|
|
glob([Path(path) for path in paths])
|
2015-09-08 12:14:39 +03:00
|
|
|
settings.update(rules=conf_rules,
|
2015-09-06 12:55:59 +03:00
|
|
|
priority={},
|
2015-09-08 12:14:39 +03:00
|
|
|
exclude_rules=exclude_rules)
|
2015-09-07 18:59:10 +03:00
|
|
|
rules = corrector.get_rules()
|
2015-09-06 12:55:59 +03:00
|
|
|
self._compare_names(rules, loaded_rules)
|
2015-07-28 22:04:27 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_get_corrected_commands(mocker):
|
|
|
|
command = Command('test', 'test', 'test')
|
2015-09-07 13:00:29 +03:00
|
|
|
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 22:04:27 +03:00
|
|
|
priority=60)]
|
|
|
|
mocker.patch('thefuck.corrector.get_rules', return_value=rules)
|
2015-09-07 18:59:10 +03:00
|
|
|
assert [cmd.script for cmd in get_corrected_commands(command)] \
|
2015-09-01 12:51:41 +03:00
|
|
|
== ['test!', 'test@', 'test;']
|
2015-09-08 12:52:10 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_organize_commands():
|
|
|
|
"""Ensures that the function removes duplicates and sorts commands."""
|
|
|
|
commands = [CorrectedCommand('ls'), CorrectedCommand('ls -la', priority=9000),
|
|
|
|
CorrectedCommand('ls -lh', priority=100),
|
2015-11-04 23:08:13 -02:00
|
|
|
CorrectedCommand(u'echo café', priority=200),
|
2015-09-08 12:52:10 +03:00
|
|
|
CorrectedCommand('ls -lh', priority=9999)]
|
|
|
|
assert list(organize_commands(iter(commands))) \
|
|
|
|
== [CorrectedCommand('ls'), CorrectedCommand('ls -lh', priority=100),
|
2015-11-04 23:08:13 -02:00
|
|
|
CorrectedCommand(u'echo café', priority=200),
|
2015-09-08 12:52:10 +03:00
|
|
|
CorrectedCommand('ls -la', priority=9000)]
|