Skip to content

Commit

Permalink
#1113: Ignore a rule that fails to load
Browse files Browse the repository at this point in the history
  • Loading branch information
scorphus committed Aug 11, 2020
1 parent c196e29 commit 2f27823
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
23 changes: 16 additions & 7 deletions tests/test_corrector.py
Expand Up @@ -8,14 +8,15 @@
from thefuck.corrector import get_corrected_commands, organize_commands


class TestGetRules(object):
@pytest.fixture
def glob(self, mocker):
results = {}
mocker.patch('thefuck.system.Path.glob',
new_callable=lambda: lambda *_: results.pop('value', []))
return lambda value: results.update({'value': value})
@pytest.fixture
def glob(mocker):
results = {}
mocker.patch('thefuck.system.Path.glob',
new_callable=lambda: lambda *_: results.pop('value', []))
return lambda value: results.update({'value': value})


class TestGetRules(object):
@pytest.fixture(autouse=True)
def load_source(self, monkeypatch):
monkeypatch.setattr('thefuck.types.load_source',
Expand All @@ -39,6 +40,14 @@ def test_get_rules(self, glob, settings, paths, conf_rules, exclude_rules,
self._compare_names(rules, loaded_rules)


def test_get_rules_rule_exception(mocker, glob):
load_source = mocker.patch('thefuck.types.load_source',
side_effect=ImportError("No module named foo..."))
glob([Path('git.py')])
assert not corrector.get_rules()
load_source.assert_called_once_with('git', 'git.py')


def test_get_corrected_commands(mocker):
command = Command('test', 'test')
rules = [Rule(match=lambda _: False),
Expand Down
6 changes: 6 additions & 0 deletions tests/test_types.py
Expand Up @@ -45,6 +45,12 @@ def test_run(self, capsys, settings, script, printed, override_settings):


class TestRule(object):
def test_from_path_rule_exception(self, mocker):
load_source = mocker.patch('thefuck.types.load_source',
side_effect=ImportError("No module named foo..."))
assert Rule.from_path(Path('git.py')) is None
load_source.assert_called_once_with('git', 'git.py')

def test_from_path(self, mocker):
match = object()
get_new_command = object()
Expand Down
2 changes: 1 addition & 1 deletion thefuck/corrector.py
Expand Up @@ -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


Expand Down
8 changes: 6 additions & 2 deletions thefuck/types.py
Expand Up @@ -137,8 +137,12 @@ def from_path(cls, path):
"""
name = path.name[:-3]
with logs.debug_time(u'Importing rule: {};'.format(name)):
rule_module = load_source(name, str(path))
priority = getattr(rule_module, 'priority', DEFAULT_PRIORITY)
try:
rule_module = load_source(name, str(path))
except Exception:
logs.exception(u"Rule {} failed to load".format(name), sys.exc_info())
return
priority = getattr(rule_module, 'priority', DEFAULT_PRIORITY)
return cls(name, rule_module.match,
rule_module.get_new_command,
getattr(rule_module, 'enabled_by_default', True),
Expand Down

0 comments on commit 2f27823

Please sign in to comment.