#!/bin/bash

# 设置退出脚本当命令失败时 (非零退出状态)
#set -e

# 函数：运行单元测试
run_unit_tests() {
    ./unit_test_systemmonitor
}

URL=$1

# 上传 result.zip 到平台
upload_result() {
    echo "current pwd : $(pwd)"
    # 收集覆盖率信息
    lcov -d . -c -o r.info

    # 删除不需要的文件或路径
    lcov -r r.info "$(pwd)/unit_test_*" "$(pwd)/main.cpp" "$(pwd)/moc_*" "$(pwd)/../src/service/*.h" "$(pwd)/kt-test-utils/*" "/usr/include/*" "/opt/*" -o coverage.info

    # 生成html覆盖率报告
    genhtml "$(pwd)/coverage.info" -o result

    # 打包
    zip -r  result.zip result
    # 上传平台
    (curl --insecure -X POST -F "file=@/$(pwd)/result.zip" -F "package=ukui-system-monitor" -F "username=hesisheng" $URL)
    echo "all parameter : $@ $0 $1"
}

# 清除过程文件
clean_project() {
    echo "clean moc* *.o"
    rm moc_* *.o
}

# 编译项目
echo "Compiling the project..."
qmake && make

# 运行各个单元测试
echo "Running unit tests..."
run_unit_tests

# 收集代码覆盖率数据，生成gcov文件
echo "Collecting coverage data..."
find ./ -name '*.o' | xargs gcov --preserve-paths

# 上传到平台
echo "upload result.zip to platform"
#upload_result

# 清理过程文件
#clean_project

echo "All tests have been run successfully, and coverage data has been collected."
