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