diff --git a/tests/test_types.py b/tests/test_types.py index 4d0f0f70..17bc74ab 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -60,20 +60,22 @@ class TestRule(object): == Rule('bash', match, get_new_command, priority=900)) load_source.assert_called_once_with('bash', rule_path) - @pytest.mark.parametrize('rules, exclude_rules, rule, is_enabled', [ - (const.DEFAULT_RULES, [], Rule('git', enabled_by_default=True), True), - (const.DEFAULT_RULES, [], Rule('git', enabled_by_default=False), False), - ([], [], Rule('git', enabled_by_default=False), False), - ([], [], Rule('git', enabled_by_default=True), False), - (const.DEFAULT_RULES + ['git'], [], Rule('git', enabled_by_default=False), True), - (['git'], [], Rule('git', enabled_by_default=False), True), - (const.DEFAULT_RULES, ['git'], Rule('git', enabled_by_default=True), False), - (const.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_enabled(self, settings, rules, exclude_rules, rule, is_enabled): - settings.update(rules=rules, - exclude_rules=exclude_rules) + def test_from_path_excluded_rule(self, mocker, settings): + load_source = mocker.patch('thefuck.types.load_source') + settings.update(exclude_rules=['git']) + rule_path = os.path.join(os.sep, 'rules', 'git.py') + assert Rule.from_path(Path(rule_path)) is None + assert not load_source.called + + @pytest.mark.parametrize('rules, rule, is_enabled', [ + (const.DEFAULT_RULES, Rule('git', enabled_by_default=True), True), + (const.DEFAULT_RULES, Rule('git', enabled_by_default=False), False), + ([], Rule('git', enabled_by_default=False), False), + ([], Rule('git', enabled_by_default=True), False), + (const.DEFAULT_RULES + ['git'], Rule('git', enabled_by_default=False), True), + (['git'], Rule('git', enabled_by_default=False), True)]) + def test_is_enabled(self, settings, rules, rule, is_enabled): + settings.update(rules=rules) assert rule.is_enabled == is_enabled def test_isnt_match(self): diff --git a/thefuck/corrector.py b/thefuck/corrector.py index 89d21ebf..fdd46983 100644 --- a/thefuck/corrector.py +++ b/thefuck/corrector.py @@ -15,7 +15,7 @@ def get_loaded_rules(rules_paths): for path in rules_paths: if path.name != '__init__.py': rule = Rule.from_path(path) - if rule.is_enabled: + if rule and rule.is_enabled: yield rule diff --git a/thefuck/types.py b/thefuck/types.py index 5d8c2cc7..372968b2 100644 --- a/thefuck/types.py +++ b/thefuck/types.py @@ -136,6 +136,9 @@ class Rule(object): """ name = path.name[:-3] + if name in settings.exclude_rules: + logs.debug(u'Ignoring excluded rule: {}'.format(name)) + return with logs.debug_time(u'Importing rule: {};'.format(name)): rule_module = load_source(name, str(path)) priority = getattr(rule_module, 'priority', DEFAULT_PRIORITY) @@ -153,14 +156,11 @@ class Rule(object): :rtype: bool """ - if self.name in settings.exclude_rules: - return False - elif self.name in settings.rules: - return True - elif self.enabled_by_default and ALL_ENABLED in settings.rules: - return True - else: - return False + return ( + self.name in settings.rules + or self.enabled_by_default + and ALL_ENABLED in settings.rules + ) def is_match(self, command): """Returns `True` if rule matches the command.