1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-01-19 12:24:29 +00:00
thefuck/tests/test_shells.py

142 lines
4.2 KiB
Python
Raw Normal View History

import pytest
from thefuck import shells
2015-05-04 04:44:16 +02:00
@pytest.fixture
2015-05-07 13:42:52 +02:00
def builtins_open(mocker):
return mocker.patch('six.moves.builtins.open')
2015-05-04 04:44:16 +02:00
@pytest.fixture
2015-05-07 13:42:52 +02:00
def isfile(mocker):
return mocker.patch('os.path.isfile', return_value=True)
2015-05-04 04:44:16 +02:00
class TestGeneric(object):
2015-05-20 16:56:42 +03:00
@pytest.fixture
def shell(self):
return shells.Generic()
2015-05-20 16:56:42 +03:00
def test_from_shell(self, shell):
assert shell.from_shell('pwd') == 'pwd'
2015-05-04 04:44:16 +02:00
2015-05-20 16:56:42 +03:00
def test_to_shell(self, shell):
assert shell.to_shell('pwd') == 'pwd'
def test_put_to_history(self, builtins_open, shell):
assert shell.put_to_history('ls') is None
2015-05-04 04:44:16 +02:00
assert builtins_open.call_count == 0
2015-05-20 16:56:42 +03:00
def test_and_(self, shell):
assert shell.and_('ls', 'cd') == 'ls && cd'
def test_get_aliases(self, shell):
assert shell.get_aliases() == {}
2015-05-04 04:44:16 +02:00
@pytest.mark.usefixtures('isfile')
class TestBash(object):
2015-05-20 16:56:42 +03:00
@pytest.fixture
def shell(self):
return shells.Bash()
@pytest.fixture(autouse=True)
2015-05-07 13:42:52 +02:00
def Popen(self, mocker):
mock = mocker.patch('thefuck.shells.Popen')
mock.return_value.stdout.read.return_value = (
b'alias l=\'ls -CF\'\n'
b'alias la=\'ls -A\'\n'
b'alias ll=\'ls -alF\'')
return mock
@pytest.mark.parametrize('before, after', [
('pwd', 'pwd'),
('ll', 'ls -alF')])
2015-05-20 16:56:42 +03:00
def test_from_shell(self, before, after, shell):
assert shell.from_shell(before) == after
2015-05-20 16:56:42 +03:00
def test_to_shell(self, shell):
assert shell.to_shell('pwd') == 'pwd'
2015-05-20 16:56:42 +03:00
def test_put_to_history(self, builtins_open, shell):
shell.put_to_history('ls')
2015-05-07 13:42:52 +02:00
builtins_open.return_value.__enter__.return_value. \
2015-05-04 04:44:16 +02:00
write.assert_called_once_with('ls\n')
2015-05-20 16:56:42 +03:00
def test_and_(self, shell):
assert shell.and_('ls', 'cd') == 'ls && cd'
def test_get_aliases(self, shell):
assert shell.get_aliases() == {'l': 'ls -CF',
'la': 'ls -A',
'll': 'ls -alF'}
2015-05-04 04:44:16 +02:00
@pytest.mark.usefixtures('isfile')
class TestFish(object):
2015-05-20 16:56:42 +03:00
@pytest.fixture
def shell(self):
return shells.Fish()
@pytest.mark.parametrize('before, after', [
('pwd', 'pwd'),
('ll', 'll')]) # Fish has no aliases but functions
2015-05-20 16:56:42 +03:00
def test_from_shell(self, before, after, shell):
assert shell.from_shell(before) == after
2015-05-20 16:56:42 +03:00
def test_to_shell(self, shell):
assert shell.to_shell('pwd') == 'pwd'
2015-05-20 16:56:42 +03:00
def test_put_to_history(self, builtins_open, mocker, shell):
mocker.patch('thefuck.shells.time',
return_value=1430707243.3517463)
2015-05-20 16:56:42 +03:00
shell.put_to_history('ls')
builtins_open.return_value.__enter__.return_value. \
write.assert_called_once_with('- cmd: ls\n when: 1430707243\n')
2015-05-20 16:56:42 +03:00
def test_and_(self, shell):
assert shell.and_('foo', 'bar') == 'foo; and bar'
def test_get_aliases(self, shell):
assert shell.get_aliases() == {}
2015-05-04 04:44:16 +02:00
@pytest.mark.usefixtures('isfile')
class TestZsh(object):
2015-05-20 16:56:42 +03:00
@pytest.fixture
def shell(self):
return shells.Zsh()
@pytest.fixture(autouse=True)
2015-05-07 13:42:52 +02:00
def Popen(self, mocker):
mock = mocker.patch('thefuck.shells.Popen')
mock.return_value.stdout.read.return_value = (
b'l=\'ls -CF\'\n'
b'la=\'ls -A\'\n'
b'll=\'ls -alF\'')
return mock
@pytest.mark.parametrize('before, after', [
('pwd', 'pwd'),
('ll', 'ls -alF')])
2015-05-20 16:56:42 +03:00
def test_from_shell(self, before, after, shell):
assert shell.from_shell(before) == after
2015-05-20 16:56:42 +03:00
def test_to_shell(self, shell):
assert shell.to_shell('pwd') == 'pwd'
2015-05-04 04:44:16 +02:00
2015-05-20 16:56:42 +03:00
def test_put_to_history(self, builtins_open, mocker, shell):
2015-05-07 13:42:52 +02:00
mocker.patch('thefuck.shells.time',
return_value=1430707243.3517463)
2015-05-20 16:56:42 +03:00
shell.put_to_history('ls')
2015-05-04 04:44:16 +02:00
builtins_open.return_value.__enter__.return_value. \
2015-05-07 13:51:27 +02:00
write.assert_called_once_with(': 1430707243:0;ls\n')
2015-05-20 16:56:42 +03:00
def test_and_(self, shell):
assert shell.and_('ls', 'cd') == 'ls && cd'
def test_get_aliases(self, shell):
assert shell.get_aliases() == {'l': 'ls -CF',
'la': 'ls -A',
'll': 'ls -alF'}