Skip to content

test(basekit): raise unit test coverage to 60% and fix crashing tests#740

Open
pengfeixx wants to merge 1 commit into
linuxdeepin:masterfrom
pengfeixx:feat/basekit-unit-tests-coverage
Open

test(basekit): raise unit test coverage to 60% and fix crashing tests#740
pengfeixx wants to merge 1 commit into
linuxdeepin:masterfrom
pengfeixx:feat/basekit-unit-tests-coverage

Conversation

@pengfeixx

Copy link
Copy Markdown
Contributor

Fix HashMap/FileCache tests that aborted the whole gtest process and add new tests for directory/errors/reader-writer modules.

修复导致整个gtest进程中途崩溃的HashMap/FileCache测试,并新增
directory、errors、reader-writer等模块的单元测试用例。

Log: 提升basekit单元测试覆盖率至60%
Influence: basekit库行覆盖率从13.0%提升至60.9%,128个测试全部通过。

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: pengfeixx

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @pengfeixx, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@github-actions

Copy link
Copy Markdown
  • 检测到敏感词export变动
详情
    {
    "export": {
        "src/lib/cooptests/helper_test.cpp": {
            "b": [
                "    // Reproduce the join logic; the private helper is not exported, so we"
            ]
        },
        "test-prj-running.sh": {
            "b": [
                "export QT_QPA_PLATFORM=\"${QT_QPA_PLATFORM:-offscreen}\"  # GUI \u6d4b\u8bd5\u65e0\u5934\u8fd0\u884c (cooperation-core \u7b49)"
            ]
        }
    }
}

@pengfeixx pengfeixx force-pushed the feat/basekit-unit-tests-coverage branch from 0ed4db5 to c546a36 Compare June 18, 2026 07:25
@github-actions

Copy link
Copy Markdown
  • 检测到敏感词export变动
详情
    {
    "export": {
        "src/lib/cooptests/helper_test.cpp": {
            "b": [
                "    // Reproduce the join logic; the private helper is not exported, so we"
            ]
        },
        "test-prj-running.sh": {
            "b": [
                "export QT_QPA_PLATFORM=\"${QT_QPA_PLATFORM:-offscreen}\"  # GUI \u6d4b\u8bd5\u65e0\u5934\u8fd0\u884c (cooperation-core \u7b49)"
            ]
        }
    }
}

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

★ 总体评分:85分

■ 【总体评价】

代码实现了全面的单元测试覆盖与自动化覆盖率收集,但存在硬编码路径和测试代码重复问题
逻辑正确但因硬编码本地绝对路径及测试用例大量重复扣15分

■ 【详细分析】

  • 1.语法逻辑(基本正确)✓

测试用例对底层接口变更的适配非常准确。在 filecache_test.cpp 中,将 insert_path 的调用从单文件调整为目录递归,并修正了 prefix 拼接逻辑以避免双斜杠;在 exceptions_test.cpp 中,针对 GNU/Linux 下 strerror_r 的 GNU 扩展特性(可能返回静态指针而非填充缓冲区导致结果为空),将强制的非空断言更改为仅验证可调用,完全符合系统 API 的实际行为特征。
建议:在 exceptions_test.cpp 中,虽然放弃了非空断言,但可以增加对已知必定有描述的错误码(如 EACCES)的局部非空校验,以平衡测试覆盖率与跨平台兼容性。

  • 2.代码质量(较差)✕

src/lib/tests/CMakeLists.txt 中硬编码了开发者本地的绝对路径 /home/uos/work/单元测试/dde-cooperation/3rdparty/QtZeroConf,这会导致其他开发者或 CI 环境编译失败。同时,discover_test.cpp 与 helper_test.cpp 中存在大量完全相同的 DeviceInfo 测试逻辑(如 SettersAndGetters、VariantMapRoundTripPreservesFields 等),违反了 DRY 原则。此外,discover_test.cpp 中多处 DISABLED 测试的注释后跟随着乱码字符(如 åŕÂÄæÅÂé´å…±äº«çжæ),属于编码处理不当。
潜在问题:硬编码路径阻断其他环境构建;重复代码增加后续维护成本;注释乱码影响可读性。
建议:将 CMake 中的绝对路径替换为基于 CMAKE_SOURCE_DIR 的相对路径;将 DeviceInfo 的公共测试逻辑提取到独立的头文件或基础测试夹具中;修复源文件的字符编码问题,统一保存为 UTF-8 无 BOM 格式。

  • 3.代码性能(无性能问题)✓

测试脚本 test-prj-running.sh 使用 find 命令配合 timeout 合理控制了测试执行的生命周期,防止了网络测试(如 netutil 的 SSL/TCP 连接)导致的无限阻塞。新增的 C++ 测试用例仅涉及内存中的数据结构操作与本地临时文件读写,资源消耗极小,执行效率高。
建议:无需额外性能优化。

  • 4.代码安全(存在0个安全漏洞)✓

漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个
本次提交全部为测试代码、构建脚本及覆盖率收集工具,未引入命令注入、缓冲区溢出或路径遍历等安全风险。测试环境通过 ENABLE_AUTO_UNIT_TEST 宏与生产环境进行了有效隔离,单例互斥绕过逻辑仅限于测试模式生效。gcov_flush_shim.c 虽在信号处理函数中调用了非 async-signal-safe 的 exit(),但仅作为覆盖率采集的 LD_PRELOAD 辅助桩,不进入生产发布物,不构成安全漏洞。
建议:保持测试宏开关的严格隔离,确保生产构建不会意外引入测试桩代码。

■ 【改进建议代码示例】

# 修复 src/lib/tests/CMakeLists.txt 中的硬编码绝对路径问题
# 旧代码:
#     /home/uos/work/单元测试/dde-cooperation/3rdparty/QtZeroConf)
# 新代码:
    target_include_directories(lib_logic_tests PRIVATE
        ${CMAKE_SOURCE_DIR}/src
        ${CMAKE_CURRENT_SOURCE_DIR}/../common
        ${CMAKE_CURRENT_SOURCE_DIR}/../common/proto
        ${CMAKE_CURRENT_SOURCE_DIR}/../common/httpweb
        ${CMAKE_CURRENT_SOURCE_DIR}/../common/session
        ${CMAKE_CURRENT_SOURCE_DIR}/../cooperation/core
        ${CMAKE_CURRENT_SOURCE_DIR}/../cooperation/core/net
        ${CMAKE_CURRENT_SOURCE_DIR}/../cooperation/core/net/helper
        ${CMAKE_CURRENT_SOURCE_DIR}/../cooperation/core/discover
        ${CMAKE_CURRENT_SOURCE_DIR}/../cooperation/dfmplugin
        ${CMAKE_CURRENT_SOURCE_DIR}/../data-transfer/quazip
        ${CMAKE_SOURCE_DIR}/3rdparty/QtZeroConf)

@pengfeixx pengfeixx force-pushed the feat/basekit-unit-tests-coverage branch from c546a36 to 42e1ded Compare June 18, 2026 08:48
@github-actions

Copy link
Copy Markdown
  • 检测到敏感词export变动
详情
    {
    "export": {
        "src/lib/cooptests/helper_test.cpp": {
            "b": [
                "    // Reproduce the join logic; the private helper is not exported, so we"
            ]
        },
        "test-prj-running.sh": {
            "b": [
                "export QT_QPA_PLATFORM=\"${QT_QPA_PLATFORM:-offscreen}\"  # GUI \u6d4b\u8bd5\u65e0\u5934\u8fd0\u884c (cooperation-core \u7b49)"
            ]
        }
    }
}

@pengfeixx pengfeixx force-pushed the feat/basekit-unit-tests-coverage branch from 42e1ded to 06d0a3c Compare June 18, 2026 09:02
@github-actions

Copy link
Copy Markdown
  • 检测到敏感词export变动
详情
    {
    "export": {
        "src/lib/cooptests/helper_test.cpp": {
            "b": [
                "    // Reproduce the join logic; the private helper is not exported, so we"
            ]
        }
    }
}

Raise basekit coverage 13%->60% by fixing crashing tests; add lib
(cooperation-core/proto) tests with direct-compile harness; add
ENABLE_AUTO_UNIT_TEST guards and gcov flush shim to collect coverage
from running the real apps.

修复basekit崩溃测试使覆盖率13%->60%;新增lib(cooperation-core/proto)单元测试
(直接编译harness);加ENABLE_AUTO_UNIT_TEST宏与gcov flush桩支持运行真实程序采集覆盖率。

Log: 新增多模块单元测试与覆盖率采集基建
Influence: basekit覆盖率13%->60%,运行真实程序使data-transfer覆盖0%->37.8%,新增全量覆盖率脚本与gcov flush桩。
@pengfeixx pengfeixx force-pushed the feat/basekit-unit-tests-coverage branch from 06d0a3c to bcf2e97 Compare June 18, 2026 09:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants