Exemple #1
0
def main():  # type: () -> None
    parser = argparse.ArgumentParser(
        description='Scan the required build tests')
    parser.add_argument('test_type',
                        choices=TEST_LABELS.keys(),
                        help='Scan test type')
    parser.add_argument('paths', nargs='+', help='One or more app paths')
    parser.add_argument('-b',
                        '--build-system',
                        choices=BUILD_SYSTEMS.keys(),
                        default=BUILD_SYSTEM_CMAKE)
    parser.add_argument('-c',
                        '--ci-config-file',
                        required=True,
                        help='gitlab ci config target-test file')
    parser.add_argument('-o',
                        '--output-path',
                        required=True,
                        help='output path of the scan result')
    parser.add_argument(
        '--exclude',
        nargs='*',
        help='Ignore specified directory. Can be used multiple times.')
    parser.add_argument(
        '--extra_test_dirs',
        nargs='*',
        help='Additional directories to preserve artifacts for local tests')
    parser.add_argument(
        '--preserve_all',
        action='store_true',
        help='add this flag to preserve artifacts for all apps')
    parser.add_argument('--build-all',
                        action='store_true',
                        help='add this flag to build all apps')

    args = parser.parse_args()
    build_test_case_apps, build_standalone_apps = _judge_build_or_not(
        args.test_type, args.build_all)

    if not os.path.exists(args.output_path):
        try:
            os.makedirs(args.output_path)
        except OSError as e:
            if e.errno != errno.EEXIST:
                raise e

    SUPPORTED_TARGETS.extend(PREVIEW_TARGETS)

    if (not build_standalone_apps) and (not build_test_case_apps):
        for target in SUPPORTED_TARGETS:
            output_json([], target, args.build_system, args.output_path)
            SystemExit(0)

    idf_path = str(os.getenv('IDF_PATH'))
    paths = set([
        os.path.join(idf_path, path) if not os.path.isabs(path) else path
        for path in args.paths
    ])

    test_cases = []
    for path in paths:
        if args.test_type == 'example_test':
            assign = _ExampleAssignTest(path, args.ci_config_file)
        elif args.test_type in ['test_apps', 'component_ut']:
            assign = _TestAppsAssignTest(path, args.ci_config_file)
        else:
            raise SystemExit(1)  # which is impossible
        test_cases.extend(assign.search_cases())
    '''
    {
        <target>: {
            'test_case_apps': [<app_dir>],   # which is used in target tests
            'standalone_apps': [<app_dir>],  # which is not
        },
        ...
    }
    '''
    scan_info_dict = defaultdict(dict)  # type: dict[str, dict]
    # store the test cases dir, exclude these folders when scan for standalone apps
    default_exclude = args.exclude if args.exclude else []

    build_system = args.build_system.lower()
    build_system_class = BUILD_SYSTEMS[build_system]

    for target in SUPPORTED_TARGETS:
        exclude_apps = deepcopy(default_exclude)

        if build_test_case_apps:
            scan_info_dict[target]['test_case_apps'] = set()
            test_dirs = args.extra_test_dirs if args.extra_test_dirs else []
            for case in test_cases:
                if case.case_info['target'].lower() == target.lower():
                    test_dirs.append(case.case_info['app_dir'])
            for app_dir in test_dirs:
                app_dir = os.path.join(
                    idf_path,
                    app_dir) if not os.path.isabs(app_dir) else app_dir
                _apps = find_apps(build_system_class, app_dir, True,
                                  exclude_apps, target.lower())
                if _apps:
                    scan_info_dict[target]['test_case_apps'].update(_apps)
                    exclude_apps.extend(_apps)
        else:
            scan_info_dict[target]['test_case_apps'] = set()

        if build_standalone_apps:
            scan_info_dict[target]['standalone_apps'] = set()
            for path in paths:
                scan_info_dict[target]['standalone_apps'].update(
                    find_apps(build_system_class, path, True, exclude_apps,
                              target.lower()))
        else:
            scan_info_dict[target]['standalone_apps'] = set()
    parser.add_argument('test_case_paths',
                        nargs='+',
                        help='test case folder or file')
    parser.add_argument('-c', '--config', help='gitlab ci config file')
    parser.add_argument('-o', '--output', help='output path of config files')
    parser.add_argument('--pipeline_id',
                        '-p',
                        type=int,
                        default=None,
                        help='pipeline_id')
    parser.add_argument(
        '--test-case-file-pattern',
        help='file name pattern used to find Python test case files')
    args = parser.parse_args()

    SUPPORTED_TARGETS.extend(PREVIEW_TARGETS)

    test_case_paths = [
        os.path.join(IDF_PATH_FROM_ENV, path)
        if not os.path.isabs(path) else path for path in args.test_case_paths
    ]
    args_list = [test_case_paths, args.config]
    if args.case_group == 'example_test':
        assigner = ExampleAssignTest(*args_list)
    elif args.case_group == 'custom_test':
        assigner = TestAppsAssignTest(*args_list)
    elif args.case_group == 'unit_test':
        assigner = UnitTestAssignTest(*args_list)
    elif args.case_group == 'component_ut':
        assigner = ComponentUTAssignTest(*args_list)
    else:
Exemple #3
0
def main():
    parser = argparse.ArgumentParser(
        description='Scan the required build tests')
    parser.add_argument('test_type',
                        choices=TEST_LABELS.keys(),
                        help='Scan test type')
    parser.add_argument('paths', nargs='+', help='One or more app paths')
    parser.add_argument('-b',
                        '--build-system',
                        choices=BUILD_SYSTEMS.keys(),
                        default=BUILD_SYSTEM_CMAKE)
    parser.add_argument('-c',
                        '--ci-config-file',
                        required=True,
                        help="gitlab ci config target-test file")
    parser.add_argument('-o',
                        '--output-path',
                        required=True,
                        help="output path of the scan result")
    parser.add_argument(
        "--exclude",
        nargs="*",
        help='Ignore specified directory. Can be used multiple times.')
    parser.add_argument(
        '--preserve',
        action="store_true",
        help='add this flag to preserve artifacts for all apps')
    parser.add_argument('--build-all',
                        action="store_true",
                        help='add this flag to build all apps')

    args = parser.parse_args()
    build_test_case_apps, build_standalone_apps = _judge_build_or_not(
        args.test_type, args.build_all)

    if not os.path.exists(args.output_path):
        try:
            os.makedirs(args.output_path)
        except OSError as e:
            if e.errno != errno.EEXIST:
                raise e

    SUPPORTED_TARGETS.extend(PREVIEW_TARGETS)

    if (not build_standalone_apps) and (not build_test_case_apps):
        for target in SUPPORTED_TARGETS:
            output_json([], target, args.build_system, args.output_path)
            SystemExit(0)

    paths = set([
        os.path.join(os.getenv('IDF_PATH'), path)
        if not os.path.isabs(path) else path for path in args.paths
    ])

    test_cases = []
    for path in paths:
        if args.test_type == 'example_test':
            assign = ExampleAssignTest(path, args.ci_config_file)
        elif args.test_type in ['test_apps', 'component_ut']:
            assign = TestAppsAssignTest(path, args.ci_config_file)
        else:
            raise SystemExit(1)  # which is impossible

        test_cases.extend(assign.search_cases())
    '''
    {
        <target>: {
            'test_case_apps': [<app_dir>],   # which is used in target tests
            'standalone_apps': [<app_dir>],  # which is not
        },
        ...
    }
    '''
    scan_info_dict = defaultdict(dict)
    # store the test cases dir, exclude these folders when scan for standalone apps
    default_exclude = args.exclude if args.exclude else []
    exclude_apps = default_exclude

    build_system = args.build_system.lower()
    build_system_class = BUILD_SYSTEMS[build_system]

    if build_test_case_apps:
        for target in SUPPORTED_TARGETS:
            target_dict = scan_info_dict[target]
            test_case_apps = target_dict['test_case_apps'] = set()
            for case in test_cases:
                app_dir = case.case_info['app_dir']
                app_target = case.case_info['target']
                if app_target.lower() != target.lower():
                    continue
                test_case_apps.update(
                    find_apps(build_system_class, app_dir, True,
                              default_exclude, target.lower()))
                exclude_apps.append(app_dir)
    else:
        for target in SUPPORTED_TARGETS:
            scan_info_dict[target]['test_case_apps'] = set()

    if build_standalone_apps:
        for target in SUPPORTED_TARGETS:
            target_dict = scan_info_dict[target]
            standalone_apps = target_dict['standalone_apps'] = set()
            for path in paths:
                standalone_apps.update(
                    find_apps(build_system_class, path, True, exclude_apps,
                              target.lower()))
    else:
        for target in SUPPORTED_TARGETS:
            scan_info_dict[target]['standalone_apps'] = set()

    test_case_apps_preserve_default = True if build_system == 'cmake' else False
    for target in SUPPORTED_TARGETS:
        apps = []
        for app_dir in scan_info_dict[target]['test_case_apps']:
            apps.append({
                'app_dir':
                app_dir,
                'build_system':
                args.build_system,
                'target':
                target,
                'preserve':
                args.preserve or test_case_apps_preserve_default
            })
        for app_dir in scan_info_dict[target]['standalone_apps']:
            apps.append({
                'app_dir': app_dir,
                'build_system': args.build_system,
                'target': target,
                'preserve': args.preserve
            })
        output_path = os.path.join(
            args.output_path, 'scan_{}_{}.json'.format(target.lower(),
                                                       build_system))
        with open(output_path, 'w') as fw:
            fw.writelines([json.dumps(app) + '\n' for app in apps])