Skip to content

test: add unit tests for common modules - #562

Merged
pengfeixx merged 1 commit into
linuxdeepin:masterfrom
pengfeixx:test/ut-common
Sep 4, 2026
Merged

test: add unit tests for common modules#562
pengfeixx merged 1 commit into
linuxdeepin:masterfrom
pengfeixx:test/ut-common

Conversation

@pengfeixx

Copy link
Copy Markdown
Contributor

内容

新增公共模块的 GTest 单元测试:settings、urlinfo、eventlog、编码检测、语法高亮、文件加载线程、性能监控、文本保存等,共 20 个文件。

说明

  • 基于 master 独立拉出,可独立评审与合并
  • 仅新增单元测试代码,不改动编辑器本体功能

@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, your pull request is larger than the review limit of 150,000 diff characters

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

AI 代码审查报告

提交信息

项目 内容
PR URL #562
PR 标题 test: add unit tests for common modules
作者 pengfeixx
分支 test/ut-common → master
修改文件数 20 个(全部为新增文件)
分析模式 全量分析(GitHub PR)

总体评分

总分 风险等级 总体评价
94 / 100 None 代码审查通过

未发现安全漏洞。本 PR 为 deepin-editor 的 common 模块添加了全面的单元测试,覆盖 Settings、Config、Utils、UrlInfo、Eventlogutils、CSyntaxHighlighter、DetectCode、FileLoadThread、IflytekAiAssistant、load_libs、PerformanceMonitor、SaveFileInterface、TextFileSaver 等核心类。测试代码结构规范,注释详尽,隔离措施完善,与 commit 目的完全一致。


漏洞统计

指标 数量
当前漏洞总数 0
新增漏洞 0
已修复漏洞 0
持平漏洞 0

漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个


四维度评分

维度1:语法逻辑(22/25)✓ 语法正确,逻辑清晰

评价: 语法正确,逻辑清晰,边界处理完善

分析:

  • 所有测试文件语法正确,使用 GTest 框架规范,Arrange/Act/Assert 三段式结构清晰
  • #define private public 技术用于访问私有成员,虽为 hack 手法但在测试代码中是业界常见做法,不影响编译正确性
  • CMake 构建配置正确,AUTOMOC/AUTORCC 启用,gtest_discover_tests 设置 QT_QPA_PLATFORM=offscreen 环境变量
  • stub 框架使用规范,stub.set_lamda 对重载函数使用 static_cast 正确定型

问题:

  1. tests/common/cov_report.sh 第 8 行硬编码构建路径 BD=/home/uos/work/ut/deepin-editor/build,缺乏可移植性,在其他环境执行将失败

建议:

# 修复方案:改为从环境变量或参数获取构建目录
BD="${BUILD_DIR:-$(dirname "$(dirname "$(readlink -f "$0")")")/build}"

维度2:代码质量(23/25)✓ 代码结构清晰,注释完整

评价: 代码结构清晰,注释完整,可读性好

分析:

  • 每个测试文件头部都有详尽的分支清单和用例映射注释,将源码分支编号与测试用例一一对应,维护性极佳
  • 测试用例命名规范统一:ClassName_MethodName_Condition_ExpectedResult 模式
  • 参数化测试(TEST_P)使用得当,如 CheckShortcutValidTestCleanPathTestEaseFuncTest 等覆盖边界值
  • 环境隔离措施完善:XDG_CONFIG_HOME/XDG_DATA_HOME 重定向到 QTemporaryDir,DBus/QProcess 全 stub
  • CMakeLists.txt 使用 add_ut_target 辅助函数封装公共逻辑,避免重复

问题:

  1. tests/common/cov_report.sh 硬编码路径缺乏参数化,脚本无法在其他 CI 环境复用

维度3:代码性能(19/20)✓ 性能良好,资源使用合理

评价: 性能良好,资源使用合理,无性能瓶颈

分析:

  • 大文件测试(41MB)有合理的 spy.wait(5000) 超时保护,不会无限阻塞
  • runSync() 方法正确处理 DeferredDelete 事件,避免内存泄漏
  • stub 框架在 SetUp/TearDown 中正确 clear,无残留状态
  • 并发测试(LoadLibsTest::GetInstance_ConcurrentCalls_SamePointerReturned)使用 8 线程验证双检锁,合理

问题:

  1. test_fileloadthread.cpp 中 41MB 文件测试每次执行都创建/删除大文件,CI 环境可能产生较大 I/O 压力,建议考虑使用内存文件系统或标记为可选

维度4:代码安全(30/30)✓ 存在0个安全漏洞

评价: 存在0个安全漏洞,安全合规

分析:

  • 测试代码全程使用 QTemporaryDir/QTemporaryFile 隔离文件操作,不触碰真实用户目录
  • DBus 交互全部 stub,QDBusConnection::sessionBus 返回伪连接,不连接真实总线
  • dlopen 仅使用不存在的路径(必失败),不加载任何真实动态库
  • 无硬编码密钥、无敏感信息泄露、无命令注入风险
  • #define private public 仅在测试编译单元内生效,不影响生产代码安全性

审查结论

本 PR 为 deepin-editor 项目添加了高质量的单元测试代码,完全符合"为 common 模块添加单元测试"的提交目的。代码覆盖了 13 个核心类的分支路径,测试隔离措施完善(临时目录、DBus stub、offscreen 平台),注释详尽(分支清单与用例映射一一对应)。唯一需要注意的是 cov_report.sh 中硬编码的构建路径问题,建议修改为可配置参数以提升可移植性。


修改文件清单

文件 类型 说明
tests/common/CMakeLists.txt 新增 common 模块测试构建配置
tests/common/cov_report.sh 新增 lcov 覆盖率报告脚本
tests/common/test_config.cpp 新增 Config 类单元测试(321行)
tests/common/test_data/encodes.ini 新增 测试数据文件
tests/common/test_data/test.qss 新增 测试 QSS 文件
tests/common/test_data/test.svg 新增 测试 SVG 文件
tests/common/test_eventlog.cpp 新增 Eventlogutils 单元测试(234行)
tests/common/test_settings.cpp 新增 Settings 类单元测试(1168行)
tests/common/test_urlinfo.cpp 新增 UrlInfo 单元测试(179行)
tests/common/test_utils.cpp 新增 Utils 类单元测试(1489行)
tests/common/ut_resources.qrc.in 新增 Qt 资源模板
tests/common2/CMakeLists.txt 新增 common2 模块测试构建配置
tests/common2/test_csyntaxhighlighter.cpp 新增 CSyntaxHighlighter 单元测试(206行)
tests/common2/test_detectcode.cpp 新增 DetectCode 单元测试(892行)
tests/common2/test_fileloadthread.cpp 新增 FileLoadThread 单元测试(423行)
tests/common2/test_iflytekaiassistant.cpp 新增 IflytekAiAssistant 单元测试(575行)
tests/common2/test_loadlibs.cpp 新增 load_libs 单元测试(196行)
tests/common2/test_performancemonitor.cpp 新增 PerformanceMonitor 单元测试(166行)
tests/common2/test_savefileinterface.cpp 新增 SaveFileInterface 单元测试(125行)
tests/common2/test_textfilesaver.cpp 新增 TextFileSaver 单元测试(355行)

本次变更为纯测试代码新增,不涉及生产代码修改,不涉及跨模块依赖,跳过代码图谱分析。

Add GTest suites for common utilities: settings, url info, event log,
encoding detection, syntax highlighter, file load thread, performance
monitor and text file saver.

新增 settings、urlinfo、eventlog、编码检测、语法高亮、文件加载线程、
性能监控、文本保存等公共模块的 GTest 单元测试。

Log: 新增公共模块单元测试
Influence: 仅新增单元测试代码,不影响编辑器本体功能。
@github-actions

github-actions Bot commented Sep 4, 2026

Copy link
Copy Markdown
  • 检测到敏感词unset变动
详情
    {
    "unset": {
        "tests/common/test_settings.cpp": {
            "b": [
                "        qunsetenv(\"XDG_CONFIG_HOME\");",
                "        qunsetenv(\"XDG_SESSION_TYPE\");",
                "        qunsetenv(\"XDG_CONFIG_HOME\");"
            ]
        },
        "tests/common/test_utils.cpp": {
            "b": [
                "        qunsetenv(\"XDG_CONFIG_HOME\");",
                "        qunsetenv(\"XDG_DATA_HOME\");",
                "        qunsetenv(\"XDG_SESSION_TYPE\");",
                "    qunsetenv(\"XDG_SESSION_TYPE\"); // \u4e0e\u672c\u7528\u4f8b qputenv \u914d\u5bf9\u8fd8\u539f"
            ]
        }
    }
}

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: lzwind, 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

@pengfeixx
pengfeixx merged commit aa5dc5d into linuxdeepin:master Sep 4, 2026
15 of 17 checks passed
@pengfeixx
pengfeixx deleted the test/ut-common branch September 4, 2026 08:52
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.

3 participants