2015-07-29 15:22:24 +03:00
|
|
|
# -*- encoding: utf-8 -*-
|
|
|
|
|
2015-07-28 22:04:27 +03:00
|
|
|
import pytest
|
|
|
|
from itertools import islice
|
|
|
|
from thefuck import ui
|
2015-09-08 12:52:10 +03:00
|
|
|
from thefuck.types import CorrectedCommand
|
2015-12-03 20:03:27 +08:00
|
|
|
from thefuck import const
|
2015-07-28 22:04:27 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2015-12-03 20:03:27 +08:00
|
|
|
def patch_get_key(monkeypatch):
|
2015-07-28 22:04:27 +03:00
|
|
|
def patch(vals):
|
2015-12-03 20:03:27 +08:00
|
|
|
vals = iter(vals)
|
|
|
|
monkeypatch.setattr('thefuck.ui.get_key', lambda: next(vals))
|
2015-07-28 22:04:27 +03:00
|
|
|
|
|
|
|
return patch
|
|
|
|
|
|
|
|
|
2015-12-03 20:03:27 +08:00
|
|
|
def test_read_actions(patch_get_key):
|
|
|
|
patch_get_key([
|
|
|
|
# Enter:
|
|
|
|
'\n',
|
|
|
|
# Enter:
|
|
|
|
'\r',
|
|
|
|
# Ignored:
|
|
|
|
'x', 'y',
|
|
|
|
# Up:
|
2016-06-25 12:08:27 +02:00
|
|
|
const.KEY_UP, 'k',
|
2015-12-03 20:03:27 +08:00
|
|
|
# Down:
|
2016-06-25 12:08:27 +02:00
|
|
|
const.KEY_DOWN, 'j',
|
2015-12-03 20:03:27 +08:00
|
|
|
# Ctrl+C:
|
2016-06-25 12:08:27 +02:00
|
|
|
const.KEY_CTRL_C, 'q'])
|
2016-10-06 15:16:43 -04:00
|
|
|
assert (list(islice(ui.read_actions(), 8))
|
|
|
|
== [const.ACTION_SELECT, const.ACTION_SELECT,
|
|
|
|
const.ACTION_PREVIOUS, const.ACTION_PREVIOUS,
|
|
|
|
const.ACTION_NEXT, const.ACTION_NEXT,
|
|
|
|
const.ACTION_ABORT, const.ACTION_ABORT])
|
2015-07-28 22:04:27 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_command_selector():
|
2015-09-08 12:52:10 +03:00
|
|
|
selector = ui.CommandSelector(iter([1, 2, 3]))
|
2015-07-28 22:04:27 +03:00
|
|
|
assert selector.value == 1
|
|
|
|
selector.next()
|
|
|
|
assert selector.value == 2
|
|
|
|
selector.next()
|
|
|
|
assert selector.value == 3
|
|
|
|
selector.next()
|
|
|
|
assert selector.value == 1
|
|
|
|
selector.previous()
|
|
|
|
assert selector.value == 3
|
|
|
|
|
|
|
|
|
2015-09-07 12:12:16 +03:00
|
|
|
@pytest.mark.usefixtures('no_colors')
|
2015-07-28 22:04:27 +03:00
|
|
|
class TestSelectCommand(object):
|
|
|
|
@pytest.fixture
|
2015-09-07 12:12:16 +03:00
|
|
|
def commands_with_side_effect(self):
|
2015-09-08 12:52:10 +03:00
|
|
|
return [CorrectedCommand('ls', lambda *_: None, 100),
|
|
|
|
CorrectedCommand('cd', lambda *_: None, 100)]
|
2015-07-28 22:04:27 +03:00
|
|
|
|
|
|
|
@pytest.fixture
|
2015-09-07 12:12:16 +03:00
|
|
|
def commands(self):
|
2015-09-08 12:52:10 +03:00
|
|
|
return [CorrectedCommand('ls', None, 100),
|
|
|
|
CorrectedCommand('cd', None, 100)]
|
2015-07-28 22:04:27 +03:00
|
|
|
|
|
|
|
def test_without_commands(self, capsys):
|
2015-09-08 12:52:10 +03:00
|
|
|
assert ui.select_command(iter([])) is None
|
2015-08-07 14:51:51 -06:00
|
|
|
assert capsys.readouterr() == ('', 'No fucks given\n')
|
2015-07-28 22:04:27 +03:00
|
|
|
|
2015-09-07 12:12:16 +03:00
|
|
|
def test_without_confirmation(self, capsys, commands, settings):
|
|
|
|
settings.require_confirmation = False
|
2015-09-08 12:52:10 +03:00
|
|
|
assert ui.select_command(iter(commands)) == commands[0]
|
2015-07-28 22:04:27 +03:00
|
|
|
assert capsys.readouterr() == ('', 'ls\n')
|
|
|
|
|
2015-09-07 12:12:16 +03:00
|
|
|
def test_without_confirmation_with_side_effects(
|
|
|
|
self, capsys, commands_with_side_effect, settings):
|
|
|
|
settings.require_confirmation = False
|
2016-10-06 15:16:43 -04:00
|
|
|
assert (ui.select_command(iter(commands_with_side_effect))
|
|
|
|
== commands_with_side_effect[0])
|
2015-07-28 22:04:27 +03:00
|
|
|
assert capsys.readouterr() == ('', 'ls (+side effect)\n')
|
|
|
|
|
2015-12-03 20:03:27 +08:00
|
|
|
def test_with_confirmation(self, capsys, patch_get_key, commands):
|
|
|
|
patch_get_key(['\n'])
|
2015-09-08 12:52:10 +03:00
|
|
|
assert ui.select_command(iter(commands)) == commands[0]
|
2015-07-29 15:22:24 +03:00
|
|
|
assert capsys.readouterr() == ('', u'\x1b[1K\rls [enter/↑/↓/ctrl+c]\n')
|
2015-07-28 22:04:27 +03:00
|
|
|
|
2015-12-03 20:03:27 +08:00
|
|
|
def test_with_confirmation_abort(self, capsys, patch_get_key, commands):
|
|
|
|
patch_get_key([const.KEY_CTRL_C])
|
2015-09-08 12:52:10 +03:00
|
|
|
assert ui.select_command(iter(commands)) is None
|
2015-07-29 15:22:24 +03:00
|
|
|
assert capsys.readouterr() == ('', u'\x1b[1K\rls [enter/↑/↓/ctrl+c]\nAborted\n')
|
2015-07-28 22:04:27 +03:00
|
|
|
|
2015-12-03 20:03:27 +08:00
|
|
|
def test_with_confirmation_with_side_effct(self, capsys, patch_get_key,
|
2015-07-28 22:04:27 +03:00
|
|
|
commands_with_side_effect):
|
2015-12-03 20:03:27 +08:00
|
|
|
patch_get_key(['\n'])
|
2016-10-06 15:16:43 -04:00
|
|
|
assert (ui.select_command(iter(commands_with_side_effect))
|
|
|
|
== commands_with_side_effect[0])
|
2015-07-29 15:22:24 +03:00
|
|
|
assert capsys.readouterr() == ('', u'\x1b[1K\rls (+side effect) [enter/↑/↓/ctrl+c]\n')
|
2015-07-28 22:04:27 +03:00
|
|
|
|
2015-12-03 20:03:27 +08:00
|
|
|
def test_with_confirmation_select_second(self, capsys, patch_get_key, commands):
|
|
|
|
patch_get_key([const.KEY_DOWN, '\n'])
|
2015-09-08 12:52:10 +03:00
|
|
|
assert ui.select_command(iter(commands)) == commands[1]
|
2015-07-28 22:04:27 +03:00
|
|
|
assert capsys.readouterr() == (
|
2015-07-29 15:22:24 +03:00
|
|
|
'', u'\x1b[1K\rls [enter/↑/↓/ctrl+c]\x1b[1K\rcd [enter/↑/↓/ctrl+c]\n')
|