From b7cb407637166f9a17c738eb6c2d58977ba5e7cf Mon Sep 17 00:00:00 2001 From: nvbn Date: Sat, 25 Apr 2015 02:35:26 +0200 Subject: [PATCH] Add useful constructors for `Rule` and `Command` for tests --- tests/rules/test_brew_install.py | 20 +++++++-------- tests/rules/test_brew_unknown_command.py | 10 ++++---- tests/rules/test_cd_parent.py | 8 +++--- tests/rules/test_composer_not_command.py | 17 ++++++++----- tests/rules/test_fix_alt_space.py | 22 +++++++++-------- tests/rules/test_git_not_command.py | 16 ++++++------ tests/rules/test_git_push.py | 10 ++++---- tests/rules/test_mkdir_p.py | 15 +++++++----- tests/rules/test_pip_unknown_command.py | 11 +++++---- tests/rules/test_python_command.py | 9 ++++--- tests/rules/test_rm_dir.py | 12 ++++----- tests/rules/test_sl_ls.py | 8 +++--- tests/rules/test_ssh_known_host.py | 23 +++++++++--------- tests/rules/test_sudo.py | 12 ++++----- tests/test_conf.py | 8 +++--- tests/test_main.py | 31 ++++++++++++------------ tests/test_utils.py | 13 +++++----- tests/utils.py | 11 +++++++++ 18 files changed, 141 insertions(+), 115 deletions(-) create mode 100644 tests/utils.py diff --git a/tests/rules/test_brew_install.py b/tests/rules/test_brew_install.py index bf06cd95..2ea58f1f 100644 --- a/tests/rules/test_brew_install.py +++ b/tests/rules/test_brew_install.py @@ -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' diff --git a/tests/rules/test_brew_unknown_command.py b/tests/rules/test_brew_unknown_command.py index 408e9989..f7bdcddd 100644 --- a/tests/rules/test_brew_unknown_command.py +++ b/tests/rules/test_brew_unknown_command.py @@ -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' diff --git a/tests/rules/test_cd_parent.py b/tests/rules/test_cd_parent.py index 60f87609..61d1ab1d 100644 --- a/tests/rules/test_cd_parent.py +++ b/tests/rules/test_cd_parent.py @@ -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 ..' diff --git a/tests/rules/test_composer_not_command.py b/tests/rules/test_composer_not_command.py index d1a2e02e..15e630d3 100644 --- a/tests/rules/test_composer_not_command.py +++ b/tests/rules/test_composer_not_command.py @@ -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' diff --git a/tests/rules/test_fix_alt_space.py b/tests/rules/test_fix_alt_space.py index 2d66e08f..2e8eea90 100644 --- a/tests/rules/test_fix_alt_space.py +++ b/tests/rules/test_fix_alt_space.py @@ -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' diff --git a/tests/rules/test_git_not_command.py b/tests/rules/test_git_not_command.py index 91df959e..d93d1330 100644 --- a/tests/rules/test_git_not_command.py +++ b/tests/rules/test_git_not_command.py @@ -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' diff --git a/tests/rules/test_git_push.py b/tests/rules/test_git_push.py index 57f63591..7a2b512d 100644 --- a/tests/rules/test_git_push.py +++ b/tests/rules/test_git_push.py @@ -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" diff --git a/tests/rules/test_mkdir_p.py b/tests/rules/test_mkdir_p.py index dae196fa..60b6b657 100644 --- a/tests/rules/test_mkdir_p.py +++ b/tests/rules/test_mkdir_p.py @@ -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' diff --git a/tests/rules/test_pip_unknown_command.py b/tests/rules/test_pip_unknown_command.py index 61b5c997..58ba2bb8 100644 --- a/tests/rules/test_pip_unknown_command.py +++ b/tests/rules/test_pip_unknown_command.py @@ -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' diff --git a/tests/rules/test_python_command.py b/tests/rules/test_python_command.py index 56d9b3f9..54be39a2 100644 --- a/tests/rules/test_python_command.py +++ b/tests/rules/test_python_command.py @@ -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' diff --git a/tests/rules/test_rm_dir.py b/tests/rules/test_rm_dir.py index 1608047d..573bbdc4 100644 --- a/tests/rules/test_rm_dir.py +++ b/tests/rules/test_rm_dir.py @@ -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(): diff --git a/tests/rules/test_sl_ls.py b/tests/rules/test_sl_ls.py index fd83b04d..4ed7499e 100644 --- a/tests/rules/test_sl_ls.py +++ b/tests/rules/test_sl_ls.py @@ -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' diff --git a/tests/rules/test_ssh_known_host.py b/tests/rules/test_ssh_known_host.py index 252cac18..8875bd44 100644 --- a/tests/rules/test_ssh_known_host.py +++ b/tests/rules/test_ssh_known_host.py @@ -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 diff --git a/tests/rules/test_sudo.py b/tests/rules/test_sudo.py index 4f3b3f50..47542c89 100644 --- a/tests/rules/test_sudo.py +++ b/tests/rules/test_sudo.py @@ -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' diff --git a/tests/test_conf.py b/tests/test_conf.py index 32e57b0d..608a49eb 100644 --- a/tests/test_conf.py +++ b/tests/test_conf.py @@ -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(): diff --git a/tests/test_main.py b/tests/test_main.py index 078f1a8e..cc5bf79a 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -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() == ('', '') diff --git a/tests/test_utils.py b/tests/test_utils.py index 995614d1..08766a9f 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -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' diff --git a/tests/utils.py b/tests/utils.py new file mode 100644 index 00000000..5191d2c8 --- /dev/null +++ b/tests/utils.py @@ -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)