From 179839c32fab0241b2ba9dc4c70d938eabfcea23 Mon Sep 17 00:00:00 2001 From: Pablo Santiago Blum de Aguiar Date: Thu, 14 May 2015 17:34:40 -0300 Subject: [PATCH] test(rules): test other rules involving `shells.and_()` Signed-off-by: Pablo Santiago Blum de Aguiar --- tests/rules/test_apt_get.py | 59 ++++++++++++++++++++++++++++++++ tests/rules/test_git_add.py | 39 +++++++++++++++++++++ tests/rules/test_git_checkout.py | 37 ++++++++++++++++++++ tests/rules/test_git_stash.py | 39 +++++++++++++++++++++ tests/rules/test_pacman.py | 53 ++++++++++++++++++++++++++++ 5 files changed, 227 insertions(+) create mode 100644 tests/rules/test_apt_get.py create mode 100644 tests/rules/test_git_add.py create mode 100644 tests/rules/test_git_checkout.py create mode 100644 tests/rules/test_git_stash.py create mode 100644 tests/rules/test_pacman.py diff --git a/tests/rules/test_apt_get.py b/tests/rules/test_apt_get.py new file mode 100644 index 00000000..56ad8208 --- /dev/null +++ b/tests/rules/test_apt_get.py @@ -0,0 +1,59 @@ +import pytest +from mock import Mock, patch +from thefuck.rules import apt_get +from thefuck.rules.apt_get import match, get_new_command +from tests.utils import Command + + +# python-commandnotfound is available in ubuntu 14.04+ +@pytest.mark.skipif(not getattr(apt_get, 'enabled_by_default', True), + reason='Skip if python-commandnotfound is not available') +@pytest.mark.parametrize('command', [ + Command(script='vim', stderr='vim: command not found')]) +def test_match(command): + assert match(command, None) + + +@pytest.mark.parametrize('command, return_value', [ + (Command(script='vim', stderr='vim: command not found'), + [('vim', 'main'), ('vim-tiny', 'main')])]) +@patch('thefuck.rules.apt_get.CommandNotFound', create=True) +@patch.multiple(apt_get, create=True, apt_get='apt_get') +def test_match_mocked(cmdnf_mock, command, return_value): + get_packages = Mock(return_value=return_value) + cmdnf_mock.CommandNotFound.return_value = Mock(getPackages=get_packages) + assert match(command, None) + assert cmdnf_mock.CommandNotFound.called + assert get_packages.called + + +@pytest.mark.parametrize('command', [ + Command(script='vim', stderr=''), Command()]) +def test_not_match(command): + assert not match(command, None) + + +# python-commandnotfound is available in ubuntu 14.04+ +@pytest.mark.skipif(not getattr(apt_get, 'enabled_by_default', True), + reason='Skip if python-commandnotfound is not available') +@pytest.mark.parametrize('command, new_command', [ + (Command('vim'), 'sudo apt-get install vim && vim'), + (Command('convert'), 'sudo apt-get install imagemagick && convert')]) +def test_get_new_command(command, new_command): + assert get_new_command(command, None) == new_command + + +@pytest.mark.parametrize('command, new_command, return_value', [ + (Command('vim'), 'sudo apt-get install vim && vim', + [('vim', 'main'), ('vim-tiny', 'main')]), + (Command('convert'), 'sudo apt-get install imagemagick && convert', + [('imagemagick', 'main'), + ('graphicsmagick-imagemagick-compat', 'universe')])]) +@patch('thefuck.rules.apt_get.CommandNotFound', create=True) +@patch.multiple(apt_get, create=True, apt_get='apt_get') +def test_get_new_command_mocked(cmdnf_mock, command, new_command, return_value): + get_packages = Mock(return_value=return_value) + cmdnf_mock.CommandNotFound.return_value = Mock(getPackages=get_packages) + assert get_new_command(command, None) == new_command + assert cmdnf_mock.CommandNotFound.called + assert get_packages.called diff --git a/tests/rules/test_git_add.py b/tests/rules/test_git_add.py new file mode 100644 index 00000000..8bad9bb6 --- /dev/null +++ b/tests/rules/test_git_add.py @@ -0,0 +1,39 @@ +import pytest +from thefuck.rules.git_add import match, get_new_command +from tests.utils import Command + + +@pytest.fixture +def did_not_match(target, did_you_forget=True): + error = ("error: pathspec '{}' did not match any " + "file(s) known to git.".format(target)) + if did_you_forget: + error = ("{}\nDid you forget to 'git add'?'".format(error)) + return error + + +@pytest.mark.parametrize('command', [ + Command(script='git submodule update unknown', + stderr=did_not_match('unknown')), + Command(script='git commit unknown', + stderr=did_not_match('unknown'))]) # Older versions of Git +def test_match(command): + assert match(command, None) + + +@pytest.mark.parametrize('command', [ + Command(script='git submodule update known', stderr=('')), + Command(script='git commit known', stderr=('')), + Command(script='git commit unknown', # Newer versions of Git + stderr=did_not_match('unknown', False))]) +def test_not_match(command): + assert not match(command, None) + + +@pytest.mark.parametrize('command, new_command', [ + (Command('git submodule update unknown', stderr=did_not_match('unknown')), + 'git add -- unknown && git submodule update unknown'), + (Command('git commit unknown', stderr=did_not_match('unknown')), # Old Git + 'git add -- unknown && git commit unknown')]) +def test_get_new_command(command, new_command): + assert get_new_command(command, None) == new_command diff --git a/tests/rules/test_git_checkout.py b/tests/rules/test_git_checkout.py new file mode 100644 index 00000000..a540b62d --- /dev/null +++ b/tests/rules/test_git_checkout.py @@ -0,0 +1,37 @@ +import pytest +from thefuck.rules.git_checkout import match, get_new_command +from tests.utils import Command + + +@pytest.fixture +def did_not_match(target, did_you_forget=False): + error = ("error: pathspec '{}' did not match any " + "file(s) known to git.".format(target)) + if did_you_forget: + error = ("{}\nDid you forget to 'git add'?'".format(error)) + return error + + +@pytest.mark.parametrize('command', [ + Command(script='git checkout unknown', stderr=did_not_match('unknown')), + Command(script='git commit unknown', stderr=did_not_match('unknown'))]) +def test_match(command): + assert match(command, None) + + +@pytest.mark.parametrize('command', [ + Command(script='git submodule update unknown', + stderr=did_not_match('unknown', True)), + Command(script='git checkout known', stderr=('')), + Command(script='git commit known', stderr=(''))]) +def test_not_match(command): + assert not match(command, None) + + +@pytest.mark.parametrize('command, new_command', [ + (Command(script='git checkout unknown', stderr=did_not_match('unknown')), + 'git branch unknown && git checkout unknown'), + (Command('git commit unknown', stderr=did_not_match('unknown')), + 'git branch unknown && git commit unknown')]) +def test_get_new_command(command, new_command): + assert get_new_command(command, None) == new_command diff --git a/tests/rules/test_git_stash.py b/tests/rules/test_git_stash.py new file mode 100644 index 00000000..c62a48aa --- /dev/null +++ b/tests/rules/test_git_stash.py @@ -0,0 +1,39 @@ +import pytest +from thefuck.rules.git_stash import match, get_new_command +from tests.utils import Command + + +@pytest.fixture +def cherry_pick_error(): + return ('error: Your local changes would be overwritten by cherry-pick.\n' + 'hint: Commit your changes or stash them to proceed.\n' + 'fatal: cherry-pick failed') + + +@pytest.fixture +def rebase_error(): + return ('Cannot rebase: Your index contains uncommitted changes.\n' + 'Please commit or stash them.') + + +@pytest.mark.parametrize('command', [ + Command(script='git cherry-pick a1b2c3d', stderr=cherry_pick_error()), + Command(script='git rebase -i HEAD~7', stderr=rebase_error())]) +def test_match(command): + assert match(command, None) + + +@pytest.mark.parametrize('command', [ + Command(script='git cherry-pick a1b2c3d', stderr=('')), + Command(script='git rebase -i HEAD~7', stderr=(''))]) +def test_not_match(command): + assert not match(command, None) + + +@pytest.mark.parametrize('command, new_command', [ + (Command(script='git cherry-pick a1b2c3d', stderr=cherry_pick_error), + 'git stash && git cherry-pick a1b2c3d'), + (Command('git rebase -i HEAD~7', stderr=rebase_error), + 'git stash && git rebase -i HEAD~7')]) +def test_get_new_command(command, new_command): + assert get_new_command(command, None) == new_command diff --git a/tests/rules/test_pacman.py b/tests/rules/test_pacman.py new file mode 100644 index 00000000..8d719cd6 --- /dev/null +++ b/tests/rules/test_pacman.py @@ -0,0 +1,53 @@ +import pytest +from mock import patch +from thefuck.rules import pacman +from thefuck.rules.pacman import match, get_new_command +from tests.utils import Command + + +pacman_cmd = getattr(pacman, 'pacman', 'pacman') + + +@pytest.mark.skipif(not getattr(pacman, 'enabled_by_default', True), + reason='Skip if pacman is not available') +@pytest.mark.parametrize('command', [ + Command(script='vim', stderr='vim: command not found')]) +def test_match(command): + assert match(command, None) + + +@pytest.mark.parametrize('command, return_value', [ + (Command(script='vim', stderr='vim: command not found'), 'vim foo bar')]) +@patch('thefuck.rules.pacman.subprocess') +@patch.multiple(pacman, create=True, pacman=pacman_cmd) +def test_match_mocked(subp_mock, command, return_value): + subp_mock.check_output.return_value = return_value + assert match(command, None) + assert subp_mock.check_output.called + + +@pytest.mark.parametrize('command', [ + Command(script='vim', stderr=''), Command()]) +def test_not_match(command): + assert not match(command, None) + + +@pytest.mark.skipif(not getattr(pacman, 'enabled_by_default', True), + reason='Skip if pacman is not available') +@pytest.mark.parametrize('command, new_command', [ + (Command('vim'), '{} -S vim && vim'.format(pacman_cmd)), + (Command('convert'), '{} -S imagemagick && convert'.format(pacman_cmd))]) +def test_get_new_command(command, new_command, mocker): + assert get_new_command(command, None) == new_command + + +@pytest.mark.parametrize('command, new_command, return_value', [ + (Command('vim'), '{} -S vim && vim'.format(pacman_cmd), 'vim foo bar'), + (Command('convert'), '{} -S imagemagick && convert'.format(pacman_cmd), + 'imagemagick foo bar')]) +@patch('thefuck.rules.pacman.subprocess') +@patch.multiple(pacman, create=True, pacman=pacman_cmd) +def test_get_new_command_mocked(subp_mock, command, new_command, return_value): + subp_mock.check_output.return_value = return_value + assert get_new_command(command, None) == new_command + assert subp_mock.check_output.called