From 9f54e148ef063baa0c8cfe5cd48c8f57da6f83ac Mon Sep 17 00:00:00 2001 From: xiepengfei Date: Tue, 21 Jul 2026 13:10:42 +0800 Subject: [PATCH] fix(tests): repair test-prj-running.sh to build and report coverage MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The cmake line was commented out and the build dir was never created, so make had no Makefile, the test binary was missing, and lcov found no .gcda files, producing an empty coverage.info. Create the build dir, run cmake with BUILD_TESTS, USE_PDFIUM_BUNDLE, Debug and CMAKE_SAFETYTEST_ARG, derive project_root via script_dir. Build test-deepin-reader with nproc, zerocounters before run, and emit artifacts to build-ut/{html,report,asan.log}. 脚本中 cmake 配置命令被注释、build 目录未创建,导致 make 无 Makefile,lcov 找不到 .gcda 文件,coverage.info 为空。 自动创建 build 目录并执行 cmake 配置(启用 BUILD_TESTS、 USE_PDFIUM_BUNDLE、Debug、CMAKE_SAFETYTEST_ARG)。 用 nproc 并行编译 test-deepin-reader,运行前 zerocounters, 报告固定输出到 build-ut/{html,report,asan.log}。 Log: 修复测试运行脚本无法构建和生成覆盖率的问题 Influence: 流水线可通过 test-prj-running.sh 一键产出测试与覆盖率报告。 --- tests/test-prj-running.sh | 77 ++++++++++++++++++++++++++++----------- 1 file changed, 55 insertions(+), 22 deletions(-) diff --git a/tests/test-prj-running.sh b/tests/test-prj-running.sh index 411c6044..b3fb9872 100644 --- a/tests/test-prj-running.sh +++ b/tests/test-prj-running.sh @@ -4,37 +4,70 @@ # # SPDX-License-Identifier: GPL-3.0-or-later +set -u + builddir=build reportdir=build-ut -# rm -rf $builddir -# rm -rf ../$builddir -# rm -rf $reportdir -# rm -rf ../$reportdir -# mkdir ../$builddir -# mkdir ../$reportdir -cd ../$builddir -#编译(启用单元测试构建) -# cmake -DCMAKE_SAFETYTEST_ARG="CMAKE_SAFETYTEST_ARG_ON" -DDOTEST=ON -DBUILD_TESTS=ON -DUSE_PDFIUM_BUNDLE=ON -DCMAKE_BUILD_TYPE=Debug .. -make -j8 -#生成asan日志和ut测试xml结果 -./tests/test-deepin-reader --gtest_output=xml:./report/report_deepin-reader.xml -workdir=$(cd ../$(dirname $0)/$builddir; pwd) +# Resolve project root (parent of the tests/ directory that holds this script) +script_dir="$(cd "$(dirname "$0")" && pwd)" +project_root="$(cd "${script_dir}/.." && pwd)" + +build_path="${project_root}/${builddir}" +report_path="${project_root}/${reportdir}" + +# Fresh build directory to ensure a clean coverage run +rm -rf "${build_path}" +mkdir -p "${build_path}" + +# Fresh report directory +rm -rf "${report_path}" +mkdir -p "${report_path}" + +cd "${build_path}" + +# Configure project with unit tests and coverage instrumentation enabled +cmake -DCMAKE_SAFETYTEST_ARG="CMAKE_SAFETYTEST_ARG_ON" \ + -DDOTEST=ON \ + -DBUILD_TESTS=ON \ + -DUSE_PDFIUM_BUNDLE=ON \ + -DCMAKE_BUILD_TYPE=Debug \ + "${project_root}" -mkdir -p report -#统计代码覆盖率并生成html报告 -lcov -d $workdir -c -o ./coverage.info +# Compile tests target +make -j"$(nproc)" test-deepin-reader -lcov --extract ./coverage.info '*/reader/*' -o ./coverage.info +# Ensure report directory used by gtest exists inside the build tree +mkdir -p "${build_path}/report" -lcov --remove ./coverage.info '*/tests/*' -o ./coverage.info +# Run tests and produce XML report +./tests/test-deepin-reader --gtest_output=xml:"${build_path}/report/report_deepin-reader.xml" +# Directory that holds coverage artifacts (build tree of the project) +workdir="${build_path}" + +# Reset any stale coverage counters before recapture +lcov --directory "${workdir}" --zerocounters || true + +# Re-run tests so .gcda files reflect a clean run +./tests/test-deepin-reader --gtest_output=xml:"${build_path}/report/report_deepin-reader.xml" + +# Collect coverage data +lcov -d "${workdir}" -c -o ./coverage.info + +# Keep only reader sources, drop tests themselves +lcov --extract ./coverage.info '*/reader/*' -o ./coverage.info +lcov --remove ./coverage.info '*/tests/*' -o ./coverage.info + +# Generate HTML report genhtml -o ./html ./coverage.info +# Rename main index for downstream tooling mv ./html/index.html ./html/cov_deepin-reader.html -#对asan、ut、代码覆盖率结果收集至指定文件夹 -cp -r html ../$reportdir/ -cp -r report ../$reportdir/ -cp -r asan*.log* ../$reportdir/asan_deepin-reader.log 2>/dev/null || true + +# Publish artifacts into project report dir +cp -r html "${report_path}/" +cp -r "${build_path}/report" "${report_path}/" +cp -r asan*.log* "${report_path}/asan_deepin-reader.log" 2>/dev/null || true exit 0