1
0
mirror of https://github.com/nvbn/thefuck.git synced 2024-10-06 10:51:11 +01:00
thefuck/tests/test_shells.py

170 lines
5.5 KiB
Python
Raw Normal View History

import pytest
from thefuck import shells
2015-05-04 03:44:16 +01:00
@pytest.fixture
2015-05-07 12:42:52 +01:00
def builtins_open(mocker):
return mocker.patch('six.moves.builtins.open')
2015-05-04 03:44:16 +01:00
@pytest.fixture
2015-05-07 12:42:52 +01:00
def isfile(mocker):
return mocker.patch('os.path.isfile', return_value=True)
2015-05-04 03:44:16 +01:00
class TestGeneric(object):
2015-05-20 14:56:42 +01:00
@pytest.fixture
def shell(self):
return shells.Generic()
2015-05-20 14:56:42 +01:00
def test_from_shell(self, shell):
assert shell.from_shell('pwd') == 'pwd'
2015-05-04 03:44:16 +01:00
2015-05-20 14:56:42 +01: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 03:44:16 +01:00
assert builtins_open.call_count == 0
2015-05-20 14:56:42 +01: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 03:44:16 +01:00
@pytest.mark.usefixtures('isfile')
class TestBash(object):
2015-05-20 14:56:42 +01:00
@pytest.fixture
def shell(self):
return shells.Bash()
@pytest.fixture(autouse=True)
2015-05-07 12:42:52 +01:00
def Popen(self, mocker):
mock = mocker.patch('thefuck.shells.Popen')
mock.return_value.stdout.read.return_value = (
b'alias fuck=\'eval $(thefuck $(fc -ln -1))\'\n'
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'),
('fuck', 'eval $(thefuck $(fc -ln -1))'),
('awk', 'awk'),
('ll', 'ls -alF')])
2015-05-20 14:56:42 +01:00
def test_from_shell(self, before, after, shell):
assert shell.from_shell(before) == after
2015-05-20 14:56:42 +01:00
def test_to_shell(self, shell):
assert shell.to_shell('pwd') == 'pwd'
2015-05-20 14:56:42 +01:00
def test_put_to_history(self, builtins_open, shell):
shell.put_to_history('ls')
2015-05-07 12:42:52 +01:00
builtins_open.return_value.__enter__.return_value. \
2015-05-04 03:44:16 +01:00
write.assert_called_once_with('ls\n')
2015-05-20 14:56:42 +01:00
def test_and_(self, shell):
assert shell.and_('ls', 'cd') == 'ls && cd'
def test_get_aliases(self, shell):
assert shell.get_aliases() == {'fuck': 'eval $(thefuck $(fc -ln -1))',
'l': 'ls -CF',
2015-05-20 14:56:42 +01:00
'la': 'ls -A',
'll': 'ls -alF'}
2015-05-04 03:44:16 +01:00
@pytest.mark.usefixtures('isfile')
class TestFish(object):
2015-05-20 14:56:42 +01:00
@pytest.fixture
def shell(self):
return shells.Fish()
@pytest.fixture(autouse=True)
def Popen(self, mocker):
mock = mocker.patch('thefuck.shells.Popen')
mock.return_value.stdout.read.return_value = (
b'fish_config\nfuck\nfunced\nfuncsave\ngrep\nhistory\nll\nmath')
return mock
@pytest.mark.parametrize('before, after', [
('pwd', 'pwd'),
('fuck', 'fish -ic "fuck"'),
('find', 'find'),
('funced', 'fish -ic "funced"'),
('awk', 'awk'),
('math "2 + 2"', r'fish -ic "math \"2 + 2\""'),
('vim', 'vim'),
('ll', 'fish -ic "ll"')]) # Fish has no aliases but functions
2015-05-20 14:56:42 +01:00
def test_from_shell(self, before, after, shell):
assert shell.from_shell(before) == after
2015-05-20 14:56:42 +01:00
def test_to_shell(self, shell):
assert shell.to_shell('pwd') == 'pwd'
2015-05-20 14:56:42 +01:00
def test_put_to_history(self, builtins_open, mocker, shell):
mocker.patch('thefuck.shells.time',
return_value=1430707243.3517463)
2015-05-20 14:56:42 +01: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 14:56:42 +01:00
def test_and_(self, shell):
assert shell.and_('foo', 'bar') == 'foo; and bar'
def test_get_aliases(self, shell):
assert shell.get_aliases() == {'fish_config': 'fish_config',
'fuck': 'fuck',
'funced': 'funced',
'funcsave': 'funcsave',
'grep': 'grep',
'history': 'history',
'll': 'll',
'math': 'math'}
2015-05-04 03:44:16 +01:00
@pytest.mark.usefixtures('isfile')
class TestZsh(object):
2015-05-20 14:56:42 +01:00
@pytest.fixture
def shell(self):
return shells.Zsh()
@pytest.fixture(autouse=True)
2015-05-07 12:42:52 +01:00
def Popen(self, mocker):
mock = mocker.patch('thefuck.shells.Popen')
mock.return_value.stdout.read.return_value = (
b'fuck=\'eval $(thefuck $(fc -ln -1 | tail -n 1))\'\n'
b'l=\'ls -CF\'\n'
b'la=\'ls -A\'\n'
b'll=\'ls -alF\'')
return mock
@pytest.mark.parametrize('before, after', [
('fuck', 'eval $(thefuck $(fc -ln -1 | tail -n 1))'),
('pwd', 'pwd'),
('ll', 'ls -alF')])
2015-05-20 14:56:42 +01:00
def test_from_shell(self, before, after, shell):
assert shell.from_shell(before) == after
2015-05-20 14:56:42 +01:00
def test_to_shell(self, shell):
assert shell.to_shell('pwd') == 'pwd'
2015-05-04 03:44:16 +01:00
2015-05-20 14:56:42 +01:00
def test_put_to_history(self, builtins_open, mocker, shell):
2015-05-07 12:42:52 +01:00
mocker.patch('thefuck.shells.time',
return_value=1430707243.3517463)
2015-05-20 14:56:42 +01:00
shell.put_to_history('ls')
2015-05-04 03:44:16 +01:00
builtins_open.return_value.__enter__.return_value. \
2015-05-07 12:51:27 +01:00
write.assert_called_once_with(': 1430707243:0;ls\n')
2015-05-20 14:56:42 +01:00
def test_and_(self, shell):
assert shell.and_('ls', 'cd') == 'ls && cd'
def test_get_aliases(self, shell):
assert shell.get_aliases() == {
'fuck': 'eval $(thefuck $(fc -ln -1 | tail -n 1))',
'l': 'ls -CF',
'la': 'ls -A',
'll': 'ls -alF'}