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

@@ -203,16 +203,341 @@
 cman')%0A%0A
+PKGFILE_OUTPUT_CONVERT = '''%0Aextra/imagemagick 6.9.1.0-1%5Ct/usr/bin/convert%0A'''%0A%0APKGFILE_OUTPUT_VIM = '''%0Aextra/gvim 7.4.712-1        %5Ct/usr/bin/vim%0Aextra/gvim-python3 7.4.712-1%5Ct/usr/bin/vim%0Aextra/vim 7.4.712-1         %5Ct/usr/bin/vim%0Aextra/vim-minimal 7.4.712-1 %5Ct/usr/bin/vim%0Aextra/vim-python3 7.4.712-1 %5Ct/usr/bin/vim%0A'''%0A%0A
 %0A@pytest
@@ -931,29 +931,34 @@
 ound'), 
-'vim foo bar'
+PKGFILE_OUTPUT_VIM
 )%5D)%0A@pat
@@ -1595,32 +1595,39 @@
 ('vim'), '%7B%7D -S 
+extra/g
 vim && vim'.form
@@ -1667,32 +1667,38 @@
 nvert'), '%7B%7D -S 
+extra/
 imagemagick && c
@@ -1935,16 +1935,23 @@
  '%7B%7D -S 
+extra/g
 vim && v
@@ -1977,22 +1977,35 @@
 md),
- 'vim foo bar'
+%0A        PKGFILE_OUTPUT_VIM
 ),%0A 
@@ -2035,16 +2035,22 @@
  '%7B%7D -S 
+extra/
 imagemag
@@ -2094,29 +2094,33 @@
     
-'imagemagick foo bar'
+   PKGFILE_OUTPUT_CONVERT
 )%5D)%0A

Fix pacman tests on Arch Linux