Ejemplo n.º 1
0
def compose_switched_products(config_paths, project_dir, mutated_project_dir,
                              custom_ant):
    lib_paths = get_dependency_lib_dirs(project_dir)
    for config_path in config_paths:
        variant_dir = get_variant_dir_from_config_path(project_dir,
                                                       config_path)
        corrupt_file = join_path(variant_dir, "corrupted_compile.log")
        if not is_path_exist(variant_dir):
            variant_dir = VariantComposer.compose_by_config(
                project_dir, config_path)
            try:
                compile_log = AntManager.compile_source_classes(
                    lib_paths=lib_paths, variant_dir=variant_dir)
            except RuntimeError:
                print("********\n__FAILED__", config_path, "\n********\n")
                touch_file(corrupt_file)
                # delete_dir(variant_dir)
                continue
            else:
                TestManager.generate_junit_test_cases(lib_paths=lib_paths,
                                                      variant_dir=variant_dir)
                TestManager.run_batch_junit_test_cases(variant_dir,
                                                       lib_paths=lib_paths,
                                                       halt_on_failure=True,
                                                       halt_on_error=True,
                                                       custom_ant=custom_ant)
        elif is_path_exist(corrupt_file):
            print("********\n\n__SKIP__FAILED__", config_path, "\n********\n")
            continue

        mutated_variant_dir = get_variant_dir_from_config_path(
            mutated_project_dir, config_path)
        if not is_path_exist(mutated_variant_dir):
            mutated_variant_dir = VariantComposer.compose_by_config(
                mutated_project_dir, config_path)
            TestManager.link_generated_junit_test_cases(
                variant_dir, mutated_variant_dir)
            try:
                are_all_tests_passed = TestManager.run_batch_junit_test_cases(
                    mutated_variant_dir,
                    lib_paths=lib_paths,
                    halt_on_failure=False,
                    halt_on_error=True,
                    custom_ant=custom_ant)
            except Exception as e:
                continue
            if are_all_tests_passed:
                file_name = SW_PASSED_TEST_FLAG_FILE_NAME
            else:
                file_name = SW_FAILED_TEST_FLAG_FILE_NAME
            test_flag_file = join_path(mutated_variant_dir, file_name)
            touch_file(test_flag_file)
Ejemplo n.º 2
0
def summarize_involving_features(project_dir, mutated_project_dir):
    variant_dirs = get_all_variant_dirs(mutated_project_dir, sort=True)
    features = get_ordered_feature_list(project_dir)
    buggy_features = get_mutated_features(mutated_project_dir)
    involving_features = set()
    for variant_dir in variant_dirs:
        variant_name = get_file_name(variant_dir)
        if is_path_exist(join_path(variant_dir,
                                   SW_PASSED_TEST_FLAG_FILE_NAME)):
            variant_postfix = variant_name.rsplit("_", 1)[1]
            if not variant_postfix.startswith(SWITCHED_VARIANT_NAME_POSTFIX):
                raise Exception(
                    "Invalid variant was considered as switched {}".format(
                        variant_dir))
            feature_index = int(
                variant_postfix[len(SWITCHED_VARIANT_NAME_POSTFIX):]) - 1
            related_feature = features[feature_index]
            involving_features.add(related_feature)

    are_buggy_features_contained = buggy_features.issubset(involving_features)
    involving_features = list(buggy_features | involving_features)
    print("-" * 20)
    print("MUTATED_PROJECT_DIR:", mutated_project_dir)
    print("MUTANT NAME:", get_file_name(mutated_project_dir))
    print("BUGGY FEATURES:", json.dumps(list(buggy_features)))
    print("INVOLVING FEATURES:", json.dumps(involving_features))
    print("SIZE: {}".format(len(involving_features)))
    print("CONTAINED |{}|".format(are_buggy_features_contained))
    print("-" * 20)
Ejemplo n.º 3
0
def get_mutated_project_dirs(project_dir, include_temp_project_dirs=False, sort=False):
    mutated_projects_dir = get_mutated_projects_dir(project_dir)
    mutated_project_dirs = list_dir(mutated_projects_dir, full_path=True)
    if include_temp_project_dirs:
        temp_mutated_project_dirs = list_dir(join_path(mutated_projects_dir, TEMP_PROJECT_FOLDER_NAME), full_path=True)
        mutated_project_dirs.extend(temp_mutated_project_dirs)
    if sort:
        mutated_project_dirs = natural_sort(mutated_project_dirs)
    return mutated_project_dirs
Ejemplo n.º 4
0
def inject_mutants(project_dir, mutant_path_tuples):
    logger.info(f"Injecting mutants to features [{get_file_name_without_ext(project_dir)}]")
    mutated_projects_dir = get_mutated_projects_dir(project_dir)
    features_dir = get_feature_source_code_dir(project_dir)
    mutated_project_dirs = []
    for tuple_index, mutant_path_tuple in enumerate(mutant_path_tuples):
        # FORMAT: ~/InputPreparation/projects/GPL-Test/mutation_result/DirectedWithEdges.GPL.Edge/traditional_mutants/Vertex_getOtherVertex(Vertex)/ROR_1/Edge.java'
        current_project_name = "_MultipleBugs_.NOB_{}.ID_{}".format(len(mutant_path_tuple), tuple_index + 1)
        current_mutated_project_dir = join_path(mutated_projects_dir, current_project_name)
        current_mutated_features_dir = get_feature_source_code_dir(project_dir=current_mutated_project_dir)
        final_mutation_log = ""
        for mutant_path in mutant_path_tuple:
            mutant_path_parts = mutant_path.rsplit("/", 5)
            full_class_name = mutant_path_parts[1]
            package, class_name = full_class_name.rsplit(".", 1)
            class_name += ".java"
            operator_index = mutant_path_parts[4]
            bug_name = f"{full_class_name}.{operator_index}"
            mutation_log_file = join_path(*mutant_path_parts[:-3], "mutation_logs", f"{bug_name}.log")
            with open(mutation_log_file) as f:
                # append more line hint to final log file
                # line log:  Base.EmailSystem.Util.AOIU_1:32:void_enterElevator(Person):weight += 1 => weight = 1
                mutated_line_hints = f.readlines()[0].split(":", 2)
                mutated_line_hints[0] = bug_name
                joined_line_hint = ":".join(mutated_line_hints)
                final_mutation_log += joined_line_hint

            for feature_name in list_dir(features_dir):
                current_feature_dir = join_path(features_dir, feature_name)
                current_mutated_feature_dir = join_path(current_mutated_features_dir, feature_name)

                if feature_name == package.split(".")[0]:
                    if is_path_exist(current_mutated_feature_dir):
                        # feature dir already copied, keep this and create symlink from mutated java file to this dir
                        if not is_symlink(current_mutated_feature_dir):
                            continue
                        unlink(current_mutated_feature_dir)
                    copy_dir(current_feature_dir, current_mutated_feature_dir)

                else:
                    if not is_path_exist(current_mutated_feature_dir):
                        create_symlink(current_feature_dir, current_mutated_feature_dir)

            mutant_path_dst = join_path(current_mutated_features_dir, *package.split("."), class_name)
            create_symlink(mutant_path, mutant_path_dst)

        # write mutated line position to final mutated log for tracing
        mutated_project_mutation_log_file = join_path(current_mutated_project_dir,
                                                      f"{current_project_name}.mutant.log")
        with open(mutated_project_mutation_log_file, "w+") as f:
            f.write(final_mutation_log)

        mutated_project_dirs.append(current_mutated_project_dir)
    return mutated_project_dirs
Ejemplo n.º 5
0
def compose_switched_configs(config_path, variant_name):
    feature_selections = get_feature_selections(config_path)
    switched_feature_selections_container = get_single_switched_feature_selections(
        feature_selections)
    configs_dir = get_outer_dir(config_path)
    switched_config_path_container = []
    for i, fs in enumerate(switched_feature_selections_container):
        switched_config_file_name = f"{variant_name}_{SWITCHED_VARIANT_NAME_POSTFIX}{i + 1}.features"
        switched_config_file_path = join_path(configs_dir,
                                              switched_config_file_name)
        if not is_path_exist(switched_config_file_path):
            with open(switched_config_file_path, "w+") as output_file:
                for f_index, name in enumerate(fs):
                    line = name if fs[name] else f"#{name}"
                    if f_index < len(fs) - 1:
                        line += "\n"
                    output_file.write(line)
        switched_config_path_container.append(switched_config_file_path)
    return switched_config_path_container
Ejemplo n.º 6
0
def generate_configs(project_dir, feature_order_file_path, sampling_output_file_path):
    logger.info(
        f"Generating configurations from sampling csv file [{get_file_name_without_ext(sampling_output_file_path)}]")
    config_dir = get_model_configs_dir(project_dir)
    sampling_file_name = get_file_name_without_ext(sampling_output_file_path).replace(".", "_")
    ordered_features = ModelManager.read_feature_order_file(feature_order_file_path)
    configurations = get_configurations_from_sampling_file(ordered_features, sampling_output_file_path)
    config_output_paths = []
    for i, config in enumerate(configurations[:]):
        config_name = generate_config_name(sampling_file_name, i)
        config_output_path = join_path(config_dir, f"{config_name}.features")
        success = write_configuration_to_file(config, config_output_path)
        if success:
            config_output_paths.append(config_output_path)
        else:
            configurations.remove(config)
    configs_report_file_path = get_model_configs_report_path(project_dir)
    generate_configs_report(ordered_features, config_output_paths, configurations, configs_report_file_path)
    return configs_report_file_path, config_output_paths
Ejemplo n.º 7
0
def get_mutated_project_dir(project_dir, mutated_project_name):
    mutated_projects_dir = get_mutated_projects_dir(project_dir)
    current_mutated_project_dir = join_path(mutated_projects_dir, mutated_project_name)
    if not is_path_exist(current_mutated_project_dir):
        logger.fatal("Can't find mutated project {} from [{}]".format(mutated_project_name, project_dir))
    return current_mutated_project_dir