Example #1
0
    def test_receive_wrong_cmake_path(self):
        """Wrong CMake Path Write in Current Directory"""

        under_test = Context()
        under_test.init(self.vs_project, '/wrong/path/to/cmake')

        # CMakeLists.txt is created in the current directory
        self.assertEqual('CMakeLists.txt', under_test.cmake)
Example #2
0
 def setUp(self):
     cur_dir = os.path.dirname(os.path.realpath(__file__))
     context = Context()
     context.verbose = False
     vs_project = '{}/datatest/foo.vcxproj'.format(cur_dir)
     context.cmake = './'
     converter = DataConverter()
     converter.convert_project(context, vs_project, cur_dir)
Example #3
0
    def test_init_files(self):
        """Data Converter Init Files"""

        under_test = Context()

        self.assertEqual(under_test.cmake, '')
        self.assertEqual(under_test.vcxproj, {})

        under_test.init(self.vs_project, self.cur_dir)

        self.assertNotEqual(under_test.cmake, '')
        self.assertIsNotNone(under_test.vcxproj)

        self.assertTrue('ns' in under_test.vcxproj)
        self.assertTrue('tree' in under_test.vcxproj)
Example #4
0
    def test_get_cmakelists(self):
        """Get CMakeLists.txt"""

        context = Context()
        under_test = get_cmake_lists(context, './')

        self.assertTrue(under_test)
        self.assertIsInstance(under_test, _io.TextIOWrapper)
        under_test.close()
Example #5
0
    def test_create_data(self):
        """Data Converter Create Data"""

        # FIXME: No such file or directory: 'CMakeLists.txt'
        return

        under_test = DataConverter()

        context = Context()
        context.init(self.vs_project, self.cur_dir)

        old_cmake = open('CMakeLists.txt', 'r')

        under_test.convert(context)

        new_cmake = open('CMakeLists.txt', 'r')

        # Assert content is not the same after
        self.assertEqual(old_cmake.read(), new_cmake.read())

        old_cmake.close()
        new_cmake.close()
Example #6
0
    def test_get_vcxproj_data(self):
        """Get VS Project Data"""

        context = Context()
        under_test = get_vcxproj_data(context, self.vs_project)

        self.assertTrue('ns' in under_test)
        self.assertEqual(
            {'ns': 'http://schemas.microsoft.com/developer/msbuild/2003'},
            under_test['ns']
        )
        self.assertTrue('tree' in under_test)
        self.assertIsInstance(under_test['tree'], lxml.etree._ElementTree)
class TestProjectFiles(unittest.TestCase):
    """
        This file test methods of ProjectFiles class.
    """

    context = Context()
    cur_dir = os.path.dirname(os.path.realpath(__file__))

    def setUp(self):
        self.context.verbose = False
        solution_file = '{}/datatest/sln/cpp.sln'.format(self.cur_dir)
        converter = VSSolutionConverter()
        converter.convert_solution(self.context, os.path.abspath(solution_file))

    @unittest.skip("how to test sources from project context?")
    def test_collects_source_files(self):
        """Collects Source Files"""

        self.assertNotEqual(len(self.context.sources), 0)
        self.assertEqual(len(self.context.headers), 0)

    def test_write_source_files(self):
        """Write Source Files"""

        with open('{}/datatest/CMakeLists.txt'.format(self.cur_dir)) as cmake_lists_test:
            content_test = cmake_lists_test.read()
            self.assertTrue('source_group("Sources" FILES ${Sources})' in content_test)

    @unittest.skip("repair test additional code")
    def test_add_additional_code(self):
        """Add Additional CMake Code"""

        # When file is empty, nothing is added
        self.data_test['cmake'] = get_cmake_lists(context, self.cur_dir)
        under_test = ProjectFiles(self.data_test)

        under_test.add_additional_code(context, '')

        under_test.cmake.close()

        cmakelists_test = open('%s/CMakeLists.txt' % self.cur_dir)
        content_test = cmakelists_test.read()

        self.assertEqual('', content_test)
        cmakelists_test.close()

        # When file exist, code is added
        under_test.cmake = get_cmake_lists(context, self.cur_dir)
        under_test.add_additional_code(context, '%s/datatest/additional_code_test.cmake' % self.cur_dir)

        under_test.cmake.close()

        cmakelists_test = open('%s/CMakeLists.txt' % self.cur_dir)
        content_test = cmakelists_test.read()

        self.assertTrue('set(ADD_CODE code)' in content_test)

        cmakelists_test.close()

        # When file does not exist, nothing is added
        under_test.cmake = get_cmake_lists(context, self.cur_dir)
        under_test.add_additional_code(context, 'nofile/additional_code_test.cmake')

        under_test.cmake.close()

        cmakelists_add_test = open('%s/CMakeLists.txt' % self.cur_dir)
        content_add_test = cmakelists_add_test.read()

        self.assertEqual('', content_add_test)

        cmakelists_test.close()

    def test_add_artefacts(self):
        """Add Artefact Target"""

        with open('{}/datatest/CMakeLists.txt'.format(self.cur_dir)) as cmake_lists_test:
            content_test = cmake_lists_test.read()
            self.assertTrue('add_executable(${PROJECT_NAME} ${ALL_FILES})' in content_test)

    @unittest.skip("include_cmake deleted")
    def test_add_include_cmake(self):
        """Add Include CMake File"""

        self.data_test['cmake'] = get_cmake_lists(context, self.cur_dir)
        under_test = ProjectFiles(self.data_test)

        under_test.add_include_cmake('path/to/file.cmake')
        self.data_test['cmake'].close()

        cmakelists_test = open('%s/CMakeLists.txt' % self.cur_dir)
        content_test = cmakelists_test.read()

        self.assertTrue('include("path/to/file.cmake")' in content_test)
Example #8
0
 def __init__(self):
     Context.__init__(self)
Example #9
0
def main():  # pragma: no cover
    """
    Define arguments and message to DataConverter()

    """

    usage = "cmake-converter -s <path/to/file.sln> " \
            "[ -h | -p | -d | -v | -w | -j | -a ]"
    parser = argparse.ArgumentParser(
        usage=usage,
        description=
        'Converts Visual Studio projects in solution (*.sln) to CMakeLists.txt tree'
    )
    parser.add_argument(
        '-s',
        '--solution',
        help='[required] valid solution file. i.e.: ../../my.sln',
        required=True,
        dest='solution')
    parser.add_argument(
        '-p',
        '--projects-filter',
        help=
        'python regexp to filter that projects should be converted from the given solution',
        dest='projects_regexp')
    parser.add_argument('-d',
                        '--dry-run',
                        help='run converter without touching files.',
                        dest='dry',
                        action='store_true')
    parser.add_argument('-v',
                        '--verbose-mode',
                        help='run converter with more messages in log.',
                        dest='verbose',
                        action='store_true')
    parser.add_argument(
        '-w',
        '--warning-level',
        help='run converter with given verbocity of warnings([1..4]default=3).',
        dest='warn',
    )
    parser.add_argument(
        '-j',
        '--jobs',
        help='run converter using given number of processes.',
        dest='jobs',
    )
    parser.add_argument(
        '-a',
        '--additional',
        help=
        '[experimental] import cmake code from file.cmake to your final CMakeLists.txt',
        dest='additional')

    parser.add_argument(
        '-pi',
        '--private-include-directories',
        help='use PRIVATE specifier for target_include_directories',
        dest='private_includes',
        default=False,
        action='store_true')

    args = parser.parse_args()

    root_context = Context()
    # Prepare context
    root_context.additional_code = args.additional

    if args.projects_regexp:
        root_context.projects_regexp = args.projects_regexp

    if args.dry:
        root_context.dry = True
        message(root_context, 'Converter runs in dry mode', 'done')

    if args.verbose:
        root_context.verbose = True
        message(root_context, 'Converter runs in verbose mode', 'done')

    if args.jobs:
        root_context.jobs = int(args.jobs)
    message(root_context, 'processes count = {}'.format(root_context.jobs),
            'done')

    if args.warn:
        root_context.warn_level = int(args.warn)
    message(root_context,
            'warnings level = {}'.format(root_context.warn_level), 'done')

    if args.private_includes:
        message(root_context, 'include directories will be PRIVATE', 'done')
        root_context.private_include_directories = True

    converter = VSSolutionConverter()
    converter.convert_solution(root_context, os.path.abspath(args.solution))
Example #10
0
 def setUp(self):
     context = Context()
     context.verbose = False
     solution_file = '{}/datatest/sln/cpp.sln'.format(self.cur_dir)
     converter = VSSolutionConverter()
     converter.convert_solution(context, os.path.abspath(solution_file))
Example #11
0
class TestProjectVariables(unittest.TestCase):
    """
        This file test methods of ProjectVariables class.
    """

    cur_dir = os.path.dirname(os.path.realpath(__file__))
    context = Context()
    vcxproj_data_test = get_vcxproj_data(context,
                                         '%s/datatest/foo.vcxproj' % cur_dir)
    cmake_lists_test = get_cmake_lists(context, cur_dir)

    data_test = {
        'cmake': cmake_lists_test,
        'cmakeoutput': None,
        'vcxproj': vcxproj_data_test,
        'include': None,
        'additional': None,
    }

    def test_init_project_variables(self):
        """Initialize Project Variables"""

        under_test = VCXProjectVariables(self.data_test)

        self.assertTrue(under_test.tree)
        self.assertTrue(under_test.ns)
        self.assertTrue(under_test.cmake)
        self.assertFalse(under_test.output)
        self.assertIsNotNone(under_test.cmake_outputs)

    def test_add_project_variables(self):
        """Add Project Variables"""

        self.data_test['cmake'] = get_cmake_lists(context, self.cur_dir)
        under_test = VCXProjectVariables(self.data_test)

        under_test.add_project_variables()

        self.data_test['cmake'].close()

        cmakelists_test = open('%s/CMakeLists.txt' % self.cur_dir)
        content_test = cmakelists_test.read()

        self.assertTrue('set(PROJECT_NAME core)' in content_test)

        cmakelists_test.close()

    def test_add_outputs_variables(self):
        """Add Outputs Variables"""

        # TODO If NO output is given
        # self.data_test['cmake'] = get_cmake_lists(context, self.cur_dir)
        under_test = VCXProjectVariables(self.data_test)
        #
        # under_test.add_project_variables()
        # under_test.add_outputs_variables()
        #
        # self.data_test['cmake'].close()
        #
        # cmakelists_test = open('%s/CMakeLists.txt' % self.cur_dir)
        # content_test = cmakelists_test.read()
        #
        # self.assertTrue('OUTPUT_DEBUG ../../../build/vc2017_x64d/bin/', content_test)
        # self.assertTrue('OUTPUT_REL ../../../build/vc2017_x64/bin/' in content_test)
        #
        # cmakelists_test.close()

        # If output is given
        under_test.output = '../output_binaries'
        under_test.cmake = get_cmake_lists(context, self.cur_dir)
        under_test.add_outputs_variables()

        under_test.cmake.close()

        cmakelists_test = open('%s/CMakeLists.txt' % self.cur_dir)
        content_test = cmakelists_test.read()

        self.assertTrue('OUTPUT_DEBUG ../output_binaries/${CMAKE_BUILD_TYPE}',
                        content_test)
        self.assertTrue('OUTPUT_REL ../output_binaries/${CMAKE_BUILD_TYPE}' in
                        content_test)

        cmakelists_test.close()

    def test_add_cmake_project(self):
        """Add CMake Project"""

        self.data_test['cmake'] = get_cmake_lists(context, self.cur_dir)
        under_test = VCXProjectVariables(self.data_test)

        # Case CXX languages
        under_test.add_cmake_project(['cpp'])

        self.data_test['cmake'].close()

        cmakelists_test = open('%s/CMakeLists.txt' % self.cur_dir, 'r')
        content_test = cmakelists_test.read()

        self.assertTrue('project(${PROJECT_NAME} CXX)' in content_test)

        cmakelists_test.close()

        # Case C languages
        under_test.cmake = get_cmake_lists(context, self.cur_dir)
        under_test.add_cmake_project(['c'])

        under_test.cmake.close()

        cmakelists_test = open('%s/CMakeLists.txt' % self.cur_dir, 'r')
        content_test = cmakelists_test.read()

        self.assertTrue('project(${PROJECT_NAME} C)' in content_test)

        cmakelists_test.close()

    def test_add_artefact_target_outputs(self):
        """Add Artefact Target Outputs"""

        self.data_test['cmake'] = get_cmake_lists(context, self.cur_dir)
        under_test = VCXProjectVariables(self.data_test)

        under_test.add_cmake_output_directories()

        self.data_test['cmake'].close()

        cmakelists_test = open('%s/CMakeLists.txt' % self.cur_dir, 'r')
        content_test = cmakelists_test.read()

        self.assertTrue(
            'set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_DIR}/${OUTPUT_DEBUG}")'
            in content_test)
        self.assertTrue(
            'set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_DIR}/${OUTPUT_DEBUG}")'
            in content_test)
        self.assertTrue(
            'set(CMAKE_EXECUTABLE_OUTPUT_DIRECTORY "${PROJECT_DIR}/${OUTPUT_DEBUG}'
            in content_test)

        self.assertTrue(
            'set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_DIR}/${OUTPUT_RELEASE}")'
            in content_test)
        self.assertTrue(
            'set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_DIR}/${OUTPUT_RELEASE}")'
            in content_test)
        self.assertTrue(
            'set(CMAKE_EXECUTABLE_OUTPUT_DIRECTORY "${PROJECT_DIR}/${OUTPUT_RELEASE}'
            in content_test)

        cmakelists_test.close()