From b2be0b3cad25ae0215187994ac6f78128ed066fe Mon Sep 17 00:00:00 2001 From: nvbn Date: Tue, 8 Sep 2015 12:14:39 +0300 Subject: [PATCH] Remove obscure `RulesNamesList` and `DefaultRulesNames` --- tests/test_conf.py | 9 --------- tests/test_corrector.py | 31 +++++++++++++++++++++---------- tests/test_types.py | 12 ++---------- thefuck/conf.py | 27 +++------------------------ thefuck/corrector.py | 17 ++++++++++++++--- thefuck/types.py | 7 ------- 6 files changed, 40 insertions(+), 63 deletions(-) diff --git a/tests/test_conf.py b/tests/test_conf.py index e1042072..b5c50b56 100644 --- a/tests/test_conf.py +++ b/tests/test_conf.py @@ -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 diff --git a/tests/test_corrector.py b/tests/test_corrector.py index f6bf2c7c..cb817b77 100644 --- a/tests/test_corrector.py +++ b/tests/test_corrector.py @@ -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) diff --git a/tests/test_types.py b/tests/test_types.py index bc57e489..e0c1e682 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -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): diff --git a/thefuck/conf.py b/thefuck/conf.py index bd7e476b..0f0f8e7f 100644 --- a/thefuck/conf.py +++ b/thefuck/conf.py @@ -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') diff --git a/thefuck/corrector.py b/thefuck/corrector.py index b9433624..acc82bef 100644 --- a/thefuck/corrector.py +++ b/thefuck/corrector.py @@ -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 diff --git a/thefuck/types.py b/thefuck/types.py index 8e302412..b3ebddce 100644 --- a/thefuck/types.py +++ b/thefuck/types.py @@ -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)