Skip to content

test: add test stub headers and coverage report generator - #559

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

test: add test stub headers and coverage report generator#559
pengfeixx merged 1 commit into
linuxdeepin:masterfrom
pengfeixx:test/ut-stub-report-generator

Conversation

@pengfeixx

@pengfeixx pengfeixx commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

内容

新增单元测试使用的 3rdparty 桩(stub)头文件(Stub/StubExt、addr_any/addr_pri,均补全 MIT 许可证头),以及 tests/report_generator 下的测试报告生成工具(Python);同时移除被 tests/3rdparty/stub/stub.h 取代的旧 tests/src/stub.h,共 19 个文件变更。

说明

  • 基于 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 代码审查报告

项目: linuxdeepin/deepin-editor
PR: #559
分支: test/ut-stub-report-generator → master
作者: pengfeixx
提交信息: test: add test stub headers and coverage report generator
审查时间: 2026-09-04
分析模式: 全量分析


总体评价

总分: 90/100
评级: 优秀
结论: 代码安全✓ 且 其他三个维度全部✓,代码质量优秀。本次 PR 为测试基础设施代码,添加了 C++ 测试桩库和 Python 覆盖率报告生成器,代码结构清晰,注释完整,无安全漏洞。存在少量轻微问题需要关注。


漏洞对比统计

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


维度1:语法逻辑(25分)✓

得分: 22/25
评价: 语法正确,逻辑清晰
标记: ✓(≥20分通过)

分析

本次新增代码涵盖 C++ 模板元编程和 Python 报告生成逻辑,语法整体正确,逻辑清晰。

  1. tests/report_generator/utils/file_utils.pyextract_module_name 函数中存在不可达代码:第一个 elif path_parts[0] == "tests" 分支(约第170行)已处理 tests 目录,而后续又有一个 elif path_parts[0] == "tests" 分支(约第197行)永远不会执行,导致 "AutoTest:" 前缀的逻辑成为死代码,所有 tests 路径均使用 "Test:" 前缀

  2. tests/3rdparty/stub/stub.hsetreset 方法的 throw("...") 后紧跟 return false 为不可达代码(throw 会中断执行流),虽然这是常见的防御性写法,但在逻辑上 return 语句永远不会执行


维度2:代码质量(25分)✓

得分: 23/25
评价: 代码结构清晰,注释完整
标记: ✓(≥20分通过)

分析

  1. 结构合理性: Python 报告生成器采用清晰的包结构(parsers/generators/utils),职责分离良好。C++ 桩库有 SPDX 许可证头和功能注释
  2. 注释完整性: 关键类和函数均有 docstring 说明,包括参数类型和返回值
  3. 代码重复: file_utils.pyextract_module_name 的两个 tests 目录处理分支存在逻辑重复
  4. 文件格式: 多个 Python 文件(__init__.pycoverage_parser.pytest_parser.pyfile_utils.pyui_utils.py)缺少末尾换行符(PEP 8 规范建议文件以换行符结尾)
  5. csv_generator.py 在函数内部 import traceback,建议移至模块顶部

维度3:代码性能(20分)✓

得分: 17/20
评价: 性能良好,资源使用合理
标记: ✓(≥16分通过)

分析

  1. tests/3rdparty/stub/addr_any.hget_lib_pathname_and_baseaddrget_func_addr 函数调用了 regcomp 编译正则表达式,但在函数返回前未调用 regfree 释放 regex_t 资源,存在资源泄漏。建议在函数返回前添加 regfree(&pathname_regex)
  2. Python 报告生成器使用逐行读取和正则匹配,处理效率合理
  3. stub-shadow.cpp 使用 unordered_map 实现 O(1) 平均查找,erase 操作正确返回迭代器避免迭代器失效
  4. html_generator.py 中对每个文件调用 os.path.exists 检查,在文件数量较多时可能有轻微 I/O 开销,但对于报告生成场景可接受

维度4:代码安全(30分)✓

得分: 28/30
评价: 存在0个安全漏洞
标记: ✓(≥24分通过)

分析

本次代码审查未发现安全漏洞。代码为测试基础设施,不处理不可信用户输入,无网络暴露面。

安全检查清单:

  • 无硬编码密钥或敏感信息
  • 无 SQL 注入风险(无数据库操作)
  • 无命令注入风险(无 subprocess 调用)
  • 无路径遍历风险(文件路径来自构建目录和测试输出)
  • XML 解析使用安全的 xml.etree.ElementTree(Python 3 默认禁用外部实体)
  • 无不安全密码算法使用

防御性编程建议(非漏洞):

  1. html_generator.py 中测试名称和失败信息通过 f-string 直接插入 HTML,未进行 HTML 转义。虽然数据来源于本地测试输出(非不可信输入),风险极低,但建议使用 html.escape() 进行防御性处理 ——非常重要

安全漏洞清单

无安全漏洞。


改进建议

1. 修复 file_utils.py 不可达代码

# 修复前:extract_module_name 中两个 tests 分支重复
# 将第二个 tests 分支合并到第一个,或调整条件判断顺序

# 建议合并为一个 tests 分支:
elif path_parts[0] == "tests":
    if len(path_parts) < 2:
        return "Tests"
    second_level = path_parts[1]
    if second_level == "plugins" and len(path_parts) >= 3:
        return f"AutoTest: {format_plugin_name(path_parts[2])}"
    elif second_level == "services" and len(path_parts) >= 3:
        return f"AutoTest: {format_service_name(path_parts[2])}"
    else:
        return f"AutoTest: {format_module_name(second_level)}"

2. 修复 addr_any.h 资源泄漏

// 在 get_lib_pathname_and_baseaddr 函数的 fclose(fp) 后添加:
regfree(&pathname_regex);

// 在 get_func_addr 函数的 return count 前添加:
regfree(&pathname_regex);

3. HTML 输出转义

import html

# 在 html_generator.py 中插入用户数据时进行转义:
# 修改前:
#   <code>{test_name}</code>
# 修改后:
#   <code>{html.escape(test_name)}</code>

修改文件清单

文件 类型 说明
tests/3rdparty/stub/addr_any.h 新增 C++ 地址解析头文件(第三方 MIT)
tests/3rdparty/stub/addr_pri.h 新增 C++ 私有成员访问头文件(第三方 MIT)
tests/3rdparty/stub/elfio.hpp 新增 ELF I/O 库头文件(第三方 MIT)
tests/3rdparty/stub/stub-shadow.cpp 新增 函数桩影子实现
tests/3rdparty/stub/stub-shadow.h 新增 函数桩影子头文件
tests/3rdparty/stub/stub.h 重命名+修改 函数桩头文件(从 tests/src/ 移入)
tests/3rdparty/stub/stubext.h 新增 扩展函数桩头文件
tests/report_generator/init.py 新增 报告生成器包初始化
tests/report_generator/generators/init.py 新增 生成器包初始化
tests/report_generator/generators/csv_generator.py 新增 CSV 报告生成器
tests/report_generator/generators/html_generator.py 新增 HTML 报告生成器
tests/report_generator/main.py 新增 主报告生成入口
tests/report_generator/parsers/init.py 新增 解析器包初始化
tests/report_generator/parsers/coverage_parser.py 新增 覆盖率数据解析器
tests/report_generator/parsers/test_parser.py 新增 测试输出解析器
tests/report_generator/utils/init.py 新增 工具包初始化
tests/report_generator/utils/file_utils.py 新增 文件工具函数
tests/report_generator/utils/ui_utils.py 新增 UI 工具函数

@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

Add the 3rdparty stub headers (Stub/StubExt, addr_any/addr_pri) used
by unit tests with proper license headers, add the Python coverage/
test report generator under tests/report_generator, and remove the
old tests/src/stub.h superseded by tests/3rdparty/stub.

新增单元测试使用的 3rdparty 桩(stub)头文件(补全 MIT 许可证头)、
tests/report_generator 测试报告生成工具(Python),并移除被
tests/3rdparty/stub/stub.h 取代的旧 tests/src/stub.h。

Log: 新增测试桩头文件与报告生成工具,移除旧 stub.h
Influence: 仅调整测试辅助代码,不影响编辑器本体功能。
@pengfeixx
pengfeixx force-pushed the test/ut-stub-report-generator branch from b296d5b to 0962f66 Compare September 4, 2026 08:31
@pengfeixx
pengfeixx merged commit aedb1fc into linuxdeepin:master Sep 4, 2026
16 of 17 checks passed
@pengfeixx
pengfeixx deleted the test/ut-stub-report-generator branch September 4, 2026 08:35
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