1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-02-22 12:58:33 +00:00

#402: Don't invoke zsh for getting aliases

This commit is contained in:
nvbn 2016-03-01 01:21:51 +03:00
parent 46f2351907
commit f7f0660114
2 changed files with 16 additions and 19 deletions

View File

@ -1,5 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import os
import pytest import pytest
from thefuck.shells.zsh import Zsh from thefuck.shells.zsh import Zsh
@ -11,14 +12,12 @@ class TestZsh(object):
return Zsh() return Zsh()
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def Popen(self, mocker): def shell_aliases(self):
mock = mocker.patch('thefuck.shells.zsh.Popen') os.environ['TF_SHELL_ALIASES'] = (
mock.return_value.stdout.read.return_value = ( 'fuck=\'eval $(thefuck $(fc -ln -1 | tail -n 1))\'\n'
b'fuck=\'eval $(thefuck $(fc -ln -1 | tail -n 1))\'\n' 'l=\'ls -CF\'\n'
b'l=\'ls -CF\'\n' 'la=\'ls -A\'\n'
b'la=\'ls -A\'\n' 'll=\'ls -alF\'')
b'll=\'ls -alF\'')
return mock
@pytest.mark.parametrize('before, after', [ @pytest.mark.parametrize('before, after', [
('fuck', 'eval $(thefuck $(fc -ln -1 | tail -n 1))'), ('fuck', 'eval $(thefuck $(fc -ln -1 | tail -n 1))'),

View File

@ -1,16 +1,17 @@
from subprocess import Popen, PIPE
from time import time from time import time
import os import os
from ..conf import settings from ..conf import settings
from ..utils import DEVNULL, memoize, cache from ..utils import memoize
from .generic import Generic from .generic import Generic
class Zsh(Generic): class Zsh(Generic):
def app_alias(self, fuck): def app_alias(self, alias_name):
alias = "alias {0}='TF_ALIAS={0} PYTHONIOENCODING=utf-8" \ alias = "alias {0}='TF_ALIAS={0}" \
" PYTHONIOENCODING=utf-8" \
' TF_SHELL_ALIASES=$(alias)' \
" TF_CMD=$(thefuck $(fc -ln -1 | tail -n 1)) &&" \ " TF_CMD=$(thefuck $(fc -ln -1 | tail -n 1)) &&" \
" eval $TF_CMD".format(fuck) " eval $TF_CMD".format(alias_name)
if settings.alter_history: if settings.alter_history:
return alias + " && print -s $TF_CMD'" return alias + " && print -s $TF_CMD'"
@ -24,13 +25,10 @@ class Zsh(Generic):
return name, value return name, value
@memoize @memoize
@cache('.zshrc')
def get_aliases(self): def get_aliases(self):
proc = Popen(['zsh', '-ic', 'alias'], stdout=PIPE, stderr=DEVNULL) raw_aliases = os.environ['TF_SHELL_ALIASES'].split('\n')
return dict( return dict(self._parse_alias(alias)
self._parse_alias(alias) for alias in raw_aliases if alias and '=' in alias)
for alias in proc.stdout.read().decode('utf-8').split('\n')
if alias and '=' in alias)
def _get_history_file_name(self): def _get_history_file_name(self):
return os.environ.get("HISTFILE", return os.environ.get("HISTFILE",