diff --git a/localCoverage/automate_execute/build_part.py b/localCoverage/automate_execute/build_part.py new file mode 100644 index 0000000000000000000000000000000000000000..4fc42151d0f76b06c81c2809b2e440203afb5adc --- /dev/null +++ b/localCoverage/automate_execute/build_part.py @@ -0,0 +1,128 @@ +#!/usr/bin/env python3 +# coding=utf-8 + +# +# Copyright (c) 2024 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import json +import os +import subprocess +import sys + + +def get_subsystem_config(part_str, developer_path): + all_system_info_path = os.path.join( + developer_path, "localCoverage/all_subsystem_config.json" + ) + if os.path.exists(all_system_info_path): + new_json_text = {} + with open(all_system_info_path, "r", encoding="utf-8") as system_text: + system_text_json = json.load(system_text) + if part_str in system_text_json: + new_json_text[part_str] = system_text_json[part_str] + else: + print(f"Error: {part_str} not in all_subsystem_config.json") + return new_json_text + else: + print(f"{all_system_info_path} not exists!") + return {} + + +def get_system_or_vendor(code_path): + repo_config_path = os.path.join(code_path, ".repo/manifests.git/.repo_config.json") + if os.path.exists(repo_config_path): + with open(repo_config_path, "r", encoding="utf-8") as fp: + text_json = json.load(fp) + if "manifest.filename" in text_json: + text = text_json["manifest.filename"][0] + if text.startswith("system"): + return "system" + if text.startswith("vendor"): + return "vendor" + else: + return "blue" + else: + print(f"Error: {repo_config_path} not exist!") + return "Error" + + +def get_bundle_json(part_str, developer_path, code_path): + part_json = get_subsystem_config(part_str, developer_path) + system_or_vendor = get_system_or_vendor(code_path) + if system_or_vendor == "system": + command = ["./build_system.sh", "--abi-type", "generic_generic_arm_64only", "--device-type", + "hisi_all_phone_standard", "--ccache", "--build-variant", "root"] + elif system_or_vendor == "system": + command = ["./build_vendor.sh", "--abi-type", "generic_generic_arm_64only", "--device-type", + "general_8425L_phone_standard", "--ccache", "--build-variant", "root", + "--gn-args", "uefi_enable=true"] + else: + command = ["./build.sh", "--product-name", "rk3568", "--ccache"] + + if part_json.get(part_str): + bundle_json_path = os.path.join(code_path, part_json[part_str]["path"][0], "bundle.json") + if os.path.exists(bundle_json_path): + with open(bundle_json_path, "r", encoding="utf-8") as bundle_json_text: + bundle_json = json.load(bundle_json_text) + os.chdir(code_path) + part_name = bundle_json["component"]["name"] + command.append("--build-target") + command.append(part_name) + + if bundle_json["component"]["build"].get("test"): + test_path = bundle_json["component"]["build"]["test"] + test_str = " ".join([i.strip("//") for i in test_path]) + command.append("--build-target") + command.append(test_str) + + command.append("--gn-args") + command.append("use_clang_coverage=true") + print(command) + if subprocess.call(command) == 0: + build_result = True + else: + build_result = False + os.chdir(developer_path) + return build_result + + else: + print(f"{bundle_json_path}不存在,不能获取编译参数,请检查该部件的bundle.json文件!") + return False + + +def execute_case(developer_test, part_name): + start_path = os.path.join(developer_test, "start.sh") + run_cmd = f"run -t UT -tp {part_name} -cov coverage \n" + print(run_cmd) + with os.popen(start_path, "w") as finput: + finput.write("1\n") + finput.write(run_cmd) + finput.write("quit\n") + finput.write("exit(0)\n") + + +if __name__ == '__main__': + test_part_str = sys.argv[1] + current_path = os.getcwd() + root_path = current_path.split("/test/testfwk/developer_test")[0] + developer_test_path = os.path.join(root_path, "test/testfwk/developer_test") + build_before_path = os.path.join( + developer_test_path, + "localCoverage/restore_comment/build_before_generate.py" + ) + subprocess.run("python3 %s %s" % (build_before_path, test_part_str), shell=True) + build_success = get_bundle_json(test_part_str, developer_test_path, root_path) + if build_success: + execute_case(developer_test_path, test_part_str) diff --git a/localCoverage/automate_execute/coverage.sh b/localCoverage/automate_execute/coverage.sh new file mode 100644 index 0000000000000000000000000000000000000000..af3848ff5b73d9fab54fda75a3d6571815e8ca3c --- /dev/null +++ b/localCoverage/automate_execute/coverage.sh @@ -0,0 +1,25 @@ +#!/bin/bash +# +# Copyright (c) 2024 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +echo "++++++++++++++++++++start install package+++++++++++++++++++++++" + +sudo apt-get install lcov +sudo apt-get install dos2unix +pip install lxml -i http://mirrors.tools.huawei.com/pypi/simple/ --trusted-host mirrors.tools.huawei.com +pip install selectolax -i http://mirrors.tools.huawei.com/pypi/simple/ --trusted-host mirrors.tools.huawei.com +pip install CppHeaderParser -i http://mirrors.tools.huawei.com/pypi/simple/ --trusted-host mirrors.tools.huawei.com + +echo "++++++++++++++++++++end install package+++++++++++++++++++++++" \ No newline at end of file diff --git a/localCoverage/automate_execute/install_coverage_tools.py b/localCoverage/automate_execute/install_coverage_tools.py new file mode 100644 index 0000000000000000000000000000000000000000..c93f5ae9c61509254fac0fa8978d5c464875bbe2 --- /dev/null +++ b/localCoverage/automate_execute/install_coverage_tools.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python3 +# coding=utf-8 + +# +# Copyright (c) 2024 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import os +import subprocess + + +def sub_command(command): + proc = subprocess.Popen(command, shell=True) + try: + proc.communicate() + except subprocess.TimeoutExpired: + proc.kill() + proc.terminate() + + +def install_tool(home_path): + coverage_sh = os.path.join( + home_path, "test/testfwk/developer_test/localCoverage/automate_execute/coverage.sh") + sub_command("chmod +x %s" % coverage_sh) + sub_command(coverage_sh) + + +def update_lcovrc(): + print("修改/etc/lcovrc文件中:lcov_branch_coverage=0为lcov_branch_coverage=1") + subprocess.call(["sudo", "echo", ""]) + file_path = "/etc/lcovrc" + os.chmod(file_path, 0o777) + with open(file_path, "r", encoding="utf-8") as f: + txt = f.read() + txt = txt.replace("lcov_branch_coverage = 0", "lcov_branch_coverage = 1") + with open(file_path, "w", encoding="utf-8") as f1: + f1.write(txt) + + +if __name__ == '__main__': + current_path = os.getcwd() + root_path = current_path.split("/test/testfwk/developer_test")[0] + if os.geteuid() == 0: + install_tool(root_path) + update_lcovrc() + else: + print("当前用户不是root用户,请在root用户下执行该脚本,因为lcov只能在root下安装," + "且需要修改/etc/lcovrc文件,其他用户没有权限修改,会导致分支覆盖率无法生成!") + + + + + diff --git a/localCoverage/restore_comment/after_lcov_branch.py b/localCoverage/restore_comment/after_lcov_branch.py index b073617009ac2fb55d7056e5816985b7849acb43..793a88ff965c15e55ad5532d03ff7d94bdbf1dcc 100644 --- a/localCoverage/restore_comment/after_lcov_branch.py +++ b/localCoverage/restore_comment/after_lcov_branch.py @@ -81,9 +81,9 @@ def recover_cpp_file(part_name_path): recover_source_file(cpp_list, keys=[" //LCOV_EXCL_BR_LINE"]) else: print("The directory does not exist.", file_path) - os.remove(path) + os.remove(part_name_path) except(FileNotFoundError, AttributeError, ValueError, KeyError): - print("recover LCOV_EXCL_BR_LINE Error") + print("recover LCOV_EXCL_BR_LINE Error") if __name__ == '__main__': diff --git a/localCoverage/restore_comment/build_before_generate.py b/localCoverage/restore_comment/build_before_generate.py index 8e1cbe7338365a5618edcd72d17e9bdd0b7c0351..2c5914151ffc73e63af4c9ff05e574476dde4230 100644 --- a/localCoverage/restore_comment/build_before_generate.py +++ b/localCoverage/restore_comment/build_before_generate.py @@ -20,6 +20,7 @@ import os import subprocess import json import stat +import sys FLAGS = os.O_WRONLY | os.O_CREAT | os.O_EXCL MODES = stat.S_IWUSR | stat.S_IRUSR @@ -119,19 +120,23 @@ def get_part_config_json(part_list, system_info_path, part_path): if __name__ == '__main__': part_name_list = [] - while True: - print("For example: run -tp partname\n" - " run -tp partname1 partname2") - - # 获取用户输入命令 - part_name = input("Please enter your command: ") - if part_name == "": - continue - if " -tp " in part_name: - part_name_list = part_name.strip().split(" -tp ")[1].split() - break - else: - continue + if sys.argv[1]: + part_name_list.append(sys.argv[1]) + print(part_name_list) + else: + while True: + print("For example: run -tp partname\n" + " run -tp partname1 partname2") + + # 获取用户输入命令 + part_name = input("Please enter your command: ") + if part_name == "": + continue + if " -tp " in part_name: + part_name_list = part_name.strip().split(" -tp ")[1].split() + break + else: + continue current_path = os.getcwd() root_path = current_path.split("/test/testfwk/developer_test")[0]