1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-02-22 04:48:57 +00:00

#364 Attach user_dir to settings

This commit is contained in:
nvbn 2015-09-07 18:59:10 +03:00
parent df4d2cc88d
commit f3525e9fe0
5 changed files with 17 additions and 9 deletions

View File

@ -1,3 +1,4 @@
from pathlib import Path
import pytest import pytest
from thefuck import conf from thefuck import conf
@ -16,7 +17,12 @@ def no_memoize(monkeypatch):
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def settings(request): def settings(request):
request.addfinalizer(lambda: conf.settings.update(conf.DEFAULT_SETTINGS)) def _reset_settings():
conf.settings.clear()
conf.settings.update(conf.DEFAULT_SETTINGS)
request.addfinalizer(_reset_settings)
conf.settings.user_dir = Path('~/.thefuck')
return conf.settings return conf.settings

View File

@ -25,7 +25,7 @@ class TestGetRules(object):
@pytest.fixture @pytest.fixture
def glob(self, mocker): def glob(self, mocker):
results = {} results = {}
mocker.patch('thefuck.corrector.Path.glob', mocker.patch('pathlib.Path.glob',
new_callable=lambda: lambda *_: results.pop('value', [])) new_callable=lambda: lambda *_: results.pop('value', []))
return lambda value: results.update({'value': value}) return lambda value: results.update({'value': value})
@ -54,7 +54,7 @@ class TestGetRules(object):
settings.update(rules=self._prepare_rules(conf_rules), settings.update(rules=self._prepare_rules(conf_rules),
priority={}, priority={},
exclude_rules=self._prepare_rules(exclude_rules)) exclude_rules=self._prepare_rules(exclude_rules))
rules = corrector.get_rules(Path('~')) rules = corrector.get_rules()
self._compare_names(rules, loaded_rules) self._compare_names(rules, loaded_rules)
@ -98,5 +98,5 @@ def test_get_corrected_commands(mocker):
get_new_command=lambda x: [x.script + '@', x.script + ';'], get_new_command=lambda x: [x.script + '@', x.script + ';'],
priority=60)] priority=60)]
mocker.patch('thefuck.corrector.get_rules', return_value=rules) mocker.patch('thefuck.corrector.get_rules', return_value=rules)
assert [cmd.script for cmd in get_corrected_commands(command, None)] \ assert [cmd.script for cmd in get_corrected_commands(command)] \
== ['test!', 'test@', 'test;'] == ['test!', 'test@', 'test;']

View File

@ -109,6 +109,8 @@ def init_settings(user_dir):
"""Fills `settings` with values from `settings.py` and env.""" """Fills `settings` with values from `settings.py` and env."""
from .logs import exception from .logs import exception
settings.user_dir = user_dir
try: try:
settings.update(_settings_from_file(user_dir)) settings.update(_settings_from_file(user_dir))
except Exception: except Exception:

View File

@ -31,12 +31,12 @@ def get_loaded_rules(rules):
yield loaded_rule yield loaded_rule
def get_rules(user_dir): def get_rules():
"""Returns all enabled rules.""" """Returns all enabled rules."""
bundled = Path(__file__).parent \ bundled = Path(__file__).parent \
.joinpath('rules') \ .joinpath('rules') \
.glob('*.py') .glob('*.py')
user = user_dir.joinpath('rules').glob('*.py') user = settings.user_dir.joinpath('rules').glob('*.py')
return sorted(get_loaded_rules(sorted(bundled) + sorted(user)), return sorted(get_loaded_rules(sorted(bundled) + sorted(user)),
key=lambda rule: rule.priority) key=lambda rule: rule.priority)
@ -66,9 +66,9 @@ def make_corrected_commands(command, rule):
priority=(n + 1) * rule.priority) priority=(n + 1) * rule.priority)
def get_corrected_commands(command, user_dir): def get_corrected_commands(command):
corrected_commands = ( corrected_commands = (
corrected for rule in get_rules(user_dir) corrected for rule in get_rules()
if is_rule_match(command, rule) if is_rule_match(command, rule)
for corrected in make_corrected_commands(command, rule)) for corrected in make_corrected_commands(command, rule))
return SortedCorrectedCommandsSequence(corrected_commands) return SortedCorrectedCommandsSequence(corrected_commands)

View File

@ -98,7 +98,7 @@ def fix_command():
logs.debug('Empty command, nothing to do') logs.debug('Empty command, nothing to do')
return return
corrected_commands = get_corrected_commands(command, user_dir) corrected_commands = get_corrected_commands(command)
selected_command = select_command(corrected_commands) selected_command = select_command(corrected_commands)
if selected_command: if selected_command:
run_command(command, selected_command) run_command(command, selected_command)