def read_coverage_file(mutated_project_dir): variants_dir = get_variants_dir(mutated_project_dir) variants_list = os.listdir(variants_dir) data = {} for variant in variants_list: variant_dir = get_variant_dir(mutated_project_dir, variant) test_coverage_dir = get_test_coverage_dir(variant_dir) coverage_file = join_path(test_coverage_dir, SPECTRUM_FAILED_COVERAGE_FILE_NAME) if os.path.isfile(coverage_file): data[variant] = [] try: tree = ET.parse(coverage_file) root = tree.getroot() project = root.find("project") for package in project: for file in package: for line in file: id = line.get('featureClass') + "." + line.get('featureLineNum') if id not in data[variant] and int(line.get('count')) != 0: data[variant].append(id) except: logging.info("Exception when parsing %s", coverage_file) return data
def run_junit_test_cases_with_coverage_on_project(project_dir, custom_ant=None): variants_dir = get_variants_dir(project_dir) lib_paths = get_dependency_lib_dirs(project_dir) for variant_dir in list_dir(variants_dir, full_path=True): run_junit_test_cases_with_coverage(variant_dir=variant_dir, lib_paths=lib_paths, custom_ant=custom_ant)
def statement_coverage_of_variants(project_dir, spectrum_coverage_prefix=""): stm_coverage_variants = {} variants_dir = get_variants_dir(project_dir) for variant in list_dir(variants_dir): variant_dir = join_path(variants_dir, variant) testing_coverage = statement_coverage(variant_dir, spectrum_coverage_prefix) stm_coverage_variants[variant] = testing_coverage return stm_coverage_variants
def get_failed_variant_dirs_from_config_report(mutated_project_dir): configs_report_file_path = get_model_configs_report_path( mutated_project_dir) variants_dir = get_variants_dir(mutated_project_dir) variant_dir_dict = { get_file_name(vd): vd for vd in list_dir(variants_dir, full_path=True) } failed_variant_dirs = [] with open(configs_report_file_path, "r") as report_csv: reader = csv.reader(report_csv) next(reader) for i, row in enumerate(reader): test_passed = row[-1] == "__PASSED__" current_variant_name = row[0] if not test_passed: failed_variant_dirs.append( variant_dir_dict.get(current_variant_name)) return failed_variant_dirs
def find_SPCs(mutated_project_dir, filtering_coverage_rate): start_time = time.time() spc_log_file_path = get_spc_log_file_path(mutated_project_dir, filtering_coverage_rate) if is_path_exist(spc_log_file_path): logger.info(f"Used Old SPC log file [{spc_log_file_path}]") return spc_log_file_path, 0 config_report_path = get_model_configs_report_path(mutated_project_dir) variants_dir = get_variants_dir(mutated_project_dir) variants_testing_coverage = statement_coverage_of_variants( mutated_project_dir) feature_names, variant_names, passed_configs, failed_configs = load_configs( config_report_path, variants_testing_coverage, filtering_coverage_rate) spc_log_file_path = detect_SPCs(feature_names, passed_configs, failed_configs, variant_names, variants_dir, spc_log_file_path) #logging.info("[Runtime] SPC runtime %s: %s", mutated_project_dir, time.time() - start_time) spc_runtime = time.time() - start_time return spc_log_file_path, spc_runtime
def is_var_bug(mutated_project_dir, filter_coverage, spectrum_coverage_prefix=""): num_of_failing_variants = 0 num_of_passing_variants = 0 variants_dir = get_variants_dir(mutated_project_dir) variants_list = list_dir(variants_dir) for variant in variants_list: variant_dir = get_variant_dir(mutated_project_dir, variant) test_coverage_dir = get_test_coverage_dir(variant_dir) stm_coverage = statement_coverage(variant_dir, spectrum_coverage_prefix) spectrum_failed_file = get_spectrum_failed_coverage_file_name_with_version( spectrum_coverage_prefix) failed_file = join_path(test_coverage_dir, spectrum_failed_file) if os.path.isfile(failed_file): num_of_failing_variants += 1 elif stm_coverage >= filter_coverage: num_of_passing_variants += 1 if num_of_failing_variants >= 1 and num_of_passing_variants >= 1: return 1 return 0
def run_batch_junit_test_cases_on_project(project_dir, custom_ant=None, custom_variant_dirs=None): if not custom_variant_dirs: variants_dir = get_variants_dir(project_dir) custom_variant_dirs = list_dir(variants_dir, full_path=True) lib_paths = get_dependency_lib_dirs(project_dir) for variant_dir in custom_variant_dirs: are_all_tests_passed = run_batch_junit_test_cases( variant_dir=variant_dir, lib_paths=lib_paths, custom_ant=custom_ant) if are_all_tests_passed is True: file_name = PASSED_TEST_FLAG_FILE_NAME elif are_all_tests_passed is False: file_name = FAILED_TEST_FLAG_FILE_NAME else: logger.warning( f"Invalid are_all_tests_passed value {are_all_tests_passed} on variant {variant_dir}" ) file_name = FAILED_TEST_FLAG_FILE_NAME test_flag_file = join_path(variant_dir, file_name) touch_file(test_flag_file)
def was_variants_composed(project_dir): variants_dir = get_variants_dir(project_dir) return len(variants_dir) > 0
def features_ranking_multiple_bugs(buggy_statements, mutated_project_dir, search_spaces, filter_coverage_rate, spectrum_expressions, spectrum_coverage_prefix=""): statements_sets = {} statements_sets[SS_STMS_IN_F_PRODUCTS] = get_set_of_stms( search_spaces[SS_STMS_IN_F_PRODUCTS]) statements_sets[SS_SLICING] = get_set_of_stms(search_spaces[SS_SLICING]) total_variants = 0 variants_testing_coverage = {} if filter_coverage_rate > 0: variants_testing_coverage = statement_coverage_of_variants( mutated_project_dir, spectrum_coverage_prefix) failing_variants = get_failing_variants(mutated_project_dir) features_info = {SS_STMS_IN_F_PRODUCTS: {}, SS_SLICING: {}} variants_dir = get_variants_dir(mutated_project_dir) for variant in list_dir(variants_dir): if variant in failing_variants or ( filter_coverage_rate == 0 or (variants_testing_coverage[variant] >= filter_coverage_rate)): total_variants += 1 variant_dir = get_variant_dir(mutated_project_dir, variant) features_info = get_coverage_infor_of_variants( variant, variant_dir, failing_variants, features_info, statements_sets, spectrum_coverage_prefix) total_passes = total_variants - len(failing_variants) total_fails = len(failing_variants) # there are no passing variants with test coverage > threshold if (total_passes == 0): return -2, -2, -2 # there are no failing variants if (total_fails == 0): return -2, -2, -2 feature_based_rank = {} for spectrum_expression in spectrum_expressions: features_info[ SS_STMS_IN_F_PRODUCTS] = features_suspiciousness_calculation( features_info[SS_STMS_IN_F_PRODUCTS], total_passes, total_fails, spectrum_expression) feature_based_rank[spectrum_expression] = {FB_RANK: {}, FB_TC_RANK: {}} for stm in buggy_statements: feature_rank, stm_rank = search_rank_worst_case( stm, features_info[SS_STMS_IN_F_PRODUCTS], features_info[SS_STMS_IN_F_PRODUCTS], spectrum_expression) feature_based_rank[spectrum_expression][FB_RANK][stm] = {} feature_based_rank[spectrum_expression][FB_RANK][stm][ RANK] = stm_rank feature_based_rank[spectrum_expression][FB_RANK][stm][EXAM] = ( stm_rank / len(statements_sets[SS_STMS_IN_F_PRODUCTS])) * 100 for stm in buggy_statements: if (stm in statements_sets[SS_SLICING]): feature_rank, stm_rank = search_rank_worst_case( stm, features_info[SS_STMS_IN_F_PRODUCTS], features_info[SS_SLICING], spectrum_expression) feature_based_rank[spectrum_expression][FB_TC_RANK][stm] = {} feature_based_rank[spectrum_expression][FB_TC_RANK][stm][ RANK] = stm_rank feature_based_rank[spectrum_expression][FB_TC_RANK][stm][ EXAM] = (stm_rank / len(statements_sets[SS_SLICING])) * 100 else: feature_based_rank[spectrum_expression][ FB_TC_RANK] = feature_based_rank[spectrum_expression][ FB_RANK] return feature_based_rank