From 4875d75a648845c0f724b1c064c3317f7b01cbc0 Mon Sep 17 00:00:00 2001 From: Vladimir Iakovlev Date: Sun, 3 Sep 2017 10:10:50 +0200 Subject: [PATCH] #682: Ensure that `script` exists --- tests/conftest.py | 2 +- tests/entrypoints/test_alias.py | 30 ++++++++++++++++++++++++++++++ thefuck/entrypoints/alias.py | 20 +++++++++++++------- 3 files changed, 44 insertions(+), 8 deletions(-) create mode 100644 tests/entrypoints/test_alias.py diff --git a/tests/conftest.py b/tests/conftest.py index 229aefdc..b2fc766e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -53,7 +53,7 @@ def source_root(): @pytest.fixture -def set_shell(monkeypatch, request): +def set_shell(monkeypatch): def _set(cls): shell = cls() monkeypatch.setattr('thefuck.shells.shell', shell) diff --git a/tests/entrypoints/test_alias.py b/tests/entrypoints/test_alias.py new file mode 100644 index 00000000..2260c752 --- /dev/null +++ b/tests/entrypoints/test_alias.py @@ -0,0 +1,30 @@ +from mock import Mock +import pytest +from thefuck.entrypoints.alias import _get_alias + + +@pytest.mark.parametrize( + 'py2, enable_experimental_instant_mode, which, is_instant', [ + (False, True, True, True), + (False, False, True, False), + (False, True, False, False), + (True, True, True, False), + (True, True, False, False), + (True, False, True, False)]) +def test_get_alias(monkeypatch, mocker, py2, + enable_experimental_instant_mode, + which, is_instant): + monkeypatch.setattr('six.PY2', py2) + args = Mock( + enable_experimental_instant_mode=enable_experimental_instant_mode, + alias='fuck', ) + mocker.patch('thefuck.entrypoints.alias.which', return_value=which) + shell = Mock(app_alias=lambda _: 'app_alias', + instant_mode_alias=lambda _: 'instant_mode_alias') + monkeypatch.setattr('thefuck.entrypoints.alias.shell', shell) + + alias = _get_alias(args) + if is_instant: + assert alias == 'instant_mode_alias' + else: + assert alias == 'app_alias' diff --git a/thefuck/entrypoints/alias.py b/thefuck/entrypoints/alias.py index a21e1fef..4bcda8b7 100644 --- a/thefuck/entrypoints/alias.py +++ b/thefuck/entrypoints/alias.py @@ -1,20 +1,26 @@ import six from ..logs import warn from ..shells import shell +from ..utils import which -def print_alias(known_args): +def _get_alias(known_args): if six.PY2: warn("The Fuck will drop Python 2 support soon, more details " "https://github.com/nvbn/thefuck/issues/685") + alias = shell.app_alias(known_args.alias) + if known_args.enable_experimental_instant_mode: if six.PY2: - warn("Instant mode not supported with Python 2") - alias = shell.app_alias(known_args.alias) + warn("Instant mode requires Python 3") + elif not which('script'): + warn("Instant mode requires `script` app") else: - alias = shell.instant_mode_alias(known_args.alias) - else: - alias = shell.app_alias(known_args.alias) + return shell.instant_mode_alias(known_args.alias) - print(alias) + return alias + + +def print_alias(known_args): + print(_get_alias(known_args))