From b8ce95ad689bfa489a9d4a24ea4f2a011706181f Mon Sep 17 00:00:00 2001 From: nvbn Date: Tue, 8 Sep 2015 15:24:49 +0300 Subject: [PATCH] Minor refactoring --- tests/conftest.py | 5 +++++ tests/functional/test_install.py | 5 +++-- tests/test_readme.py | 14 +++++--------- tests/utils.py | 4 ---- thefuck/exceptions.py | 6 +++++- thefuck/main.py | 11 +++++------ thefuck/ui.py | 8 ++++++-- thefuck/utils.py | 12 +++++++----- 8 files changed, 36 insertions(+), 29 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 2579594e..d66d9beb 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -41,3 +41,8 @@ def functional(request): if request.node.get_marker('functional') \ and not request.config.getoption('enable_functional'): pytest.skip('functional tests are disabled') + + +@pytest.fixture +def source_root(): + return Path(__file__).parent.parent.resolve() diff --git a/tests/functional/test_install.py b/tests/functional/test_install.py index 4b2becb7..9463077d 100644 --- a/tests/functional/test_install.py +++ b/tests/functional/test_install.py @@ -1,5 +1,5 @@ import pytest -from thefuck.main import _get_current_version +from thefuck.utils import get_installation_info envs = ((u'bash', 'thefuck/ubuntu-bash', u''' FROM ubuntu:latest @@ -18,7 +18,8 @@ def test_installation(spawnu, shell, TIMEOUT, tag, dockerfile): proc = spawnu(tag, dockerfile, shell) proc.sendline(u'cat /src/install.sh | sh - && $0') proc.sendline(u'thefuck --version') - assert proc.expect([TIMEOUT, u'thefuck {}'.format(_get_current_version())], + version = get_installation_info().version + assert proc.expect([TIMEOUT, u'thefuck {}'.format(version)], timeout=600) proc.sendline(u'fuck') assert proc.expect([TIMEOUT, u'No fucks given']) diff --git a/tests/test_readme.py b/tests/test_readme.py index 5a5ccab4..1d5fe213 100644 --- a/tests/test_readme.py +++ b/tests/test_readme.py @@ -1,14 +1,10 @@ -from tests.utils import root - - -def test_readme(): - with root.joinpath('README.md').open() as f: +def test_readme(source_root): + with source_root.joinpath('README.md').open() as f: readme = f.read() - bundled = root \ - .joinpath('thefuck') \ - .joinpath('rules') \ - .glob('*.py') + bundled = source_root.joinpath('thefuck') \ + .joinpath('rules') \ + .glob('*.py') for rule in bundled: if rule.stem != '__init__': diff --git a/tests/utils.py b/tests/utils.py index 8d0c1f9b..378187d2 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -1,4 +1,3 @@ -from pathlib import Path from thefuck import types from thefuck.conf import DEFAULT_PRIORITY @@ -24,6 +23,3 @@ class CorrectedCommand(types.CorrectedCommand): def __init__(self, script='', side_effect=None, priority=DEFAULT_PRIORITY): super(CorrectedCommand, self).__init__( script, side_effect, priority) - - -root = Path(__file__).parent.parent.resolve() diff --git a/thefuck/exceptions.py b/thefuck/exceptions.py index 87660a95..be888092 100644 --- a/thefuck/exceptions.py +++ b/thefuck/exceptions.py @@ -1,2 +1,6 @@ class EmptyCommand(Exception): - """Raises when empty command passed to `thefuck`.""" + """Raised when empty command passed to `thefuck`.""" + + +class NoRuleMatched(Exception): + """Raised when no rule matched for some command.""" diff --git a/thefuck/main.py b/thefuck/main.py index 617c4e10..b65b10d5 100644 --- a/thefuck/main.py +++ b/thefuck/main.py @@ -1,17 +1,18 @@ from argparse import ArgumentParser from warnings import warn from pprint import pformat -import pkg_resources import sys import colorama from . import logs, types, shells from .conf import settings from .corrector import get_corrected_commands from .exceptions import EmptyCommand +from .utils import get_installation_info from .ui import select_command def fix_command(): + """Fixes previous command. Used when `thefuck` called without arguments.""" colorama.init() settings.init() with logs.debug_time('Total'): @@ -29,11 +30,8 @@ def fix_command(): selected_command.run(command) -def _get_current_version(): - return pkg_resources.require('thefuck')[0].version - - def print_alias(entry_point=True): + """Prints alias for current shell.""" if entry_point: warn('`thefuck-alias` is deprecated, use `thefuck --alias` instead.') position = 1 @@ -59,9 +57,10 @@ def how_to_configure_alias(): def main(): parser = ArgumentParser(prog='thefuck') + version = get_installation_info().version parser.add_argument('-v', '--version', action='version', - version='%(prog)s {}'.format(_get_current_version())) + version='%(prog)s {}'.format(version)) parser.add_argument('-a', '--alias', action='store_true', help='[custom-alias-name] prints alias for current shell') diff --git a/thefuck/ui.py b/thefuck/ui.py index d04bea8d..90ee34ab 100644 --- a/thefuck/ui.py +++ b/thefuck/ui.py @@ -2,6 +2,7 @@ import sys from .conf import settings +from .exceptions import NoRuleMatched from . import logs try: @@ -54,7 +55,10 @@ class CommandSelector(object): def __init__(self, commands): self._commands_gen = commands - self._commands = [next(self._commands_gen)] + try: + self._commands = [next(self._commands_gen)] + except StopIteration: + raise NoRuleMatched self._realised = False self._index = 0 @@ -86,7 +90,7 @@ def select_command(corrected_commands): """ try: selector = CommandSelector(corrected_commands) - except StopIteration: + except NoRuleMatched: logs.failed('No fucks given') return diff --git a/thefuck/utils.py b/thefuck/utils.py index 0292cb76..06ec9342 100644 --- a/thefuck/utils.py +++ b/thefuck/utils.py @@ -4,7 +4,6 @@ import shelve from warnings import warn from decorator import decorator from contextlib import closing -import tempfile import os import pickle @@ -99,10 +98,9 @@ def get_all_executables(): return fallback tf_alias = thefuck_alias() - tf_entry_points = pkg_resources.require('thefuck')[0]\ - .get_entry_map()\ - .get('console_scripts', {})\ - .keys() + tf_entry_points = get_installation_info().get_entry_map()\ + .get('console_scripts', {})\ + .keys() bins = [exe.name for path in os.environ.get('PATH', '').split(':') for exe in _safe(lambda: list(Path(path).iterdir()), []) @@ -224,3 +222,7 @@ def compatibility_call(fn, *args): .format(fn.__name__, fn.__module__)) args += (settings,) return fn(*args) + + +def get_installation_info(): + return pkg_resources.require('thefuck')[0]