mirror of
https://github.com/nvbn/thefuck.git
synced 2025-02-21 12:28:41 +00:00
Merge branch 'issue-221-tf-alias' of https://github.com/scorphus/thefuck into scorphus-issue-221-tf-alias
This commit is contained in:
commit
eb537bef81
@ -1,19 +1,36 @@
|
|||||||
from mock import patch, Mock
|
from mock import patch, Mock
|
||||||
from thefuck.rules.no_command import match, get_new_command
|
from thefuck.rules.no_command import match, get_new_command, _get_all_callables
|
||||||
|
|
||||||
|
|
||||||
def test_match():
|
@patch('thefuck.rules.no_command._safe', return_value=[])
|
||||||
with patch('thefuck.rules.no_command._get_all_callables',
|
@patch('thefuck.rules.no_command.get_aliases',
|
||||||
return_value=['vim', 'apt-get']):
|
return_value=['vim', 'apt-get', 'fsck', 'fuck'])
|
||||||
assert match(Mock(stderr='vom: not found', script='vom file.py'), None)
|
def test_get_all_callables(*args):
|
||||||
assert not match(Mock(stderr='qweqwe: not found', script='qweqwe'), None)
|
all_callables = _get_all_callables()
|
||||||
assert not match(Mock(stderr='some text', script='vom file.py'), None)
|
assert 'vim' in all_callables
|
||||||
|
assert 'fsck' in all_callables
|
||||||
|
assert 'fuck' not in all_callables
|
||||||
|
|
||||||
|
|
||||||
def test_get_new_command():
|
@patch('thefuck.rules.no_command._safe', return_value=[])
|
||||||
with patch('thefuck.rules.no_command._get_all_callables',
|
@patch('thefuck.rules.no_command.get_aliases',
|
||||||
return_value=['vim', 'apt-get']):
|
return_value=['vim', 'apt-get', 'fsck', 'fuck'])
|
||||||
assert get_new_command(
|
def test_match(*args):
|
||||||
Mock(stderr='vom: not found',
|
assert match(Mock(stderr='vom: not found', script='vom file.py'), None)
|
||||||
script='vom file.py'),
|
assert match(Mock(stderr='fucck: not found', script='fucck'), None)
|
||||||
None) == 'vim file.py'
|
assert not match(Mock(stderr='qweqwe: not found', script='qweqwe'), None)
|
||||||
|
assert not match(Mock(stderr='some text', script='vom file.py'), None)
|
||||||
|
|
||||||
|
|
||||||
|
@patch('thefuck.rules.no_command._safe', return_value=[])
|
||||||
|
@patch('thefuck.rules.no_command.get_aliases',
|
||||||
|
return_value=['vim', 'apt-get', 'fsck', 'fuck'])
|
||||||
|
def test_get_new_command(*args):
|
||||||
|
assert get_new_command(
|
||||||
|
Mock(stderr='vom: not found',
|
||||||
|
script='vom file.py'),
|
||||||
|
None) == 'vim file.py'
|
||||||
|
assert get_new_command(
|
||||||
|
Mock(stderr='fucck: not found',
|
||||||
|
script='fucck'),
|
||||||
|
None) == 'fsck'
|
||||||
|
@ -33,6 +33,11 @@ class TestGeneric(object):
|
|||||||
def test_get_aliases(self, shell):
|
def test_get_aliases(self, shell):
|
||||||
assert shell.get_aliases() == {}
|
assert shell.get_aliases() == {}
|
||||||
|
|
||||||
|
def test_app_alias(self, shell):
|
||||||
|
assert 'alias fuck' in shell.app_alias()
|
||||||
|
assert 'thefuck' in shell.app_alias()
|
||||||
|
assert 'TF_ALIAS' in shell.app_alias()
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.usefixtures('isfile')
|
@pytest.mark.usefixtures('isfile')
|
||||||
class TestBash(object):
|
class TestBash(object):
|
||||||
@ -44,6 +49,7 @@ class TestBash(object):
|
|||||||
def Popen(self, mocker):
|
def Popen(self, mocker):
|
||||||
mock = mocker.patch('thefuck.shells.Popen')
|
mock = mocker.patch('thefuck.shells.Popen')
|
||||||
mock.return_value.stdout.read.return_value = (
|
mock.return_value.stdout.read.return_value = (
|
||||||
|
b'alias fuck=\'eval $(thefuck $(fc -ln -1))\'\n'
|
||||||
b'alias l=\'ls -CF\'\n'
|
b'alias l=\'ls -CF\'\n'
|
||||||
b'alias la=\'ls -A\'\n'
|
b'alias la=\'ls -A\'\n'
|
||||||
b'alias ll=\'ls -alF\'')
|
b'alias ll=\'ls -alF\'')
|
||||||
@ -51,6 +57,8 @@ class TestBash(object):
|
|||||||
|
|
||||||
@pytest.mark.parametrize('before, after', [
|
@pytest.mark.parametrize('before, after', [
|
||||||
('pwd', 'pwd'),
|
('pwd', 'pwd'),
|
||||||
|
('fuck', 'eval $(thefuck $(fc -ln -1))'),
|
||||||
|
('awk', 'awk'),
|
||||||
('ll', 'ls -alF')])
|
('ll', 'ls -alF')])
|
||||||
def test_from_shell(self, before, after, shell):
|
def test_from_shell(self, before, after, shell):
|
||||||
assert shell.from_shell(before) == after
|
assert shell.from_shell(before) == after
|
||||||
@ -67,10 +75,16 @@ class TestBash(object):
|
|||||||
assert shell.and_('ls', 'cd') == 'ls && cd'
|
assert shell.and_('ls', 'cd') == 'ls && cd'
|
||||||
|
|
||||||
def test_get_aliases(self, shell):
|
def test_get_aliases(self, shell):
|
||||||
assert shell.get_aliases() == {'l': 'ls -CF',
|
assert shell.get_aliases() == {'fuck': 'eval $(thefuck $(fc -ln -1))',
|
||||||
|
'l': 'ls -CF',
|
||||||
'la': 'ls -A',
|
'la': 'ls -A',
|
||||||
'll': 'ls -alF'}
|
'll': 'ls -alF'}
|
||||||
|
|
||||||
|
def test_app_alias(self, shell):
|
||||||
|
assert 'alias fuck' in shell.app_alias()
|
||||||
|
assert 'thefuck' in shell.app_alias()
|
||||||
|
assert 'TF_ALIAS' in shell.app_alias()
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.usefixtures('isfile')
|
@pytest.mark.usefixtures('isfile')
|
||||||
class TestFish(object):
|
class TestFish(object):
|
||||||
@ -120,6 +134,11 @@ class TestFish(object):
|
|||||||
'll': 'll',
|
'll': 'll',
|
||||||
'math': 'math'}
|
'math': 'math'}
|
||||||
|
|
||||||
|
def test_app_alias(self, shell):
|
||||||
|
assert 'function fuck' in shell.app_alias()
|
||||||
|
assert 'thefuck' in shell.app_alias()
|
||||||
|
assert 'TF_ALIAS' in shell.app_alias()
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.usefixtures('isfile')
|
@pytest.mark.usefixtures('isfile')
|
||||||
class TestZsh(object):
|
class TestZsh(object):
|
||||||
@ -131,12 +150,14 @@ class TestZsh(object):
|
|||||||
def Popen(self, mocker):
|
def Popen(self, mocker):
|
||||||
mock = mocker.patch('thefuck.shells.Popen')
|
mock = mocker.patch('thefuck.shells.Popen')
|
||||||
mock.return_value.stdout.read.return_value = (
|
mock.return_value.stdout.read.return_value = (
|
||||||
|
b'fuck=\'eval $(thefuck $(fc -ln -1 | tail -n 1))\'\n'
|
||||||
b'l=\'ls -CF\'\n'
|
b'l=\'ls -CF\'\n'
|
||||||
b'la=\'ls -A\'\n'
|
b'la=\'ls -A\'\n'
|
||||||
b'll=\'ls -alF\'')
|
b'll=\'ls -alF\'')
|
||||||
return mock
|
return mock
|
||||||
|
|
||||||
@pytest.mark.parametrize('before, after', [
|
@pytest.mark.parametrize('before, after', [
|
||||||
|
('fuck', 'eval $(thefuck $(fc -ln -1 | tail -n 1))'),
|
||||||
('pwd', 'pwd'),
|
('pwd', 'pwd'),
|
||||||
('ll', 'ls -alF')])
|
('ll', 'ls -alF')])
|
||||||
def test_from_shell(self, before, after, shell):
|
def test_from_shell(self, before, after, shell):
|
||||||
@ -156,6 +177,13 @@ class TestZsh(object):
|
|||||||
assert shell.and_('ls', 'cd') == 'ls && cd'
|
assert shell.and_('ls', 'cd') == 'ls && cd'
|
||||||
|
|
||||||
def test_get_aliases(self, shell):
|
def test_get_aliases(self, shell):
|
||||||
assert shell.get_aliases() == {'l': 'ls -CF',
|
assert shell.get_aliases() == {
|
||||||
'la': 'ls -A',
|
'fuck': 'eval $(thefuck $(fc -ln -1 | tail -n 1))',
|
||||||
'll': 'ls -alF'}
|
'l': 'ls -CF',
|
||||||
|
'la': 'ls -A',
|
||||||
|
'll': 'ls -alF'}
|
||||||
|
|
||||||
|
def test_app_alias(self, shell):
|
||||||
|
assert 'alias fuck' in shell.app_alias()
|
||||||
|
assert 'thefuck' in shell.app_alias()
|
||||||
|
assert 'TF_ALIAS' in shell.app_alias()
|
||||||
|
@ -2,7 +2,7 @@ from difflib import get_close_matches
|
|||||||
import os
|
import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from thefuck.utils import sudo_support
|
from thefuck.utils import sudo_support
|
||||||
from thefuck.shells import get_aliases
|
from thefuck.shells import thefuck_alias, get_aliases
|
||||||
|
|
||||||
|
|
||||||
def _safe(fn, fallback):
|
def _safe(fn, fallback):
|
||||||
@ -13,10 +13,12 @@ def _safe(fn, fallback):
|
|||||||
|
|
||||||
|
|
||||||
def _get_all_callables():
|
def _get_all_callables():
|
||||||
|
tf_alias = thefuck_alias()
|
||||||
return [exe.name
|
return [exe.name
|
||||||
for path in os.environ.get('PATH', '').split(':')
|
for path in os.environ.get('PATH', '').split(':')
|
||||||
for exe in _safe(lambda: list(Path(path).iterdir()), [])
|
for exe in _safe(lambda: list(Path(path).iterdir()), [])
|
||||||
if not _safe(exe.is_dir, True)] + get_aliases()
|
if not _safe(exe.is_dir, True)] + [
|
||||||
|
alias for alias in get_aliases() if alias != tf_alias]
|
||||||
|
|
||||||
|
|
||||||
@sudo_support
|
@sudo_support
|
||||||
|
@ -33,7 +33,7 @@ class Generic(object):
|
|||||||
return command_script
|
return command_script
|
||||||
|
|
||||||
def app_alias(self):
|
def app_alias(self):
|
||||||
return "\nalias fuck='eval $(thefuck $(fc -ln -1))'\n"
|
return "\nalias fuck='TF_ALIAS=fuck eval $(thefuck $(fc -ln -1))'\n"
|
||||||
|
|
||||||
def _get_history_file_name(self):
|
def _get_history_file_name(self):
|
||||||
return ''
|
return ''
|
||||||
@ -54,7 +54,7 @@ class Generic(object):
|
|||||||
|
|
||||||
class Bash(Generic):
|
class Bash(Generic):
|
||||||
def app_alias(self):
|
def app_alias(self):
|
||||||
return "\nalias fuck='eval $(thefuck $(fc -ln -1)); history -r'\n"
|
return "\nalias fuck='TF_ALIAS=fuck eval $(thefuck $(fc -ln -1)); history -r'\n"
|
||||||
|
|
||||||
def _parse_alias(self, alias):
|
def _parse_alias(self, alias):
|
||||||
name, value = alias.replace('alias ', '', 1).split('=', 1)
|
name, value = alias.replace('alias ', '', 1).split('=', 1)
|
||||||
@ -83,6 +83,7 @@ class Fish(Generic):
|
|||||||
def app_alias(self):
|
def app_alias(self):
|
||||||
return ("function fuck -d 'Correct your previous console command'\n"
|
return ("function fuck -d 'Correct your previous console command'\n"
|
||||||
" set -l exit_code $status\n"
|
" set -l exit_code $status\n"
|
||||||
|
" set -l TF_ALIAS fuck\n"
|
||||||
" set -l eval_script"
|
" set -l eval_script"
|
||||||
" (mktemp 2>/dev/null ; or mktemp -t 'thefuck')\n"
|
" (mktemp 2>/dev/null ; or mktemp -t 'thefuck')\n"
|
||||||
" set -l fucked_up_commandd $history[1]\n"
|
" set -l fucked_up_commandd $history[1]\n"
|
||||||
@ -125,7 +126,7 @@ class Fish(Generic):
|
|||||||
|
|
||||||
class Zsh(Generic):
|
class Zsh(Generic):
|
||||||
def app_alias(self):
|
def app_alias(self):
|
||||||
return "\nalias fuck='eval $(thefuck $(fc -ln -1 | tail -n 1)); fc -R'\n"
|
return "\nalias fuck='TF_ALIAS=fuck eval $(thefuck $(fc -ln -1 | tail -n 1)); fc -R'\n"
|
||||||
|
|
||||||
def _parse_alias(self, alias):
|
def _parse_alias(self, alias):
|
||||||
name, value = alias.split('=', 1)
|
name, value = alias.split('=', 1)
|
||||||
@ -152,7 +153,7 @@ class Zsh(Generic):
|
|||||||
|
|
||||||
class Tcsh(Generic):
|
class Tcsh(Generic):
|
||||||
def app_alias(self):
|
def app_alias(self):
|
||||||
return "\nalias fuck 'set fucked_cmd=`history -h 2 | head -n 1` && eval `thefuck ${fucked_cmd}`'\n"
|
return "\nalias fuck 'setenv TF_ALIAS fuck && set fucked_cmd=`history -h 2 | head -n 1` && eval `thefuck ${fucked_cmd}`'\n"
|
||||||
|
|
||||||
def _parse_alias(self, alias):
|
def _parse_alias(self, alias):
|
||||||
name, value = alias.split("\t", 1)
|
name, value = alias.split("\t", 1)
|
||||||
@ -203,6 +204,10 @@ def app_alias():
|
|||||||
print(_get_shell().app_alias())
|
print(_get_shell().app_alias())
|
||||||
|
|
||||||
|
|
||||||
|
def thefuck_alias():
|
||||||
|
return os.environ.get('TF_ALIAS', 'fuck')
|
||||||
|
|
||||||
|
|
||||||
def put_to_history(command):
|
def put_to_history(command):
|
||||||
return _get_shell().put_to_history(command)
|
return _get_shell().put_to_history(command)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user