Esempio n. 1
0
def init_control_center():
    cc_assets_path = os.path.join(
        os.path.realpath(os.path.split(__file__)[0] + '/..'), 'control_center',
        'assets')
    logger.debug('Copying Control Center assets from %s to %s' %
                 (cc_assets_path, get_core_args().workdir))
    copy_tree(cc_assets_path, get_core_args().workdir)
    if os.path.exists(os.path.join(PathManager.get_module_dir(), 'targets')):
        logger.debug('Looking for CC files in module directory.')
        targets_dir = os.path.join(PathManager.get_module_dir(), 'targets')
    else:
        logger.debug('Looking for CC files in package directory.')
        targets_dir = os.path.join(Settings.PACKAGE_ROOT, 'mattapi', 'targets')

    exclude_dirs = {'__pycache__'}
    for path, dirs, files in PathManager.sorted_walk(targets_dir):
        [dirs.remove(d) for d in list(dirs) if d in exclude_dirs]
        for target in dirs:
            src = os.path.join(targets_dir, target, 'icon.png')
            dest = os.path.join(get_core_args().workdir, 'images',
                                '%s.png' % target)
            try:
                shutil.copyfile(src, dest)
            except FileNotFoundError:
                logger.warning('Could not find icon file for target: %s' %
                               target)
        break
    create_target_json()
Esempio n. 2
0
def scan_all_tests():
    tests_directory = PathManager.get_tests_dir()
    logger.debug('Path %s found. Checking content ...', tests_directory)

    test_list = {}
    rootdir = tests_directory.rstrip(os.sep)
    start = rootdir.rfind(os.sep) + 1
    exclude_dirs = {'images', '.pytest_cache', '__pycache__'}
    exclude_files = {'__init__.py', 'pytest.ini', '.DS_Store'}

    for path, dirs, files in PathManager.sorted_walk(rootdir):
        [dirs.remove(d) for d in list(dirs) if d in exclude_dirs]
        [files.remove(d) for d in list(files) if d in exclude_files]

        folders = path[start:].split(os.sep)
        subdir = dict.fromkeys(files)
        parent = reduce(dict.get, folders[:-1], test_list)
        parent[folders[-1]] = subdir

        if len(files) > 0:
            if os.path.isdir(path):
                my_plugin = TestCollector()
                pytest.main(['--collect-only', '-p', 'no:terminal', path],
                            plugins=[my_plugin])
                for module in my_plugin.get_collected_items():
                    try:
                        module_path = str(module.fspath)
                        module_name = os.path.basename(module_path)
                        temp = module_path.split(
                            '%stests%s' %
                            (os.sep, os.sep))[1].split(module_name)[0]
                        package = os.path.join('tests', temp)
                        current_test = module.own_markers[0].kwargs
                        test_object = {
                            'name': module_name,
                            'module': module_path,
                            'description': current_test.get('description'),
                            'package': package
                        }
                        if not current_test.get('values'):
                            pass
                        else:
                            for value in current_test.get('values').kwargs:
                                test_object[value] = current_test.get(
                                    'values').kwargs[value]
                        subdir[module_name] = test_object

                    except TypeError as e:
                        logger.warning('Error in test - %s: %s' %
                                       (module, e.message))
                    except AttributeError:
                        logger.warning('[%s] is not a test file. Skipping...',
                                       module)
    return test_list
Esempio n. 3
0
def collect_tests():
    """Collects tests based on include/exclude criteria and selected target."""
    target = core_args.target
    test_list = []

    include = core_args.test
    exclude = core_args.exclude
    if os.path.isfile(include):
        with open(include, 'r') as f:
            for line in f:
                test_list.append(line.rstrip('\n'))
        f.close()
    else:
        tests_dir = os.path.join(PathManager.get_tests_dir(), target)
        if not os.path.exists(tests_dir):
            path_warning(tests_dir)
            return test_list

        logger.debug('Path %s found. Checking content ...', tests_dir)
        for dir_path, sub_dirs, all_files in PathManager.sorted_walk(tests_dir):
            for current_file in all_files:
                directory = '%s%s%s' % (os.sep, core_args.directory, os.sep)
                include_params = [include]
                exclude_params = [exclude]
                if ',' in include:
                    include_params = include.split(',')
                if ',' in exclude:
                    exclude_params = exclude.split(',')
                current_full_path = os.path.join(dir_path, current_file)
                if current_file.endswith('.py') and not current_file.startswith('__'):
                    if include is '' and exclude is '' and directory is '':
                        if not current_full_path in test_list:
                            test_list.append(current_full_path)
                    else:
                        if core_args.directory is '' or directory in current_full_path:
                            for include_param in include_params:
                                if include_param is '' or include_param in current_full_path:
                                    for exclude_param in exclude_params:
                                        if exclude_param is '':
                                            if not current_full_path in test_list:
                                                test_list.append(current_full_path)
                                        else:
                                            if exclude_param not in current_full_path:
                                                if not current_full_path in test_list:
                                                    test_list.append(current_full_path)
        if len(test_list) == 0:
            logger.error('\'%s\' does not contain tests based on your search criteria. Exiting program.' % tests_dir)
        else:
            logger.debug('List of all tests found: [%s]' % ', '.join(map(str, test_list)))

    return test_list