mirror of
https://github.com/nvbn/thefuck.git
synced 2025-01-19 12:24:29 +00:00
ebf1ea88f5
The pathlib backport module is no longer maintained. The development has moved to the pathlib2 module instead. Quoting from the pathlib's README: "Attention: this backport module isn't maintained anymore. If you want to report issues or contribute patches, please consider the pathlib2 project instead."
69 lines
2.8 KiB
Python
69 lines
2.8 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
import pytest
|
|
try:
|
|
from pathlib import Path
|
|
pathlib_name = 'pathlib'
|
|
except ImportError:
|
|
from pathlib2 import Path
|
|
pathlib_name = 'pathlib2'
|
|
from thefuck import corrector, const
|
|
from tests.utils import Rule, Command, CorrectedCommand
|
|
from thefuck.corrector import get_corrected_commands, organize_commands
|
|
|
|
|
|
class TestGetRules(object):
|
|
@pytest.fixture
|
|
def glob(self, mocker):
|
|
results = {}
|
|
mocker.patch(pathlib_name + '.Path.glob',
|
|
new_callable=lambda: lambda *_: results.pop('value', []))
|
|
return lambda value: results.update({'value': value})
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def load_source(self, monkeypatch):
|
|
monkeypatch.setattr('thefuck.types.load_source',
|
|
lambda x, _: Rule(x))
|
|
|
|
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', [
|
|
(['git.py', 'bash.py'], const.DEFAULT_RULES, [], ['git', 'bash']),
|
|
(['git.py', 'bash.py'], ['git'], [], ['git']),
|
|
(['git.py', 'bash.py'], const.DEFAULT_RULES, ['git'], ['bash']),
|
|
(['git.py', 'bash.py'], ['git'], ['git'], [])])
|
|
def test_get_rules(self, glob, settings, paths, conf_rules, exclude_rules,
|
|
loaded_rules):
|
|
glob([Path(path) for path in paths])
|
|
settings.update(rules=conf_rules,
|
|
priority={},
|
|
exclude_rules=exclude_rules)
|
|
rules = corrector.get_rules()
|
|
self._compare_names(rules, loaded_rules)
|
|
|
|
|
|
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 + ';'],
|
|
priority=60)]
|
|
mocker.patch('thefuck.corrector.get_rules', return_value=rules)
|
|
assert [cmd.script for cmd in get_corrected_commands(command)] \
|
|
== ['test!', 'test@', 'test;']
|
|
|
|
|
|
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),
|
|
CorrectedCommand(u'echo café', priority=200),
|
|
CorrectedCommand('ls -lh', priority=9999)]
|
|
assert list(organize_commands(iter(commands))) \
|
|
== [CorrectedCommand('ls'), CorrectedCommand('ls -lh', priority=100),
|
|
CorrectedCommand(u'echo café', priority=200),
|
|
CorrectedCommand('ls -la', priority=9000)]
|