#  This file is part of PyBuilder
#
#  Copyright 2011-2014 PyBuilder Team
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

import unittest

from mock import patch

from pybuilder.core import Project
from pybuilder.plugins.python.cram_plugin import (
    _command,
    _find_files,
    _report_file,

)


class CramPluginTests(unittest.TestCase):

    def test_command_respects_no_verbose(self):
        project = Project('.')
        project.set_property('verbose', False)
        expected = ['cram']
        received = _command(project)
        self.assertEquals(expected, received)

    def test_command_respects_verbose(self):
        project = Project('.')
        project.set_property('verbose', True)
        expected = ['cram', '--verbose']
        received = _command(project)
        self.assertEquals(expected, received)

    @patch('pybuilder.plugins.python.cram_plugin.discover_files_matching')
    def test_find_files(self, discover_mock):
        project = Project('.')
        project.set_property('dir_source_cmdlinetest', '/any/dir')
        expected = ['/any/dir/test.cram']
        discover_mock.return_value = expected
        received = _find_files(project)
        self.assertEquals(expected, received)
        discover_mock.assert_called_once_with('/any/dir', '*.cram')

    def test_report(self):
        project = Project('.')
        project.set_property('dir_reports', '/any/dir')
        expected = './any/dir/cram.err'
        received = _report_file(project)
        self.assertEquals(expected, received)

@@ -662,17 +662,29 @@
 rt patch
+, Mock, call
 %0A
-
 %0Afrom py
@@ -811,17 +811,26 @@
 t_file,%0A
+    cram,
 %0A
-
 )%0A%0A%0Aclas
@@ -2002,32 +2002,32 @@
 t_file(project)%0A
-
         self.ass
@@ -2036,28 +2036,1340 @@
 tEquals(expected, received)%0A
+%0A    @patch('pybuilder.plugins.python.cram_plugin._command')%0A    @patch('pybuilder.plugins.python.cram_plugin._find_files')%0A    @patch('pybuilder.plugins.python.cram_plugin._report_file')%0A    @patch('pybuilder.plugins.python.cram_plugin.read_file')%0A    @patch('pybuilder.plugins.python.cram_plugin.execute_command')%0A    def test_running_plugin(self,%0A                            execute_mock,%0A                            read_file_mock,%0A                            report_mock,%0A                            find_files_mock,%0A                            command_mock%0A                            ):%0A        project = Project('.')%0A        project.set_property('verbose', False)%0A        logger = Mock()%0A%0A        command_mock.return_value = %5B'cram'%5D%0A        find_files_mock.return_value = %5B'test1.cram', 'test2.cram'%5D%0A        report_mock.return_value = 'report_file'%0A        read_file_mock.return_value = %5B'# results'%5D%0A        execute_mock.return_value = 0%0A%0A        cram(project, logger)%0A        execute_mock.assert_called_once_with(%0A            %5B'cram', 'test1.cram', 'test2.cram'%5D, 'report_file')%0A        expected = %5Bcall('Running Cram tests'),%0A                    call('Cram tests were fine'),%0A                    call('results'),%0A                    %5D%0A        self.assertEquals(expected, logger.info.call_args_list)%0A

test cram happy trail