From f7f0660114a02fe49578ec5684dd02c81042d175 Mon Sep 17 00:00:00 2001 From: nvbn Date: Tue, 1 Mar 2016 01:21:51 +0300 Subject: [PATCH] #402: Don't invoke zsh for getting aliases --- tests/shells/test_zsh.py | 15 +++++++-------- thefuck/shells/zsh.py | 20 +++++++++----------- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/tests/shells/test_zsh.py b/tests/shells/test_zsh.py index 0ea31f1c..450191d1 100644 --- a/tests/shells/test_zsh.py +++ b/tests/shells/test_zsh.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- +import os import pytest from thefuck.shells.zsh import Zsh @@ -11,14 +12,12 @@ class TestZsh(object): 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 + def shell_aliases(self): + os.environ['TF_SHELL_ALIASES'] = ( + 'fuck=\'eval $(thefuck $(fc -ln -1 | tail -n 1))\'\n' + 'l=\'ls -CF\'\n' + 'la=\'ls -A\'\n' + 'll=\'ls -alF\'') @pytest.mark.parametrize('before, after', [ ('fuck', 'eval $(thefuck $(fc -ln -1 | tail -n 1))'), diff --git a/thefuck/shells/zsh.py b/thefuck/shells/zsh.py index bea5fbe9..f4e6c2eb 100644 --- a/thefuck/shells/zsh.py +++ b/thefuck/shells/zsh.py @@ -1,16 +1,17 @@ -from subprocess import Popen, PIPE from time import time import os from ..conf import settings -from ..utils import DEVNULL, memoize, cache +from ..utils import memoize from .generic import Generic class Zsh(Generic): - def app_alias(self, fuck): - alias = "alias {0}='TF_ALIAS={0} PYTHONIOENCODING=utf-8" \ + def app_alias(self, alias_name): + alias = "alias {0}='TF_ALIAS={0}" \ + " PYTHONIOENCODING=utf-8" \ + ' TF_SHELL_ALIASES=$(alias)' \ " TF_CMD=$(thefuck $(fc -ln -1 | tail -n 1)) &&" \ - " eval $TF_CMD".format(fuck) + " eval $TF_CMD".format(alias_name) if settings.alter_history: return alias + " && print -s $TF_CMD'" @@ -24,13 +25,10 @@ class Zsh(Generic): return name, value @memoize - @cache('.zshrc') def get_aliases(self): - proc = Popen(['zsh', '-ic', 'alias'], stdout=PIPE, stderr=DEVNULL) - return dict( - self._parse_alias(alias) - for alias in proc.stdout.read().decode('utf-8').split('\n') - if alias and '=' in alias) + raw_aliases = os.environ['TF_SHELL_ALIASES'].split('\n') + return dict(self._parse_alias(alias) + for alias in raw_aliases if alias and '=' in alias) def _get_history_file_name(self): return os.environ.get("HISTFILE",