Skip to content

Commit

Permalink
Merge 08af616 into 122541b
Browse files Browse the repository at this point in the history
  • Loading branch information
nvbn committed Sep 8, 2015
2 parents 122541b + 08af616 commit 745fcb8
Show file tree
Hide file tree
Showing 20 changed files with 615 additions and 536 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: 5 additions & 0 deletions tests/functional/plots.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from time import sleep


def _set_confirmation(proc, require):
proc.sendline(u'mkdir -p ~/.thefuck')
proc.sendline(
Expand All @@ -22,12 +25,14 @@ def with_confirmation(proc, TIMEOUT):

def history_changed(proc, TIMEOUT, to):
"""Ensures that history changed."""
sleep(1)
proc.send('\033[A')
assert proc.expect([TIMEOUT, to])


def history_not_changed(proc, TIMEOUT):
"""Ensures that history not changed."""
sleep(1)
proc.send('\033[A')
assert proc.expect([TIMEOUT, u'fuck'])

Expand Down
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'])
1 change: 0 additions & 1 deletion tests/rules/test_fix_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import os
from thefuck.rules.fix_file import match, get_new_command
from tests.utils import Command
from thefuck.types import Settings


# (script, file, line, col (or None), stdout, stderr)
Expand Down
31 changes: 11 additions & 20 deletions tests/test_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,6 @@
import six
from mock import Mock
from thefuck import conf
from tests.utils import Rule


@pytest.mark.parametrize('enabled, rules, result', [
(True, conf.DEFAULT_RULES, True),
(False, conf.DEFAULT_RULES, False),
(False, conf.DEFAULT_RULES + ['test'], True)])
def test_default(enabled, rules, result):
assert (Rule('test', enabled_by_default=enabled) in rules) == result


@pytest.fixture
Expand All @@ -28,7 +19,7 @@ def environ(monkeypatch):
@pytest.mark.usefixture('environ')
def test_settings_defaults(load_source, settings):
load_source.return_value = object()
conf.init_settings(Mock())
settings.init()
for key, val in conf.DEFAULT_SETTINGS.items():
assert getattr(settings, key) == val

Expand All @@ -42,7 +33,7 @@ def test_from_file(self, load_source, settings):
no_colors=True,
priority={'vim': 100},
exclude_rules=['git'])
conf.init_settings(Mock())
settings.init()
assert settings.rules == ['test']
assert settings.wait_command == 10
assert settings.require_confirmation is True
Expand All @@ -56,7 +47,7 @@ def test_from_file_with_DEFAULT(self, load_source, settings):
exclude_rules=[],
require_confirmation=True,
no_colors=True)
conf.init_settings(Mock())
settings.init()
assert settings.rules == conf.DEFAULT_RULES + ['test']


Expand All @@ -69,7 +60,7 @@ def test_from_env(self, environ, settings):
'THEFUCK_REQUIRE_CONFIRMATION': 'true',
'THEFUCK_NO_COLORS': 'false',
'THEFUCK_PRIORITY': 'bash=10:lisp=wrong:vim=15'})
conf.init_settings(Mock())
settings.init()
assert settings.rules == ['bash', 'lisp']
assert settings.exclude_rules == ['git', 'vim']
assert settings.wait_command == 55
Expand All @@ -79,26 +70,26 @@ def test_from_env(self, environ, settings):

def test_from_env_with_DEFAULT(self, environ, settings):
environ.update({'THEFUCK_RULES': 'DEFAULT_RULES:bash:lisp'})
conf.init_settings(Mock())
settings.init()
assert settings.rules == conf.DEFAULT_RULES + ['bash', 'lisp']


class TestInitializeSettingsFile(object):
def test_ignore_if_exists(self):
def test_ignore_if_exists(self, settings):
settings_path_mock = Mock(is_file=Mock(return_value=True), open=Mock())
user_dir_mock = Mock(joinpath=Mock(return_value=settings_path_mock))
conf.initialize_settings_file(user_dir_mock)
settings.user_dir = Mock(joinpath=Mock(return_value=settings_path_mock))
settings._init_settings_file()
assert settings_path_mock.is_file.call_count == 1
assert not settings_path_mock.open.called

def test_create_if_doesnt_exists(self):
def test_create_if_doesnt_exists(self, settings):
settings_file = six.StringIO()
settings_path_mock = Mock(
is_file=Mock(return_value=False),
open=Mock(return_value=Mock(
__exit__=lambda *args: None, __enter__=lambda *args: settings_file)))
user_dir_mock = Mock(joinpath=Mock(return_value=settings_path_mock))
conf.initialize_settings_file(user_dir_mock)
settings.user_dir = Mock(joinpath=Mock(return_value=settings_path_mock))
settings._init_settings_file()
settings_file_contents = settings_file.getvalue()
assert settings_path_mock.is_file.call_count == 1
assert settings_path_mock.open.call_count == 1
Expand Down
75 changes: 16 additions & 59 deletions tests/test_corrector.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,8 @@
import pytest
from pathlib import PosixPath, Path
from mock import Mock
from thefuck import corrector, conf, types
from pathlib import PosixPath
from thefuck import corrector, conf
from tests.utils import Rule, Command, CorrectedCommand
from thefuck.corrector import make_corrected_commands, get_corrected_commands


def test_load_rule(mocker):
match = object()
get_new_command = object()
load_source = mocker.patch(
'thefuck.corrector.load_source',
return_value=Mock(match=match,
get_new_command=get_new_command,
enabled_by_default=True,
priority=900,
requires_output=True))
assert corrector.load_rule(Path('/rules/bash.py')) \
== Rule('bash', match, get_new_command, priority=900)
load_source.assert_called_once_with('bash', '/rules/bash.py')
from thefuck.corrector import get_corrected_commands, organize_commands


class TestGetRules(object):
Expand All @@ -31,18 +15,12 @@ def glob(self, mocker):

@pytest.fixture(autouse=True)
def load_source(self, monkeypatch):
monkeypatch.setattr('thefuck.corrector.load_source',
monkeypatch.setattr('thefuck.types.load_source',
lambda x, _: Rule(x))

def _compare_names(self, rules, names):
assert {r.name for r in rules} == set(names)

def _prepare_rules(self, rules):
if rules == conf.DEFAULT_RULES:
return rules
else:
return types.RulesNamesList(rules)

@pytest.mark.parametrize('paths, conf_rules, exclude_rules, loaded_rules', [
(['git.py', 'bash.py'], conf.DEFAULT_RULES, [], ['git', 'bash']),
(['git.py', 'bash.py'], ['git'], [], ['git']),
Expand All @@ -51,44 +29,13 @@ def _prepare_rules(self, rules):
def test_get_rules(self, glob, settings, paths, conf_rules, exclude_rules,
loaded_rules):
glob([PosixPath(path) for path in paths])
settings.update(rules=self._prepare_rules(conf_rules),
settings.update(rules=conf_rules,
priority={},
exclude_rules=self._prepare_rules(exclude_rules))
exclude_rules=exclude_rules)
rules = corrector.get_rules()
self._compare_names(rules, loaded_rules)


class TestIsRuleMatch(object):
def test_no_match(self):
assert not corrector.is_rule_match(
Command('ls'), Rule('', lambda _: False))

def test_match(self):
rule = Rule('', lambda x: x.script == 'cd ..')
assert corrector.is_rule_match(Command('cd ..'), rule)

@pytest.mark.usefixtures('no_colors')
def test_when_rule_failed(self, capsys):
rule = Rule('test', Mock(side_effect=OSError('Denied')),
requires_output=False)
assert not corrector.is_rule_match(Command('ls'), rule)
assert capsys.readouterr()[1].split('\n')[0] == '[WARN] Rule test:'


class TestMakeCorrectedCommands(object):
def test_with_rule_returns_list(self):
rule = Rule(get_new_command=lambda x: [x.script + '!', x.script + '@'],
priority=100)
assert list(make_corrected_commands(Command(script='test'), rule)) \
== [CorrectedCommand(script='test!', priority=100),
CorrectedCommand(script='test@', priority=200)]

def test_with_rule_returns_command(self):
rule = Rule(get_new_command=lambda x: x.script + '!',
priority=100)
assert list(make_corrected_commands(Command(script='test'), rule)) \
== [CorrectedCommand(script='test!', priority=100)]

def test_get_corrected_commands(mocker):
command = Command('test', 'test', 'test')
rules = [Rule(match=lambda _: False),
Expand All @@ -100,3 +47,13 @@ def test_get_corrected_commands(mocker):
mocker.patch('thefuck.corrector.get_rules', return_value=rules)
assert [cmd.script for cmd in get_corrected_commands(command)] \
== ['test!', 'test@', 'test;']


def test_organize_commands():
"""Ensures that the function removes duplicates and sorts commands."""
commands = [CorrectedCommand('ls'), CorrectedCommand('ls -la', priority=9000),
CorrectedCommand('ls -lh', priority=100),
CorrectedCommand('ls -lh', priority=9999)]
assert list(organize_commands(iter(commands))) \
== [CorrectedCommand('ls'), CorrectedCommand('ls -lh', priority=100),
CorrectedCommand('ls -la', priority=9000)]
46 changes: 0 additions & 46 deletions tests/test_main.py

This file was deleted.

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

0 comments on commit 745fcb8

Please sign in to comment.