1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-02-20 20:09:07 +00:00

Add useful constructors for Rule and Command for tests

This commit is contained in:
nvbn 2015-04-25 02:35:26 +02:00
parent bb6b9a638c
commit b7cb407637
18 changed files with 141 additions and 115 deletions

View File

@ -1,7 +1,7 @@
import pytest
from thefuck.types import Command
from thefuck.rules.brew_install import match, get_new_command
from thefuck.rules.brew_install import brew_formulas
from tests.utils import Command
@pytest.fixture
@ -29,21 +29,21 @@ def _is_not_okay_to_test():
reason='No need to run if there\'s no formula')
def test_match(brew_no_available_formula, brew_already_installed,
brew_install_no_argument):
assert match(Command('brew install elsticsearch', '',
brew_no_available_formula), None)
assert not match(Command('brew install git', '',
brew_already_installed), None)
assert not match(Command('brew install', '', brew_install_no_argument),
assert match(Command('brew install elsticsearch',
stderr=brew_no_available_formula), None)
assert not match(Command('brew install git',
stderr=brew_already_installed), None)
assert not match(Command('brew install', stderr=brew_install_no_argument),
None)
@pytest.mark.skipif(_is_not_okay_to_test(),
reason='No need to run if there\'s no formula')
def test_get_new_command(brew_no_available_formula):
assert get_new_command(Command('brew install elsticsearch', '',
brew_no_available_formula), None)\
assert get_new_command(Command('brew install elsticsearch',
stderr=brew_no_available_formula), None)\
== 'brew install elasticsearch'
assert get_new_command(Command('brew install aa', '',
brew_no_available_formula),
assert get_new_command(Command('brew install aa',
stderr=brew_no_available_formula),
None) != 'brew install aha'

View File

@ -1,7 +1,7 @@
import pytest
from thefuck.types import Command
from thefuck.rules.brew_unknown_command import match, get_new_command
from thefuck.rules.brew_unknown_command import brew_commands
from tests.utils import Command
@pytest.fixture
@ -15,14 +15,14 @@ def brew_unknown_cmd_instaa():
def test_match(brew_unknown_cmd):
assert match(Command('brew inst', '', brew_unknown_cmd), None)
assert match(Command('brew inst', stderr=brew_unknown_cmd), None)
for command in brew_commands:
assert not match(Command('brew ' + command, '', ''), None)
assert not match(Command('brew ' + command), None)
def test_get_new_command(brew_unknown_cmd, brew_unknown_cmd_instaa):
assert get_new_command(Command('brew inst', '', brew_unknown_cmd), None)\
assert get_new_command(Command('brew inst', stderr=brew_unknown_cmd), None)\
== 'brew list'
assert get_new_command(Command('brew instaa', '', brew_unknown_cmd_instaa),
assert get_new_command(Command('brew instaa', stderr=brew_unknown_cmd_instaa),
None) == 'brew install'

View File

@ -1,12 +1,12 @@
from thefuck.types import Command
from thefuck.rules.cd_parent import match, get_new_command
from tests.utils import Command
def test_match():
assert match(Command('cd..', '', 'cd..: command not found'), None)
assert not match(Command('', '', ''), None)
assert match(Command('cd..', stderr='cd..: command not found'), None)
assert not match(Command(), None)
def test_get_new_command():
assert get_new_command(
Command('cd..', '', ''), None) == 'cd ..'
Command('cd..'), None) == 'cd ..'

View File

@ -1,6 +1,6 @@
import pytest
from thefuck.types import Command
from thefuck.rules.composer_not_command import match, get_new_command
from tests.utils import Command
@pytest.fixture
@ -36,13 +36,18 @@ def composer_not_command_one_of_this():
def test_match(composer_not_command, composer_not_command_one_of_this):
assert match(Command('composer udpate', '', composer_not_command), None)
assert match(Command('composer pdate', '', composer_not_command_one_of_this), None)
assert not match(Command('ls update', '', composer_not_command), None)
assert match(Command('composer udpate',
stderr=composer_not_command), None)
assert match(Command('composer pdate',
stderr=composer_not_command_one_of_this), None)
assert not match(Command('ls update', stderr=composer_not_command),
None)
def test_get_new_command(composer_not_command, composer_not_command_one_of_this):
assert get_new_command(Command('composer udpate', '', composer_not_command), None) \
assert get_new_command(Command('composer udpate',
stderr=composer_not_command), None) \
== 'composer update'
assert get_new_command(
Command('composer pdate', '', composer_not_command_one_of_this), None) == 'composer selfupdate'
Command('composer pdate', stderr=composer_not_command_one_of_this),
None) == 'composer selfupdate'

View File

@ -1,18 +1,20 @@
# -*- encoding: utf-8 -*-
from thefuck.types import Command
from thefuck.rules.fix_alt_space import match, get_new_command
from tests.utils import Command
def test_match():
""" The character before 'grep' is Alt+Space, which happens frequently on the Mac when typing
the pipe character (Alt+7), and holding the Alt key pressed for longer than necessary. """
assert match(Command(u'ps -ef | grep foo', '', u'-bash:  grep: command not found'), None)
assert not match(Command('ps -ef | grep foo', '', ''), None)
assert not match(Command('', '', ''), None)
"""The character before 'grep' is Alt+Space, which happens frequently
on the Mac when typing the pipe character (Alt+7), and holding the Alt
key pressed for longer than necessary.
"""
assert match(Command(u'ps -ef | grep foo',
stderr=u'-bash:  grep: command not found'), None)
assert not match(Command('ps -ef | grep foo'), None)
assert not match(Command(), None)
def test_get_new_command():
""" Replace the Alt+Space character by a simple space """
assert get_new_command(Command(u'ps -ef | grep foo', '', ''), None) == 'ps -ef | grep foo'
assert get_new_command(Command(u'ps -ef | grep foo'), None)\
== 'ps -ef | grep foo'

View File

@ -1,6 +1,6 @@
import pytest
from thefuck.types import Command
from thefuck.rules.git_not_command import match, get_new_command
from tests.utils import Command
@pytest.fixture
@ -31,14 +31,14 @@ def git_command():
def test_match(git_not_command, git_command, git_not_command_one_of_this):
assert match(Command('git brnch', '', git_not_command), None)
assert match(Command('git st', '', git_not_command_one_of_this), None)
assert not match(Command('ls brnch', '', git_not_command), None)
assert not match(Command('git branch', '', git_command), None)
assert match(Command('git brnch', stderr=git_not_command), None)
assert match(Command('git st', stderr=git_not_command_one_of_this), None)
assert not match(Command('ls brnch', stderr=git_not_command), None)
assert not match(Command('git branch', stderr=git_command), None)
def test_get_new_command(git_not_command, git_not_command_one_of_this):
assert get_new_command(Command('git brnch', '', git_not_command), None)\
assert get_new_command(Command('git brnch', stderr=git_not_command), None)\
== 'git branch'
assert get_new_command(
Command('git st', '', git_not_command_one_of_this), None) == 'git status'
assert get_new_command(Command('git st', stderr=git_not_command_one_of_this),
None) == 'git status'

View File

@ -1,6 +1,6 @@
import pytest
from thefuck.types import Command
from thefuck.rules.git_push import match, get_new_command
from tests.utils import Command
@pytest.fixture
@ -14,11 +14,11 @@ To push the current branch and set the remote as upstream, use
def test_match(stderr):
assert match(Command('git push master', '', stderr), None)
assert not match(Command('git push master', '', ''), None)
assert not match(Command('ls', '', stderr), None)
assert match(Command('git push master', stderr=stderr), None)
assert not match(Command('git push master'), None)
assert not match(Command('ls', stderr=stderr), None)
def test_get_new_command(stderr):
assert get_new_command(Command('', '', stderr), None)\
assert get_new_command(Command(stderr=stderr), None)\
== "git push --set-upstream origin master"

View File

@ -1,13 +1,16 @@
from thefuck.types import Command
from thefuck.rules.mkdir_p import match, get_new_command
from tests.utils import Command
def test_match():
assert match(Command('mkdir foo/bar/baz', '', 'mkdir: foo/bar: No such file or directory'), None)
assert not match(Command('mkdir foo/bar/baz', '', ''), None)
assert not match(Command('mkdir foo/bar/baz', '', 'foo bar baz'), None)
assert not match(Command('', '', ''), None)
assert match(Command('mkdir foo/bar/baz',
stderr='mkdir: foo/bar: No such file or directory'),
None)
assert not match(Command('mkdir foo/bar/baz'), None)
assert not match(Command('mkdir foo/bar/baz', stderr='foo bar baz'), None)
assert not match(Command(), None)
def test_get_new_command():
assert get_new_command(Command('mkdir foo/bar/baz', '', ''), None) == 'mkdir -p foo/bar/baz'
assert get_new_command(Command('mkdir foo/bar/baz'), None)\
== 'mkdir -p foo/bar/baz'

View File

@ -1,6 +1,6 @@
import pytest
from thefuck.types import Command
from thefuck.rules.pip_unknown_command import match, get_new_command
from tests.utils import Command
@pytest.fixture
@ -14,11 +14,12 @@ def pip_unknown_cmd_without_recommend():
def test_match(pip_unknown_cmd, pip_unknown_cmd_without_recommend):
assert match(Command('pip instatl', '', pip_unknown_cmd), None)
assert not match(Command('pip i', '', pip_unknown_cmd_without_recommend),
assert match(Command('pip instatl', stderr=pip_unknown_cmd), None)
assert not match(Command('pip i',
stderr=pip_unknown_cmd_without_recommend),
None)
def test_get_new_command(pip_unknown_cmd):
assert get_new_command(Command('pip instatl', '', pip_unknown_cmd), None)\
== 'pip install'
assert get_new_command(Command('pip instatl', stderr=pip_unknown_cmd),
None) == 'pip install'

View File

@ -1,11 +1,12 @@
from thefuck.types import Command
from thefuck.rules.python_command import match, get_new_command
from tests.utils import Command
def test_match():
assert match(Command('temp.py', '', 'Permission denied'), None)
assert not match(Command('', '', ''), None)
assert match(Command('temp.py', stderr='Permission denied'), None)
assert not match(Command(), None)
def test_get_new_command():
assert get_new_command(Command('./test_sudo.py', '', ''), None) == 'python ./test_sudo.py'
assert get_new_command(Command('./test_sudo.py'), None)\
== 'python ./test_sudo.py'

View File

@ -1,13 +1,13 @@
from thefuck.types import Command
from thefuck.rules.rm_dir import match, get_new_command
from tests.utils import Command
def test_match():
assert match(Command('rm foo', '', 'rm: foo: is a directory'), None)
assert match(Command('rm foo', '', 'rm: foo: Is a directory'), None)
assert not match(Command('rm foo', '', ''), None)
assert not match(Command('rm foo', '', 'foo bar baz'), None)
assert not match(Command('', '', ''), None)
assert match(Command('rm foo', stderr='rm: foo: is a directory'), None)
assert match(Command('rm foo', stderr='rm: foo: Is a directory'), None)
assert not match(Command('rm foo'), None)
assert not match(Command('rm foo', stderr='foo bar baz'), None)
assert not match(Command(), None)
def test_get_new_command():

View File

@ -1,12 +1,12 @@
from thefuck.types import Command
from thefuck.rules.sl_ls import match, get_new_command
from tests.utils import Command
def test_match():
assert match(Command('sl', '', ''), None)
assert not match(Command('ls', '', ''), None)
assert match(Command('sl'), None)
assert not match(Command('ls'), None)
def test_get_new_command():
assert get_new_command(Command('sl', '', ''), None) == 'ls'
assert get_new_command(Command('sl'), None) == 'ls'

View File

@ -1,8 +1,9 @@
import os
import pytest
from mock import Mock
from thefuck.types import Command
from thefuck.rules.ssh_known_hosts import match, get_new_command, remove_offending_keys
from thefuck.rules.ssh_known_hosts import match, get_new_command,\
remove_offending_keys
from tests.utils import Command
@pytest.fixture
@ -43,18 +44,18 @@ Host key verification failed.""".format(path, '98.765.432.321')
def test_match(ssh_error):
errormsg, _, _, _ = ssh_error
assert match(Command('ssh', '', errormsg), None)
assert match(Command('ssh', '', errormsg), None)
assert match(Command('scp something something', '', errormsg), None)
assert match(Command('scp something something', '', errormsg), None)
assert not match(Command('', '', errormsg), None)
assert not match(Command('notssh', '', errormsg), None)
assert not match(Command('ssh', '', ''), None)
assert match(Command('ssh', stderr=errormsg), None)
assert match(Command('ssh', stderr=errormsg), None)
assert match(Command('scp something something', stderr=errormsg), None)
assert match(Command('scp something something', stderr=errormsg), None)
assert not match(Command(stderr=errormsg), None)
assert not match(Command('notssh', stderr=errormsg), None)
assert not match(Command('ssh'), None)
def test_remove_offending_keys(ssh_error):
errormsg, path, reset, known_hosts = ssh_error
command = Command('ssh user@host', '', errormsg)
command = Command('ssh user@host', stderr=errormsg)
remove_offending_keys(command, None)
expected = ['123.234.567.890 asdjkasjdakjsd\n', '111.222.333.444 qwepoiwqepoiss\n']
assert known_hosts(path) == expected
@ -65,5 +66,5 @@ def test_get_new_command(ssh_error, monkeypatch):
method = Mock()
monkeypatch.setattr('thefuck.rules.ssh_known_hosts.remove_offending_keys', method)
assert get_new_command(Command('ssh user@host', '', errormsg), None) == 'ssh user@host'
assert get_new_command(Command('ssh user@host', stderr=errormsg), None) == 'ssh user@host'
assert method.call_count

View File

@ -1,13 +1,13 @@
from thefuck.types import Command
from thefuck.rules.sudo import match, get_new_command
from tests.utils import Command
def test_match():
assert match(Command('', '', 'Permission denied'), None)
assert match(Command('', '', 'permission denied'), None)
assert match(Command('', '', "npm ERR! Error: EACCES, unlink"), None)
assert not match(Command('', '', ''), None)
assert match(Command(stderr='Permission denied'), None)
assert match(Command(stderr='permission denied'), None)
assert match(Command(stderr="npm ERR! Error: EACCES, unlink"), None)
assert not match(Command(), None)
def test_get_new_command():
assert get_new_command(Command('ls', '', ''), None) == 'sudo ls'
assert get_new_command(Command('ls'), None) == 'sudo ls'

View File

@ -1,13 +1,13 @@
import six
from mock import patch, Mock
from thefuck.types import Rule
from thefuck import conf
from tests.utils import Rule
def test_default():
assert Rule('test', None, None, True) in conf.DEFAULT_RULES
assert Rule('test', None, None, False) not in conf.DEFAULT_RULES
assert Rule('test', None, None, False) in (conf.DEFAULT_RULES + ['test'])
assert Rule('test', enabled_by_default=True) in conf.DEFAULT_RULES
assert Rule('test', enabled_by_default=False) not in conf.DEFAULT_RULES
assert Rule('test', enabled_by_default=False) in (conf.DEFAULT_RULES + ['test'])
def test_settings_defaults():

View File

@ -2,6 +2,7 @@ from subprocess import PIPE
from pathlib import PosixPath, Path
from mock import patch, Mock
from thefuck import main, conf, types
from tests.utils import Rule, Command
def test_load_rule():
@ -13,7 +14,7 @@ def test_load_rule():
get_new_command=get_new_command,
enabled_by_default=True)) as load_source:
assert main.load_rule(Path('/rules/bash.py')) \
== types.Rule('bash', match, get_new_command, True)
== Rule('bash', match, get_new_command)
load_source.assert_called_once_with('bash', '/rules/bash.py')
@ -26,15 +27,15 @@ def test_get_rules():
assert list(main.get_rules(
Path('~'),
Mock(rules=conf.DEFAULT_RULES))) \
== [types.Rule('bash', 'bash', 'bash', True),
types.Rule('lisp', 'lisp', 'lisp', True),
types.Rule('bash', 'bash', 'bash', True),
types.Rule('lisp', 'lisp', 'lisp', True)]
== [Rule('bash', 'bash', 'bash'),
Rule('lisp', 'lisp', 'lisp'),
Rule('bash', 'bash', 'bash'),
Rule('lisp', 'lisp', 'lisp')]
assert list(main.get_rules(
Path('~'),
Mock(rules=types.RulesNamesList(['bash'])))) \
== [types.Rule('bash', 'bash', 'bash', True),
types.Rule('bash', 'bash', 'bash', True)]
== [Rule('bash', 'bash', 'bash'),
Rule('bash', 'bash', 'bash')]
def test_get_command():
@ -47,7 +48,7 @@ def test_get_command():
Popen.return_value.stderr.read.return_value = b'stderr'
assert main.get_command(Mock(), ['thefuck', 'apt-get',
'search', 'vim']) \
== types.Command('apt-get search vim', 'stdout', 'stderr')
== Command('apt-get search vim', 'stdout', 'stderr')
Popen.assert_called_once_with('apt-get search vim',
shell=True,
stdout=PIPE,
@ -57,12 +58,12 @@ def test_get_command():
def test_get_matched_rule(capsys):
rules = [types.Rule('', lambda x, _: x.script == 'cd ..', None, True),
types.Rule('', lambda *_: False, None, True),
types.Rule('rule', Mock(side_effect=OSError('Denied')), None, True)]
assert main.get_matched_rule(types.Command('ls', '', ''),
rules = [Rule('', lambda x, _: x.script == 'cd ..'),
Rule('', lambda *_: False),
Rule('rule', Mock(side_effect=OSError('Denied')))]
assert main.get_matched_rule(Command('ls'),
rules, Mock(no_colors=True)) is None
assert main.get_matched_rule(types.Command('cd ..', '', ''),
assert main.get_matched_rule(Command('cd ..'),
rules, Mock(no_colors=True)) == rules[0]
assert capsys.readouterr()[1].split('\n')[0] \
== '[WARN] Rule rule:'
@ -70,11 +71,11 @@ def test_get_matched_rule(capsys):
def test_run_rule(capsys):
with patch('thefuck.main.confirm', return_value=True):
main.run_rule(types.Rule('', None, lambda *_: 'new-command', True),
main.run_rule(Rule(get_new_command=lambda *_: 'new-command'),
None, None)
assert capsys.readouterr() == ('new-command\n', '')
with patch('thefuck.main.confirm', return_value=False):
main.run_rule(types.Rule('', None, lambda *_: 'new-command', True),
main.run_rule(Rule(get_new_command=lambda *_: 'new-command'),
None, None)
assert capsys.readouterr() == ('', '')

View File

@ -1,6 +1,7 @@
from mock import Mock
from thefuck.utils import sudo_support, wrap_settings
from thefuck.types import Command, Settings
from thefuck.types import Settings
from tests.utils import Command
def test_wrap_settings():
@ -13,13 +14,13 @@ def test_wrap_settings():
def test_sudo_support():
fn = Mock(return_value=True, __name__='')
assert sudo_support(fn)(Command('sudo ls', 'out', 'err'), None)
fn.assert_called_once_with(Command('ls', 'out', 'err'), None)
assert sudo_support(fn)(Command('sudo ls'), None)
fn.assert_called_once_with(Command('ls'), None)
fn.return_value = False
assert not sudo_support(fn)(Command('sudo ls', 'out', 'err'), None)
assert not sudo_support(fn)(Command('sudo ls'), None)
fn.return_value = 'pwd'
assert sudo_support(fn)(Command('sudo ls', 'out', 'err'), None) == 'sudo pwd'
assert sudo_support(fn)(Command('sudo ls'), None) == 'sudo pwd'
assert sudo_support(fn)(Command('ls', 'out', 'err'), None) == 'pwd'
assert sudo_support(fn)(Command('ls'), None) == 'pwd'

11
tests/utils.py Normal file
View File

@ -0,0 +1,11 @@
from thefuck import types
def Command(script='', stdout='', stderr=''):
return types.Command(script, stdout, stderr)
def Rule(name='', match=lambda *_: True,
get_new_command=lambda *_: '',
enabled_by_default=True):
return types.Rule(name, match, get_new_command, enabled_by_default)