1#!/bin/bash 2# Copyright (c) 2022 Huawei Device Co., Ltd. 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15# This Script used to push test data to devices 16# Usage: 17# ./prepare_testdata.sh path 18# path is the rootdir of ohos projects. 19set -e 20 21if [ $# -lt 1 ];then 22 echo "Usage $0 <product name>" 23 echo "example $0 rk3568" 24 exit 1 25fi 26 27function get_root_dir() { 28 local cur_path=$(pwd) 29 while [ "${cur_path}" != "" ] 30 do 31 cur_path=${cur_path%/*} 32 if [ "${cur_path}" == "" ];then 33 echo "[error] get code root dir fail" 34 exit 1 35 fi 36 if [ "$(basename ${cur_path})" == "base" ]; then 37 ohos_root=${cur_path%/*} 38 return 39 fi 40 done 41} 42 43function hdc_shell_cmd() { 44 # do nothing if there are not any arguments 45 if [ $# -eq 0 ];then 46 return; 47 fi 48 echo "Running command $@" 49 hdc shell $@ 50} 51 52function hdc_push_cmd() { 53 # do nothing if there are not any arguments 54 if [ $# -ne 2 ];then 55 return; 56 fi 57 echo "Pushing resources to device" 58 hdc file send $@ 59 sleep 0.2 60} 61 62get_root_dir 63product_name=$1 64 65if [ ! -d "${ohos_root}/out/${product_name}" ]; then 66 echo "product ${product_name} not exist" 67 exit 1 68fi 69 70hdc target mount 71sleep 0.2 72hdc_shell_cmd "mount -o remount,rw /" 73ut_target_path="/data/appspawn_ut" 74echo "Remove ${ut_target_path}" 75hdc_shell_cmd "rm -rf ${ut_target_path}" 76hdc_shell_cmd "rm /bin/AppSpawn_ut" 77 78echo "Create ${ut_target_path}" 79hdc_shell_cmd "umask 022" 80hdc_shell_cmd "mkdir -p ${ut_target_path}" 81hdc_shell_cmd "mkdir -p ${ut_target_path}/proc" 82 83ohos_init="${ohos_root}/base/startup" 84 85hdc_shell_cmd "mkdir -p ${ut_target_path}/coverage" 86sleep 0.25 87 88# copy file to test 89 90hdc file send ${ohos_root}/out/${product_name}/tests/unittest/appspawn/appspawn_l2/AppSpawn_ut ${ut_target_path}/ 91sleep 0.25 92hdc_shell_cmd "cp ${ut_target_path}/AppSpawn_ut /bin/AppSpawn_ut" 93 94hdc_shell_cmd "chmod 777 ${ut_target_path}/* -R" 95sleep 0.2 96hdc_shell_cmd "chmod 777 /bin/AppSpawn_ut" 97 98hdc_shell_cmd "export GCOV_PREFIX=${ut_target_path}/coverage&&export GCOV_PREFIX_STRIP=20&&AppSpawn_ut" 99sleep 0.2 100 101if [ $? -ne 0 ]; then 102 echo "Execute init_unittest in device failed. please check the log" 103fi 104echo "Running appspawn unittests end..." 105echo "Ready to generate coverage..." 106pushd ${ohos_init} 107rm -rf ./g.sh 108rm -rf *.gcno 109rm -rf *.gcda 110echo "Copy .gcta files to ${ohos_init}}" 111 112for file in $(hdc_shell_cmd ls /data/appspawn_ut/coverage/*.gcda); do 113 hdc file recv ${file} ${ohos_init}/${file:27} 114 chmod 777 ${ohos_init}/${file:27} 115done 116 117 118echo "Find out all gcno files and copy to ${ohos_init}" 119find ${ohos_root}/out/${product_name}/obj/base/startup/ -name "*.gcno" -type f -exec cp {} . \; 120if [ $? -ne 0 ]; then 121 echo "find gcno failed." 122 popd 2>&1 > /dev/null 123 exit 1 124fi 125 126if [ ! -f "${ohos_init}/g.sh" ]; then 127 echo "create g.sh" 128 touch ${ohos_init}/g.sh 129 echo "${ohos_root}/prebuilts/clang/ohos/linux-x86_64/llvm/bin/llvm-cov gcov \$@" > ${ohos_init}/g.sh 130 chmod 755 ${ohos_init}/g.sh 131fi 132 133echo "Running command lcov" 134lcov -d . -o "${ohos_init}/appspawn_ut_tmp.info" -b . -c --gcov-tool ${ohos_init}/g.sh 135 136if [ $? -ne 0 ]; then 137 echo "Run command lcov failed" 138 popd 2>&1 > /dev/null 139fi 140 141echo "Filter out don\'t cared dir" 142lcov --remove appspawn_ut_tmp.info "*foundation*" "*third_party*" \ 143 "*device.c*" "*prebuilts*" "*test/unittest/*" "*utils/native/*" "*utils/system/*" \ 144 -o ${ohos_init}/appspawn_ut.info 145 146genhtml -o ${HOME}/appspawn_coverage appspawn_ut.info 147if [ $? -ne 0 ]; then 148 echo "Run genhtml failed." 149 popd 2>&1 > /dev/null 150 exit 1 151fi 152echo "Clear tmp files" 153rm -rf ./g.sh *.gcno *.gcda appspawn_ut.info appspawn_ut_tmp.info 154hdc_shell_cmd "rm -rf ${ut_target_path}" 155echo 156echo "Generate appspawn ut coverage done." 157echo "Check coverage in ${HOME}/appspawn_coverage." 158echo 159popd 2>&1 > /dev/null 160