#89 #152 Use shell history

This commit is contained in:
nvbn
2015-05-04 04:44:16 +02:00
parent 72ac9650f9
commit 3f2fe0d275
7 changed files with 94 additions and 114 deletions
-42
View File
@@ -1,42 +0,0 @@
import pytest
from mock import Mock
from thefuck.history import History
class TestHistory(object):
@pytest.fixture(autouse=True)
def process(self, monkeypatch):
Process = Mock()
Process.return_value.parent.return_value.pid = 1
monkeypatch.setattr('thefuck.history.Process', Process)
return Process
@pytest.fixture(autouse=True)
def db(self, monkeypatch):
class DBMock(dict):
def __init__(self):
super(DBMock, self).__init__()
self.sync = Mock()
def __call__(self, *args, **kwargs):
return self
db = DBMock()
monkeypatch.setattr('thefuck.history.shelve.open', db)
return db
def test_set(self, db):
history = History()
history.update(last_command='ls',
last_fixed_command=None)
assert db == {'1-last_command': 'ls',
'1-last_fixed_command': None}
def test_get(self, db):
history = History()
db['1-last_command'] = 'cd ..'
assert history.last_command == 'cd ..'
def test_get_without_value(self):
history = History()
assert history.last_command is None
+10 -25
View File
@@ -56,7 +56,7 @@ class TestGetCommand(object):
monkeypatch.setattr('thefuck.shells.to_shell', lambda x: x)
def test_get_command_calls(self, Popen):
assert main.get_command(Mock(), Mock(),
assert main.get_command(Mock(),
['thefuck', 'apt-get', 'search', 'vim']) \
== Command('apt-get search vim', 'stdout', 'stderr')
Popen.assert_called_once_with('apt-get search vim',
@@ -64,22 +64,14 @@ class TestGetCommand(object):
stdout=PIPE,
stderr=PIPE,
env={'LANG': 'C'})
@pytest.mark.parametrize('history, args, result', [
(Mock(), [''], None),
(Mock(last_command='ls', last_fixed_command='ls -la'),
['thefuck', 'fuck'], 'ls -la'),
(Mock(last_command='ls', last_fixed_command='ls -la'),
['thefuck', 'ls'], 'ls -la'),
(Mock(last_command='ls', last_fixed_command=''),
['thefuck', 'ls'], 'ls'),
(Mock(last_command='ls', last_fixed_command=''),
['thefuck', 'fuck'], 'ls')])
def test_get_command_script(self, history, args, result):
@pytest.mark.parametrize('args, result', [
(['thefuck', 'ls', '-la'], 'ls -la'),
(['thefuck', 'ls'], 'ls')])
def test_get_command_script(self, args, result):
if result:
assert main.get_command(Mock(), history, args).script == result
assert main.get_command(Mock(), args).script == result
else:
assert main.get_command(Mock(), history, args) is None
assert main.get_command(Mock(), args) is None
class TestGetMatchedRule(object):
@@ -109,7 +101,7 @@ class TestRunRule(object):
def test_run_rule(self, capsys):
main.run_rule(Rule(get_new_command=lambda *_: 'new-command'),
Command(), Mock(), None)
Command(), None)
assert capsys.readouterr() == ('new-command\n', '')
def test_run_rule_with_side_effect(self, capsys):
@@ -118,21 +110,14 @@ class TestRunRule(object):
command = Command()
main.run_rule(Rule(get_new_command=lambda *_: 'new-command',
side_effect=side_effect),
command, Mock(), settings)
command, settings)
assert capsys.readouterr() == ('new-command\n', '')
side_effect.assert_called_once_with(command, settings)
def test_hisotry_updated(self):
history = Mock()
main.run_rule(Rule(get_new_command=lambda *_: 'ls -lah'),
Command('ls'), history, None)
history.update.assert_called_once_with(last_command='ls',
last_fixed_command='ls -lah')
def test_when_not_comfirmed(self, capsys, confirm):
confirm.return_value = False
main.run_rule(Rule(get_new_command=lambda *_: 'new-command'),
Command(), Mock(), None)
Command(), None)
assert capsys.readouterr() == ('', '')
+35 -3
View File
@@ -1,16 +1,35 @@
import pytest
from mock import Mock
from mock import Mock, MagicMock
from thefuck import shells
@pytest.fixture
def builtins_open(monkeypatch):
mock = MagicMock()
monkeypatch.setattr('six.moves.builtins.open', mock)
return mock
@pytest.fixture
def isfile(monkeypatch):
mock = Mock(return_value=True)
monkeypatch.setattr('os.path.isfile', mock)
return mock
class TestGeneric(object):
def test_from_shell(self):
assert shells.Generic().from_shell('pwd') == 'pwd'
def test_to_shell(self):
assert shells.Bash().to_shell('pwd') == 'pwd'
assert shells.Generic().to_shell('pwd') == 'pwd'
def test_put_to_history(self, builtins_open):
assert shells.Generic().put_to_history('ls') is None
assert builtins_open.call_count == 0
@pytest.mark.usefixtures('isfile')
class TestBash(object):
@pytest.fixture(autouse=True)
def Popen(self, monkeypatch):
@@ -31,7 +50,13 @@ class TestBash(object):
def test_to_shell(self):
assert shells.Bash().to_shell('pwd') == 'pwd'
def test_put_to_history(self, builtins_open):
shells.Bash().put_to_history('ls')
builtins_open.return_value.__enter__.return_value.\
write.assert_called_once_with('ls\n')
@pytest.mark.usefixtures('isfile')
class TestZsh(object):
@pytest.fixture(autouse=True)
def Popen(self, monkeypatch):
@@ -50,4 +75,11 @@ class TestZsh(object):
assert shells.Zsh().from_shell(before) == after
def test_to_shell(self):
assert shells.Zsh().to_shell('pwd') == 'pwd'
assert shells.Zsh().to_shell('pwd') == 'pwd'
def test_put_to_history(self, builtins_open, monkeypatch):
monkeypatch.setattr('thefuck.shells.time',
lambda: 1430707243.3517463)
shells.Zsh().put_to_history('ls')
builtins_open.return_value.__enter__.return_value. \
write.assert_called_once_with(': 1430707243:0;ls\n')