2016-01-23 05:06:22 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2016-03-01 01:28:21 +03:00
|
|
|
import os
|
2016-01-23 05:06:22 +03:00
|
|
|
import pytest
|
|
|
|
from thefuck.shells import Bash
|
|
|
|
|
|
|
|
|
2016-01-29 13:09:40 +03:00
|
|
|
@pytest.mark.usefixtures('isfile', 'no_memoize', 'no_cache')
|
2016-01-23 05:06:22 +03:00
|
|
|
class TestBash(object):
|
|
|
|
@pytest.fixture
|
|
|
|
def shell(self):
|
|
|
|
return Bash()
|
|
|
|
|
2019-05-27 18:24:55 +02:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def Popen(self, mocker):
|
|
|
|
mock = mocker.patch('thefuck.shells.bash.Popen')
|
|
|
|
return mock
|
|
|
|
|
2016-01-23 05:06:22 +03:00
|
|
|
@pytest.fixture(autouse=True)
|
2016-03-01 01:28:21 +03:00
|
|
|
def shell_aliases(self):
|
|
|
|
os.environ['TF_SHELL_ALIASES'] = (
|
|
|
|
'alias fuck=\'eval $(thefuck $(fc -ln -1))\'\n'
|
|
|
|
'alias l=\'ls -CF\'\n'
|
|
|
|
'alias la=\'ls -A\'\n'
|
|
|
|
'alias ll=\'ls -alF\'')
|
2016-01-23 05:06:22 +03:00
|
|
|
|
|
|
|
@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'
|
|
|
|
|
|
|
|
def test_and_(self, shell):
|
|
|
|
assert shell.and_('ls', 'cd') == 'ls && cd'
|
|
|
|
|
2017-03-28 18:09:38 +02:00
|
|
|
def test_or_(self, shell):
|
|
|
|
assert shell.or_('ls', 'cd') == 'ls || cd'
|
|
|
|
|
2016-01-23 05:06:22 +03:00
|
|
|
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):
|
2017-03-28 12:01:09 +02:00
|
|
|
assert 'fuck () {' in shell.app_alias('fuck')
|
|
|
|
assert 'FUCK () {' in shell.app_alias('FUCK')
|
2016-01-23 05:06:22 +03:00
|
|
|
assert 'thefuck' in shell.app_alias('fuck')
|
2017-03-28 12:01:09 +02:00
|
|
|
assert 'PYTHONIOENCODING' in shell.app_alias('fuck')
|
2016-01-23 05:06:22 +03:00
|
|
|
|
2016-03-09 21:58:18 -03:00
|
|
|
def test_app_alias_variables_correctly_set(self, shell):
|
|
|
|
alias = shell.app_alias('fuck')
|
2017-03-28 12:01:09 +02:00
|
|
|
assert "fuck () {" in alias
|
2017-12-17 18:01:40 -02:00
|
|
|
assert 'TF_SHELL=bash' in alias
|
2017-03-28 12:01:09 +02:00
|
|
|
assert "TF_ALIAS=fuck" in alias
|
|
|
|
assert 'PYTHONIOENCODING=utf-8' in alias
|
|
|
|
assert 'TF_SHELL_ALIASES=$(alias)' in alias
|
2016-03-09 21:58:18 -03:00
|
|
|
|
2016-01-23 05:06:22 +03:00
|
|
|
def test_get_history(self, history_lines, shell):
|
|
|
|
history_lines(['ls', 'rm'])
|
|
|
|
assert list(shell.get_history()) == ['ls', 'rm']
|
2016-11-17 14:34:55 -05:00
|
|
|
|
|
|
|
def test_split_command(self, shell):
|
2017-01-11 14:58:50 +01:00
|
|
|
command = 'git log -p'
|
|
|
|
command_parts = ['git', 'log', '-p']
|
2016-11-17 14:34:55 -05:00
|
|
|
assert shell.split_command(command) == command_parts
|
2017-03-13 21:50:13 +01:00
|
|
|
|
|
|
|
def test_how_to_configure(self, shell, config_exists):
|
|
|
|
config_exists.return_value = True
|
|
|
|
assert shell.how_to_configure().can_configure_automatically
|
|
|
|
|
|
|
|
def test_how_to_configure_when_config_not_found(self, shell,
|
|
|
|
config_exists):
|
|
|
|
config_exists.return_value = False
|
|
|
|
assert not shell.how_to_configure().can_configure_automatically
|
2018-10-08 22:32:30 +02:00
|
|
|
|
2019-05-27 18:24:55 +02:00
|
|
|
def test_info(self, shell, Popen):
|
|
|
|
Popen.return_value.stdout.read.side_effect = [b'3.5.9']
|
2018-10-08 22:32:30 +02:00
|
|
|
assert shell.info() == 'Bash 3.5.9'
|
2019-05-27 18:24:55 +02:00
|
|
|
|
|
|
|
def test_get_version_error(self, shell, Popen):
|
|
|
|
Popen.return_value.stdout.read.side_effect = OSError
|
|
|
|
with pytest.raises(OSError):
|
|
|
|
shell._get_version()
|
|
|
|
assert Popen.call_args[0][0] == ['bash', '-c', 'echo $BASH_VERSION']
|