mirror of
https://github.com/nvbn/thefuck.git
synced 2025-10-30 06:34:09 +00:00
#102 Add support of rules with side effects
This commit is contained in:
@@ -74,6 +74,15 @@ def test_run_rule(capsys):
|
||||
main.run_rule(Rule(get_new_command=lambda *_: 'new-command'),
|
||||
None, None)
|
||||
assert capsys.readouterr() == ('new-command\n', '')
|
||||
# With side effect:
|
||||
side_effect = Mock()
|
||||
settings = Mock()
|
||||
command = Mock()
|
||||
main.run_rule(Rule(get_new_command=lambda *_: 'new-command',
|
||||
side_effect=side_effect),
|
||||
command, settings)
|
||||
assert capsys.readouterr() == ('new-command\n', '')
|
||||
side_effect.assert_called_once_with(command, settings)
|
||||
with patch('thefuck.main.confirm', return_value=False):
|
||||
main.run_rule(Rule(get_new_command=lambda *_: 'new-command'),
|
||||
None, None)
|
||||
@@ -82,15 +91,25 @@ def test_run_rule(capsys):
|
||||
|
||||
def test_confirm(capsys):
|
||||
# When confirmation not required:
|
||||
assert main.confirm('command', Mock(require_confirmation=False))
|
||||
assert main.confirm('command', None, Mock(require_confirmation=False))
|
||||
assert capsys.readouterr() == ('', 'command\n')
|
||||
# With side effect and without confirmation:
|
||||
assert main.confirm('command', Mock(), Mock(require_confirmation=False))
|
||||
assert capsys.readouterr() == ('', 'command*\n')
|
||||
# When confirmation required and confirmed:
|
||||
with patch('thefuck.main.sys.stdin.read', return_value='\n'):
|
||||
assert main.confirm('command', Mock(require_confirmation=True,
|
||||
no_colors=True))
|
||||
assert main.confirm(
|
||||
'command', None, Mock(require_confirmation=True,
|
||||
no_colors=True))
|
||||
assert capsys.readouterr() == ('', 'command [enter/ctrl+c]')
|
||||
# With side effect:
|
||||
assert main.confirm(
|
||||
'command', Mock(), Mock(require_confirmation=True,
|
||||
no_colors=True))
|
||||
assert capsys.readouterr() == ('', 'command* [enter/ctrl+c]')
|
||||
# When confirmation required and ctrl+c:
|
||||
with patch('thefuck.main.sys.stdin.read', side_effect=KeyboardInterrupt):
|
||||
assert not main.confirm('command', Mock(require_confirmation=True,
|
||||
no_colors=True))
|
||||
assert not main.confirm('command', None,
|
||||
Mock(require_confirmation=True,
|
||||
no_colors=True))
|
||||
assert capsys.readouterr() == ('', 'command [enter/ctrl+c]Aborted\n')
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
from thefuck.types import Rule, RulesNamesList, Settings
|
||||
from thefuck.types import RulesNamesList, Settings
|
||||
from tests.utils import Rule
|
||||
|
||||
|
||||
def test_rules_names_list():
|
||||
assert RulesNamesList(['bash', 'lisp']) == ['bash', 'lisp']
|
||||
assert RulesNamesList(['bash', 'lisp']) == RulesNamesList(['bash', 'lisp'])
|
||||
assert Rule('lisp', None, None, False) in RulesNamesList(['lisp'])
|
||||
assert Rule('bash', None, None, False) not in RulesNamesList(['lisp'])
|
||||
assert Rule('lisp') in RulesNamesList(['lisp'])
|
||||
assert Rule('bash') not in RulesNamesList(['lisp'])
|
||||
|
||||
|
||||
def test_update_settings():
|
||||
|
||||
@@ -7,5 +7,7 @@ def Command(script='', stdout='', stderr=''):
|
||||
|
||||
def Rule(name='', match=lambda *_: True,
|
||||
get_new_command=lambda *_: '',
|
||||
enabled_by_default=True):
|
||||
return types.Rule(name, match, get_new_command, enabled_by_default)
|
||||
enabled_by_default=True,
|
||||
side_effect=None):
|
||||
return types.Rule(name, match, get_new_command,
|
||||
enabled_by_default, side_effect)
|
||||
|
||||
Reference in New Issue
Block a user