from mock import patch
from pyfakefs import fake_filesystem_unittest
from shellfoundry.utilities.python_dependencies_packager import PythonDependenciesPackager
from tests.asserts import *
from shellfoundry.commands.pack_command import PackCommandExecutor


class TestPackCommandExecutor(fake_filesystem_unittest.TestCase):
    def setUp(self):
        self.setUpPyfakefs()

    @patch('click.echo')
    @patch('shellfoundry.utilities.python_dependencies_packager.pip')
    def test_build_package_package_created(self, pip_mock, echo_mock):
        # Arrange
        self.fs.CreateFile('nut_shell/shell.yml', contents="""
shell:
    name: nut_shell
    author: Chuck Norris
    email: chuck@hollywood.io
    description: Save the world
    version: 1.0.0
    """)
        self.fs.CreateFile('nut_shell/datamodel/metadata.xml')
        self.fs.CreateFile('nut_shell/datamodel/datamodel.xml')
        self.fs.CreateFile('nut_shell/datamodel/shellconfig.xml')
        self.fs.CreateFile('nut_shell/src/driver.py')
        os.chdir('nut_shell')

        command_executor = PackCommandExecutor()

        # Act
        command_executor.pack()

        # Assert
        assertFileExists(self, 'dist/nut_shell.zip')
        echo_mock.assert_any_call(u'Shell package was successfully created:')

    @patch('click.echo')
    @patch('shellfoundry.utilities.python_dependencies_packager.pip')
    def test_proper_error_message_displayed_when_shell_yml_is_in_wrong_format(self, pip_mock, echo_mock):
        # Arrange
        self.fs.CreateFile('nut_shell/shell.yml', contents='WRONG YAML FORMAT')
        os.chdir('nut_shell')

        command_executor = PackCommandExecutor()

        # Act
        command_executor.pack()

        # Assert
        echo_mock.assert_any_call(u'shell.yml format is wrong')

    @patch('click.echo')
    @patch('shellfoundry.utilities.python_dependencies_packager.pip')
    def test_proper_error_message_displayed_when_shell_yml_missing(self, pip_mock, echo_mock):
        # Arrange
        self.fs.CreateFile('nut_shell/datamodel/datamodel.xml')
        os.chdir('nut_shell')

        command_executor = PackCommandExecutor()

        # Act
        command_executor.pack()

        # Assert
        echo_mock.assert_any_call(u'shell.yml file is missing')

    @patch('click.echo')
    @patch('shellfoundry.utilities.python_dependencies_packager.pip')
    def test_tosca_based_shell_packed(self, pip_mock, echo_mock):
        # Arrange
        self.fs.CreateFile('nut-shell/TOSCA-Metadata/TOSCA.meta',
                           contents='TOSCA-Meta-File-Version: 1.0 '
                                    'CSAR-Version: 1.1 '
                                    'Created-By: Anonymous'
                                    'Entry-Definitions: shell-definition.yml')

        self.fs.CreateFile('nut-shell/shell-definition.yml',
                           contents='SOME SHELL DEFINITION')

        self.fs.CreateFile('nut-shell/shell-icon.png',
                           contents='IMAGE')

        os.chdir('nut-shell')

        command_executor = PackCommandExecutor()

        # Act
        command_executor.pack()

        # Assert
        assertFileExists(self, 'dist/shell-package.zip')

@@ -2287,149 +2287,163 @@
 ch('
-click.echo')%0A    @patch('shellfoundry.utilities.python_dependencies_packager.pip')%0A    def test_tosca_based_shell_packed(self, pip_mock, echo
+shellfoundry.commands.pack_command.ShellPackageBuilder.pack')%0A    def test_tosca_based_shell_packager_called_when_shell_contains_tosca_meta_file(self, pack
 _moc
@@ -2596,16 +2596,18 @@
 on: 1.0 
+%5Cn
 '%0A      
@@ -2655,16 +2655,18 @@
 on: 1.1 
+%5Cn
 '%0A      
@@ -2717,16 +2717,19 @@
 nonymous
+ %5Cn
 '%0A      
@@ -2929,109 +2929,8 @@
 ')%0A%0A
-        self.fs.CreateFile('nut-shell/shell-icon.png',%0A                           contents='IMAGE')%0A%0A
     
@@ -3082,53 +3082,38 @@
     
+self.
 assert
-FileExists(self, 'dist/shell-package.zip'
+True(pack_mock.called
 )%0A

fix failing test