1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-02-22 12:58:33 +00:00

#74 Don't fail when rule throws exception

This commit is contained in:
nvbn 2015-04-21 14:40:52 +02:00
parent d5b4bddc4c
commit 888756d519
2 changed files with 25 additions and 16 deletions

View File

@ -24,7 +24,7 @@ def test_load_rule():
return_value=Mock( return_value=Mock(
match=match, match=match,
get_new_command=get_new_command)) as load_source: get_new_command=get_new_command)) as load_source:
assert main.load_rule(Path('/rules/bash.py')) == main.Rule(match, get_new_command) assert main.load_rule(Path('/rules/bash.py')) == main.Rule('bash', match, get_new_command)
load_source.assert_called_once_with('bash', '/rules/bash.py') load_source.assert_called_once_with('bash', '/rules/bash.py')
@ -35,14 +35,14 @@ def test_get_rules():
glob.return_value = [PosixPath('bash.py'), PosixPath('lisp.py')] glob.return_value = [PosixPath('bash.py'), PosixPath('lisp.py')]
assert main.get_rules( assert main.get_rules(
Path('~'), Path('~'),
Mock(rules=None)) == [main.Rule('bash', 'bash'), Mock(rules=None)) == [main.Rule('bash', 'bash', 'bash'),
main.Rule('lisp', 'lisp'), main.Rule('lisp', 'lisp', 'lisp'),
main.Rule('bash', 'bash'), main.Rule('bash', 'bash', 'bash'),
main.Rule('lisp', 'lisp')] main.Rule('lisp', 'lisp', 'lisp')]
assert main.get_rules( assert main.get_rules(
Path('~'), Path('~'),
Mock(rules=['bash'])) == [main.Rule('bash', 'bash'), Mock(rules=['bash'])) == [main.Rule('bash', 'bash', 'bash'),
main.Rule('bash', 'bash')] main.Rule('bash', 'bash', 'bash')]
def test_get_command(): def test_get_command():
@ -64,22 +64,25 @@ def test_get_command():
assert main.get_command(Mock(), ['']) is None assert main.get_command(Mock(), ['']) is None
def test_get_matched_rule(): def test_get_matched_rule(capsys):
rules = [main.Rule(lambda x, _: x.script == 'cd ..', None), rules = [main.Rule('', lambda x, _: x.script == 'cd ..', None),
main.Rule(lambda *_: False, None)] main.Rule('', lambda *_: False, None),
main.Rule('rule', Mock(side_effect=OSError('Denied')), None)]
assert main.get_matched_rule(main.Command('ls', '', ''), assert main.get_matched_rule(main.Command('ls', '', ''),
rules, None) is None rules, None) is None
assert main.get_matched_rule(main.Command('cd ..', '', ''), assert main.get_matched_rule(main.Command('cd ..', '', ''),
rules, None) == rules[0] rules, None) == rules[0]
assert capsys.readouterr()[1].split('\n')[0]\
== '[WARN] rule: Traceback (most recent call last):'
def test_run_rule(capsys): def test_run_rule(capsys):
with patch('thefuck.main.confirm', return_value=True): with patch('thefuck.main.confirm', return_value=True):
main.run_rule(main.Rule(None, lambda *_: 'new-command'), main.run_rule(main.Rule('', None, lambda *_: 'new-command'),
None, None) None, None)
assert capsys.readouterr() == ('new-command\n', '') assert capsys.readouterr() == ('new-command\n', '')
with patch('thefuck.main.confirm', return_value=False): with patch('thefuck.main.confirm', return_value=False):
main.run_rule(main.Rule(None, lambda *_: 'new-command'), main.run_rule(main.Rule('', None, lambda *_: 'new-command'),
None, None) None, None)
assert capsys.readouterr() == ('', '') assert capsys.readouterr() == ('', '')

View File

@ -5,11 +5,12 @@ from os.path import expanduser
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
import os import os
import sys import sys
from traceback import format_exception
from psutil import Process, TimeoutExpired from psutil import Process, TimeoutExpired
Command = namedtuple('Command', ('script', 'stdout', 'stderr')) Command = namedtuple('Command', ('script', 'stdout', 'stderr'))
Rule = namedtuple('Rule', ('match', 'get_new_command')) Rule = namedtuple('Rule', ('name', 'match', 'get_new_command'))
def setup_user_dir(): def setup_user_dir():
@ -43,7 +44,8 @@ def is_rule_enabled(settings, rule):
def load_rule(rule): def load_rule(rule):
"""Imports rule module and returns it.""" """Imports rule module and returns it."""
rule_module = load_source(rule.name[:-3], str(rule)) rule_module = load_source(rule.name[:-3], str(rule))
return Rule(rule_module.match, rule_module.get_new_command) return Rule(rule.name[:-3], rule_module.match,
rule_module.get_new_command)
def get_rules(user_dir, settings): def get_rules(user_dir, settings):
@ -94,8 +96,12 @@ def get_command(settings, args):
def get_matched_rule(command, rules, settings): def get_matched_rule(command, rules, settings):
"""Returns first matched rule for command.""" """Returns first matched rule for command."""
for rule in rules: for rule in rules:
try:
if rule.match(command, settings): if rule.match(command, settings):
return rule return rule
except Exception:
sys.stderr.write(u'[WARN] {}: {}---------------------\n\n'.format(
rule.name, ''.join(format_exception(*sys.exc_info()))))
def confirm(new_command, settings): def confirm(new_command, settings):