Exemplo n.º 1
0
    def test_receive_wrong_cmake_path(self):
        """Wrong CMake Path Write in Current Directory"""

        under_test = VSContext()
        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)
Exemplo n.º 2
0
    def test_context_init(self):
        """Data Converter Init Files"""

        under_test = VSContext()

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

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

        self.assertNotEqual(under_test.cmake, '')
Exemplo n.º 3
0
 def setUp(self):
     self.context = VSContext()
     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))
Exemplo n.º 4
0
    def test_get_cmakelists(self):
        """Get CMakeLists.txt"""

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

        self.assertTrue(under_test)
        self.assertIsInstance(under_test, _io.TextIOWrapper)
        under_test.close()
Exemplo n.º 5
0
    def test_get_vcxproj_data(self):
        """Get VS Project Data"""

        context = VSContext()
        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)
Exemplo n.º 6
0
    def test_create_data(self):
        """Data Converter Create Data"""

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

        under_test = DataConverter()

        context = VSContext()
        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()
def main():  # pragma: no cover
    """
    Define arguments and message to DataConverter()

    """

    usage = "cmake-converter -s <path/to/file.sln> " \
            "[ -h | -s | -p | -i | -d | -v | -w | -j | -a | -pi | -ias ]"
    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(
        '-i', '--indent',
        help='indentation for formatting of out CMakeLists.txt',
        dest='indent'
    )
    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 verbosity of warnings([1..4] default=2).',
        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'
    )
    parser.add_argument(
        '-ias', '--ignore-absent-sources',
        help='do not add absent source files to CMakeLists.txt',
        dest='ignore_absent_sources',
        default=False,
        action='store_true'
    )

    args = parser.parse_args()

    project_context = VSContext()
    # Prepare context
    project_context.additional_code = args.additional

    if args.projects_regexp:
        project_context.projects_regexp = args.projects_regexp

    if args.indent:
        project_context.indent = args.indent

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

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

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

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

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

    if args.ignore_absent_sources:
        message(project_context, 'absent source files will be ignored', 'done')
        project_context.ignore_absent_sources = True

    converter = VSSolutionConverter()
    converter.convert_solution(project_context, os.path.abspath(args.solution))
class TestProjectVariables(unittest.TestCase):
    """
        This file test methods of ProjectVariables class.
    """

    cur_dir = os.path.dirname(os.path.realpath(__file__))
    context = VSContext()
    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()
Exemplo n.º 9
0
class TestProjectFiles(unittest.TestCase):
    """
        This file test methods of ProjectFiles class.
    """

    context = VSContext()
    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)