Skip to content

Commit

Permalink
Minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
nvbn committed Sep 8, 2015
1 parent 1173f9f commit b8ce95a
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 29 deletions.
5 changes: 5 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
5 changes: 3 additions & 2 deletions tests/functional/test_install.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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'])
14 changes: 5 additions & 9 deletions tests/test_readme.py
Original file line number Diff line number Diff line change
@@ -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__':
Expand Down
4 changes: 0 additions & 4 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from pathlib import Path
from thefuck import types
from thefuck.conf import DEFAULT_PRIORITY

Expand All @@ -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()
6 changes: 5 additions & 1 deletion thefuck/exceptions.py
Original file line number Diff line number Diff line change
@@ -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."""
11 changes: 5 additions & 6 deletions thefuck/main.py
Original file line number Diff line number Diff line change
@@ -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'):
Expand All @@ -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
Expand All @@ -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')
Expand Down
8 changes: 6 additions & 2 deletions thefuck/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import sys
from .conf import settings
from .exceptions import NoRuleMatched
from . import logs

try:
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -86,7 +90,7 @@ def select_command(corrected_commands):
"""
try:
selector = CommandSelector(corrected_commands)
except StopIteration:
except NoRuleMatched:
logs.failed('No fucks given')
return

Expand Down
12 changes: 7 additions & 5 deletions thefuck/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from warnings import warn
from decorator import decorator
from contextlib import closing
import tempfile

import os
import pickle
Expand Down Expand Up @@ -99,10 +98,9 @@ def _safe(fn, fallback):
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()), [])
Expand Down Expand Up @@ -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]

0 comments on commit b8ce95a

Please sign in to comment.