mirror of
https://github.com/nvbn/thefuck.git
synced 2025-09-19 03:32:31 +01:00
#414: Move system-dependent utils in system
module
This commit is contained in:
@@ -4,37 +4,32 @@ import pytest
|
||||
from itertools import islice
|
||||
from thefuck import ui
|
||||
from thefuck.types import CorrectedCommand
|
||||
from thefuck import const
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def patch_getch(monkeypatch):
|
||||
def patch_get_key(monkeypatch):
|
||||
def patch(vals):
|
||||
def getch():
|
||||
for val in vals:
|
||||
if val == KeyboardInterrupt:
|
||||
raise val
|
||||
else:
|
||||
yield val
|
||||
|
||||
getch_gen = getch()
|
||||
monkeypatch.setattr('thefuck.ui.getch', lambda: next(getch_gen))
|
||||
vals = iter(vals)
|
||||
monkeypatch.setattr('thefuck.ui.get_key', lambda: next(vals))
|
||||
|
||||
return patch
|
||||
|
||||
|
||||
def test_read_actions(patch_getch):
|
||||
patch_getch([ # Enter:
|
||||
'\n',
|
||||
# Enter:
|
||||
'\r',
|
||||
# Ignored:
|
||||
'x', 'y',
|
||||
# Up:
|
||||
'\x1b', '[', 'A',
|
||||
# Down:
|
||||
'\x1b', '[', 'B',
|
||||
# Ctrl+C:
|
||||
KeyboardInterrupt], )
|
||||
def test_read_actions(patch_get_key):
|
||||
patch_get_key([
|
||||
# Enter:
|
||||
'\n',
|
||||
# Enter:
|
||||
'\r',
|
||||
# Ignored:
|
||||
'x', 'y',
|
||||
# Up:
|
||||
const.KEY_UP,
|
||||
# Down:
|
||||
const.KEY_DOWN,
|
||||
# Ctrl+C:
|
||||
const.KEY_CTRL_C])
|
||||
assert list(islice(ui.read_actions(), 5)) \
|
||||
== [ui.SELECT, ui.SELECT, ui.PREVIOUS, ui.NEXT, ui.ABORT]
|
||||
|
||||
@@ -80,25 +75,25 @@ class TestSelectCommand(object):
|
||||
== commands_with_side_effect[0]
|
||||
assert capsys.readouterr() == ('', 'ls (+side effect)\n')
|
||||
|
||||
def test_with_confirmation(self, capsys, patch_getch, commands):
|
||||
patch_getch(['\n'])
|
||||
def test_with_confirmation(self, capsys, patch_get_key, commands):
|
||||
patch_get_key(['\n'])
|
||||
assert ui.select_command(iter(commands)) == commands[0]
|
||||
assert capsys.readouterr() == ('', u'\x1b[1K\rls [enter/↑/↓/ctrl+c]\n')
|
||||
|
||||
def test_with_confirmation_abort(self, capsys, patch_getch, commands):
|
||||
patch_getch([KeyboardInterrupt])
|
||||
def test_with_confirmation_abort(self, capsys, patch_get_key, commands):
|
||||
patch_get_key([const.KEY_CTRL_C])
|
||||
assert ui.select_command(iter(commands)) is None
|
||||
assert capsys.readouterr() == ('', u'\x1b[1K\rls [enter/↑/↓/ctrl+c]\nAborted\n')
|
||||
|
||||
def test_with_confirmation_with_side_effct(self, capsys, patch_getch,
|
||||
def test_with_confirmation_with_side_effct(self, capsys, patch_get_key,
|
||||
commands_with_side_effect):
|
||||
patch_getch(['\n'])
|
||||
assert ui.select_command(iter(commands_with_side_effect))\
|
||||
patch_get_key(['\n'])
|
||||
assert ui.select_command(iter(commands_with_side_effect)) \
|
||||
== commands_with_side_effect[0]
|
||||
assert capsys.readouterr() == ('', u'\x1b[1K\rls (+side effect) [enter/↑/↓/ctrl+c]\n')
|
||||
|
||||
def test_with_confirmation_select_second(self, capsys, patch_getch, commands):
|
||||
patch_getch(['\x1b', '[', 'B', '\n'])
|
||||
def test_with_confirmation_select_second(self, capsys, patch_get_key, commands):
|
||||
patch_get_key([const.KEY_DOWN, '\n'])
|
||||
assert ui.select_command(iter(commands)) == commands[1]
|
||||
assert capsys.readouterr() == (
|
||||
'', u'\x1b[1K\rls [enter/↑/↓/ctrl+c]\x1b[1K\rcd [enter/↑/↓/ctrl+c]\n')
|
||||
|
Reference in New Issue
Block a user