From bb5f6bb705a3b217eb682f3357ec6bbb709555c1 Mon Sep 17 00:00:00 2001 From: Pablo Santiago Blum de Aguiar Date: Wed, 9 Mar 2016 21:58:18 -0300 Subject: [PATCH] #301: Set variables within the alias Fix #301 --- tests/shells/test_bash.py | 7 +++++++ tests/shells/test_zsh.py | 7 +++++++ thefuck/shells/bash.py | 8 +++++--- thefuck/shells/fish.py | 1 + thefuck/shells/zsh.py | 7 ++++--- thefuck/types.py | 2 +- 6 files changed, 25 insertions(+), 7 deletions(-) diff --git a/tests/shells/test_bash.py b/tests/shells/test_bash.py index 1a00e9bd..c3738f44 100644 --- a/tests/shells/test_bash.py +++ b/tests/shells/test_bash.py @@ -46,6 +46,13 @@ class TestBash(object): assert 'TF_ALIAS=fuck' in shell.app_alias('fuck') assert 'PYTHONIOENCODING=utf-8' in shell.app_alias('fuck') + def test_app_alias_variables_correctly_set(self, shell): + alias = shell.app_alias('fuck') + assert "alias fuck='TF_CMD=$(TF_ALIAS" in alias + assert '$(TF_ALIAS=fuck PYTHONIOENCODING' in alias + assert 'PYTHONIOENCODING=utf-8 TF_SHELL_ALIASES' in alias + assert 'ALIASES=$(alias) thefuck' in alias + def test_get_history(self, history_lines, shell): history_lines(['ls', 'rm']) assert list(shell.get_history()) == ['ls', 'rm'] diff --git a/tests/shells/test_zsh.py b/tests/shells/test_zsh.py index 450191d1..f44f0517 100644 --- a/tests/shells/test_zsh.py +++ b/tests/shells/test_zsh.py @@ -45,6 +45,13 @@ class TestZsh(object): assert 'thefuck' in shell.app_alias('fuck') assert 'PYTHONIOENCODING' in shell.app_alias('fuck') + def test_app_alias_variables_correctly_set(self, shell): + alias = shell.app_alias('fuck') + assert "alias fuck='TF_CMD=$(TF_ALIAS" in alias + assert '$(TF_ALIAS=fuck PYTHONIOENCODING' in alias + assert 'PYTHONIOENCODING=utf-8 TF_SHELL_ALIASES' in alias + assert 'ALIASES=$(alias) thefuck' in alias + def test_get_history(self, history_lines, shell): history_lines([': 1432613911:0;ls', ': 1432613916:0;rm']) assert list(shell.get_history()) == ['ls', 'rm'] diff --git a/thefuck/shells/bash.py b/thefuck/shells/bash.py index d6e9b2c8..8f4e0e1c 100644 --- a/thefuck/shells/bash.py +++ b/thefuck/shells/bash.py @@ -6,9 +6,11 @@ from .generic import Generic class Bash(Generic): def app_alias(self, fuck): - alias = "TF_ALIAS={0}" \ - " alias {0}='PYTHONIOENCODING=utf-8" \ - " TF_CMD=$(TF_SHELL_ALIASES=$(alias) thefuck $(fc -ln -1)) && " \ + # It is VERY important to have the variables declared WITHIN the alias + alias = "alias {0}='TF_CMD=$(TF_ALIAS={0}" \ + " PYTHONIOENCODING=utf-8" \ + " TF_SHELL_ALIASES=$(alias)" \ + " thefuck $(fc -ln -1)) &&" \ " eval $TF_CMD".format(fuck) if settings.alter_history: diff --git a/thefuck/shells/fish.py b/thefuck/shells/fish.py index fff003bf..bc2b2ec7 100644 --- a/thefuck/shells/fish.py +++ b/thefuck/shells/fish.py @@ -14,6 +14,7 @@ class Fish(Generic): return ['cd', 'grep', 'ls', 'man', 'open'] def app_alias(self, fuck): + # It is VERY important to have the variables declared WITHIN the alias return ('function {0} -d "Correct your previous console command"\n' ' set -l fucked_up_command $history[1]\n' ' env TF_ALIAS={0} PYTHONIOENCODING=utf-8' diff --git a/thefuck/shells/zsh.py b/thefuck/shells/zsh.py index a8c05877..e522d6a3 100644 --- a/thefuck/shells/zsh.py +++ b/thefuck/shells/zsh.py @@ -7,10 +7,11 @@ from .generic import Generic class Zsh(Generic): def app_alias(self, alias_name): - alias = "alias {0}='TF_ALIAS={0}" \ + # It is VERY important to have the variables declared WITHIN the alias + alias = "alias {0}='TF_CMD=$(TF_ALIAS={0}" \ " PYTHONIOENCODING=utf-8" \ - ' TF_SHELL_ALIASES=$(alias)' \ - " TF_CMD=$(thefuck $(fc -ln -1 | tail -n 1)) &&" \ + " TF_SHELL_ALIASES=$(alias)" \ + " thefuck $(fc -ln -1 | tail -n 1)) &&" \ " eval $TF_CMD".format(alias_name) if settings.alter_history: diff --git a/thefuck/types.py b/thefuck/types.py index dcd99b6b..81a7d1bb 100644 --- a/thefuck/types.py +++ b/thefuck/types.py @@ -282,5 +282,5 @@ class CorrectedCommand(object): compatibility_call(self.side_effect, old_cmd, self.script) # This depends on correct setting of PYTHONIOENCODING by the alias: logs.debug(u'PYTHONIOENCODING: {}'.format( - os.environ.get('PYTHONIOENCODING', '>-not-set-<'))) + os.environ.get('PYTHONIOENCODING', '!!not-set!!'))) print(self.script)