1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-02-20 20:09:07 +00:00

#402: Don't invoke bash for getting aliases

This commit is contained in:
nvbn 2016-03-01 01:28:21 +03:00
parent f7f0660114
commit 7ce4307c87
3 changed files with 13 additions and 18 deletions

View File

@ -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'),

View File

@ -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",

View File

@ -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)