## Copyright (C) 2024, KylinSoft Co., Ltd.
##
## This library is free software; you can redistribute it and/or
## modify it under the terms of the GNU Lesser General Public
## License as published by the Free Software Foundation; either
## version 3 of the License, or (at your option) any later version.
##
## This library is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## Lesser General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this library.  If not, see <https://www.gnu.org/licenses/>.
##
## Authors: Zhenyu Wang <wangzhenyu@kylinos.cn>

#!/bin/bash

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

# 函数：运行单元测试
run_unit_tests() {
    local pattern="$(pwd)/test-*"
    
    # 遍历所有匹配模式的目录
    for dir in $pattern; do
        if [ -d "$dir" ] && [ -x "$dir/$(basename "$dir")" ]; then
            echo "Running tests in $dir..."
	    # 执行单元测试
	    (cd "$dir" && "./$(basename "$dir")")
        # 收集代码覆盖率数据
	    echo "Collecting coverage data..."
	    (cd "$dir" && (find ./ -name '*.o' | xargs gcov --preserve-paths))
        else
	    echo "Skipping non-existent or non-executable directory: $dir, $dir/$(basename "$dir")"
        fi
    done
}

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

    # 删除不需要的文件或路径
    lcov -r $(pwd)/coverage.info "**moc**" "$(pwd)/tests/**" "/usr/include/**" -o $(pwd)/coverage.info

    sed -i "s|$(pwd)/||g" coverage.info

    # 删除不需要的文件或路径
    git clean -fd

    python3 $(pwd)/tests/convert_lcov2xml.py -l $(pwd)/coverage.info -o $(pwd)/debian/libkysdk-desktop-coverage.xml
}

# 编译项目
echo "Compiling the project..."
qmake $(pwd)/tests && make -j8

# 运行各个单元测试
echo "Running unit tests..."
run_unit_tests
genhtml -o result coverage.info

# 上传到平台
upload_result

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