import unittest
from biicode.common.settings.version import Version
from mock import patch
from biicode.client.setups.finders.finders import gnu_version
from biicode.client.setups.rpi_cross_compiler import find_gnu_arm
from biicode.client.workspace.bii_paths import get_biicode_env_folder_path


GCC_VERSION_MAC = '''Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.1 (clang-503.0.38) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.1.0
Thread model: posix'''

GCC_VERSION_UBUNTU = '''gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
'''

GCC_VERSION_WIN = '''gcc (GCC) 4.8.1
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.'''


class FindersTest(unittest.TestCase):

    @patch('biicode.client.setups.finders.finders.execute')
    def test_gnu_version_detection(self, execute_mock):
        execute_mock.return_value = ("", GCC_VERSION_MAC)
        self.assertEquals(gnu_version('gnu'), Version('4.2.1'))
        execute_mock.return_value = ("", GCC_VERSION_UBUNTU)
        self.assertEquals(gnu_version('gnu'), Version('4.8.1'))
        execute_mock.return_value = ("", GCC_VERSION_WIN)
        self.assertEquals(gnu_version('gnu'), Version('4.8.1'))

    @patch('os.path.exists')
    def test_find_gnu_arm(self, exists):
        exists.return_value = False
        self.assertEqual((None, None), find_gnu_arm())

        exists.return_value = True
        c_path, cpp_path = find_gnu_arm()
        inst_path = get_biicode_env_folder_path().replace('\\', '/')
        c_path = c_path.replace('\\', '/')
        cpp_path = cpp_path.replace('\\', '/')
        inst_path = '%s/raspberry_cross_compilers/arm-bcm2708/'\
                    'arm-bcm2708hardfp-linux-gnueabi/bin/'\
                    'arm-bcm2708hardfp-linux-gnueabi' % inst_path
        self.assertTrue(cpp_path.endswith('%s-g++' % inst_path))
        self.assertTrue(c_path.endswith('%s-gcc' % inst_path))

[((28, 5, 28, 59), 'mock.patch', 'patch', ({(28, 11, 28, 58): '"""biicode.client.setups.finders.finders.execute"""'}, {}), "('biicode.client.setups.finders.finders.execute')", False, 'from mock import patch\n'), ((37, 5, 37, 28), 'mock.patch', 'patch', ({(37, 11, 37, 27): '"""os.path.exists"""'}, {}), "('os.path.exists')", False, 'from mock import patch\n'), ((43, 27, 43, 41), 'biicode.client.setups.rpi_cross_compiler.find_gnu_arm', 'find_gnu_arm', ({}, {}), '()', False, 'from biicode.client.setups.rpi_cross_compiler import find_gnu_arm\n'), ((31, 26, 31, 44), 'biicode.client.setups.finders.finders.gnu_version', 'gnu_version', ({(31, 38, 31, 43): '"""gnu"""'}, {}), "('gnu')", False, 'from biicode.client.setups.finders.finders import gnu_version\n'), ((31, 46, 31, 62), 'biicode.common.settings.version.Version', 'Version', ({(31, 54, 31, 61): '"""4.2.1"""'}, {}), "('4.2.1')", False, 'from biicode.common.settings.version import Version\n'), ((33, 26, 33, 44), 'biicode.client.setups.finders.finders.gnu_version', 'gnu_version', ({(33, 38, 33, 43): '"""gnu"""'}, {}), "('gnu')", False, 'from biicode.client.setups.finders.finders import gnu_version\n'), ((33, 46, 33, 62), 'biicode.common.settings.version.Version', 'Version', ({(33, 54, 33, 61): '"""4.8.1"""'}, {}), "('4.8.1')", False, 'from biicode.common.settings.version import Version\n'), ((35, 26, 35, 44), 'biicode.client.setups.finders.finders.gnu_version', 'gnu_version', ({(35, 38, 35, 43): '"""gnu"""'}, {}), "('gnu')", False, 'from biicode.client.setups.finders.finders import gnu_version\n'), ((35, 46, 35, 62), 'biicode.common.settings.version.Version', 'Version', ({(35, 54, 35, 61): '"""4.8.1"""'}, {}), "('4.8.1')", False, 'from biicode.common.settings.version import Version\n'), ((40, 39, 40, 53), 'biicode.client.setups.rpi_cross_compiler.find_gnu_arm', 'find_gnu_arm', ({}, {}), '()', False, 'from biicode.client.setups.rpi_cross_compiler import find_gnu_arm\n'), ((44, 20, 44, 49), 'biicode.client.workspace.bii_paths.get_biicode_env_folder_path', 'get_biicode_env_folder_path', ({}, {}), '()', False, 'from biicode.client.workspace.bii_paths import get_biicode_env_folder_path\n')]