diff --git a/tests/test_corrector.py b/tests/test_corrector.py index cfbc3fda..0496e61e 100644 --- a/tests/test_corrector.py +++ b/tests/test_corrector.py @@ -2,8 +2,8 @@ import pytest from pathlib import PosixPath, Path from mock import Mock from thefuck import corrector, conf, types -from tests.utils import Rule, Command -from thefuck.corrector import make_corrected_commands, get_corrected_commands +from tests.utils import Rule, Command, CorrectedCommand +from thefuck.corrector import make_corrected_commands, get_corrected_commands, remove_duplicates def test_load_rule(mocker): @@ -65,14 +65,23 @@ class TestGetCorrectedCommands(object): rule = Rule(get_new_command=lambda x, _: [x.script + '!', x.script + '@'], priority=100) assert list(make_corrected_commands(Command(script='test'), [rule], None)) \ - == [types.CorrectedCommand(script='test!', priority=100, side_effect=None), - types.CorrectedCommand(script='test@', priority=200, side_effect=None)] + == [CorrectedCommand(script='test!', priority=100), + CorrectedCommand(script='test@', priority=200)] def test_with_rule_returns_command(self): rule = Rule(get_new_command=lambda x, _: x.script + '!', priority=100) assert list(make_corrected_commands(Command(script='test'), [rule], None)) \ - == [types.CorrectedCommand(script='test!', priority=100, side_effect=None)] + == [CorrectedCommand(script='test!', priority=100)] + + +def test_remove_duplicates(): + side_effect = lambda *_: None + assert set(remove_duplicates([CorrectedCommand('ls', priority=100), + CorrectedCommand('ls', priority=200), + CorrectedCommand('ls', side_effect, 300)])) \ + == {CorrectedCommand('ls', priority=100), + CorrectedCommand('ls', side_effect, 300)} def test_get_corrected_commands(mocker): @@ -84,5 +93,5 @@ def test_get_corrected_commands(mocker): get_new_command=lambda x, _: [x.script + '@', x.script + ';'], priority=60)] mocker.patch('thefuck.corrector.get_rules', return_value=rules) - assert [cmd.script for cmd in get_corrected_commands(command, None, Mock(debug=False))]\ - == ['test@', 'test!', 'test;'] + assert [cmd.script for cmd in get_corrected_commands(command, None, Mock(debug=False))] \ + == ['test@', 'test!', 'test;'] diff --git a/tests/utils.py b/tests/utils.py index 6efeafa8..23a55b84 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -15,3 +15,7 @@ def Rule(name='', match=lambda *_: True, return types.Rule(name, match, get_new_command, enabled_by_default, side_effect, priority, requires_output) + + +def CorrectedCommand(script='', side_effect=None, priority=DEFAULT_PRIORITY): + return types.CorrectedCommand(script, side_effect, priority) diff --git a/thefuck/corrector.py b/thefuck/corrector.py index d1b4cfc8..41713582 100644 --- a/thefuck/corrector.py +++ b/thefuck/corrector.py @@ -66,6 +66,13 @@ def make_corrected_commands(command, rules, settings): priority=(n + 1) * rule.priority) +def remove_duplicates(corrected_commands): + commands = {(command.script, command.side_effect): command + for command in sorted(corrected_commands, + key=lambda command: -command.priority)} + return commands.values() + + def get_corrected_commands(command, user_dir, settings): rules = get_rules(user_dir, settings) logs.debug( @@ -76,5 +83,5 @@ def get_corrected_commands(command, user_dir, settings): u'Matched rules: {}'.format(', '.join(rule.name for rule in matched)), settings) corrected_commands = make_corrected_commands(command, matched, settings) - return sorted(corrected_commands, + return sorted(remove_duplicates(corrected_commands), key=lambda corrected_command: corrected_command.priority)