1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-03-20 01:28:56 +00:00

#1113: Do not load excluded rules

This commit is contained in:
Pablo Santiago Blum de Aguiar 2020-08-11 17:01:23 +02:00
parent c196e2901c
commit db6a421d5f
3 changed files with 25 additions and 23 deletions

View File

@ -60,20 +60,22 @@ class TestRule(object):
== Rule('bash', match, get_new_command, priority=900)) == Rule('bash', match, get_new_command, priority=900))
load_source.assert_called_once_with('bash', rule_path) load_source.assert_called_once_with('bash', rule_path)
@pytest.mark.parametrize('rules, exclude_rules, rule, is_enabled', [ def test_from_path_excluded_rule(self, mocker, settings):
(const.DEFAULT_RULES, [], Rule('git', enabled_by_default=True), True), load_source = mocker.patch('thefuck.types.load_source')
(const.DEFAULT_RULES, [], Rule('git', enabled_by_default=False), False), settings.update(exclude_rules=['git'])
([], [], Rule('git', enabled_by_default=False), False), rule_path = os.path.join(os.sep, 'rules', 'git.py')
([], [], Rule('git', enabled_by_default=True), False), assert Rule.from_path(Path(rule_path)) is None
(const.DEFAULT_RULES + ['git'], [], Rule('git', enabled_by_default=False), True), assert not load_source.called
(['git'], [], Rule('git', enabled_by_default=False), True),
(const.DEFAULT_RULES, ['git'], Rule('git', enabled_by_default=True), False), @pytest.mark.parametrize('rules, rule, is_enabled', [
(const.DEFAULT_RULES, ['git'], Rule('git', enabled_by_default=False), False), (const.DEFAULT_RULES, Rule('git', enabled_by_default=True), True),
([], ['git'], Rule('git', enabled_by_default=True), False), (const.DEFAULT_RULES, Rule('git', enabled_by_default=False), False),
([], ['git'], Rule('git', enabled_by_default=False), False)]) ([], Rule('git', enabled_by_default=False), False),
def test_is_enabled(self, settings, rules, exclude_rules, rule, is_enabled): ([], Rule('git', enabled_by_default=True), False),
settings.update(rules=rules, (const.DEFAULT_RULES + ['git'], Rule('git', enabled_by_default=False), True),
exclude_rules=exclude_rules) (['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 assert rule.is_enabled == is_enabled
def test_isnt_match(self): def test_isnt_match(self):

View File

@ -15,7 +15,7 @@ def get_loaded_rules(rules_paths):
for path in rules_paths: for path in rules_paths:
if path.name != '__init__.py': if path.name != '__init__.py':
rule = Rule.from_path(path) rule = Rule.from_path(path)
if rule.is_enabled: if rule and rule.is_enabled:
yield rule yield rule

View File

@ -136,6 +136,9 @@ class Rule(object):
""" """
name = path.name[:-3] 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)): with logs.debug_time(u'Importing rule: {};'.format(name)):
rule_module = load_source(name, str(path)) rule_module = load_source(name, str(path))
priority = getattr(rule_module, 'priority', DEFAULT_PRIORITY) priority = getattr(rule_module, 'priority', DEFAULT_PRIORITY)
@ -153,14 +156,11 @@ class Rule(object):
:rtype: bool :rtype: bool
""" """
if self.name in settings.exclude_rules: return (
return False self.name in settings.rules
elif self.name in settings.rules: or self.enabled_by_default
return True and ALL_ENABLED in settings.rules
elif self.enabled_by_default and ALL_ENABLED in settings.rules: )
return True
else:
return False
def is_match(self, command): def is_match(self, command):
"""Returns `True` if rule matches the command. """Returns `True` if rule matches the command.