mirror of
https://github.com/nvbn/thefuck.git
synced 2025-02-22 12:58:33 +00:00
Simplify corrector steps
This commit is contained in:
parent
61937e9e8f
commit
21103d1b50
@ -41,40 +41,37 @@ class TestGetRules(object):
|
|||||||
rules)
|
rules)
|
||||||
|
|
||||||
|
|
||||||
class TestGetMatchedRules(object):
|
class TestIsRuleMatch(object):
|
||||||
def test_no_match(self):
|
def test_no_match(self, settings):
|
||||||
assert list(corrector.get_matched_rules(
|
assert not corrector.is_rule_match(
|
||||||
Command('ls'), [Rule('', lambda *_: False)],
|
Command('ls'), Rule('', lambda *_: False), settings)
|
||||||
Mock(no_colors=True))) == []
|
|
||||||
|
|
||||||
def test_match(self):
|
def test_match(self, settings):
|
||||||
rule = Rule('', lambda x, _: x.script == 'cd ..')
|
rule = Rule('', lambda x, _: x.script == 'cd ..')
|
||||||
assert list(corrector.get_matched_rules(
|
assert corrector.is_rule_match(Command('cd ..'), rule, settings)
|
||||||
Command('cd ..'), [rule], Mock(no_colors=True))) == [rule]
|
|
||||||
|
|
||||||
def test_when_rule_failed(self, capsys):
|
def test_when_rule_failed(self, capsys, settings):
|
||||||
all(corrector.get_matched_rules(
|
rule = Rule('test', Mock(side_effect=OSError('Denied')),
|
||||||
Command('ls'), [Rule('test', Mock(side_effect=OSError('Denied')),
|
requires_output=False)
|
||||||
requires_output=False)],
|
assert not corrector.is_rule_match(
|
||||||
Mock(no_colors=True, debug=False)))
|
Command('ls'), rule, settings)
|
||||||
assert capsys.readouterr()[1].split('\n')[0] == '[WARN] Rule test:'
|
assert capsys.readouterr()[1].split('\n')[0] == '[WARN] Rule test:'
|
||||||
|
|
||||||
|
|
||||||
class TestGetCorrectedCommands(object):
|
class TestMakeCorrectedCommands(object):
|
||||||
def test_with_rule_returns_list(self):
|
def test_with_rule_returns_list(self):
|
||||||
rule = Rule(get_new_command=lambda x, _: [x.script + '!', x.script + '@'],
|
rule = Rule(get_new_command=lambda x, _: [x.script + '!', x.script + '@'],
|
||||||
priority=100)
|
priority=100)
|
||||||
assert list(make_corrected_commands(Command(script='test'), [rule], None)) \
|
assert list(make_corrected_commands(Command(script='test'), rule, None)) \
|
||||||
== [CorrectedCommand(script='test!', priority=100),
|
== [CorrectedCommand(script='test!', priority=100),
|
||||||
CorrectedCommand(script='test@', priority=200)]
|
CorrectedCommand(script='test@', priority=200)]
|
||||||
|
|
||||||
def test_with_rule_returns_command(self):
|
def test_with_rule_returns_command(self):
|
||||||
rule = Rule(get_new_command=lambda x, _: x.script + '!',
|
rule = Rule(get_new_command=lambda x, _: x.script + '!',
|
||||||
priority=100)
|
priority=100)
|
||||||
assert list(make_corrected_commands(Command(script='test'), [rule], None)) \
|
assert list(make_corrected_commands(Command(script='test'), rule, None)) \
|
||||||
== [CorrectedCommand(script='test!', priority=100)]
|
== [CorrectedCommand(script='test!', priority=100)]
|
||||||
|
|
||||||
|
|
||||||
def test_get_corrected_commands(mocker):
|
def test_get_corrected_commands(mocker):
|
||||||
command = Command('test', 'test', 'test')
|
command = Command('test', 'test', 'test')
|
||||||
rules = [Rule(match=lambda *_: False),
|
rules = [Rule(match=lambda *_: False),
|
||||||
|
@ -36,25 +36,23 @@ def get_rules(user_dir, settings):
|
|||||||
key=lambda rule: rule.priority)
|
key=lambda rule: rule.priority)
|
||||||
|
|
||||||
|
|
||||||
def get_matched_rules(command, rules, settings):
|
def is_rule_match(command, rule, settings):
|
||||||
"""Returns first matched rule for command."""
|
"""Returns first matched rule for command."""
|
||||||
script_only = command.stdout is None and command.stderr is None
|
script_only = command.stdout is None and command.stderr is None
|
||||||
|
|
||||||
for rule in rules:
|
|
||||||
if script_only and rule.requires_output:
|
if script_only and rule.requires_output:
|
||||||
continue
|
return False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with logs.debug_time(u'Trying rule: {};'.format(rule.name),
|
with logs.debug_time(u'Trying rule: {};'.format(rule.name),
|
||||||
settings):
|
settings):
|
||||||
if rule.match(command, settings):
|
if rule.match(command, settings):
|
||||||
yield rule
|
return True
|
||||||
except Exception:
|
except Exception:
|
||||||
logs.rule_failed(rule, sys.exc_info(), settings)
|
logs.rule_failed(rule, sys.exc_info(), settings)
|
||||||
|
|
||||||
|
|
||||||
def make_corrected_commands(command, rules, settings):
|
def make_corrected_commands(command, rule, settings):
|
||||||
for rule in rules:
|
|
||||||
new_commands = rule.get_new_command(command, settings)
|
new_commands = rule.get_new_command(command, settings)
|
||||||
if not isinstance(new_commands, list):
|
if not isinstance(new_commands, list):
|
||||||
new_commands = [new_commands]
|
new_commands = [new_commands]
|
||||||
@ -65,7 +63,8 @@ def make_corrected_commands(command, rules, settings):
|
|||||||
|
|
||||||
|
|
||||||
def get_corrected_commands(command, user_dir, settings):
|
def get_corrected_commands(command, user_dir, settings):
|
||||||
rules = get_rules(user_dir, settings)
|
corrected_commands = (
|
||||||
matched = get_matched_rules(command, rules, settings)
|
corrected for rule in get_rules(user_dir, settings)
|
||||||
corrected_commands = make_corrected_commands(command, matched, settings)
|
if is_rule_match(command, rule, settings)
|
||||||
|
for corrected in make_corrected_commands(command, rule, settings))
|
||||||
return types.SortedCorrectedCommandsSequence(corrected_commands, settings)
|
return types.SortedCorrectedCommandsSequence(corrected_commands, settings)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user