From 2117659c403cedf285c44e7ea6c31b7e8fc71399 Mon Sep 17 00:00:00 2001 From: Pablo Santiago Blum de Aguiar Date: Sat, 25 Jul 2015 20:59:18 -0300 Subject: [PATCH] Add `tsuru_login` rule --- README.md | 1 + tests/rules/test_tsuru_login.py | 37 +++++++++++++++++++++++++++++++++ thefuck/rules/tsuru_login.py | 11 ++++++++++ 3 files changed, 49 insertions(+) create mode 100644 tests/rules/test_tsuru_login.py create mode 100644 thefuck/rules/tsuru_login.py diff --git a/README.md b/README.md index 38f08a26..fac0b1d7 100644 --- a/README.md +++ b/README.md @@ -185,6 +185,7 @@ using the matched rule and runs it. Rules enabled by default are as follows: * `switch_layout` – switches command from your local layout to en; * `systemctl` – correctly orders parameters of confusing `systemctl`; * `test.py` – runs `py.test` instead of `test.py`; +* `tsuru_login.py` – runs `tsuru login` if not authenticated or session expired; * `tmux` – fixes `tmux` commands; * `whois` – fixes `whois` command. diff --git a/tests/rules/test_tsuru_login.py b/tests/rules/test_tsuru_login.py new file mode 100644 index 00000000..a1a7aa14 --- /dev/null +++ b/tests/rules/test_tsuru_login.py @@ -0,0 +1,37 @@ +import pytest +from thefuck.rules.tsuru_login import match, get_new_command +from tests.utils import Command + + +error_msg = ( + "Error: you're not authenticated or your session has expired.", + ("You're not authenticated or your session has expired. " + "Please use \"login\" command for authentication."), +) + + +@pytest.mark.parametrize('command', [ + Command(script='tsuru app-shell', stderr=error_msg[0]), + Command(script='tsuru app-log -f', stderr=error_msg[1]), +]) +def test_match(command): + assert match(command, {}) + + +@pytest.mark.parametrize('command', [ + Command(script='tsuru'), + Command(script='tsuru app-restart', stderr=('Error: unauthorized')), + Command(script='tsuru app-log -f', stderr=('Error: unparseable data')), +]) +def test_not_match(command): + assert not match(command, {}) + + +@pytest.mark.parametrize('command, new_command', [ + (Command('tsuru app-shell', stderr=error_msg[0]), + 'tsuru login && tsuru app-shell'), + (Command('tsuru app-log -f', stderr=error_msg[1]), + 'tsuru login && tsuru app-log -f'), +]) +def test_get_new_command(command, new_command): + assert get_new_command(command, {}) == new_command diff --git a/thefuck/rules/tsuru_login.py b/thefuck/rules/tsuru_login.py new file mode 100644 index 00000000..b71803fd --- /dev/null +++ b/thefuck/rules/tsuru_login.py @@ -0,0 +1,11 @@ +from thefuck import shells + + +def match(command, settings): + return (command.script.startswith('tsuru') + and 'not authenticated' in command.stderr + and 'session has expired' in command.stderr) + + +def get_new_command(command, settings): + return shells.and_('tsuru login', command.script)