1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-01-18 12:06:04 +00:00

Remove obscure RulesNamesList and DefaultRulesNames

This commit is contained in:
nvbn 2015-09-08 12:14:39 +03:00
parent 122541b7d8
commit b2be0b3cad
6 changed files with 40 additions and 63 deletions

View File

@ -2,15 +2,6 @@ import pytest
import six
from mock import Mock
from thefuck import conf
from tests.utils import Rule
@pytest.mark.parametrize('enabled, rules, result', [
(True, conf.DEFAULT_RULES, True),
(False, conf.DEFAULT_RULES, False),
(False, conf.DEFAULT_RULES + ['test'], True)])
def test_default(enabled, rules, result):
assert (Rule('test', enabled_by_default=enabled) in rules) == result
@pytest.fixture

View File

@ -1,9 +1,9 @@
import pytest
from pathlib import PosixPath, Path
from mock import Mock
from thefuck import corrector, conf, types
from thefuck import corrector, conf
from tests.utils import Rule, Command, CorrectedCommand
from thefuck.corrector import make_corrected_commands, get_corrected_commands
from thefuck.corrector import make_corrected_commands, get_corrected_commands, is_rule_enabled
def test_load_rule(mocker):
@ -21,6 +21,23 @@ def test_load_rule(mocker):
load_source.assert_called_once_with('bash', '/rules/bash.py')
@pytest.mark.parametrize('rules, exclude_rules, rule, is_enabled', [
(conf.DEFAULT_RULES, [], Rule('git', enabled_by_default=True), True),
(conf.DEFAULT_RULES, [], Rule('git', enabled_by_default=False), False),
([], [], Rule('git', enabled_by_default=False), False),
([], [], Rule('git', enabled_by_default=True), False),
(conf.DEFAULT_RULES + ['git'], [], Rule('git', enabled_by_default=False), True),
(['git'], [], Rule('git', enabled_by_default=False), True),
(conf.DEFAULT_RULES, ['git'], Rule('git', enabled_by_default=True), False),
(conf.DEFAULT_RULES, ['git'], Rule('git', enabled_by_default=False), False),
([], ['git'], Rule('git', enabled_by_default=True), False),
([], ['git'], Rule('git', enabled_by_default=False), False)])
def test_is_rule_enabled(settings, rules, exclude_rules, rule, is_enabled):
settings.update(rules=rules,
exclude_rules=exclude_rules)
assert is_rule_enabled(rule) == is_enabled
class TestGetRules(object):
@pytest.fixture
def glob(self, mocker):
@ -37,12 +54,6 @@ class TestGetRules(object):
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']),
@ -51,9 +62,9 @@ class TestGetRules(object):
def test_get_rules(self, glob, settings, paths, conf_rules, exclude_rules,
loaded_rules):
glob([PosixPath(path) for path in paths])
settings.update(rules=self._prepare_rules(conf_rules),
settings.update(rules=conf_rules,
priority={},
exclude_rules=self._prepare_rules(exclude_rules))
exclude_rules=exclude_rules)
rules = corrector.get_rules()
self._compare_names(rules, loaded_rules)

View File

@ -1,13 +1,5 @@
from thefuck.types import RulesNamesList, Settings, \
SortedCorrectedCommandsSequence
from tests.utils import Rule, CorrectedCommand
def test_rules_names_list():
assert RulesNamesList(['bash', 'lisp']) == ['bash', 'lisp']
assert RulesNamesList(['bash', 'lisp']) == RulesNamesList(['bash', 'lisp'])
assert Rule('lisp') in RulesNamesList(['lisp'])
assert Rule('bash') not in RulesNamesList(['lisp'])
from thefuck.types import SortedCorrectedCommandsSequence
from tests.utils import CorrectedCommand
class TestSortedCorrectedCommandsSequence(object):

View File

@ -2,25 +2,10 @@ from imp import load_source
import os
import sys
from six import text_type
from .types import RulesNamesList, Settings
from .types import Settings
class _DefaultRulesNames(RulesNamesList):
def __add__(self, items):
return _DefaultRulesNames(list(self) + items)
def __contains__(self, item):
return item.enabled_by_default or \
super(_DefaultRulesNames, self).__contains__(item)
def __eq__(self, other):
if isinstance(other, _DefaultRulesNames):
return super(_DefaultRulesNames, self).__eq__(other)
else:
return False
DEFAULT_RULES = _DefaultRulesNames([])
ALL_ENABLED = object()
DEFAULT_RULES = [ALL_ENABLED]
DEFAULT_PRIORITY = 1000
DEFAULT_SETTINGS = {'rules': DEFAULT_RULES,
@ -121,12 +106,6 @@ def init_settings(user_dir):
except Exception:
exception("Can't load settings from env", sys.exc_info())
if not isinstance(settings['rules'], RulesNamesList):
settings.rules = RulesNamesList(settings['rules'])
if not isinstance(settings.exclude_rules, RulesNamesList):
settings.exclude_rules = RulesNamesList(settings.exclude_rules)
def initialize_settings_file(user_dir):
settings_path = user_dir.joinpath('settings.py')

View File

@ -1,7 +1,7 @@
import sys
from imp import load_source
from pathlib import Path
from .conf import settings, DEFAULT_PRIORITY
from .conf import settings, DEFAULT_PRIORITY, ALL_ENABLED
from .types import Rule, CorrectedCommand, SortedCorrectedCommandsSequence
from .utils import compatibility_call
from . import logs
@ -21,13 +21,24 @@ def load_rule(rule):
getattr(rule_module, 'requires_output', True))
def is_rule_enabled(rule):
"""Returns `True` when rule enabled."""
if rule.name in settings.exclude_rules:
return False
elif rule.name in settings.rules:
return True
elif rule.enabled_by_default and ALL_ENABLED in settings.rules:
return True
else:
return False
def get_loaded_rules(rules):
"""Yields all available rules."""
for rule in rules:
if rule.name != '__init__.py':
loaded_rule = load_rule(rule)
if loaded_rule in settings.rules and \
loaded_rule not in settings.exclude_rules:
if is_rule_enabled(loaded_rule):
yield loaded_rule

View File

@ -30,13 +30,6 @@ class CorrectedCommand(object):
self.script, self.side_effect, self.priority)
class RulesNamesList(list):
"""Wrapper a top of list for storing rules names."""
def __contains__(self, item):
return super(RulesNamesList, self).__contains__(item.name)
class Settings(dict):
def __getattr__(self, item):
return self.get(item)