From 9b056ad070fccf5df8ea4813c76e345114bb4db7 Mon Sep 17 00:00:00 2001 From: caojiale1 Date: Sat, 13 May 2023 14:58:27 +0800 Subject: [PATCH 1/3] Signed-off-by caojiale1 caojiale1@huawei.com Signed-off-by: caojiale1 --- README_zh.md | 1 + localCoverage/coverage_tools.py | 17 +++++++++++-- .../interfaceCoverage/get_innerkits_json.py | 13 +++++----- .../interfaceCoverage_gcov_lcov.py | 24 +++++++++---------- .../restore_comment/after_lcov_branch.py | 2 -- localCoverage/utils.py | 16 +++++++++++++ 6 files changed, 51 insertions(+), 22 deletions(-) diff --git a/README_zh.md b/README_zh.md index 5b85628..e3339f9 100755 --- a/README_zh.md +++ b/README_zh.md @@ -1147,6 +1147,7 @@ reports/latest 2)安装dos2unix, 安装命令:apt install dos2unix. 3)安装lxml, 安装命令: pip install lxml 4)安装selectolax, 安装命令: pip install selectolax + 5)安装CppHeaderParser, 安装命令 pip install CppHeaderParser 4. 远程映射设备,修改usr_config.xml中的ip号,设备映射方式查看上面介绍的远程端口映射, diff --git a/localCoverage/coverage_tools.py b/localCoverage/coverage_tools.py index dd59983..0fe1355 100644 --- a/localCoverage/coverage_tools.py +++ b/localCoverage/coverage_tools.py @@ -52,7 +52,7 @@ def get_subsystem_config(part_list, developer_path): def copy_coverage(developer_path): - print("[*************** Start TO Get Coverage Report ***************]") + print("*" * 40, "Start TO Get Coverage Report", "*" * 40) coverage_path = os.path.join(developer_path, "reports/coverage") code_path = os.path.join( developer_path, "localCoverage/codeCoverage/results/coverage" @@ -135,7 +135,7 @@ def get_subsystem_name(part_list, product_name): def execute_interface_cov_tools(subsystem_str, developer_path): - print("[*************** Start TO Get Interface Coverage Report ***************]") + print("*" * 40, "Start TO Get Interface Coverage Report", "*" * 40) innerkits_json_path = os.path.join( developer_path, "localCoverage/interfaceCoverage/get_innerkits_json.py" @@ -174,6 +174,17 @@ if __name__ == '__main__': # 执行代码覆盖率 execute_code_cov_tools(developer_test_path) + # 执行接口覆盖率 + if subsystem_args_str: + subsystem_str_name = subsystem_args_str + else: + subsystem_str_name = get_subsystem_name(test_part_list, product_names) + + if subsystem_str_name: + execute_interface_cov_tools(subsystem_str_name, developer_test_path) + else: + print("subsystem or part without!") + # 源代码还原 after_lcov_branch_path = os.path.join( developer_test_path, "localCoverage/restore_comment/after_lcov_branch.py") @@ -189,3 +200,5 @@ if __name__ == '__main__': print(r"See the code coverage report in: " r"/test/testfwk/developer_test/localCoverage/codeCoverage/results/coverage/reports/cxx/html") + print(r"See the interface coverage report in: " + r"/test/testfwk/developer_test/localCoverage/interfaceCoverage/results/coverage/interface_kits") diff --git a/localCoverage/interfaceCoverage/get_innerkits_json.py b/localCoverage/interfaceCoverage/get_innerkits_json.py index b85ddaa..f1cc62f 100644 --- a/localCoverage/interfaceCoverage/get_innerkits_json.py +++ b/localCoverage/interfaceCoverage/get_innerkits_json.py @@ -27,14 +27,14 @@ def _init_sys_config(): sys.path.insert(0, sys.localcoverage_path) -def gen_parts_info_json(folder_list, output_json_path): +def gen_parts_info_json(folder_list, output_json_path, target_cpu): """ 根据部件信息,生成字典至json文件中 """ if len(folder_list) != 0: data_dict = {} for folder_str in folder_list: - data_dict[folder_str] = f"innerkits/ohos-arm64/{folder_str}" + data_dict[folder_str] = f"innerkits/ohos-{target_cpu}/{folder_str}" output_json_path = os.path.join(output_json_path, "kits_modules_info.json") json_str = json.dumps(data_dict, indent=2) with open(output_json_path, "w") as json_file: @@ -58,16 +58,17 @@ def get_parts_list(path): if __name__ == "__main__": current_path = os.getcwd() _init_sys_config() - from localCoverage.utils import get_product_name, coverage_command + from localCoverage.utils import get_product_name, coverage_command, get_target_cpu root_path = current_path.split("/test/testfwk/developer_test")[0] product_name = get_product_name(root_path) + cpu_type = get_target_cpu(root_path) part_info_path = os.path.join( - root_path, "out", product_name, "innerkits/ohos-arm64" + root_path, "out", product_name, "innerkits/ohos-%s" % cpu_type ) json_path = os.path.join( - root_path, "out", product_name, "packages/phone/innerkits/ohos-arm64" + root_path, "out", product_name, "packages/phone/innerkits/ohos-%s" % cpu_type ) coverage_command("mkdir -p %s" % json_path) part_list = get_parts_list(part_info_path) - gen_parts_info_json(part_list, json_path) + gen_parts_info_json(part_list, json_path, cpu_type) diff --git a/localCoverage/interfaceCoverage/interfaceCoverage_gcov_lcov.py b/localCoverage/interfaceCoverage/interfaceCoverage_gcov_lcov.py index 415b93a..344057a 100644 --- a/localCoverage/interfaceCoverage/interfaceCoverage_gcov_lcov.py +++ b/localCoverage/interfaceCoverage/interfaceCoverage_gcov_lcov.py @@ -54,13 +54,12 @@ def get_subsystem_part_list(project_rootpath): subsystme_part_dict = {} subsystem_part_config_filepath = os.path.join( project_rootpath, "out", product_name, "build_configs/infos_for_testfwk.json") - print(subsystem_part_config_filepath) if os.path.exists(subsystem_part_config_filepath): try: with open(subsystem_part_config_filepath, 'r') as f: data = json.load(f) except IOError: - print("Error for open subsystem config file: ") + print("Error for open subsystem config file. ") if not data: print("subsystem_part config file error.") else: @@ -184,7 +183,7 @@ def get_pubilc_func_list_from_headfile(cxx_header_filepath): func_returntype) ) except CppHeaderParser.CppParseError: - print("error") + print("") return pubilc_func_list @@ -205,8 +204,6 @@ def get_sdk_interface_func_list(part_name): print("get interface error ", sdk_path) else: print("Error: %s is not exist." % sdk_path) - - print("interface_func_list:", interface_func_list) return interface_func_list @@ -299,7 +296,7 @@ def get_function_para_count(func_info): return len(para_list) -def get_covered_result_data(public_interface_func_list, covered_func_list, subsystem_name): +def get_covered_result_data(public_interface_func_list, covered_func_list): coverage_result_list = [] for item in public_interface_func_list: data_list = list(item) @@ -361,7 +358,7 @@ def get_interface_coverage_result_list(subsystem_name, subsystem_part_dict): print("####") covered_func_list = get_covered_function_list(subsystem_name) interface_coverage_result_list = get_covered_result_data( - public_interface_func_list, covered_func_list, subsystem_name) + public_interface_func_list, covered_func_list) return interface_coverage_result_list @@ -449,17 +446,20 @@ def make_interface_coverage_result(): if __name__ == "__main__": current_path = os.getcwd() _init_sys_config() - from localCoverage.utils import get_product_name + from localCoverage.utils import get_product_name, get_target_cpu product_name = get_product_name(CODEPATH) - PATH_INFO_PATH = "out/{}/innerkits/ohos-arm64".format(product_name) - OUTPUT_JSON_PATH = "out/{}/packages/phone/innerkits/ohos-arm64".format(product_name) - KIT_MODULES_INFO = "out/{}/packages/phone/innerkits/ohos-arm64/kits_modules_info.json".format(product_name) + cpu_type = get_target_cpu(CODEPATH) + PATH_INFO_PATH = "out/{}/innerkits/ohos-{}".format(product_name, cpu_type) + OUTPUT_JSON_PATH = "out/{}/packages/phone/innerkits/ohos-{}".format( + product_name, cpu_type) + KIT_MODULES_INFO = "out/{}/packages/phone/innerkits/ohos-{}/kits_modules_info.json".format( + product_name, cpu_type) system_args = sys.argv[1] system_name_list = system_args.split(",") get_innerkits_json.gen_parts_info_json( get_innerkits_json.get_parts_list(os.path.join(CODEPATH, PATH_INFO_PATH)), - os.path.join(CODEPATH, OUTPUT_JSON_PATH) + os.path.join(CODEPATH, OUTPUT_JSON_PATH), cpu_type ) if len(system_name_list) > 0: make_interface_coverage_result() diff --git a/localCoverage/restore_comment/after_lcov_branch.py b/localCoverage/restore_comment/after_lcov_branch.py index 34a8d1b..28e9ccd 100644 --- a/localCoverage/restore_comment/after_lcov_branch.py +++ b/localCoverage/restore_comment/after_lcov_branch.py @@ -50,7 +50,6 @@ def recover_source_file(cpp_arr_list, keys): print("no any .cpp file here") return - print("[********** Start Recover Source File **********]") for path in cpp_arr_list: if not os.path.exists(path): return @@ -68,7 +67,6 @@ def recover_source_file(cpp_arr_list, keys): os.remove(path) subprocess.Popen("mv %s %s" % (f"{path.split('.')[0]}_bk.html", path), shell=True).communicate() - print("[********** End Recover Source File **********]") if __name__ == '__main__': diff --git a/localCoverage/utils.py b/localCoverage/utils.py index 458cbbb..74d75ab 100644 --- a/localCoverage/utils.py +++ b/localCoverage/utils.py @@ -64,6 +64,22 @@ def get_product_name(root_path): return "" +def get_target_cpu(root_path): + """ + 从ohos_config.json中获取编译cpu + :param root_path: ohos_config.json所在的目录 + :return: 编译产量生成的路径 + """ + ohos_config = os.path.join(root_path, "ohos_config.json") + json_obj = json_parse(ohos_config) + if json_obj: + target_cpu = json_obj["target_cpu"] + return target_cpu + + logger("{} not exist.".format(ohos_config), "ERROR") + return "" + + def shell_command(command_list: list): """ 命令行执行命令 -- Gitee From 61453f942f91a3f06797c7e8fef1c7961ad505f7 Mon Sep 17 00:00:00 2001 From: caojiale1 Date: Mon, 15 May 2023 17:56:53 +0800 Subject: [PATCH 2/3] Signed-off-by caojiale1 caojiale1@huawei.com Signed-off-by: caojiale1 --- localCoverage/coverage_tools.py | 15 ++---- .../interfaceCoverage_gcov_lcov.py | 51 +++++++++---------- .../interfaceCoverage/make_report.py | 9 ++-- src/core/command/run.py | 5 +- 4 files changed, 34 insertions(+), 46 deletions(-) diff --git a/localCoverage/coverage_tools.py b/localCoverage/coverage_tools.py index 0fe1355..6aad297 100644 --- a/localCoverage/coverage_tools.py +++ b/localCoverage/coverage_tools.py @@ -134,7 +134,7 @@ def get_subsystem_name(part_list, product_name): return "" -def execute_interface_cov_tools(subsystem_str, developer_path): +def execute_interface_cov_tools(partname_str, developer_path): print("*" * 40, "Start TO Get Interface Coverage Report", "*" * 40) innerkits_json_path = os.path.join( developer_path, @@ -146,14 +146,12 @@ def execute_interface_cov_tools(subsystem_str, developer_path): developer_path, "localCoverage/interfaceCoverage/interfaceCoverage_gcov_lcov.py" ) - subprocess.run("python3 %s %s" % (interface_path, subsystem_str), shell=True) + subprocess.run("python3 %s %s" % (interface_path, partname_str), shell=True) if __name__ == '__main__': testpart_args = sys.argv[1] - subsystem_args = sys.argv[2] test_part_list = testpart_args.split("testpart=")[1].split(",") - subsystem_args_str = subsystem_args.split("subsystem=")[1] current_path = os.getcwd() root_path = current_path.split("/test/testfwk/developer_test")[0] @@ -175,13 +173,8 @@ if __name__ == '__main__': execute_code_cov_tools(developer_test_path) # 执行接口覆盖率 - if subsystem_args_str: - subsystem_str_name = subsystem_args_str - else: - subsystem_str_name = get_subsystem_name(test_part_list, product_names) - - if subsystem_str_name: - execute_interface_cov_tools(subsystem_str_name, developer_test_path) + if len(test_part_list) > 0: + execute_interface_cov_tools(testpart_args, developer_test_path) else: print("subsystem or part without!") diff --git a/localCoverage/interfaceCoverage/interfaceCoverage_gcov_lcov.py b/localCoverage/interfaceCoverage/interfaceCoverage_gcov_lcov.py index 344057a..9e59f4b 100644 --- a/localCoverage/interfaceCoverage/interfaceCoverage_gcov_lcov.py +++ b/localCoverage/interfaceCoverage/interfaceCoverage_gcov_lcov.py @@ -218,9 +218,9 @@ def get_function_info_string(func_string): return function_info -def get_covered_function_list(subsystem_name): +def get_covered_function_list(part_name): covered_function_list = [] - file_name = f"{subsystem_name}_strip.info" + file_name = f"{part_name}_strip.info" file_path = os.path.join(SUB_SYSTEM_INFO_PATH, file_name) if os.path.exists(file_path): with open(file_path, "r") as fd: @@ -230,12 +230,13 @@ def get_covered_function_list(subsystem_name): temp_list = sub_line_string.split(",") if len(temp_list) == 2 and int(temp_list[0]) != 0: func_info = get_function_info_string(temp_list[1]) - if "" == func_info: + after_func_info = func_info.decode("utf-8") + if "" == after_func_info: continue - func_info = func_info.replace("\n", "") - if func_info == temp_list[1] and func_info.startswith("_"): + after_func_info = after_func_info.replace("\n", "") + if after_func_info == temp_list[1] and after_func_info.startswith("_"): continue - covered_function_list.append(func_info) + covered_function_list.append(after_func_info) else: pass return covered_function_list @@ -347,16 +348,14 @@ def get_covered_result_data(public_interface_func_list, covered_func_list): return coverage_result_list -def get_interface_coverage_result_list(subsystem_name, subsystem_part_dict): - part_list = subsystem_part_dict.get(subsystem_name, []) +def get_interface_coverage_result_list(part_name): public_interface_func_list = [] - for part_str in part_list: - try: - interface_func_list = get_sdk_interface_func_list(part_str) - public_interface_func_list.extend(interface_func_list) - except Exception: - print("####") - covered_func_list = get_covered_function_list(subsystem_name) + try: + interface_func_list = get_sdk_interface_func_list(part_name) + public_interface_func_list.extend(interface_func_list) + except Exception: + print("####") + covered_func_list = get_covered_function_list(part_name) interface_coverage_result_list = get_covered_result_data( public_interface_func_list, covered_func_list) return interface_coverage_result_list @@ -369,7 +368,7 @@ def get_coverage_data(data_list): for item in data_list: if "Y" == item[2] or "Recorded" == item[2]: covered_count += 1 - coverage = str(covered_count * 100 / total_count) + "%" + coverage = str("%.2f" % (covered_count * 100 / total_count)) + "%" else: coverage = "0%" return covered_count, coverage @@ -389,7 +388,7 @@ def get_summary_data(interface_data_list): total_count += len(data_list) covered_count += count if 0 != total_count: - total_coverage = str(covered_count * 100 / total_count) + "%" + total_coverage = str("%.2f" % (covered_count * 100 / total_count)) + "%" summary_list.append(["Summary", total_count, covered_count, total_coverage]) return summary_list @@ -431,14 +430,12 @@ def make_coverage_result_file(interface_data_list, output_path, title_name): make_result_file(interface_data_list, summary_list, output_path, title_name) -def make_interface_coverage_result(): - subsystem_name_list = system_name_list +def make_interface_coverage_result(part_list): interface_data_list = [] - subsystem_part_dict = get_subsystem_part_list(CODEPATH) - for subsystem_name in subsystem_name_list: + for part_name in part_list: coverage_result_list = get_interface_coverage_result_list( - subsystem_name, subsystem_part_dict) - interface_data_list.append([subsystem_name, coverage_result_list]) + part_name) + interface_data_list.append([part_name, coverage_result_list]) make_coverage_result_file(interface_data_list, OUTPUT_REPORT_PATH, "Inner Interface") @@ -455,13 +452,13 @@ if __name__ == "__main__": KIT_MODULES_INFO = "out/{}/packages/phone/innerkits/ohos-{}/kits_modules_info.json".format( product_name, cpu_type) - system_args = sys.argv[1] - system_name_list = system_args.split(",") + part_args = sys.argv[1] + part_name_list = part_args.split("testpart=")[1].split(",") get_innerkits_json.gen_parts_info_json( get_innerkits_json.get_parts_list(os.path.join(CODEPATH, PATH_INFO_PATH)), os.path.join(CODEPATH, OUTPUT_JSON_PATH), cpu_type ) - if len(system_name_list) > 0: - make_interface_coverage_result() + if len(part_name_list) > 0: + make_interface_coverage_result(part_name_list) else: print("subsystem_name not exists!") diff --git a/localCoverage/interfaceCoverage/make_report.py b/localCoverage/interfaceCoverage/make_report.py index 7a1dfd1..9b5b75c 100644 --- a/localCoverage/interfaceCoverage/make_report.py +++ b/localCoverage/interfaceCoverage/make_report.py @@ -113,7 +113,6 @@ def create_title(reportpath, title_name, summary_list): """ - summary_list.sort(key=sort_by_field_element, reverse=False) subsystems = "" count = 0 for item in summary_list: @@ -140,7 +139,7 @@ def create_summary(reportpath, summary_list): table_head = """ - SubsystemName + PartName TotalCount CoveredCount Coverage @@ -179,7 +178,7 @@ def create_table_test(reportpath, subsystem_name, datalist, total_count, covered table_start = """
""" table_head = """ - + @@ -201,7 +200,7 @@ def create_table_test(reportpath, subsystem_name, datalist, total_count, covered """ try: with open(reportpath, "a") as report: - print("subsystem_name==" + subsystem_name) + print("part_name==" + subsystem_name) tabletitle = table_title % (subsystem_name) print("tabletitle==" + tabletitle) tabletitle = "

" + tabletitle + "

" @@ -224,7 +223,7 @@ def create_table_test(reportpath, subsystem_name, datalist, total_count, covered "" + str(line[2]) + "") report.write(content) if 0 != total_count: - coverage = str(covered_count * 100 / total_count) + "%" + coverage = str("%.2f" % (covered_count * 100 / total_count)) + "%" else: coverage = "0%" coverage = table_summary % (total_count, covered_count, coverage) diff --git a/src/core/command/run.py b/src/core/command/run.py index b6c384f..2202613 100644 --- a/src/core/command/run.py +++ b/src/core/command/run.py @@ -306,10 +306,9 @@ class Run(object): if not options.pullgcda: cov_main_file_path = os.path.join(sys.framework_root_dir, "localCoverage/coverage_tools.py") testpart = ",".join(list(map(str, options.partname_list))) - subsystem = ",".join(list(map(str, options.subsystem))) if os.path.exists(cov_main_file_path): - subprocess.run("python3 %s testpart=%s subsystem=%s" % ( - cov_main_file_path, testpart, subsystem), shell=True) + subprocess.run("python3 %s testpart=%s" % ( + cov_main_file_path, testpart), shell=True) else: print(f"{cov_main_file_path} not exists.") return -- Gitee From 57399b3e487cb3e7b98bcaddf0d244b58f95a897 Mon Sep 17 00:00:00 2001 From: caojiale1 Date: Tue, 16 May 2023 09:46:57 +0800 Subject: [PATCH 3/3] Signed-off-by caojiale1 caojiale1@huawei.com Signed-off-by: caojiale1 --- localCoverage/coverage_tools.py | 2 +- localCoverage/interfaceCoverage/make_report.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/localCoverage/coverage_tools.py b/localCoverage/coverage_tools.py index 6aad297..fd9d8d7 100644 --- a/localCoverage/coverage_tools.py +++ b/localCoverage/coverage_tools.py @@ -52,7 +52,7 @@ def get_subsystem_config(part_list, developer_path): def copy_coverage(developer_path): - print("*" * 40, "Start TO Get Coverage Report", "*" * 40) + print("*" * 40, "Start TO Get Code Coverage Report", "*" * 40) coverage_path = os.path.join(developer_path, "reports/coverage") code_path = os.path.join( developer_path, "localCoverage/codeCoverage/results/coverage" diff --git a/localCoverage/interfaceCoverage/make_report.py b/localCoverage/interfaceCoverage/make_report.py index 9b5b75c..c9a6d5e 100644 --- a/localCoverage/interfaceCoverage/make_report.py +++ b/localCoverage/interfaceCoverage/make_report.py @@ -104,7 +104,6 @@ def create_title(reportpath, title_name, summary_list): report_title = content + """
SubsystemNamePartName ClassName InterfaceName IsCovered
-

Summary Report

接口总数%s, 已覆盖%s, 未覆盖%s

-- Gitee