mirror of
https://github.com/nvbn/thefuck.git
synced 2025-10-30 06:34:09 +00:00
#N/A: Split shells module
This commit is contained in:
0
tests/shells/__init__.py
Normal file
0
tests/shells/__init__.py
Normal file
21
tests/shells/conftest.py
Normal file
21
tests/shells/conftest.py
Normal file
@@ -0,0 +1,21 @@
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def builtins_open(mocker):
|
||||
return mocker.patch('six.moves.builtins.open')
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def isfile(mocker):
|
||||
return mocker.patch('os.path.isfile', return_value=True)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@pytest.mark.usefixtures('isfile')
|
||||
def history_lines(mocker):
|
||||
def aux(lines):
|
||||
mock = mocker.patch('io.open')
|
||||
mock.return_value.__enter__ \
|
||||
.return_value.readlines.return_value = lines
|
||||
return aux
|
||||
60
tests/shells/test_bash.py
Normal file
60
tests/shells/test_bash.py
Normal file
@@ -0,0 +1,60 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import pytest
|
||||
from thefuck.shells import Bash
|
||||
|
||||
|
||||
@pytest.mark.usefixtures('isfile')
|
||||
class TestBash(object):
|
||||
@pytest.fixture
|
||||
def shell(self):
|
||||
return Bash()
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def Popen(self, mocker):
|
||||
mock = mocker.patch('thefuck.shells.bash.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')])
|
||||
def test_from_shell(self, before, after, shell):
|
||||
assert shell.from_shell(before) == after
|
||||
|
||||
def test_to_shell(self, shell):
|
||||
assert shell.to_shell('pwd') == 'pwd'
|
||||
|
||||
@pytest.mark.parametrize('entry, entry_utf8', [
|
||||
('ls', 'ls\n'),
|
||||
(u'echo café', 'echo café\n')])
|
||||
def test_put_to_history(self, entry, entry_utf8, builtins_open, shell):
|
||||
shell.put_to_history(entry)
|
||||
builtins_open.return_value.__enter__.return_value. \
|
||||
write.assert_called_once_with(entry_utf8)
|
||||
|
||||
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',
|
||||
'la': 'ls -A',
|
||||
'll': 'ls -alF'}
|
||||
|
||||
def test_app_alias(self, shell):
|
||||
assert 'alias fuck' in shell.app_alias('fuck')
|
||||
assert 'alias FUCK' in shell.app_alias('FUCK')
|
||||
assert 'thefuck' in shell.app_alias('fuck')
|
||||
assert 'TF_ALIAS=fuck PYTHONIOENCODING' in shell.app_alias('fuck')
|
||||
assert 'PYTHONIOENCODING=utf-8 thefuck' in shell.app_alias('fuck')
|
||||
|
||||
def test_get_history(self, history_lines, shell):
|
||||
history_lines(['ls', 'rm'])
|
||||
assert list(shell.get_history()) == ['ls', 'rm']
|
||||
86
tests/shells/test_fish.py
Normal file
86
tests/shells/test_fish.py
Normal file
@@ -0,0 +1,86 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import pytest
|
||||
from thefuck.shells import Fish
|
||||
|
||||
|
||||
@pytest.mark.usefixtures('isfile')
|
||||
class TestFish(object):
|
||||
@pytest.fixture
|
||||
def shell(self):
|
||||
return Fish()
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def Popen(self, mocker):
|
||||
mock = mocker.patch('thefuck.shells.fish.Popen')
|
||||
mock.return_value.stdout.read.return_value = (
|
||||
b'cd\nfish_config\nfuck\nfunced\nfuncsave\ngrep\nhistory\nll\nls\n'
|
||||
b'man\nmath\npopd\npushd\nruby')
|
||||
return mock
|
||||
|
||||
@pytest.fixture
|
||||
def environ(self, monkeypatch):
|
||||
data = {'TF_OVERRIDDEN_ALIASES': 'cd, ls, man, open'}
|
||||
monkeypatch.setattr('thefuck.shells.fish.os.environ', data)
|
||||
return data
|
||||
|
||||
@pytest.mark.usefixture('environ')
|
||||
def test_get_overridden_aliases(self, shell, environ):
|
||||
assert shell._get_overridden_aliases() == ['cd', 'ls', 'man', 'open']
|
||||
|
||||
@pytest.mark.parametrize('before, after', [
|
||||
('cd', 'cd'),
|
||||
('pwd', 'pwd'),
|
||||
('fuck', 'fish -ic "fuck"'),
|
||||
('find', 'find'),
|
||||
('funced', 'fish -ic "funced"'),
|
||||
('grep', 'grep'),
|
||||
('awk', 'awk'),
|
||||
('math "2 + 2"', r'fish -ic "math \"2 + 2\""'),
|
||||
('man', 'man'),
|
||||
('open', 'open'),
|
||||
('vim', 'vim'),
|
||||
('ll', 'fish -ic "ll"'),
|
||||
('ls', 'ls')]) # Fish has no aliases but functions
|
||||
def test_from_shell(self, before, after, shell):
|
||||
assert shell.from_shell(before) == after
|
||||
|
||||
def test_to_shell(self, shell):
|
||||
assert shell.to_shell('pwd') == 'pwd'
|
||||
|
||||
@pytest.mark.parametrize('entry, entry_utf8', [
|
||||
('ls', '- cmd: ls\n when: 1430707243\n'),
|
||||
(u'echo café', '- cmd: echo café\n when: 1430707243\n')])
|
||||
def test_put_to_history(self, entry, entry_utf8, builtins_open, mocker, shell):
|
||||
mocker.patch('thefuck.shells.fish.time',
|
||||
return_value=1430707243.3517463)
|
||||
shell.put_to_history(entry)
|
||||
builtins_open.return_value.__enter__.return_value. \
|
||||
write.assert_called_once_with(entry_utf8)
|
||||
|
||||
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',
|
||||
'history': 'history',
|
||||
'll': 'll',
|
||||
'math': 'math',
|
||||
'popd': 'popd',
|
||||
'pushd': 'pushd',
|
||||
'ruby': 'ruby'}
|
||||
|
||||
def test_app_alias(self, shell):
|
||||
assert 'function fuck' in shell.app_alias('fuck')
|
||||
assert 'function FUCK' in shell.app_alias('FUCK')
|
||||
assert 'thefuck' in shell.app_alias('fuck')
|
||||
assert 'TF_ALIAS=fuck PYTHONIOENCODING' in shell.app_alias('fuck')
|
||||
assert 'PYTHONIOENCODING=utf-8 thefuck' in shell.app_alias('fuck')
|
||||
|
||||
def test_get_history(self, history_lines, shell):
|
||||
history_lines(['- cmd: ls', ' when: 1432613911',
|
||||
'- cmd: rm', ' when: 1432613916'])
|
||||
assert list(shell.get_history()) == ['ls', 'rm']
|
||||
44
tests/shells/test_generic.py
Normal file
44
tests/shells/test_generic.py
Normal file
@@ -0,0 +1,44 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import pytest
|
||||
from thefuck.shells import Generic
|
||||
|
||||
|
||||
class TestGeneric(object):
|
||||
@pytest.fixture
|
||||
def shell(self):
|
||||
return Generic()
|
||||
|
||||
def test_from_shell(self, shell):
|
||||
assert shell.from_shell('pwd') == 'pwd'
|
||||
|
||||
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
|
||||
assert shell.put_to_history(u'echo café') is None
|
||||
assert builtins_open.call_count == 0
|
||||
|
||||
def test_and_(self, shell):
|
||||
assert shell.and_('ls', 'cd') == 'ls && cd'
|
||||
|
||||
def test_get_aliases(self, shell):
|
||||
assert shell.get_aliases() == {}
|
||||
|
||||
def test_app_alias(self, shell):
|
||||
assert 'alias fuck' in shell.app_alias('fuck')
|
||||
assert 'alias FUCK' in shell.app_alias('FUCK')
|
||||
assert 'thefuck' in shell.app_alias('fuck')
|
||||
assert 'TF_ALIAS=fuck PYTHONIOENCODING' in shell.app_alias('fuck')
|
||||
assert 'PYTHONIOENCODING=utf-8 thefuck' in shell.app_alias('fuck')
|
||||
|
||||
def test_get_history(self, history_lines, shell):
|
||||
history_lines(['ls', 'rm'])
|
||||
# We don't know what to do in generic shell with history lines,
|
||||
# so just ignore them:
|
||||
assert list(shell.get_history()) == []
|
||||
|
||||
def test_split_command(self, shell):
|
||||
assert shell.split_command('ls') == ['ls']
|
||||
assert shell.split_command(u'echo café') == [u'echo', u'café']
|
||||
62
tests/shells/test_zsh.py
Normal file
62
tests/shells/test_zsh.py
Normal file
@@ -0,0 +1,62 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import pytest
|
||||
from thefuck.shells.zsh import Zsh
|
||||
|
||||
|
||||
@pytest.mark.usefixtures('isfile')
|
||||
class TestZsh(object):
|
||||
@pytest.fixture
|
||||
def shell(self):
|
||||
return Zsh()
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def Popen(self, mocker):
|
||||
mock = mocker.patch('thefuck.shells.zsh.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')])
|
||||
def test_from_shell(self, before, after, shell):
|
||||
assert shell.from_shell(before) == after
|
||||
|
||||
def test_to_shell(self, shell):
|
||||
assert shell.to_shell('pwd') == 'pwd'
|
||||
|
||||
@pytest.mark.parametrize('entry, entry_utf8', [
|
||||
('ls', ': 1430707243:0;ls\n'),
|
||||
(u'echo café', ': 1430707243:0;echo café\n')])
|
||||
def test_put_to_history(self, entry, entry_utf8, builtins_open, mocker, shell):
|
||||
mocker.patch('thefuck.shells.zsh.time',
|
||||
return_value=1430707243.3517463)
|
||||
shell.put_to_history(entry)
|
||||
builtins_open.return_value.__enter__.return_value. \
|
||||
write.assert_called_once_with(entry_utf8)
|
||||
|
||||
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'}
|
||||
|
||||
def test_app_alias(self, shell):
|
||||
assert 'alias fuck' in shell.app_alias('fuck')
|
||||
assert 'alias FUCK' in shell.app_alias('FUCK')
|
||||
assert 'thefuck' in shell.app_alias('fuck')
|
||||
assert 'TF_ALIAS=fuck PYTHONIOENCODING' in shell.app_alias('fuck')
|
||||
assert 'PYTHONIOENCODING=utf-8 thefuck' in shell.app_alias('fuck')
|
||||
|
||||
def test_get_history(self, history_lines, shell):
|
||||
history_lines([': 1432613911:0;ls', ': 1432613916:0;rm'])
|
||||
assert list(shell.get_history()) == ['ls', 'rm']
|
||||
Reference in New Issue
Block a user