From 7ce4307c87c1e2e4106db2c961e48e249be987be Mon Sep 17 00:00:00 2001 From: nvbn Date: Tue, 1 Mar 2016 01:28:21 +0300 Subject: [PATCH] #402: Don't invoke bash for getting aliases --- tests/shells/test_bash.py | 15 +++++++-------- thefuck/shells/bash.py | 14 +++++--------- thefuck/shells/zsh.py | 2 +- 3 files changed, 13 insertions(+), 18 deletions(-) diff --git a/tests/shells/test_bash.py b/tests/shells/test_bash.py index 56591a5a..1a00e9bd 100644 --- a/tests/shells/test_bash.py +++ b/tests/shells/test_bash.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- +import os import pytest from thefuck.shells import Bash @@ -11,14 +12,12 @@ class TestBash(object): 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 + 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\'') @pytest.mark.parametrize('before, after', [ ('pwd', 'pwd'), diff --git a/thefuck/shells/bash.py b/thefuck/shells/bash.py index 98349376..d6e9b2c8 100644 --- a/thefuck/shells/bash.py +++ b/thefuck/shells/bash.py @@ -1,7 +1,6 @@ -from subprocess import Popen, PIPE import os from ..conf import settings -from ..utils import DEVNULL, memoize, cache +from ..utils import memoize from .generic import Generic @@ -9,7 +8,7 @@ class Bash(Generic): def app_alias(self, fuck): alias = "TF_ALIAS={0}" \ " alias {0}='PYTHONIOENCODING=utf-8" \ - " TF_CMD=$(thefuck $(fc -ln -1)) && " \ + " TF_CMD=$(TF_SHELL_ALIASES=$(alias) thefuck $(fc -ln -1)) && " \ " eval $TF_CMD".format(fuck) if settings.alter_history: @@ -24,13 +23,10 @@ class Bash(Generic): return name, value @memoize - @cache('.bashrc', '.bash_profile') def get_aliases(self): - proc = Popen(['bash', '-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.get('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", diff --git a/thefuck/shells/zsh.py b/thefuck/shells/zsh.py index f4e6c2eb..a8c05877 100644 --- a/thefuck/shells/zsh.py +++ b/thefuck/shells/zsh.py @@ -26,7 +26,7 @@ class Zsh(Generic): @memoize def get_aliases(self): - raw_aliases = os.environ['TF_SHELL_ALIASES'].split('\n') + raw_aliases = os.environ.get('TF_SHELL_ALIASES', '').split('\n') return dict(self._parse_alias(alias) for alias in raw_aliases if alias and '=' in alias)