Exemple #1
0
def generate_feature_order_file(project_dir):
    logger.info(
        f"Generating Feature Order file from model file at project [{get_file_name(project_dir)}]"
    )
    model_file_path = get_model_file_path(project_dir)
    implemented_features = get_implemented_features(project_dir)
    output_feature_order_path = get_feature_order_file_path(project_dir)
    if not is_path_exist(output_feature_order_path):
        execute_shell_command(f'java -jar {PLUGIN_PATH} ',
                              extra_args=[
                                  {
                                      "--feature_model": model_file_path
                                  },
                                  {
                                      "--available_features":
                                      ",".join(implemented_features)
                                  },
                                  {
                                      "--output_feature_order_path":
                                      output_feature_order_path
                                  },
                              ])
    else:
        logger.info(f"Used Custom Feature Order file")

    return output_feature_order_path
Exemple #2
0
def make_mutants(current_project_dir, optional_feature_names):
    projects_dir, project_name = split_path(current_project_dir)
    execute_shell_command(
        f'java -Dmujava_home={projects_dir} -Dallowed_features={",".join(optional_feature_names)} -cp {PLUGIN_PATH} mujava.cli.genmutes',
        extra_args=[
            {"-all": ""},
            {project_name: ""},
        ])
    mutation_result_dir = get_mutation_result_dir(current_project_dir)
    mutant_paths = get_all_mutant_paths(mutation_result_dir)
    mutant_count = count_mutants(mutant_paths)
    logger.info(f"Composed {mutant_count} mutants [{get_file_name_without_ext(mutation_result_dir)}]")
    return mutant_paths
Exemple #3
0
def sampling(model_file_path, t_wise):
    logger.info(f"Running sampling for model file [{get_file_name_without_ext(model_file_path)}] with {t_wise}-wise")
    output_log = execute_shell_command(f'java -jar {PLUGIN_PATH} ', extra_args=[
        {"-fm": model_file_path},
        {"-t": "t_wise"},
        {"-a": "Chvatal"},
        {"-s": t_wise},
    ])
    return get_sampling_file_path(output_log)
Exemple #4
0
def run_junit_test_cases_with_coverage(variant_dir,
                                       lib_paths=None,
                                       halt_on_failure=False,
                                       halt_on_error=True,
                                       custom_ant=None):
    if lib_paths is None:
        lib_paths = []
    logger.info(
        f"Running JUnit Test for variant [{get_file_name_without_ext(variant_dir)}] - Using custom ant [{custom_ant}]"
    )
    src_dir = get_src_dir(variant_dir)
    test_dir = get_test_dir(variant_dir)
    src_classes_dir = get_compiled_source_classes_dir(variant_dir)
    test_classes_dir = get_compiled_test_classes_dir(variant_dir)
    test_coverage_dir = get_test_coverage_dir(variant_dir)
    output_log = execute_shell_command(
        f'/bin/sh {JUNIT_PLUGIN_PATH}',
        extra_args=[
            {
                "-src": src_dir
            },
            {
                "-test": test_dir
            },
            {
                "-build.classes": src_classes_dir
            },
            {
                "-build.testclasses": test_classes_dir
            },
            {
                "-report.coveragedir": test_coverage_dir
            },
            {
                "-junit.haltonfailure": "yes" if halt_on_failure else "no"
            },
            {
                "-ant.name": custom_ant
            },
            {
                "-lib_path": ":".join(lib_paths)
            },
        ],
        log_to_file=True)

    is_test_failure = re.search(
        "(Failures: [1-9]+|Errors: [1-9]+|BUILD FAILED)", output_log)
    if is_test_failure:
        if halt_on_failure or (halt_on_error
                               and "BUILD FAILED" in is_test_failure.group()):
            logger.fatal(
                "Some test cases were failed, see log for more detail\n{}".
                format(output_log))
            raise RuntimeError("Test case failures")
        return False
    return True
Exemple #5
0
def get_all_test_case_name_from_source_files(variant_dir):
    test_dir = get_test_dir(variant_dir) + "/"
    shell_command = f"""find {test_dir} -name "*_ESTest.java" -exec egrep -oH "void test[0-9]+\(\)  throws" {{}} \; | sed 's|{test_dir}||1' | sed 's/^\///1; s/\//./g; s/\.java:void /./1; s/()  throws//1;'"""
    output = execute_shell_command(shell_command, show_command=False)
    test_cases_names = output.split("\n")[0:-1]
    test_cases_name_set = set(test_cases_names)
    assert len(test_cases_names) == len(
        test_cases_name_set
    ), f"\n---\nDuplicate test case names\nBefore [LIST]: {test_cases_names}\nAfter [SET]: {test_cases_name_set}\n\nVariant dir: {variant_dir}\n---"
    return test_cases_name_set
Exemple #6
0
def compile_classes(src, out, lib_path=None):
    if lib_path is None:
        lib_path = []
    joined_lib_path = ":".join(lib_path)

    output_log = execute_shell_command(f'/bin/sh {SHELL_BUILDER_PLUGIN_PATH}', extra_args=[
        {"-src": src},
        {"-out": out},
        {"-classpath": joined_lib_path},
    ])
    if output_log.find("BUILD SUCCESSFUL") < 0:
        raise RuntimeError(f"Failed to compile source code [{src}]")
    return output_log
Exemple #7
0
def compose_by_config(project_dir, config_file_path):
    logger.info(
        f"Composing [{get_file_name(project_dir)}] project's source code with config file [{get_file_name_without_ext(config_file_path)}]"
    )
    config_name = get_file_name_without_ext(config_file_path)
    output_dir = get_variant_dir(project_dir, config_name)
    execute_shell_command(f'java -jar {PLUGIN_PATH}',
                          extra_args=[{
                              "--expression": config_file_path
                          }, {
                              "--base-directory":
                              get_feature_source_code_dir(project_dir)
                          }, {
                              "--output-directory": output_dir
                          }, {
                              "--export_roles_json": ""
                          }, {
                              "--featureAnnotationJava": ""
                          }])
    output_src_dir = join_path(output_dir, config_name)
    if is_path_exist(output_src_dir):
        renamed_folder_dir = get_src_dir(output_dir)
        move_file(output_src_dir, renamed_folder_dir)
    return output_dir
Exemple #8
0
def get_all_mutant_paths(mutation_result_dir):
    """
        Mutation result tracing path for each mutated operator
            mutation_result -> feature_class (eg. Base.GPL.EdgeIter)
            -> traditional_mutants -> mutated method (eg. boolean_hasNext())
            -> mutation operators (eg. SDL_1) -> mutated class file (eg. EdgeIter.java)
        """

    output = execute_shell_command(f"find {mutation_result_dir}",
                                   extra_args=[
                                       {"-mindepth": 4},
                                       {"-type": "f"},
                                       {"-name": "\"*.java\""},
                                   ])
    return output.split("\n")[:-1]
Exemple #9
0
def generate_junit_test_cases(lib_paths, variant_dir):
    logger.info(
        f"Generating JUnit Test for variant [{get_file_name_without_ext(variant_dir)}]"
    )
    compiled_classes_dir = get_compiled_source_classes_dir(variant_dir)

    evosuite_temp_dir = join_path(".evosuite_" + hash_md5(variant_dir))
    delete_dir(evosuite_temp_dir)

    test_cases_dir = get_test_dir(variant_dir, force_mkdir=False)
    delete_dir(test_cases_dir)

    output_log = execute_shell_command(
        f'java -jar {EVOSUITE_PLUGIN_PATH}',
        extra_args=[
            {
                "-projectCP": ":".join([compiled_classes_dir] + lib_paths)
            },
            {
                "-seed": 1583738192420
            },
            {
                "-target": compiled_classes_dir
            },
            {
                "-continuous": "execute"
            },
            {
                "-Dctg_memory": "4000"
            },
            {
                "-Dctg_cores": "4"
            },
            {
                "-Dctg_dir": evosuite_temp_dir
            },
            {
                "-Dctg_export_folder": test_cases_dir
            },
        ],
        log_to_file=True)
Exemple #10
0
def rebuild_spectrum_coverage(input_coverage_dir,
                              spectrum_output_path,
                              specific_test_cases=None,
                              random=True,
                              max_test_cases=-1):
    if is_path_exist(spectrum_output_path):
        logger.info(
            f"Ignoring spectrum coverage file for [{get_file_name_with_parent(input_coverage_dir)}]"
        )
        return
    if isinstance(specific_test_cases, list):
        joined_test_cases = ",".join(specific_test_cases)
    else:
        joined_test_cases = ""
    logger.info(
        f"Building spectrum coverage file for [{get_file_name_with_parent(input_coverage_dir)}]"
    )
    output_log = execute_shell_command(
        f'java -Xmx64m -Drandom={str(random).lower()} -Dupper_bound={max_test_cases} -Dtest_cases={joined_test_cases} -Dcoverage_dir={input_coverage_dir} -Doutput_path={spectrum_output_path} -cp {PLUGIN_PATH} ',
        extra_args=[],
        log_to_file=True)
    print(output_log)
Exemple #11
0
def assign_current_project_as_new_session(current_project_dir):
    projects_dir, project_name = split_path(current_project_dir)
    execute_shell_command(f'java -Dmujava_home={projects_dir} -cp {PLUGIN_PATH} mujava.cli.testnew', extra_args=[
        {project_name: ""},
    ])